-
Notifications
You must be signed in to change notification settings - Fork 2
/
FocusWithin.js
177 lines (158 loc) · 3.96 KB
/
FocusWithin.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* eslint-env node, browser */
import React from 'react'
import PropTypes from 'prop-types'
const noOutlineStyles = {
outline: 'none'
}
class FocusWithin extends React.Component {
state = {
focused: false
}
lastBlurEvent = null
ref = React.createRef()
componentDidMount() {
/**
* In order for document.body to receive focus events
* it needs to be focusable. Adding `tabindex="-1"` makes it focusable
* but prevents it from receiving the focus on user interaction.
*/
if (document != null) {
document.querySelector('body').setAttribute('tabindex', '-1')
}
}
/**
* Calls `focus` method on the container node
*
* @public
* @method focus
* */
focus() {
const node = this.ref.current
if (node != null && typeof node.focus === 'function') {
node.focus()
}
}
/**
* Event handler that fires if the FocusEvent bubbled up to the document.
*
* @private
* @method _onFocusIn
*
* We check if 3 conditions are met:
* 1. Current state is focused
* 2. Blur occured inside the container
* 3. Focus occured outside of the container
*
* In this case we fire `onBlur` callback.
*/
_onFocusIn = () => {
if (
this.lastBlurEvent &&
this.isInsideNode(this.ref.current, this.lastBlurEvent.target) &&
!this.isInsideNode(this.ref.current, document.activeElement)
) {
this.setState(
{
focused: false
},
() => {
document.removeEventListener('focusin', this._onFocusIn)
this.props.onBlur(this.lastBlurEvent)
}
)
}
}
/**
* @private
* @method onFocus
*/
onFocus = evt => {
const { onFocus } = this.props
const { focused } = this.state
/**
* If it's not focused yet we'll set the state to `focused: true`
*/
if (!focused) {
this.setState(
{
focused: true
},
() => {
/**
* Attach a native event listener to the document. We have to use `focusin` since
* native `focus` event doesn't bubble. See
* https://developer.mozilla.org/en-US/docs/Web/Events/focusin and
* https://developer.mozilla.org/en-US/docs/Web/Events/focus
*/
document.addEventListener('focusin', this._onFocusIn)
onFocus(evt)
}
)
}
}
/**
* @private
* @method onBlur
*/
onBlur = evt => {
evt.persist() // Persist the original event since it will be fired later
this.lastBlurEvent = evt
}
/**
* Checks if the parentNode contains the node
*
* @private
* @method isInsideNode
* @param parentNode
* @param node
* @returns {boolean}
*/
isInsideNode = (parentNode, node) => {
if (process.env.NODE_ENV === 'development') {
if (parentNode == null || Object(parentNode).nodeType !== 1) {
throw new Error(
'A ref to a valid DOM Node must be supplied to' +
' FocusWithin.\n' +
' You have probably provided a ref to a React Element.\n See https://reactjs.org/docs/react-api.html#refs'
)
}
}
return parentNode.contains(node)
}
render() {
const { children } = this.props
const { focused } = this.state
const events = {
onFocus: this.onFocus,
onBlur: this.onBlur
}
if (typeof children === 'function') {
return React.cloneElement(
children({
focused,
getRef: this.ref
}),
events
)
}
return (
<div ref={this.ref} style={noOutlineStyles} {...events}>
{children}
</div>
)
}
}
FocusWithin.propTypes = {
/**
* Function has the following signature:
* `({ focused: Boolean, getRef: Function }) => React.Element`
*/
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
onBlur: PropTypes.func,
onFocus: PropTypes.func
}
FocusWithin.defaultProps = {
onBlur: () => {},
onFocus: () => {}
}
export default FocusWithin