-
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.
Close widget on lost focus, render widget into portal (#162)
- Loading branch information
1 parent
0bc0368
commit abba48d
Showing
10 changed files
with
217 additions
and
154 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 was deleted.
Oops, something went wrong.
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
78 changes: 78 additions & 0 deletions
78
src/client/components/PlainMarkdownInput/FocusBlur.react.js
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,78 @@ | ||
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.el = findDOMNode(this); | ||
this.el.addEventListener('focusin', this.handleFocusIn); | ||
this.el.addEventListener('focusout', this.handleFocusOut); | ||
} | ||
|
||
componentDidUpdate(prevProps, prevState) { | ||
const prevFocused = prevState.isFocused; | ||
const { isFocused } = this.state; | ||
|
||
if (isFocused && prevFocused !== isFocused) { | ||
this.props.onFocus() | ||
} | ||
|
||
if (!isFocused && prevFocused !== isFocused) { | ||
this.props.onBlur() | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
this.el.removeEventListener('focusin', this.handleFocusIn) | ||
this.el.removeEventListener('focusout', this.handleFocusOut) | ||
} | ||
|
||
handleFocusIn = event => { | ||
// IE11 & firefox trigger focusout when you click on PlainMarkdownInput. | ||
// In order to eliminate it we can stop listening for focusout when focusin occurs. | ||
this.el.removeEventListener('focusout', this.handleFocusOut); | ||
setTimeout(_ => this.el && this.el.addEventListener('focusout', this.handleFocusOut)); | ||
return this.setState(prevState => prevState.isFocused ? {} : { isFocused: true }) | ||
}; | ||
|
||
timeout = null; | ||
|
||
abortFocusOut = _ => { | ||
clearTimeout(this.timeout); | ||
} | ||
|
||
handleFocusOut = event => { | ||
this.el.addEventListener('focusin', this.abortFocusOut); | ||
this.timeout = setTimeout(_ => { | ||
if (this.el) { | ||
this.el.removeEventListener('focusin', this.abortFocusOut); | ||
} | ||
this.setState({ isFocused: false }); | ||
}); | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
return children instanceof Function ? children() : children; | ||
} | ||
} |
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
Oops, something went wrong.