Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Fix input focus, transition overlay when background scaling is disabled

emilkowalski224c7a2423e6
diff --git a/src/index.tsx b/src/index.tsx
index cecaf85..2f65bd4 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -71,16 +71,17 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
defaultProp: defaultOpen,
onChange: onOpenChange,
});
+ const [isDragging, setIsDragging] = React.useState(false);
const overlayRef = React.useRef<HTMLDivElement>(null);
const dragStartTime = React.useRef<Date | null>(null);
const dragEndTime = React.useRef<Date | null>(null);
const lastTimeDragPrevented = React.useRef<Date | null>(null);
- const isMouseDown = React.useRef(false);
const pointerStartY = React.useRef(0);
const drawerRef = React.useRef<HTMLDivElement>(null);
const initialViewportHeight = React.useRef(0);
+
usePreventScroll({
- isDisabled: !isOpen,
+ isDisabled: !isOpen || isDragging,
});
function getScale() {
@@ -88,7 +89,7 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
}
function onPress(event: React.PointerEvent<HTMLDivElement>) {
- isMouseDown.current = true;
+ setIsDragging(true);
dragStartTime.current = new Date();
// Ensure we maintain correct pointer capture even when going outside of the drawer
@@ -144,7 +145,7 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
function onMove(event: React.PointerEvent<HTMLDivElement>) {
// We need to know how much of the drawer has been dragged in percentages so that we can transform background accordingly
- if (isMouseDown.current) {
+ if (isDragging) {
const draggedDistance = pointerStartY.current - (event as unknown as React.PointerEvent<HTMLDivElement>).clientY;
const isDraggingDown = draggedDistance > 0;
@@ -168,13 +169,23 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
const absDraggedDistance = Math.abs(draggedDistance);
const wrapper = document.querySelector('[vaul-drawer-wrapper]');
+ const percentageDragged = absDraggedDistance / drawerHeight;
+ const opacityValue = 1 - percentageDragged;
+
+ set(
+ overlayRef.current,
+ {
+ opacity: `${opacityValue}`,
+ },
+ true,
+ );
+
if (wrapper && overlayRef.current && shouldScaleBackground) {
// Calculate percentageDragged as a fraction (0 to 1)
- const percentageDragged = absDraggedDistance / drawerHeight;
const scaleValue = Math.min(getScale() + percentageDragged * (1 - getScale()), 1);
const borderRadiusValue = 8 - percentageDragged * 8;
- const opacityValue = 1 - percentageDragged;
+
const translateYValue = Math.max(0, 14 - percentageDragged * 14);
set(
@@ -186,14 +197,6 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
},
true,
);
-
- set(
- overlayRef.current,
- {
- opacity: `${opacityValue}`,
- },
- true,
- );
}
set(drawerRef.current, {
@@ -286,7 +289,7 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
}
function onRelease(event: React.PointerEvent<HTMLDivElement>) {
- isMouseDown.current = false;
+ setIsDragging(false);
dragEndTime.current = new Date();
const swipeAmount = drawerRef.current
? getComputedStyle(drawerRef.current).getPropertyValue('--swipe-amount').slice(0, -2)
@@ -360,7 +363,7 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
<DialogPrimitive.Root
open={isOpen}
onOpenChange={(o) => {
- isMouseDown.current = false;
+ setIsDragging(false);
setIsOpen(o);
}}
>
diff --git a/website/src/app/components/hero.tsx b/website/src/app/components/hero.tsx
index 0582355..c5e4441 100644
--- a/website/src/app/components/hero.tsx
+++ b/website/src/app/components/hero.tsx
@@ -40,7 +40,7 @@ export function Hero() {
<p className="text-gray-600 text-xl">Drawer component for React.</p>
</div>
<div className="flex gap-4 justify-center mt-6">
- <Drawer.Root shouldScaleBackground>
+ <Drawer.Root>
<Drawer.Trigger asChild>
<button
type="button"
@@ -51,16 +51,15 @@ export function Hero() {
</Drawer.Trigger>
<Drawer.Overlay className="fixed inset-0 bg-black/40" />
<Drawer.Portal>
- <Drawer.Content className="bg-gray-100 flex flex-col rounded-t-[10px] h-full mt-24 max-h-[96%] fixed bottom-0 left-0 right-0">
+ <Drawer.Content className="bg-zinc-100 flex flex-col rounded-t-[10px] mt-24 fixed bottom-0 left-0 right-0">
<div className="p-4 bg-white rounded-t-[10px] flex-1">
- <div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-gray-300 mb-8" />
+ <div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-zinc-300 mb-8" />
<div className="max-w-md mx-auto">
- <Drawer.Title className="font-medium mb-4">Drawer for React.</Drawer.Title>
- <p className="text-gray-600 mb-2">
- This component can be used as a Dialog replacement on mobile and tablet devices.
+ <Drawer.Title className="font-medium mb-4">Unstyled drawer for React.</Drawer.Title>
+ <p className="text-zinc-600 mb-2">
+ This component can be used as a replacement for a Dialog on mobile and tablet devices.
</p>
- <p className="text-gray-600 mb-2">It comes unstyled and has gesture-driven animations.</p>
- <p className="text-gray-600 mb-8">
+ <p className="text-zinc-600 mb-8">
It uses{' '}
<a
href="https://www.radix-ui.com/docs/primitives/components/dialog"
@@ -80,10 +79,10 @@ export function Hero() {
</p>
</div>
</div>
- <div className="p-4 bg-gray-100 border-t border-gray-200 mt-auto">
+ <div className="p-4 bg-zinc-100 border-t border-zinc-200 mt-auto">
<div className="flex gap-6 justify-end max-w-md mx-auto">
<a
- className="text-xs text-gray-600 flex items-center gap-0.25"
+ className="text-xs text-zinc-600 flex items-center gap-0.25"
href="https://github.com/emilkowalski/vaul"
target="_blank"
>
@@ -106,7 +105,7 @@ export function Hero() {
</svg>
</a>
<a
- className="text-xs text-gray-600 flex items-center gap-0.25"
+ className="text-xs text-zinc-600 flex items-center gap-0.25"
href="https://twitter.com/emilkowalski_"
target="_blank"
>