2021-09-29 20:59:37 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-09-27 20:24:21 +00:00
|
|
|
import { useState, useCallback } from 'react';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { SpringValues } from '@react-spring/web';
|
|
|
|
import { useChain, useSpring, useSpringRef } from '@react-spring/web';
|
2021-09-29 20:59:37 +00:00
|
|
|
|
2021-10-14 16:52:42 +00:00
|
|
|
export type ModalConfigType = {
|
|
|
|
opacity: number;
|
|
|
|
transform?: string;
|
2023-04-20 16:31:59 +00:00
|
|
|
marginTop?: string;
|
2021-10-14 16:52:42 +00:00
|
|
|
};
|
|
|
|
|
2022-12-14 01:56:32 +00:00
|
|
|
enum ModalState {
|
2023-06-20 23:38:16 +00:00
|
|
|
Opening = 'Opening',
|
2022-12-14 01:56:32 +00:00
|
|
|
Open = 'Open',
|
|
|
|
Closing = 'Closing',
|
|
|
|
Closed = 'Closed',
|
|
|
|
}
|
|
|
|
|
2021-10-14 16:52:42 +00:00
|
|
|
export function useAnimated(
|
|
|
|
onClose: () => unknown,
|
|
|
|
{
|
|
|
|
getFrom,
|
|
|
|
getTo,
|
|
|
|
}: {
|
|
|
|
getFrom: (isOpen: boolean) => ModalConfigType;
|
|
|
|
getTo: (isOpen: boolean) => ModalConfigType;
|
|
|
|
}
|
2021-09-29 20:59:37 +00:00
|
|
|
): {
|
|
|
|
close: () => unknown;
|
2022-12-14 01:56:32 +00:00
|
|
|
isClosed: boolean;
|
2021-10-14 16:52:42 +00:00
|
|
|
modalStyles: SpringValues<ModalConfigType>;
|
|
|
|
overlayStyles: SpringValues<ModalConfigType>;
|
2021-09-29 20:59:37 +00:00
|
|
|
} {
|
2023-06-20 23:38:16 +00:00
|
|
|
const [state, setState] = useState(ModalState.Opening);
|
|
|
|
const shouldShowModal =
|
|
|
|
state === ModalState.Open || state === ModalState.Opening;
|
2022-12-14 01:56:32 +00:00
|
|
|
const isClosed = state === ModalState.Closed;
|
2021-09-29 20:59:37 +00:00
|
|
|
|
2021-10-14 16:52:42 +00:00
|
|
|
const modalRef = useSpringRef();
|
|
|
|
|
|
|
|
const modalStyles = useSpring({
|
2023-06-20 23:38:16 +00:00
|
|
|
from: getFrom(shouldShowModal),
|
|
|
|
to: getTo(shouldShowModal),
|
2021-10-14 16:52:42 +00:00
|
|
|
onRest: () => {
|
2022-12-14 01:56:32 +00:00
|
|
|
if (state === ModalState.Closing) {
|
|
|
|
setState(ModalState.Closed);
|
2021-10-14 16:52:42 +00:00
|
|
|
onClose();
|
2023-06-20 23:38:16 +00:00
|
|
|
} else if (state === ModalState.Opening) {
|
|
|
|
setState(ModalState.Open);
|
2021-10-14 16:52:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
clamp: true,
|
|
|
|
friction: 20,
|
|
|
|
mass: 0.5,
|
|
|
|
tension: 350,
|
2021-09-29 20:59:37 +00:00
|
|
|
},
|
2021-10-14 16:52:42 +00:00
|
|
|
ref: modalRef,
|
|
|
|
});
|
|
|
|
|
|
|
|
const overlayRef = useSpringRef();
|
|
|
|
|
|
|
|
const overlayStyles = useSpring({
|
|
|
|
from: { opacity: 0 },
|
2023-06-20 23:38:16 +00:00
|
|
|
to: { opacity: shouldShowModal ? 1 : 0 },
|
2021-09-29 20:59:37 +00:00
|
|
|
config: {
|
2021-10-14 16:52:42 +00:00
|
|
|
clamp: true,
|
|
|
|
friction: 22,
|
|
|
|
tension: 360,
|
2021-09-29 20:59:37 +00:00
|
|
|
},
|
2021-10-14 16:52:42 +00:00
|
|
|
ref: overlayRef,
|
2021-09-29 20:59:37 +00:00
|
|
|
});
|
|
|
|
|
2023-06-20 23:38:16 +00:00
|
|
|
useChain(shouldShowModal ? [overlayRef, modalRef] : [modalRef, overlayRef]);
|
2022-12-14 01:56:32 +00:00
|
|
|
const close = useCallback(() => {
|
|
|
|
setState(currentState => {
|
|
|
|
if (currentState === ModalState.Open) {
|
|
|
|
return ModalState.Closing;
|
|
|
|
}
|
|
|
|
return currentState;
|
|
|
|
});
|
|
|
|
}, []);
|
2021-10-14 16:52:42 +00:00
|
|
|
|
2021-09-29 20:59:37 +00:00
|
|
|
return {
|
2022-09-27 20:24:21 +00:00
|
|
|
close,
|
2022-12-14 01:56:32 +00:00
|
|
|
isClosed,
|
2021-10-14 16:52:42 +00:00
|
|
|
overlayStyles,
|
|
|
|
modalStyles,
|
2021-09-29 20:59:37 +00:00
|
|
|
};
|
|
|
|
}
|