Skip to content

Commit 6582362

Browse files
committed
Apply useModalClose to collection ShareURL.
1 parent 0854d43 commit 6582362

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

client/modules/User/components/Collection.jsx

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import React, { useState, useRef, useEffect } from 'react';
2+
import React, { useState, useCallback } from 'react';
33
import { Helmet } from 'react-helmet';
44
import { connect } from 'react-redux';
55
import { Link } from 'react-router';
@@ -9,6 +9,7 @@ import classNames from 'classnames';
99

1010
import Button from '../../../common/Button';
1111
import { DropdownArrowIcon } from '../../../common/icons';
12+
import useModalClose from '../../../common/useModalClose';
1213
import * as ProjectActions from '../../IDE/actions/project';
1314
import * as ProjectsActions from '../../IDE/actions/projects';
1415
import * as CollectionsActions from '../../IDE/actions/collections';
@@ -30,30 +31,12 @@ import RemoveIcon from '../../../images/close.svg';
3031

3132
const 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

Comments
 (0)