Skip to content

Commit 36f7be6

Browse files
committed
Close Collection Share dropdown when clicking outside of it
1 parent 7ae02be commit 36f7be6

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

client/modules/User/components/Collection.jsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import format from 'date-fns/format';
22
import PropTypes from 'prop-types';
3-
import React from 'react';
3+
import React, { useState, useRef, useEffect } from 'react';
44
import { Helmet } from 'react-helmet';
55
import InlineSVG from 'react-inlinesvg';
66
import { connect } from 'react-redux';
@@ -27,11 +27,34 @@ const arrowDown = require('../../../images/sort-arrow-down.svg');
2727
const removeIcon = require('../../../images/close.svg');
2828

2929
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]);
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

Comments
 (0)