Download spectrum/src/components/outsideClickHandler/index.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/spectrum/src/components/outsideClickHandler/index.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/spectrum/src/components/outsideClickHandler/index.js
-
curl -L -o index.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/spectrum/src/components/outsideClickHandler/index.js
1.16 kB
| // @flow | |
| import * as React from 'react'; | |
| type Props = { | |
| children: React$Node, | |
| style?: Object, | |
| onOutsideClick: Function, | |
| }; | |
| class OutsideAlerter extends React.Component<Props> { | |
| wrapperRef: React$Node; | |
| // iOS bug, see: https://stackoverflow.com/questions/10165141/jquery-on-and-delegate-doesnt-work-on-ipad | |
| componentDidMount() { | |
| // $FlowFixMe | |
| document | |
| .getElementById('root') | |
| .addEventListener('mousedown', this.handleClickOutside); | |
| } | |
| componentWillUnmount() { | |
| // $FlowFixMe | |
| document | |
| .getElementById('root') | |
| .removeEventListener('mousedown', this.handleClickOutside); | |
| } | |
| setWrapperRef = (node: React$Node) => { | |
| this.wrapperRef = node; | |
| }; | |
| /** | |
| * Alert if clicked on outside of element | |
| */ | |
| handleClickOutside = (event: any) => { | |
| // $FlowFixMe | |
| if (this.wrapperRef && !this.wrapperRef.contains(event.target)) { | |
| this.props.onOutsideClick(); | |
| } | |
| }; | |
| render() { | |
| const { style = {}, children } = this.props; | |
| return ( | |
| // $FlowFixMe | |
| <div style={style} ref={this.setWrapperRef}> | |
| {children} | |
| </div> | |
| ); | |
| } | |
| } | |
| export default OutsideAlerter; | |