Disable auto focus on dialog open #2307
Replies: 7 comments 11 replies
-
The same question arises for the Sheet component 👍 |
Beta Was this translation helpful? Give feedback.
-
Maybe I'm wrong but I think is necessary for Accessibility. |
Beta Was this translation helpful? Give feedback.
-
I wanted to contribute a bit by making this open source project, where I used shadcn , you can contribute and customize some objects if you want, here is my repo : https://github.com/PiotrWarzachowski/DocMind Feel free to check it out, its public on vercel. You can leave a star ⭐ if you want 💓. Have a good year |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
just add "tabIndex={-1}" on the input element. |
Beta Was this translation helpful? Give feedback.
-
Vue.
|
Beta Was this translation helpful? Give feedback.
-
Here's my solution: Define the following // @/hooks/use-prevent-auto-focus.ts
import { useRef, useCallback } from 'react';
function usePreventAutoFocus<E extends HTMLElement = HTMLDivElement>() {
const ref = useRef<E>(null);
const onOpenAutoFocus = useCallback((event: Event) => {
event.preventDefault();
ref.current?.focus({ preventScroll: true });
}, []);
return { ref, onOpenAutoFocus, tabIndex: -1 as const };
}
export default usePreventAutoFocus; And here's how to use it in a dialog: import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus'
const prevent = usePreventAutoFocus();
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogContent {...prevent} className="outline-none">
<DialogHeader>
<DialogTitle>Share Folder</DialogTitle>
<DialogDescription>
Select the folder to share and the members to share it with.
</DialogDescription>
</DialogHeader>
<div>
<Label className="text-base font-semibold block mb-2">Select Folder</Label>
<InputSearch
placeholder="Search folders..."
value={folderSearchTerm}
onChange={(e) => setFolderSearchTerm(e.target.value)}
className="my-2 h-9"
disabled={sharing}
/>
</div>
</DialogContent>
</Dialog>
); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using the default Dialog component with input fields from the shadcn docs.
I wanted to know if it's possible to disable the auto focus on inputs when the dialog opens?
Beta Was this translation helpful? Give feedback.
All reactions