Skip to content

Commit

Permalink
feat: add autofocus prop (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski authored Sep 24, 2024
1 parent 2b1be8b commit 2a32c61
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface DrawerContextValue {
noBodyStyles: boolean;
handleOnly?: boolean;
container?: HTMLElement | null;
autoFocus?: boolean;
}

export const DrawerContext = React.createContext<DrawerContextValue>({
Expand Down Expand Up @@ -59,6 +60,7 @@ export const DrawerContext = React.createContext<DrawerContextValue>({
setBackgroundColorOnScale: true,
noBodyStyles: false,
container: null,
autoFocus: false,
});

export const useDrawerContext = () => {
Expand Down
16 changes: 13 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type DialogProps = {
container?: HTMLElement | null;
onAnimationEnd?: (open: boolean) => void;
preventScrollRestoration?: boolean;
autoFocus?: boolean;
} & (WithFadeFromProps | WithoutFadeFromProps);

export function Root({
Expand Down Expand Up @@ -91,6 +92,7 @@ export function Root({
repositionInputs = true,
onAnimationEnd,
container,
autoFocus = false,
}: DialogProps) {
const [isOpen = false, setIsOpen] = useControllableState({
defaultProp: defaultOpen,
Expand Down Expand Up @@ -638,7 +640,7 @@ export function Root({
<DialogPrimitive.Root
defaultOpen={defaultOpen}
onOpenChange={(open) => {
if (!dismissible) return;
if (!dismissible && !open) return;
if (open) {
setHasBeenOpened(true);
} else {
Expand Down Expand Up @@ -677,6 +679,7 @@ export function Root({
setBackgroundColorOnScale,
noBodyStyles,
container,
autoFocus,
}}
>
{children}
Expand Down Expand Up @@ -710,7 +713,7 @@ Overlay.displayName = 'Drawer.Overlay';
export type ContentProps = React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>;

export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
{ onPointerDownOutside, style, ...rest },
{ onPointerDownOutside, style, onOpenAutoFocus, ...rest },
ref,
) {
const {
Expand All @@ -726,6 +729,7 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
snapPoints,
container,
handleOnly,
autoFocus,
} = useDrawerContext();
// Needed to use transition instead of animations
const [delayedSnapPoints, setDelayedSnapPoints] = React.useState(false);
Expand Down Expand Up @@ -797,7 +801,13 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(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);

Expand Down
2 changes: 1 addition & 1 deletion test/src/app/scrollable-with-inputs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Drawer } from 'vaul';
export default function Page() {
return (
<div className="w-screen h-screen bg-white p-8 flex justify-center items-center" data-vaul-drawer-wrapper="">
<Drawer.Root>
<Drawer.Root autoFocus={true}>
<Drawer.Trigger asChild>
<button>Open Drawer</button>
</Drawer.Trigger>
Expand Down

0 comments on commit 2a32c61

Please sign in to comment.