Skip to content

Commit

Permalink
feat(Modal, Select): Add optional closeTimeoutMS prop (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
slwzero authored Dec 29, 2021
1 parent 87f4e3d commit 471c219
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions src/components/Modal/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type Props = Testable & {
closeOnEsc?: boolean;
closeOnBackgroundClick?: boolean;
onClose?: () => void;
closeTimeoutMS?: number;
};

export const Component = ({
Expand All @@ -51,6 +52,7 @@ export const Component = ({
closeOnBackgroundClick = true,
onClose,
'data-testid': testId,
closeTimeoutMS = CLOSE_MODAL_TIMEOUT,
}: Props) => {
const { buildTestId } = useBuildTestId({ id: testId });

Expand All @@ -59,14 +61,20 @@ export const Component = ({
const [show, setShow] = useState(false);

const close = useCallback(() => {
onClose?.();

if (closeTimeoutMS === CLOSE_MODAL_TIMEOUT) {
onClose?.();
return;
}
if (typeof window !== 'undefined') {
window.setTimeout(() => {
if (isMounted) setShow(false);
}, CLOSE_MODAL_TIMEOUT);
onClose?.();
}, closeTimeoutMS);
}
}, [onClose]);
}, [closeTimeoutMS, onClose]);

const handleOnAfterClose = useCallback(() => {
if (isMounted) setShow(false);
}, []);

const context = useMemo(() => ({ loading, onClose: close, testId }), [loading, close, testId]);

Expand All @@ -88,9 +96,10 @@ export const Component = ({
<ReactModal
isOpen={open}
onRequestClose={close}
onAfterClose={handleOnAfterClose}
className={className}
parentSelector={PARENT_SELECTOR}
closeTimeoutMS={CLOSE_MODAL_TIMEOUT}
closeTimeoutMS={closeTimeoutMS}
shouldCloseOnEsc={closeOnEsc}
shouldCloseOnOverlayClick={closeOnBackgroundClick}
contentElement={(props, contentElement) => (
Expand Down
2 changes: 1 addition & 1 deletion src/components/Select/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type Props = Pick<React.HTMLProps<HTMLElement>, 'children'> &
target: React.ReactNode;
open?: boolean;
onClose?: () => void;
className?: string;
closeTimeoutMS?: number;
};

export const Component = ({
Expand Down

0 comments on commit 471c219

Please sign in to comment.