How to turn off initial focus when opening headlessui? #2549
Unanswered
youngseo-im
asked this question in
Help
Replies: 1 comment
-
For accessibility reasons, dialog should contain at least one focusable element. By default, the Dialog component will focus the first focusable element (by DOM order) once it is rendered, and pressing the Tab key will cycle through all additional focusable elements within the contents. To disable this behavior or modify it first you need to define a react ref state: const dialogRef = useRef(null); Then define <Dialog initialFocus={dialogRef ? dialogRef : undefined}>
<div style={{ display: "none" }} ref={dialogRef} />
</Dialog> You can also set the element you want to be focused at first render like this: <Dialog.Panel>
<Dialog.Title>...</Dialog.Title>
<button ref={dialogRef}>
....
</button>
</Dialog.Panel> Vue code defining const completeButtonRef = ref(null)
<Dialog :initialFocus="dialogRef">
<div ref="dialogRef" :style="{display : 'none' }" />
</Dialog> <button ref="dialogRef">
...
</button> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using headless ui in my vue3 + typescript project
How can I turn off the initial focus on a slotted component input?
Assigning a null value to initialFocus did not work.
`
<script setup lang="ts"> import { Dialog, DialogOverlay, TransitionRoot, TransitionChild, DialogPanel } from '@headlessui/vue' import { ref } from 'vue' const props = defineProps<{ isOpen: boolean closeModal: () => void }>() const initialFocus = ref(null) </script>Beta Was this translation helpful? Give feedback.
All reactions