1
1
import format from 'date-fns/format' ;
2
2
import PropTypes from 'prop-types' ;
3
- import React from 'react' ;
3
+ import React , { useState , useRef , useEffect } from 'react' ;
4
4
import { Helmet } from 'react-helmet' ;
5
5
import InlineSVG from 'react-inlinesvg' ;
6
6
import { connect } from 'react-redux' ;
@@ -27,11 +27,34 @@ const arrowDown = require('../../../images/sort-arrow-down.svg');
27
27
const removeIcon = require ( '../../../images/close.svg' ) ;
28
28
29
29
const 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 ] ) ;
31
51
32
52
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
+ >
35
58
< span > Share</ span >
36
59
< InlineSVG className = "collection-share__arrow" src = { dropdownArrow } />
37
60
</ button >
0 commit comments