A drawer component for React.
Fix outside click when input is focused
emilkowalski6d9b62dc6567
diff --git a/src/context.ts b/src/context.tsindex 663e32b..ebc14d2 100644--- a/src/context.ts+++ b/src/context.ts@@ -9,17 +9,9 @@ interface DrawerContextValue {onMove: (event: React.PointerEvent<HTMLDivElement>) => void;dismissible: boolean;isOpen: boolean;+ keyboardIsOpen: React.MutableRefObject<boolean>;}-export const DrawerContext = React.createContext<DrawerContextValue>({- drawerRef: React.createRef(),- overlayRef: React.createRef(),- onAnimationStart: () => {},- onPress: () => {},- onRelease: () => {},- onMove: () => {},- dismissible: true,- isOpen: false,-});+export const DrawerContext = React.createContext<DrawerContextValue | undefined>(undefined);export const useDrawerContext = () => React.useContext(DrawerContext);diff --git a/src/index.tsx b/src/index.tsxindex 421e199..6106d81 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -106,7 +106,6 @@ function Root({const lastTimeDragPrevented = React.useRef<Date | null>(null);const pointerStartY = React.useRef(0);const keyboardIsOpen = React.useRef(false);- const animationEndTimer = React.useRef<NodeJS.Timeout>(null);const drawerRef = React.useRef<HTMLDivElement>(null);const initialViewportHeight = React.useRef(0);@@ -412,6 +411,7 @@ function Root({onMove,dismissible,isOpen,+ keyboardIsOpen,}}>{children}@@ -437,7 +437,8 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(function ({ children, onOpenAutoFocus, onPointerDownOutside, onAnimationEnd, ...rest },ref,) {- const { drawerRef, onPress, onRelease, onAnimationStart, onMove, dismissible, isOpen } = useDrawerContext();+ const { drawerRef, onPress, onRelease, onAnimationStart, onMove, dismissible, isOpen, keyboardIsOpen } =+ useDrawerContext();const composedRef = useComposedRefs(ref, drawerRef);const animationEndTimer = React.useRef<NodeJS.Timeout>(null);@@ -461,6 +462,12 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (}}}onPointerDownOutside={(e) => {+ if (keyboardIsOpen.current) {+ keyboardIsOpen.current = false;+ set(drawerRef.current, {+ '--hide-to': `200%`,+ });+ }if (!dismissible) {e.preventDefault();}diff --git a/src/style.css b/src/style.cssindex 01e3f2f..b2c20c3 100644--- a/src/style.css+++ b/src/style.css@@ -41,7 +41,7 @@transform: translateY(var(--hide-from, 0));}to {- transform: translateY(100%);+ transform: translateY(var(--hide-to, 100%));}}diff --git a/website/src/app/components/hero.tsx b/website/src/app/components/hero.tsxindex 0582355..ef1552c 100644--- a/website/src/app/components/hero.tsx+++ b/website/src/app/components/hero.tsx@@ -4,7 +4,7 @@ import { Drawer } from 'vaul';export function Hero() {return (- <div className="relative">+ <div className="relative h-[200vh]"><divaria-hiddenclassName="absolute top-0 w-[1000px] z-10 h-[400px] left-1/2 -translate-x-1/2 -translate-y-1/2 opacity-[0.15] pointer-events-none"@@ -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><buttontype="button"@@ -51,84 +51,11 @@ 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">- <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="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.- </p>- <p className="text-gray-600 mb-2">It comes unstyled and has gesture-driven animations.</p>- <p className="text-gray-600 mb-8">- It uses{' '}- <a- href="https://www.radix-ui.com/docs/primitives/components/dialog"- className="underline"- target="_blank"- >- Radix's Dialog primitive- </a>{' '}- under the hood and is inspired by{' '}- <a- href="https://twitter.com/devongovett/status/1674470185783402496"- className="underline"- target="_blank"- >- this tweet.- </a>- </p>- </div>- </div>- <div className="p-4 bg-gray-100 border-t border-gray-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"- href="https://github.com/emilkowalski/vaul"- target="_blank"- >- GitHub- <svg- fill="none"- height="16"- stroke="currentColor"- stroke-linecap="round"- stroke-linejoin="round"- stroke-width="2"- viewBox="0 0 24 24"- width="16"- aria-hidden="true"- className="w-3 h-3 ml-1"- >- <path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"></path>- <path d="M15 3h6v6"></path>- <path d="M10 14L21 3"></path>- </svg>- </a>- <a- className="text-xs text-gray-600 flex items-center gap-0.25"- href="https://twitter.com/emilkowalski_"- target="_blank"- >- <svg- fill="none"- height="16"- stroke="currentColor"- stroke-linecap="round"- stroke-linejoin="round"- stroke-width="2"- viewBox="0 0 24 24"- width="16"- aria-hidden="true"- className="w-3 h-3 ml-1"- >- <path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"></path>- <path d="M15 3h6v6"></path>- <path d="M10 14L21 3"></path>- </svg>- </a>- </div>+ <Drawer.Content className="bg-white flex flex-col fixed bottom-0 left-0 right-0 max-h-[50vh] rounded-t-[10px]">+ <div className="max-w-md w-full mx-auto flex flex-col overflow-auto p-4 rounded-t-[10px]">+ <input className="border border-gray-400 my-8" placeholder="Input" />++ <input className="border border-gray-400 my-8" placeholder="Input" /></div></Drawer.Content></Drawer.Portal>diff --git a/website/src/app/globals.css b/website/src/app/globals.cssindex 875c8f4..748e424 100644--- a/website/src/app/globals.css+++ b/website/src/app/globals.css@@ -2,11 +2,6 @@@tailwind components;@tailwind utilities;-html,-body {- overflow: hidden;-}-body,main {min-height: 100vh;