tutu/src/material/BottomSheet.tsx

168 lines
4.2 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 JSX,
type ParentComponent,
2024-08-13 07:01:06 +00:00
type ResolvedChildren,
} from "solid-js";
2024-10-31 14:00:32 +00:00
import "./BottomSheet.css";
import material from "./material.module.css";
2024-11-03 10:39:09 +00:00
import {
ANIM_CURVE_ACELERATION,
ANIM_CURVE_DECELERATION,
} from "./theme";
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-07-22 13:57:04 +00:00
const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
let element: HTMLDialogElement;
let animation: Animation | undefined;
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();
};
2024-08-12 13:55:26 +00:00
const animatedClose = () => {
2024-10-09 15:04:20 +00:00
if (window.innerWidth > 560 && !props.bottomUp) {
onClose();
return;
}
const onAnimationEnd = () => {
2024-11-03 10:39:09 +00:00
element.classList.remove("animated");
onClose();
};
element.classList.add("animated");
animation = props.bottomUp
2024-10-09 15:04:20 +00:00
? 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");
};
animation = animateSlideInFromRight(element, {
easing: ANIM_CURVE_DECELERATION,
});
animation.addEventListener("finish", onAnimationEnd);
animation.addEventListener("cancel", onAnimationEnd);
2024-10-09 15:04:20 +00:00
}
};
const animateSlideInFromBottom = (
element: HTMLElement,
reserve?: boolean,
) => {
const rect = element.getBoundingClientRect();
const easing = "cubic-bezier(0.4, 0, 0.2, 1)";
2024-10-31 14:00:32 +00:00
element.classList.add("animated");
const oldOverflow = document.body.style.overflow;
document.body.style.overflow = "hidden";
2024-10-09 15:04:20 +00:00
const distance = Math.abs(rect.top - window.innerHeight);
const duration = (distance / MOVE_SPEED) * 1000;
animation = element.animate(
{
top: reserve
? [`${rect.top}px`, `${window.innerHeight}px`]
: [`${window.innerHeight}px`, `${rect.top}px`],
},
{ easing, duration },
);
const onAnimationEnd = () => {
2024-10-31 14:00:32 +00:00
element.classList.remove("animated");
document.body.style.overflow = oldOverflow;
2024-10-09 15:04:20 +00:00
animation = undefined;
};
animation.addEventListener("cancel", onAnimationEnd);
animation.addEventListener("finish", onAnimationEnd);
return animation;
};
onCleanup(() => {
if (animation) {
animation.cancel();
}
});
const onDialogClick = (
event: MouseEvent & { currentTarget: HTMLDialogElement },
) => {
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-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;