tutu/src/material/BottomSheet.tsx

132 lines
3.2 KiB
TypeScript
Raw Normal View History

import {
2024-08-13 07:01:06 +00:00
children,
createEffect,
createRenderEffect,
2024-08-13 07:01:06 +00:00
createSignal,
onCleanup,
onMount,
2024-08-12 13:55:26 +00:00
startTransition,
useTransition,
2024-08-13 07:01:06 +00:00
type ChildrenReturn,
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;
};
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 = 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()) {
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
}
}
});
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);
const onClose = () => {
element.close();
};
animation.addEventListener("finish", onClose);
animation.addEventListener("cancel", onClose);
} else {
element.close();
}
};
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();
}
});
2024-08-05 07:33:00 +00:00
return (
<dialog class={styles.bottomSheet} 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;