tutu/src/material/BottomSheet.tsx

154 lines
3.7 KiB
TypeScript
Raw Normal View History

import {
2024-08-13 07:01:06 +00:00
children,
createEffect,
2024-08-13 07:01:06 +00:00
createSignal,
onCleanup,
2024-08-12 13:55:26 +00:00
useTransition,
type ParentComponent,
2024-08-13 07:01:06 +00:00
type ResolvedChildren,
} from "solid-js";
2024-08-05 07:33:00 +00:00
import styles from "./BottomSheet.module.css";
import { useHeroSignal } from "../platform/anim";
2024-07-22 13:57:04 +00:00
export type BottomSheetProps = {
open?: boolean;
2024-09-25 11:30:05 +00:00
bottomUp?: boolean;
onClose?(reason: "backdrop"): void;
2024-07-22 13:57:04 +00:00
};
export const HERO = Symbol("BottomSheet Hero Symbol");
2024-08-12 13:55:26 +00:00
function composeAnimationFrame(
{
top,
left,
height,
width,
}: Record<"top" | "left" | "height" | "width", number>,
x: Record<string, unknown>,
) {
return {
top: `${top}px`,
left: `${left}px`,
height: `${height}px`,
width: `${width}px`,
2024-08-12 13:55:26 +00:00
...x,
};
}
const MOVE_SPEED = 1400; // 1400px/s, bottom sheet is big and a bit heavier than small papers
2024-07-22 13:57:04 +00:00
const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
let element: HTMLDialogElement;
let animation: Animation | undefined;
const [hero, setHero] = useHeroSignal(HERO);
2024-08-13 07:01:06 +00:00
const [cache, setCache] = createSignal<ResolvedChildren | undefined>();
const ochildren = children(() => props.children);
2024-07-22 13:57:04 +00:00
2024-08-13 07:01:06 +00:00
const [pending] = useTransition();
2024-08-12 13:55:26 +00:00
2024-07-22 13:57:04 +00:00
createEffect(() => {
if (props.open) {
2024-08-12 13:55:26 +00:00
if (!element.open && !pending()) {
requestAnimationFrame(animatedOpen);
2024-08-13 07:01:06 +00:00
setCache(ochildren());
2024-07-22 13:57:04 +00:00
}
} else {
if (element.open) {
2024-08-12 13:55:26 +00:00
animatedClose();
2024-08-13 07:01:06 +00:00
setCache(undefined);
2024-07-22 13:57:04 +00:00
}
}
});
const onClose = () => {
element.close();
setHero();
};
2024-08-12 13:55:26 +00:00
const animatedClose = () => {
const endRect = hero();
if (endRect) {
const startRect = element.getBoundingClientRect();
const animation = animateHero(startRect, endRect, element, true);
animation.addEventListener("finish", onClose);
animation.addEventListener("cancel", onClose);
} else {
element.close();
setHero();
2024-08-12 13:55:26 +00:00
}
};
const animatedOpen = () => {
element.showModal();
const startRect = hero();
if (!startRect) return;
const endRect = element.getBoundingClientRect();
2024-08-12 13:55:26 +00:00
animateHero(startRect, endRect, element);
};
const animateHero = (
startRect: DOMRect,
endRect: DOMRect,
element: HTMLElement,
reserve?: boolean,
) => {
const easing = "cubic-bezier(0.4, 0, 0.2, 1)";
element.classList.add(styles.animated);
const distance = Math.sqrt(
Math.pow(Math.abs(startRect.top - endRect.top), 2) +
Math.pow(Math.abs(startRect.left - startRect.top), 2),
);
const duration = (distance / MOVE_SPEED) * 1000;
animation = element.animate(
2024-08-12 13:55:26 +00:00
[
composeAnimationFrame(startRect, { opacity: reserve ? 1 : 0.5 }),
composeAnimationFrame(endRect, { opacity: reserve ? 0.5 : 1 }),
],
{ easing, duration },
);
const onAnimationEnd = () => {
element.classList.remove(styles.animated);
animation = undefined;
};
animation.addEventListener("finish", onAnimationEnd);
animation.addEventListener("cancel", onAnimationEnd);
2024-08-12 13:55:26 +00:00
return animation;
};
onCleanup(() => {
if (animation) {
animation.cancel();
}
});
const onDialogClick = (
event: MouseEvent & { currentTarget: HTMLDialogElement },
) => {
const rect = event.currentTarget.getBoundingClientRect();
const isInDialog =
rect.top <= event.clientY &&
event.clientY <= rect.top + rect.height &&
rect.left <= event.clientX &&
event.clientX <= rect.left + rect.width;
if (!isInDialog) {
props.onClose?.("backdrop");
}
};
2024-09-25 11:30:05 +00:00
2024-08-05 07:33:00 +00:00
return (
2024-09-25 11:30:05 +00:00
<dialog
classList={{
[styles.bottomSheet]: true,
[styles.bottom]: props.bottomUp,
}}
onClick={onDialogClick}
2024-09-25 11:30:05 +00:00
ref={element!}
>
2024-08-13 07:01:06 +00:00
{ochildren() ?? cache()}
2024-08-05 07:33:00 +00:00
</dialog>
);
2024-07-22 13:57:04 +00:00
};
export default BottomSheet;