-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
b060286
commit 1127996
Showing
3 changed files
with
81 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { PureComponent } from 'react'; | ||
import { findDOMNode } from 'react-dom'; | ||
import PropTypes from 'prop-types'; | ||
|
||
/** | ||
* This setup is needed to create proper `blur` handler. | ||
* Slate-react Editor has `onBlur` prop, but if we just supply it to Editor component, | ||
* `blur` will fire when a user clicks buttons B, I etc. But we need `blur` to fire only when | ||
* component loses focus, and not when we operate with internal elements of editor. | ||
*/ | ||
export default class ProvideBlur extends PureComponent { | ||
static propTypes = { | ||
onBlur: PropTypes.func, | ||
onFocus: PropTypes.func | ||
} | ||
|
||
static defaultProps = { | ||
onBlur: () => {}, | ||
onFocus: () => {} | ||
} | ||
|
||
state = { | ||
isFocused: false | ||
} | ||
|
||
componentDidMount() { | ||
this.selfDOMNode = findDOMNode(this); | ||
this.selfDOMNode.addEventListener('focusin', this.handleFocusIn); | ||
this.selfDOMNode.addEventListener('focusout', this.handleFocusOut); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.selfDOMNode.removeEventListener('focusin', this.handleFocusIn) | ||
this.selfDOMNode.removeEventListener('focusout', this.handleFocusOut) | ||
} | ||
|
||
handleFocusIn = _ => { | ||
if (!this.state.isFocused) { | ||
this.setState( | ||
{ isFocused: true }, | ||
(_ => this.props.onFocus()) | ||
); | ||
} | ||
}; | ||
|
||
handleFocusOut = _ => { | ||
const data = { timeout: null }; | ||
const abortFocusOut = _ => clearTimeout(data.timeout); | ||
|
||
data.timeout = setTimeout(_ => { | ||
this.selfDOMNode.removeEventListener('focusin', abortFocusOut); | ||
this.setState({ isFocused: false }, this.props.onBlur); | ||
}) | ||
|
||
this.selfDOMNode.addEventListener('focusin', abortFocusOut) | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
return children instanceof Function ? children() : children; | ||
} | ||
} |