-
Notifications
You must be signed in to change notification settings - Fork 100
/
FocusManager.js
40 lines (38 loc) · 900 Bytes
/
FocusManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as React from 'react'
import Value from './Value'
import renderProps from '../utils/renderProps'
const FocusManager = ({ onChange, ...props }) => {
let canBlur = true
return (
<Value initial={false} onChange={onChange}>
{({ value, set }) =>
renderProps(props, {
focused: value,
blur: () => {
if (value) {
document.activeElement.blur()
}
},
bind: {
tabIndex: -1,
onBlur: () => {
if (canBlur) {
set(false)
}
},
onFocus: () => {
set(true)
},
onMouseDown: () => {
canBlur = false
},
onMouseUp: () => {
canBlur = true
},
},
})
}
</Value>
)
}
export default FocusManager