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-10-31 14:33:29 +00:00
|
|
|
type JSX,
|
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-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;
|
2024-10-31 14:33:29 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-11-03 09:36:03 +00:00
|
|
|
const MOVE_SPEED = 1600;
|
2024-08-12 09:25:03 +00:00
|
|
|
|
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-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();
|
|
|
|
};
|
|
|
|
|
2024-08-12 13:55:26 +00:00
|
|
|
const animatedClose = () => {
|
2024-11-19 09:49:49 +00:00
|
|
|
|
2024-10-09 15:04:20 +00:00
|
|
|
if (window.innerWidth > 560 && !props.bottomUp) {
|
|
|
|
onClose();
|
|
|
|
return;
|
|
|
|
}
|
2024-11-03 09:36:03 +00:00
|
|
|
const onAnimationEnd = () => {
|
2024-11-03 10:39:09 +00:00
|
|
|
element.classList.remove("animated");
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
element.classList.add("animated");
|
2024-11-03 09:36:03 +00:00
|
|
|
animation = props.bottomUp
|
2024-10-09 15:04:20 +00:00
|
|
|
? animateSlideInFromBottom(element, true)
|
2024-11-03 09:36:03 +00:00
|
|
|
: animateSlideOutToRight(element, { easing: ANIM_CURVE_ACELERATION });
|
|
|
|
animation.addEventListener("finish", onAnimationEnd);
|
|
|
|
animation.addEventListener("cancel", onAnimationEnd);
|
2024-11-19 09:49:49 +00:00
|
|
|
|
2024-08-12 13:55:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const animatedOpen = () => {
|
|
|
|
element.showModal();
|
2024-11-19 09:49:49 +00:00
|
|
|
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");
|
2024-11-03 09:36:03 +00:00
|
|
|
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");
|
2024-10-09 15:27:47 +00:00
|
|
|
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");
|
2024-10-09 15:27:47 +00:00
|
|
|
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;
|
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 },
|
|
|
|
) => {
|
2024-11-07 08:17:42 +00:00
|
|
|
if (event.target !== event.currentTarget) return;
|
2024-09-27 10:04:09 +00:00
|
|
|
const rect = event.currentTarget.getBoundingClientRect();
|
2024-11-08 07:24:25 +00:00
|
|
|
const isNotInDialog =
|
|
|
|
event.clientY < rect.top ||
|
|
|
|
event.clientY > rect.bottom ||
|
2024-11-07 08:17:42 +00:00
|
|
|
event.clientX < rect.left ||
|
|
|
|
event.clientX > rect.right;
|
|
|
|
if (isNotInDialog) {
|
2024-09-27 10:04:09 +00:00
|
|
|
props.onClose?.("backdrop");
|
|
|
|
}
|
|
|
|
};
|
2024-09-25 11:30:05 +00:00
|
|
|
|
2024-11-08 07:24:25 +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
|
2024-10-31 14:33:29 +00:00
|
|
|
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
|
|
|
}}
|
2024-09-27 10:04:09 +00:00
|
|
|
onClick={onDialogClick}
|
2024-11-08 07:24:25 +00:00
|
|
|
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;
|