From 2a32c61d8518d1411b7f949b28fe64ad0c5ac2f2 Mon Sep 17 00:00:00 2001 From: Emil Kowalski <36730035+emilkowalski@users.noreply.github.com> Date: Tue, 24 Sep 2024 07:21:49 +0200 Subject: [PATCH] feat: add autofocus prop (#442) --- src/context.ts | 2 ++ src/index.tsx | 16 +++++++++++++--- test/src/app/scrollable-with-inputs/page.tsx | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/context.ts b/src/context.ts index 0c7bf62c..b08603a2 100644 --- a/src/context.ts +++ b/src/context.ts @@ -29,6 +29,7 @@ interface DrawerContextValue { noBodyStyles: boolean; handleOnly?: boolean; container?: HTMLElement | null; + autoFocus?: boolean; } export const DrawerContext = React.createContext({ @@ -59,6 +60,7 @@ export const DrawerContext = React.createContext({ setBackgroundColorOnScale: true, noBodyStyles: false, container: null, + autoFocus: false, }); export const useDrawerContext = () => { diff --git a/src/index.tsx b/src/index.tsx index 34d8cc66..d820a271 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -60,6 +60,7 @@ export type DialogProps = { container?: HTMLElement | null; onAnimationEnd?: (open: boolean) => void; preventScrollRestoration?: boolean; + autoFocus?: boolean; } & (WithFadeFromProps | WithoutFadeFromProps); export function Root({ @@ -91,6 +92,7 @@ export function Root({ repositionInputs = true, onAnimationEnd, container, + autoFocus = false, }: DialogProps) { const [isOpen = false, setIsOpen] = useControllableState({ defaultProp: defaultOpen, @@ -638,7 +640,7 @@ export function Root({ { - if (!dismissible) return; + if (!dismissible && !open) return; if (open) { setHasBeenOpened(true); } else { @@ -677,6 +679,7 @@ export function Root({ setBackgroundColorOnScale, noBodyStyles, container, + autoFocus, }} > {children} @@ -710,7 +713,7 @@ Overlay.displayName = 'Drawer.Overlay'; export type ContentProps = React.ComponentPropsWithoutRef; export const Content = React.forwardRef(function ( - { onPointerDownOutside, style, ...rest }, + { onPointerDownOutside, style, onOpenAutoFocus, ...rest }, ref, ) { const { @@ -726,6 +729,7 @@ export const Content = React.forwardRef(function ( snapPoints, container, handleOnly, + autoFocus, } = useDrawerContext(); // Needed to use transition instead of animations const [delayedSnapPoints, setDelayedSnapPoints] = React.useState(false); @@ -797,7 +801,13 @@ export const Content = React.forwardRef(function ( pointerStartRef.current = { x: event.pageX, y: event.pageY }; onPress(event); }} - onOpenAutoFocus={(e) => e.preventDefault()} + onOpenAutoFocus={(e) => { + onOpenAutoFocus?.(e); + + if (!autoFocus) { + e.preventDefault(); + } + }} onPointerDownOutside={(e) => { onPointerDownOutside?.(e); diff --git a/test/src/app/scrollable-with-inputs/page.tsx b/test/src/app/scrollable-with-inputs/page.tsx index d29482ca..8fb331de 100644 --- a/test/src/app/scrollable-with-inputs/page.tsx +++ b/test/src/app/scrollable-with-inputs/page.tsx @@ -5,7 +5,7 @@ import { Drawer } from 'vaul'; export default function Page() { return (
- +