Make sure that closed Modals are always removed

This commit is contained in:
Fedor Indutny 2022-12-13 17:56:32 -08:00 committed by GitHub
parent 94875efaf6
commit 6cd16cf117
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 8 deletions

View file

@ -10,6 +10,12 @@ export type ModalConfigType = {
transform?: string;
};
enum ModalState {
Open = 'Open',
Closing = 'Closing',
Closed = 'Closed',
}
export function useAnimated(
onClose: () => unknown,
{
@ -21,10 +27,13 @@ export function useAnimated(
}
): {
close: () => unknown;
isClosed: boolean;
modalStyles: SpringValues<ModalConfigType>;
overlayStyles: SpringValues<ModalConfigType>;
} {
const [isOpen, setIsOpen] = useState(true);
const [state, setState] = useState(ModalState.Open);
const isOpen = state === ModalState.Open;
const isClosed = state === ModalState.Closed;
const modalRef = useSpringRef();
@ -32,7 +41,8 @@ export function useAnimated(
from: getFrom(isOpen),
to: getTo(isOpen),
onRest: () => {
if (!isOpen) {
if (state === ModalState.Closing) {
setState(ModalState.Closed);
onClose();
}
},
@ -59,10 +69,18 @@ export function useAnimated(
});
useChain(isOpen ? [overlayRef, modalRef] : [modalRef, overlayRef]);
const close = useCallback(() => setIsOpen(false), []);
const close = useCallback(() => {
setState(currentState => {
if (currentState === ModalState.Open) {
return ModalState.Closing;
}
return currentState;
});
}, []);
return {
close,
isClosed,
overlayStyles,
modalStyles,
};