tutu/src/material/BottomSheet.tsx

240 lines
6.5 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 { useHeroSignal } from "../platform/anim";
2024-10-31 14:00:32 +00:00
import material from "./material.module.css";
2024-11-03 10:39:09 +00:00
import {
ANIM_CURVE_ACELERATION,
ANIM_CURVE_DECELERATION,
ANIM_CURVE_STD,
} from "./theme";
import {
animateSlideInFromRight,
animateSlideOutToRight,
} 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
};
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 = 1600;
2024-07-22 13:57:04 +00:00
const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
let element: HTMLDialogElement;
let animation: Animation | undefined;
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()) {
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 = () => {
2024-10-09 15:04:20 +00:00
const srcElement = hero();
if (srcElement) {
srcElement.style.visibility = "unset";
}
element.close();
setHero();
};
2024-08-12 13:55:26 +00:00
const animatedClose = () => {
2024-10-09 15:04:20 +00:00
const srcElement = hero();
2024-10-09 09:33:27 +00:00
const endRect = srcElement?.getBoundingClientRect();
2024-08-12 13:55:26 +00:00
if (endRect) {
const startRect = element.getBoundingClientRect();
const animation = animateHero(startRect, endRect, element, true);
animation.addEventListener("finish", onClose);
animation.addEventListener("cancel", onClose);
} else {
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();
2024-10-09 15:04:20 +00:00
const srcElement = hero();
2024-10-09 09:33:27 +00:00
const startRect = srcElement?.getBoundingClientRect();
2024-10-29 07:22:25 +00:00
if (!startRect) {
2024-10-31 14:00:32 +00:00
console.debug("no source element");
2024-10-29 07:22:25 +00:00
}
2024-10-09 15:04:20 +00:00
if (startRect) {
srcElement!.style.visibility = "hidden";
const endRect = element.getBoundingClientRect();
animateHero(startRect, endRect, element);
} else if (props.bottomUp) {
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;
2024-08-12 13:55:26 +00:00
};
const animateHero = (
startRect: DOMRect,
endRect: DOMRect,
element: HTMLElement,
reserve?: boolean,
) => {
2024-11-03 10:39:09 +00:00
const easing = ANIM_CURVE_STD;
2024-10-31 14:00:32 +00:00
element.classList.add("animated");
2024-11-03 10:39:09 +00:00
// distance_lt = (|top_start - top_end|^2 + |left_end - left_end|^2)^(-2)
const distancelt = Math.sqrt(
Math.pow(Math.abs(startRect.top - endRect.top), 2) +
2024-11-03 10:39:09 +00:00
Math.pow(Math.abs(startRect.left - endRect.left), 2),
);
2024-11-03 10:39:09 +00:00
const distancerb = Math.sqrt(
Math.pow(Math.abs(startRect.bottom - endRect.bottom), 2) +
Math.pow(Math.abs(startRect.right - endRect.right), 2),
);
const distance = distancelt + distancerb;
const duration = distance / 1.6;
animation = element.animate(
2024-08-12 13:55:26 +00:00
[
composeAnimationFrame(startRect, { transform: "none" }),
composeAnimationFrame(endRect, { transform: "none" }),
2024-08-12 13:55:26 +00:00
],
{ easing, duration },
);
const onAnimationEnd = () => {
2024-10-31 14:00:32 +00:00
element.classList.remove("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();
}
});
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
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}
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;