A dialog is a window overlaid on either the primary window or another dialog
window. Content behind a modal dialog is inert, meaning that users cannot
interact with it.
When the modal opens, focus is sent into the modal and set to the first tabbable
element. If there are no tabbled elements, focus is set on ModalContent.
When the dialog closes, it returns focus to the element that triggered it. Set
finalFocusRef to change the element that should receive focus when the modal
closes.
Some other content that'll receive focus on close.
functionReturnFocus(){
const{ isOpen, onOpen, onClose }=useDisclosure()
const finalRef = React.useRef()
return(
<>
<Boxref={finalRef}tabIndex={-1}aria-label="Focus moved to this box">
Some other content that'll receive focus on close.
For accessibility, it's recommended to block scrolling on the main document
behind the modal. Chakra does this by default but you can set
blockScrollOnMount to false to allow scrolling behind the modal.
Chakra automatically sets focus on the first tabbable element in the modal.
However, there might be scenarios where you need to manually control where focus
goes.
Chakra provides 2 props for this use case:
initialFocusRef: The ref of the component that receives focus when the
modal opens.
finalFocusRef: The ref of the component that receives focus when the modal
closes.
If you set finalFocusRef, internally we set returnFocusOnClose to false
so it doesn't return focus to the element that triggered it.
By default the modal has a vertical offset of 3.75rem which you can change by
passing top to the ModalContent. If you need to vertically center the modal,
pass the isCentered prop.
If the content within the modal overflows beyond the viewport, don't use this
prop. Try setting the overflow behavior instead.
The Modal doesn't come with any transitions by default so you can manage this
yourself. Chakra exports four transition components (Fade, ScaleFade, Slide, and SlideFade) to
provide simple transitions.
When adding transitions, kindly pay attention to the timeout for the overlay
and content transitions. In this example, Fade transitions slower than the
SlideFade for it to work correctly.
When the modal is open, it's rendered within a portal and all its siblings have
aria-hidden set to true so the only thing screen readers see is the modal.
To disable this behavior, set useInert to false.
By default the modal, alert dialog and drawer locks the focus inside them.
Normally this is what you want to maintain accessibility standards.
While strongly discourage this use case due to it's accessibility impacts,
there are certain situations where you might not want the modal to trap focus.
To prevent focus trapping, pass trapFocus and set it's value to false.
When the modal opens, focus is automatically set to the first enabled element,
or the element from initialFocusRef.
When the modal closes, focus returns to the element that was focused before
the modal activated, or the element from finalFocusRef.
Clicking on the overlay closes the Modal.
Pressing Esc closes the Modal.
Scrolling is blocked on the elements behind the modal.
The modal is rendered in a portal attached to the end of document.body to
break it out of the source order and make it easy to add aria-hidden to its
siblings.