Files
planka/client/src/lib/popup/with-popup.jsx

121 lines
2.9 KiB
React
Raw Normal View History

2022-09-16 14:39:41 +05:00
import { ResizeObserver } from '@juggle/resize-observer';
import React, { useCallback, useRef, useState } from 'react';
2019-08-31 04:07:25 +05:00
import PropTypes from 'prop-types';
import { Button, Popup as SemanticUIPopup } from 'semantic-ui-react';
import styles from './Popup.module.css';
export default (WrappedComponent, defaultProps) => {
2022-12-26 21:10:50 +01:00
const Popup = React.memo(({ children, onClose, ...props }) => {
2019-08-31 04:07:25 +05:00
const [isOpened, setIsOpened] = useState(false);
2022-09-16 14:39:41 +05:00
const wrapper = useRef(null);
const resizeObserver = useRef(null);
2019-08-31 04:07:25 +05:00
const handleOpen = useCallback(() => {
setIsOpened(true);
}, []);
const handleClose = useCallback(() => {
setIsOpened(false);
2022-12-26 21:10:50 +01:00
if (onClose) {
onClose();
}
}, [onClose]);
2019-08-31 04:07:25 +05:00
2020-03-25 00:15:47 +05:00
const handleMouseDown = useCallback((event) => {
2019-08-31 04:07:25 +05:00
event.stopPropagation();
}, []);
2020-03-25 00:15:47 +05:00
const handleClick = useCallback((event) => {
2019-08-31 04:07:25 +05:00
event.stopPropagation();
}, []);
const handleTriggerClick = useCallback(
2020-03-25 00:15:47 +05:00
(event) => {
2019-08-31 04:07:25 +05:00
event.stopPropagation();
const { onClick } = children;
if (onClick) {
onClick(event);
}
},
[children],
);
2022-09-16 14:39:41 +05:00
const handleContentRef = useCallback((element) => {
if (resizeObserver.current) {
resizeObserver.current.disconnect();
}
if (!element) {
resizeObserver.current = null;
return;
}
resizeObserver.current = new ResizeObserver(() => {
if (resizeObserver.current.isInitial) {
resizeObserver.current.isInitial = false;
return;
}
wrapper.current.positionUpdate();
});
resizeObserver.current.isInitial = true;
resizeObserver.current.observe(element);
}, []);
2019-08-31 04:07:25 +05:00
const tigger = React.cloneElement(children, {
onClick: handleTriggerClick,
});
return (
<SemanticUIPopup
basic
wide
2022-09-16 14:39:41 +05:00
ref={wrapper}
2019-08-31 04:07:25 +05:00
trigger={tigger}
on="click"
open={isOpened}
position="bottom left"
2020-10-20 16:10:25 +05:00
popperModifiers={[
{
name: 'preventOverflow',
2022-12-15 01:38:03 +01:00
enabled: true,
2020-10-20 16:10:25 +05:00
options: {
2022-12-15 01:38:03 +01:00
altAxis: true,
padding: 20,
2020-10-20 16:10:25 +05:00
},
},
2020-10-20 16:10:25 +05:00
]}
2019-08-31 04:07:25 +05:00
className={styles.wrapper}
onOpen={handleOpen}
onClose={handleClose}
onMouseDown={handleMouseDown}
onClick={handleClick}
{...defaultProps} // eslint-disable-line react/jsx-props-no-spreading
2019-08-31 04:07:25 +05:00
>
2022-09-16 14:39:41 +05:00
<div ref={handleContentRef}>
<Button icon="close" onClick={handleClose} className={styles.closeButton} />
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
<WrappedComponent {...props} onClose={handleClose} />
</div>
2019-08-31 04:07:25 +05:00
</SemanticUIPopup>
);
});
Popup.propTypes = {
children: PropTypes.node.isRequired,
2022-12-26 21:10:50 +01:00
onClose: PropTypes.func,
};
Popup.defaultProps = {
onClose: undefined,
2019-08-31 04:07:25 +05:00
};
return Popup;
};