tutu/src/material/BottomSheet.tsx

157 lines
4 KiB
TypeScript
Raw Normal View History

import {
2024-08-13 07:01:06 +00:00
children,
createEffect,
onCleanup,
2024-08-12 13:55:26 +00:00
useTransition,
type JSX,
type ParentComponent,
} from "solid-js";
2024-10-31 14:00:32 +00:00
import "./BottomSheet.css";
import material from "./material.module.css";
2024-11-23 12:19:43 +00:00
import { ANIM_CURVE_ACELERATION, ANIM_CURVE_DECELERATION } from "./theme";
2024-11-03 10:39:09 +00:00
import {
animateSlideInFromRight,
animateSlideOutToRight,
2024-11-22 09:16:56 +00:00
} 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;
class?: JSX.HTMLAttributes<HTMLElement>["class"];
2024-09-25 11:30:05 +00:00
onClose?(reason: "backdrop"): void;
2024-07-22 13:57:04 +00:00
};
const MOVE_SPEED = 1600;
2024-11-23 12:19:43 +00:00
function animateSlideInFromBottom(element: HTMLElement, reverse?: boolean) {
const rect = element.getBoundingClientRect();
const easing = "cubic-bezier(0.4, 0, 0.2, 1)";
element.classList.add("animated");
const oldOverflow = document.body.style.overflow;
document.body.style.overflow = "hidden";
const distance = Math.abs(rect.top - window.innerHeight);
const duration = (distance / MOVE_SPEED) * 1000;
const animation = element.animate(
{
top: reverse
? [`${rect.top}px`, `${window.innerHeight}px`]
: [`${window.innerHeight}px`, `${rect.top}px`],
},
{ easing, duration },
);
const onAnimationEnd = () => {
element.classList.remove("animated");
document.body.style.overflow = oldOverflow;
};
animation.addEventListener("cancel", onAnimationEnd);
animation.addEventListener("finish", onAnimationEnd);
return animation;
}
2024-07-22 13:57:04 +00:00
const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
let element: HTMLDialogElement;
let animation: Animation | undefined;
2024-11-23 12:19:43 +00:00
const child = 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-07-22 13:57:04 +00:00
}
} else {
if (element.open) {
2024-08-12 13:55:26 +00:00
animatedClose();
2024-07-22 13:57:04 +00:00
}
}
});
const onClose = () => {
element.close();
};
2024-08-12 13:55:26 +00:00
const animatedClose = () => {
2024-11-23 12:19:43 +00:00
if (window.innerWidth > 560 && !props.bottomUp) {
onClose();
return;
}
const onAnimationEnd = () => {
element.classList.remove("animated");
animation = undefined;
onClose();
};
element.classList.add("animated");
animation = props.bottomUp
? animateSlideInFromBottom(element, true)
: animateSlideOutToRight(element, { easing: ANIM_CURVE_ACELERATION });
animation.addEventListener("finish", onAnimationEnd);
animation.addEventListener("cancel", onAnimationEnd);
2024-08-12 13:55:26 +00:00
};
const animatedOpen = () => {
element.showModal();
if (props.bottomUp) {
2024-10-09 15:04:20 +00:00
animateSlideInFromBottom(element);
} else if (window.innerWidth <= 560) {
2024-11-03 10:39:09 +00:00
element.classList.add("animated");
const onAnimationEnd = () => {
2024-11-03 10:39:09 +00:00
element.classList.remove("animated");
2024-11-23 12:19:43 +00:00
animation = undefined;
2024-11-03 10:39:09 +00:00
};
animation = animateSlideInFromRight(element, {
easing: ANIM_CURVE_DECELERATION,
});
animation.addEventListener("finish", onAnimationEnd);
animation.addEventListener("cancel", onAnimationEnd);
2024-10-09 15:04:20 +00:00
}
};
onCleanup(() => {
if (animation) {
animation.cancel();
}
});
const onDialogClick = (
event: MouseEvent & { currentTarget: HTMLDialogElement },
) => {
2024-11-23 12:19:43 +00:00
event.stopPropagation();
if (event.target !== event.currentTarget) return;
const rect = event.currentTarget.getBoundingClientRect();
const isNotInDialog =
event.clientY < rect.top ||
event.clientY > rect.bottom ||
event.clientX < rect.left ||
event.clientX > rect.right;
if (isNotInDialog) {
props.onClose?.("backdrop");
}
};
2024-09-25 11:30:05 +00:00
const onDialogCancel = (event: Event) => {
event.preventDefault();
props.onClose?.("backdrop");
};
2024-08-05 07:33:00 +00:00
return (
2024-09-25 11:30:05 +00:00
<dialog
class={`BottomSheet ${material.surface} ${props.class || ""}`}
2024-09-25 11:30:05 +00:00
classList={{
2024-10-31 14:00:32 +00:00
["bottom"]: props.bottomUp,
2024-09-25 11:30:05 +00:00
}}
onClick={onDialogClick}
onCancel={onDialogCancel}
2024-09-25 11:30:05 +00:00
ref={element!}
2024-11-04 10:03:05 +00:00
tabIndex={-1}
role="presentation"
2024-09-25 11:30:05 +00:00
>
2024-11-23 12:19:43 +00:00
{child()}
2024-08-05 07:33:00 +00:00
</dialog>
);
2024-07-22 13:57:04 +00:00
};
export default BottomSheet;