BottomSheet: stop clicks propagation
This commit is contained in:
parent
62aaaeee9a
commit
f50ed8d907
1 changed files with 46 additions and 57 deletions
|
@ -1,19 +1,14 @@
|
||||||
import {
|
import {
|
||||||
children,
|
children,
|
||||||
createEffect,
|
createEffect,
|
||||||
createSignal,
|
|
||||||
onCleanup,
|
onCleanup,
|
||||||
useTransition,
|
useTransition,
|
||||||
type JSX,
|
type JSX,
|
||||||
type ParentComponent,
|
type ParentComponent,
|
||||||
type ResolvedChildren,
|
|
||||||
} from "solid-js";
|
} from "solid-js";
|
||||||
import "./BottomSheet.css";
|
import "./BottomSheet.css";
|
||||||
import material from "./material.module.css";
|
import material from "./material.module.css";
|
||||||
import {
|
import { ANIM_CURVE_ACELERATION, ANIM_CURVE_DECELERATION } from "./theme";
|
||||||
ANIM_CURVE_ACELERATION,
|
|
||||||
ANIM_CURVE_DECELERATION,
|
|
||||||
} from "./theme";
|
|
||||||
import {
|
import {
|
||||||
animateSlideInFromRight,
|
animateSlideInFromRight,
|
||||||
animateSlideOutToRight,
|
animateSlideOutToRight,
|
||||||
|
@ -28,11 +23,36 @@ export type BottomSheetProps = {
|
||||||
|
|
||||||
const MOVE_SPEED = 1600;
|
const MOVE_SPEED = 1600;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
|
const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
|
||||||
let element: HTMLDialogElement;
|
let element: HTMLDialogElement;
|
||||||
let animation: Animation | undefined;
|
let animation: Animation | undefined;
|
||||||
const [cache, setCache] = createSignal<ResolvedChildren | undefined>();
|
const child = children(() => props.children);
|
||||||
const ochildren = children(() => props.children);
|
|
||||||
|
|
||||||
const [pending] = useTransition();
|
const [pending] = useTransition();
|
||||||
|
|
||||||
|
@ -40,12 +60,10 @@ const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
|
||||||
if (props.open) {
|
if (props.open) {
|
||||||
if (!element.open && !pending()) {
|
if (!element.open && !pending()) {
|
||||||
requestAnimationFrame(animatedOpen);
|
requestAnimationFrame(animatedOpen);
|
||||||
setCache(ochildren());
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (element.open) {
|
if (element.open) {
|
||||||
animatedClose();
|
animatedClose();
|
||||||
setCache(undefined);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -55,22 +73,21 @@ const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const animatedClose = () => {
|
const animatedClose = () => {
|
||||||
|
if (window.innerWidth > 560 && !props.bottomUp) {
|
||||||
if (window.innerWidth > 560 && !props.bottomUp) {
|
onClose();
|
||||||
onClose();
|
return;
|
||||||
return;
|
}
|
||||||
}
|
const onAnimationEnd = () => {
|
||||||
const onAnimationEnd = () => {
|
element.classList.remove("animated");
|
||||||
element.classList.remove("animated");
|
animation = undefined;
|
||||||
onClose();
|
onClose();
|
||||||
};
|
};
|
||||||
element.classList.add("animated");
|
element.classList.add("animated");
|
||||||
animation = props.bottomUp
|
animation = props.bottomUp
|
||||||
? animateSlideInFromBottom(element, true)
|
? animateSlideInFromBottom(element, true)
|
||||||
: animateSlideOutToRight(element, { easing: ANIM_CURVE_ACELERATION });
|
: animateSlideOutToRight(element, { easing: ANIM_CURVE_ACELERATION });
|
||||||
animation.addEventListener("finish", onAnimationEnd);
|
animation.addEventListener("finish", onAnimationEnd);
|
||||||
animation.addEventListener("cancel", onAnimationEnd);
|
animation.addEventListener("cancel", onAnimationEnd);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const animatedOpen = () => {
|
const animatedOpen = () => {
|
||||||
|
@ -81,6 +98,7 @@ const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
|
||||||
element.classList.add("animated");
|
element.classList.add("animated");
|
||||||
const onAnimationEnd = () => {
|
const onAnimationEnd = () => {
|
||||||
element.classList.remove("animated");
|
element.classList.remove("animated");
|
||||||
|
animation = undefined;
|
||||||
};
|
};
|
||||||
animation = animateSlideInFromRight(element, {
|
animation = animateSlideInFromRight(element, {
|
||||||
easing: ANIM_CURVE_DECELERATION,
|
easing: ANIM_CURVE_DECELERATION,
|
||||||
|
@ -90,36 +108,6 @@ const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const animateSlideInFromBottom = (
|
|
||||||
element: HTMLElement,
|
|
||||||
reserve?: 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;
|
|
||||||
|
|
||||||
animation = element.animate(
|
|
||||||
{
|
|
||||||
top: reserve
|
|
||||||
? [`${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 = undefined;
|
|
||||||
};
|
|
||||||
animation.addEventListener("cancel", onAnimationEnd);
|
|
||||||
animation.addEventListener("finish", onAnimationEnd);
|
|
||||||
return animation;
|
|
||||||
};
|
|
||||||
|
|
||||||
onCleanup(() => {
|
onCleanup(() => {
|
||||||
if (animation) {
|
if (animation) {
|
||||||
animation.cancel();
|
animation.cancel();
|
||||||
|
@ -129,6 +117,7 @@ const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
|
||||||
const onDialogClick = (
|
const onDialogClick = (
|
||||||
event: MouseEvent & { currentTarget: HTMLDialogElement },
|
event: MouseEvent & { currentTarget: HTMLDialogElement },
|
||||||
) => {
|
) => {
|
||||||
|
event.stopPropagation();
|
||||||
if (event.target !== event.currentTarget) return;
|
if (event.target !== event.currentTarget) return;
|
||||||
const rect = event.currentTarget.getBoundingClientRect();
|
const rect = event.currentTarget.getBoundingClientRect();
|
||||||
const isNotInDialog =
|
const isNotInDialog =
|
||||||
|
@ -159,7 +148,7 @@ const BottomSheet: ParentComponent<BottomSheetProps> = (props) => {
|
||||||
tabIndex={-1}
|
tabIndex={-1}
|
||||||
role="presentation"
|
role="presentation"
|
||||||
>
|
>
|
||||||
{ochildren() ?? cache()}
|
{child()}
|
||||||
</dialog>
|
</dialog>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue