2024-08-12 09:25:03 +00:00
|
|
|
import {
|
2024-08-13 07:01:06 +00:00
|
|
|
children,
|
2024-08-12 09:25:03 +00:00
|
|
|
createEffect,
|
2024-08-13 07:01:06 +00:00
|
|
|
createSignal,
|
2024-08-12 09:25:03 +00:00
|
|
|
onCleanup,
|
2024-08-12 13:55:26 +00:00
|
|
|
useTransition,
|
2024-08-12 09:25:03 +00:00
|
|
|
type ParentComponent,
|
2024-08-13 07:01:06 +00:00
|
|
|
type ResolvedChildren,
|
2024-08-12 09:25:03 +00:00
|
|
|
} from "solid-js";
|
2024-08-05 07:33:00 +00:00
|
|
|
import styles from "./BottomSheet.module.css";
|
2024-08-12 09:25:03 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-08-12 09:25:03 +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>,
|
|
|
|
) {
|
2024-08-12 09:25:03 +00:00
|
|
|
return {
|
|
|
|
top: `${top}px`,
|
|
|
|
left: `${left}px`,
|
|
|
|
height: `${height}px`,
|
|
|
|
width: `${width}px`,
|
2024-08-12 13:55:26 +00:00
|
|
|
...x,
|
2024-08-12 09:25:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2024-08-12 09:25:03 +00:00
|
|
|
let animation: Animation | undefined;
|
2024-08-17 07:37:10 +00:00
|
|
|
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()) {
|
2024-09-27 10:04:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-09-27 10:04:09 +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();
|
2024-08-17 07:37:10 +00:00
|
|
|
setHero();
|
2024-08-12 13:55:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const animatedOpen = () => {
|
|
|
|
element.showModal();
|
2024-08-12 09:25:03 +00:00
|
|
|
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)";
|
2024-08-12 09:25:03 +00:00
|
|
|
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 }),
|
|
|
|
],
|
2024-08-12 09:25:03 +00:00
|
|
|
{ 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;
|
2024-08-12 09:25:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
onCleanup(() => {
|
|
|
|
if (animation) {
|
|
|
|
animation.cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-09-27 10:04:09 +00:00
|
|
|
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,
|
|
|
|
}}
|
2024-09-27 10:04:09 +00:00
|
|
|
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;
|