A drawer component for React.
Remove the need for a fixedHeight prop
emilkowalski014ce0fa153c
diff --git a/src/index.tsx b/src/index.tsxindex 7c9f3c1..2fcf2fd 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -5,7 +5,7 @@ import { useControllableState } from './use-controllable-state';import { DrawerContext, useDrawerContext } from './context';import React, { useEffect } from 'react';import './style.css';-import { usePreventScroll } from './use-prevent-scroll';+import { usePreventScroll, isInput } from './use-prevent-scroll';import { useComposedRefs } from './use-composed-refs';const CLOSE_TRESHOLD = 0.75;@@ -23,6 +23,18 @@ interface Style {[key: string]: string;}+function isInView(el: HTMLElement): boolean {+ const rect = el.getBoundingClientRect();++ return (+ rect.top >= 0 &&+ rect.left >= 0 &&+ // Need + 40 for safari detection+ rect.bottom <= window.visualViewport.height + 40 &&+ rect.right <= window.visualViewport.width+ );+}+function set(el?: Element | HTMLElement | null, styles?: Style, ignoreCache = false) {if (!el || !(el instanceof HTMLElement) || !styles) return;let originalStyles: Style = {};@@ -67,7 +79,6 @@ interface DialogProps {onOpenChange?(open: boolean): void;shouldScaleBackground?: boolean;dismissible?: boolean;- fixedHeight?: boolean;}function Root({@@ -76,7 +87,6 @@ function Root({onOpenChange,children,shouldScaleBackground,- fixedHeight,closeTreshold = CLOSE_TRESHOLD,dismissible = true,}: DialogProps) {@@ -91,11 +101,12 @@ function Root({const dragEndTime = React.useRef<Date | null>(null);const lastTimeDragPrevented = React.useRef<Date | null>(null);const pointerStartY = React.useRef(0);+ const keyboardIsOpen = React.useRef(false);const drawerRef = React.useRef<HTMLDivElement>(null);const initialViewportHeight = React.useRef(0);usePreventScroll({- isDisabled: !isOpen || isDragging || fixedHeight,+ isDisabled: !isOpen || isDragging,});function getScale() {@@ -223,20 +234,24 @@ function Root({initialViewportHeight.current = window.visualViewport.height;function onVisualViewportChange() {- if (!drawerRef.current || fixedHeight) return;-- const visualViewportHeight = window.visualViewport.height;- const diffFromInitial = initialViewportHeight.current - visualViewportHeight;- const drawerHeight = drawerRef.current?.getBoundingClientRect().height || 0;- const offsetFromTop = drawerRef.current?.getBoundingClientRect().top;+ if (!drawerRef.current) return;+ const focusedElement = document.activeElement as HTMLElement;++ if ((!isInView(focusedElement) && isInput(focusedElement)) || keyboardIsOpen.current) {+ const visualViewportHeight = window.visualViewport.height;+ const diffFromInitial = initialViewportHeight.current - visualViewportHeight;+ const drawerHeight = drawerRef.current?.getBoundingClientRect().height || 0;+ const offsetFromTop = drawerRef.current?.getBoundingClientRect().top;+ keyboardIsOpen.current = !keyboardIsOpen.current;+ // We don't have to change the height if the input is in view, when we are here we are in the opened keyboard state so we can accuretly check if the input is in view+ if (drawerHeight > visualViewportHeight) {+ drawerRef.current.style.height = `${visualViewportHeight - offsetFromTop}px`;+ } else {+ drawerRef.current.style.height = 'initial';+ }- if (drawerHeight > visualViewportHeight) {- drawerRef.current.style.height = `${visualViewportHeight - offsetFromTop}px`;- } else {- drawerRef.current.style.height = 'initial';+ drawerRef.current.style.bottom = `${diffFromInitial}px`;}-- drawerRef.current.style.bottom = `${diffFromInitial}px`;}window.visualViewport.addEventListener('resize', onVisualViewportChange);diff --git a/src/use-prevent-scroll.ts b/src/use-prevent-scroll.tsindex 4a5d0c2..c80c174 100644--- a/src/use-prevent-scroll.ts+++ b/src/use-prevent-scroll.ts@@ -190,7 +190,7 @@ function preventScrollMobileSafari() {let target = e.target as HTMLElement;// Apply this change if we're not already focused on the target element- if (willOpenKeyboard(target) && target !== document.activeElement) {+ if (isInput(target) && target !== document.activeElement) {e.preventDefault();// Apply a transform to trick Safari into thinking the input is at the top of the page@@ -206,7 +206,7 @@ function preventScrollMobileSafari() {let onFocus = (e: FocusEvent) => {let target = e.target as HTMLElement;- if (willOpenKeyboard(target)) {+ if (isInput(target)) {// Transform also needs to be applied in the focus event in cases where focus moves// other than tapping on an input directly, e.g. the next/previous buttons in the// software keyboard. In these cases, it seems applying the transform in the focus event@@ -317,7 +317,7 @@ function scrollIntoView(target: Element) {}}-function willOpenKeyboard(target: Element) {+export function isInput(target: Element) {return ((target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type)) ||target instanceof HTMLTextAreaElement ||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 3b047df..748e424 100644--- a/website/src/app/globals.css+++ b/website/src/app/globals.css@@ -2,16 +2,12 @@@tailwind components;@tailwind utilities;-html,-body {- overflow: hidden;-}-body,main {min-height: 100vh;/* mobile viewport bug fix */min-height: -webkit-fill-available;+ overflow-x: hidden;}html {