feat: add control of movePercentage to add backdrop brightness

This commit is contained in:
p-sw 2024-06-02 01:15:30 +09:00
parent ad9dfb38f3
commit bd3892e314

View File

@ -73,7 +73,7 @@ const DrawerTrigger = ({ children }: { children: React.ReactNode }) => {
}; };
const [drawerOverlayVariant, resolveDrawerOverlayVariantProps] = vcn({ const [drawerOverlayVariant, resolveDrawerOverlayVariantProps] = vcn({
base: "fixed inset-0", base: "fixed inset-0 transition-[backdrop-filter] duration-75",
variants: { variants: {
opened: { opened: {
true: "pointer-events-auto select-auto", true: "pointer-events-auto select-auto",
@ -85,6 +85,8 @@ const [drawerOverlayVariant, resolveDrawerOverlayVariantProps] = vcn({
}, },
}); });
const DRAWER_OVERLAY_BACKDROP_FILTER_BRIGHTNESS = 0.3;
interface DrawerOverlayProps interface DrawerOverlayProps
extends VariantProps<typeof drawerOverlayVariant>, extends VariantProps<typeof drawerOverlayVariant>,
AsChild, AsChild,
@ -116,7 +118,16 @@ const DrawerOverlay = forwardRef<HTMLDivElement, DrawerOverlayProps>(
opened: state.isDragging ? true : state.opened, opened: state.isDragging ? true : state.opened,
})} })}
onClick={onOutsideClick} onClick={onOutsideClick}
style={/*{ backdropFilter: "brightness(0.5)" }*/ undefined} style={{
backdropFilter: `brightness(${
state.isDragging
? state.movePercentage + DRAWER_OVERLAY_BACKDROP_FILTER_BRIGHTNESS
: state.opened
? DRAWER_OVERLAY_BACKDROP_FILTER_BRIGHTNESS
: 1
})`,
transitionDuration: state.isDragging ? "0s" : undefined,
}}
ref={ref} ref={ref}
/>, />,
document.body document.body
@ -197,11 +208,13 @@ const DrawerContent = forwardRef<HTMLDivElement, DrawerContentProps>(
Math.abs(dragState.delta) > state.closeThreshold * size Math.abs(dragState.delta) > state.closeThreshold * size
? false ? false
: prev.opened, : prev.opened,
movePercentage: 0,
})); }));
} else { } else {
setState((prev) => ({ setState((prev) => ({
...prev, ...prev,
isDragging: false, isDragging: false,
movePercentage: 0,
})); }));
} }
setDragState({ setDragState({
@ -232,6 +245,24 @@ const DrawerContent = forwardRef<HTMLDivElement, DrawerContentProps>(
delta: prev.delta + movement, delta: prev.delta + movement,
}; };
}); });
if (
e.target instanceof Element &&
internalRef.current &&
internalRef.current.contains(e.target)
) {
const size = ["top", "bottom"].includes(position)
? e.target.getBoundingClientRect().height
: e.target.getBoundingClientRect().width;
const movePercentage = dragState.delta / size;
setState((prev) => ({
...prev,
movePercentage: ["top", "left"].includes(position)
? -movePercentage
: movePercentage,
}));
}
} }
} }