11import PropTypes from 'prop-types' ;
2- import React , { useState , useRef , useEffect } from 'react' ;
2+ import React , { useState , useCallback } from 'react' ;
33import { Helmet } from 'react-helmet' ;
44import { connect } from 'react-redux' ;
55import { Link } from 'react-router' ;
@@ -9,6 +9,7 @@ import classNames from 'classnames';
99
1010import Button from '../../../common/Button' ;
1111import { DropdownArrowIcon } from '../../../common/icons' ;
12+ import useModalClose from '../../../common/useModalClose' ;
1213import * as ProjectActions from '../../IDE/actions/project' ;
1314import * as ProjectsActions from '../../IDE/actions/projects' ;
1415import * as CollectionsActions from '../../IDE/actions/collections' ;
@@ -30,30 +31,12 @@ import RemoveIcon from '../../../images/close.svg';
3031
3132const ShareURL = ( { value } ) => {
3233 const [ showURL , setShowURL ] = useState ( false ) ;
33- const node = useRef ( ) ;
3434 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 ) ;
5437
5538 return (
56- < div className = "collection-share" ref = { node } >
39+ < div className = "collection-share" ref = { ref } >
5740 < Button
5841 onClick = { ( ) => setShowURL ( ! showURL ) }
5942 iconAfter = { < DropdownArrowIcon /> }
0 commit comments