11import format from 'date-fns/format' ;
22import PropTypes from 'prop-types' ;
3- import React from 'react' ;
3+ import React , { useState , useRef , useEffect } from 'react' ;
44import { Helmet } from 'react-helmet' ;
55import InlineSVG from 'react-inlinesvg' ;
66import { connect } from 'react-redux' ;
@@ -27,11 +27,34 @@ const arrowDown = require('../../../images/sort-arrow-down.svg');
2727const removeIcon = require ( '../../../images/close.svg' ) ;
2828
2929const ShareURL = ( { value } ) => {
30- const [ showURL , setShowURL ] = React . useState ( false ) ;
30+ const [ showURL , setShowURL ] = useState ( false ) ;
31+ const node = useRef ( ) ;
32+
33+ const handleClickOutside = ( e ) => {
34+ if ( node . current . contains ( e . target ) ) {
35+ return ;
36+ }
37+ setShowURL ( false ) ;
38+ } ;
39+
40+ useEffect ( ( ) => {
41+ if ( showURL ) {
42+ document . addEventListener ( 'mousedown' , handleClickOutside ) ;
43+ } else {
44+ document . removeEventListener ( 'mousedown' , handleClickOutside ) ;
45+ }
46+
47+ return ( ) => {
48+ document . removeEventListener ( 'mousedown' , handleClickOutside ) ;
49+ } ;
50+ } , [ showURL ] ) ;
3151
3252 return (
33- < div className = "collection-share" >
34- < button className = "collection-share__button" onClick = { ( ) => setShowURL ( ! showURL ) } >
53+ < div className = "collection-share" ref = { node } >
54+ < button
55+ className = "collection-share__button"
56+ onClick = { ( ) => setShowURL ( ! showURL ) }
57+ >
3558 < span > Share</ span >
3659 < InlineSVG className = "collection-share__arrow" src = { dropdownArrow } />
3760 </ button >
0 commit comments