1
1
import PropTypes from 'prop-types' ;
2
- import React , { useState , useRef , useEffect } from 'react' ;
2
+ import React , { useState , useCallback } from 'react' ;
3
3
import { Helmet } from 'react-helmet' ;
4
4
import { connect } from 'react-redux' ;
5
5
import { Link } from 'react-router' ;
@@ -9,6 +9,7 @@ import classNames from 'classnames';
9
9
10
10
import Button from '../../../common/Button' ;
11
11
import { DropdownArrowIcon } from '../../../common/icons' ;
12
+ import useModalClose from '../../../common/useModalClose' ;
12
13
import * as ProjectActions from '../../IDE/actions/project' ;
13
14
import * as ProjectsActions from '../../IDE/actions/projects' ;
14
15
import * as CollectionsActions from '../../IDE/actions/collections' ;
@@ -30,30 +31,12 @@ import RemoveIcon from '../../../images/close.svg';
30
31
31
32
const ShareURL = ( { value } ) => {
32
33
const [ showURL , setShowURL ] = useState ( false ) ;
33
- const node = useRef ( ) ;
34
34
const { t } = useTranslation ( ) ;
35
-
36
- const handleClickOutside = ( e ) => {
37
- if ( node . current . contains ( e . target ) ) {
38
- return ;
39
- }
40
- setShowURL ( false ) ;
41
- } ;
42
-
43
- useEffect ( ( ) => {
44
- if ( showURL ) {
45
- document . addEventListener ( 'mousedown' , handleClickOutside ) ;
46
- } else {
47
- document . removeEventListener ( 'mousedown' , handleClickOutside ) ;
48
- }
49
-
50
- return ( ) => {
51
- document . removeEventListener ( 'mousedown' , handleClickOutside ) ;
52
- } ;
53
- } , [ showURL ] ) ;
35
+ const close = useCallback ( ( ) => setShowURL ( false ) , [ setShowURL ] ) ;
36
+ const ref = useModalClose ( close ) ;
54
37
55
38
return (
56
- < div className = "collection-share" ref = { node } >
39
+ < div className = "collection-share" ref = { ref } >
57
40
< Button
58
41
onClick = { ( ) => setShowURL ( ! showURL ) }
59
42
iconAfter = { < DropdownArrowIcon /> }
0 commit comments