Overlays
Modal
Display a modal dialog.
Import
tsximport { Modal } from "@hope-ui/core";
tsximport { Modal } from "@hope-ui/core";
- Modal: The wrapper that provides props, state, and context to its children.
- Modal.Overlay: The backdrop behind the modal.
- Modal.Content: The modal itself.
- Modal.CloseButton: A button to close the modal.
- Modal.Heading: An optional heading for the modal.
- Modal.Description: An optional description for the modal.
Usage
tsxfunction BasicUsageExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
tsxfunction BasicUsageExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
Initial focus
When the modal opens, focus is automatically set on the first focusable element in Modal.Content.
Use the data-autofocus attribute on any element to mark it as the initial focus element.
tsxfunction InitialFocusExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><Text mb={4}>The content of the Modal.</Text><HStack justifyContent="flex-end" spacing={4}><Button data-autofocus _focus={{ color: "red" }}>Action</Button></HStack></Modal.Content></Modal></>);}
tsxfunction InitialFocusExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><Text mb={4}>The content of the Modal.</Text><HStack justifyContent="flex-end" spacing={4}><Button data-autofocus _focus={{ color: "red" }}>Action</Button></HStack></Modal.Content></Modal></>);}
You can also use a different css selector by using the initialFocusSelector prop.
tsxfunction CustomInitialFocusExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><ModalisOpen={isOpen()}onClose={() => setIsOpen(false)}initialFocusSelector="#initial-focus"><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><Text mb={4}>The content of the Modal.</Text><HStack justifyContent="flex-end" spacing={4}><Button id="initial-focus" _focus={{ color: "red" }}>Action</Button></HStack></Modal.Content></Modal></>);}
tsxfunction CustomInitialFocusExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><ModalisOpen={isOpen()}onClose={() => setIsOpen(false)}initialFocusSelector="#initial-focus"><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><Text mb={4}>The content of the Modal.</Text><HStack justifyContent="flex-end" spacing={4}><Button id="initial-focus" _focus={{ color: "red" }}>Action</Button></HStack></Modal.Content></Modal></>);}
If the modal contains no focusable element, focus is set to the Modal.Content itself.
Restore focus
When the modal closes, focus is set back to the trigger element. Use the restoreFocusSelector prop and pass in a css selector to target another element.
tsxfunction CustomRestoreFocusExample() {const [isOpen, setIsOpen] = createSignal(false);return (<HStack spacing={4}><Button onClick={() => setIsOpen(true)}>Open Modal</Button><ModalisOpen={isOpen()}onClose={() => setIsOpen(false)}restoreFocusSelector="[data-finalfocus]"><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal><Button data-finalfocus _focus={{ color: "red" }}>Final focus element</Button></HStack>);}
tsxfunction CustomRestoreFocusExample() {const [isOpen, setIsOpen] = createSignal(false);return (<HStack spacing={4}><Button onClick={() => setIsOpen(true)}>Open Modal</Button><ModalisOpen={isOpen()}onClose={() => setIsOpen(false)}restoreFocusSelector="[data-finalfocus]"><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal><Button data-finalfocus _focus={{ color: "red" }}>Final focus element</Button></HStack>);}
Focus trap
By default, focus is lock inside the modal for accessibility reason. Set the trapFocus prop to false if you want to disable this behavior.
tsxfunction DisableFocusTrapExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} trapFocus={false}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
tsxfunction DisableFocusTrapExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} trapFocus={false}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
Prevent scroll
For accessibility reason scrolling on the main document behind the modal is blocked by default. Set the preventScroll prop to false to allow scrolling behind the modal.
tsxfunction DisablePreventScrollExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} preventScroll={false}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
tsxfunction DisablePreventScrollExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} preventScroll={false}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
Heading and description
Use Modal.Heading to add an accessible heading to the modal, it renders an h2 by default.
Similarly Modal.Description is used add an accessible description and renders a p by default.
Under the hood, Hope UI will set the correct aria-labelledby and aria-describedby attributes.
tsxfunction HeadingAndDescriptionExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)}><Modal.Overlay /><Modal.Content p={4}><HStack alignItems="flex-start" justifyContent="space-between" mb={4}><VStack alignItems="flex-start"><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.DescriptionfontSize="sm"color={{ light: "neutral.600", dark: "neutral.300" }}mb={4}>Description</Modal.Description></VStack><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
tsxfunction HeadingAndDescriptionExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)}><Modal.Overlay /><Modal.Content p={4}><HStack alignItems="flex-start" justifyContent="space-between" mb={4}><VStack alignItems="flex-start"><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.DescriptionfontSize="sm"color={{ light: "neutral.600", dark: "neutral.300" }}mb={4}>Description</Modal.Description></VStack><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
Open/close transitions
The contentTransitionOptions and overlayTransitionOptions props allows to customize the modal content and overlay open/close transitions. You can use predefined transitions or create custom ones.
tsxfunction TransitionExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><ModalisOpen={isOpen()}onClose={() => setIsOpen(false)}contentTransitionOptions={{transition: "slide-up",duration: 400,exitDuration: 250,easing: "ease-out",exitEasing: "ease-in",}}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
tsxfunction TransitionExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><ModalisOpen={isOpen()}onClose={() => setIsOpen(false)}contentTransitionOptions={{transition: "slide-up",duration: 400,exitDuration: 250,easing: "ease-out",exitEasing: "ease-in",}}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
To learn more about the transition API check out the createTransition documentation.
Center modal vertically
Use the isCentered prop to vertically center the modal.
tsxfunction VerticallyCenteredExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} isCentered><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
tsxfunction VerticallyCenteredExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} isCentered><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
Size
You can change the modal width by setting the size prop.
tsxfunction SizeExample() {const [size, setSize] = createSignal<ModalProps["size"]>("md");const [isOpen, setIsOpen] = createSignal(false);const handleClick = (newSize: ModalProps["size"]) => {setSize(newSize);setIsOpen(true);};const sizes: Array<ModalProps["size"]> = ["xs", "sm", "md", "lg", "xl", "full"];return (<><HStack spacing={4}><For each={sizes}>{size => <Button onClick={() => handleClick(size)}>{size}</Button>}</For></HStack><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} size={size()}><Show when={size() !== "full"}><Modal.Overlay /></Show><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
tsxfunction SizeExample() {const [size, setSize] = createSignal<ModalProps["size"]>("md");const [isOpen, setIsOpen] = createSignal(false);const handleClick = (newSize: ModalProps["size"]) => {setSize(newSize);setIsOpen(true);};const sizes: Array<ModalProps["size"]> = ["xs", "sm", "md", "lg", "xl", "full"];return (<><HStack spacing={4}><For each={sizes}>{size => <Button onClick={() => handleClick(size)}>{size}</Button>}</For></HStack><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} size={size()}><Show when={size() !== "full"}><Modal.Overlay /></Show><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
Styling the backdrop
You can use style props and sx on the Modal.Overlay component to customize the modal backdrop.
tsxfunction CustomBackdropExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)}><Modal.Overlaysx={{bg: "blackAlpha.300",backdropFilter: "blur(10px) hue-rotate(90deg)",}}/><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
tsxfunction CustomBackdropExample() {const [isOpen, setIsOpen] = createSignal(false);return (<><Button onClick={() => setIsOpen(true)}>Open Modal</Button><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)}><Modal.Overlaysx={{bg: "blackAlpha.300",backdropFilter: "blur(10px) hue-rotate(90deg)",}}/><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><p>The content of the Modal.</p></Modal.Content></Modal></>);}
Please be aware that not every browser supports the backdrop-filter CSS property used in the
example above.
Overflow behavior
If the content within the modal overflows beyond the viewport, you can use the scrollBehavior prop to control how scrolling should happen.
outside: overflow is handled by the modal wrapper.inside: overflow is handled by theModal.Content.
tsxfunction ScrollBehaviorExample() {const [scrollBehavior, setScrollBehavior] = createSignal<ModalProps["scrollBehavior"]>("outside");const [isOpen, setIsOpen] = createSignal(false);const handleClick = (newValue: ModalProps["scrollBehavior"]) => {setScrollBehavior(newValue);setIsOpen(true);};const scrollBehaviors: Array<ModalProps["scrollBehavior"]> = ["outside", "inside"];return (<><HStack spacing={4}><For each={scrollBehaviors}>{scrollBehavior => (<Button onClick={() => handleClick(scrollBehavior)}>{scrollBehavior} overflow</Button>)}</For></HStack><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} scrollBehavior={scrollBehavior()}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><For each={Array(100).fill("")}>{() => <p>The content of the Modal.</p>}</For></Modal.Content></Modal></>);}
tsxfunction ScrollBehaviorExample() {const [scrollBehavior, setScrollBehavior] = createSignal<ModalProps["scrollBehavior"]>("outside");const [isOpen, setIsOpen] = createSignal(false);const handleClick = (newValue: ModalProps["scrollBehavior"]) => {setScrollBehavior(newValue);setIsOpen(true);};const scrollBehaviors: Array<ModalProps["scrollBehavior"]> = ["outside", "inside"];return (<><HStack spacing={4}><For each={scrollBehaviors}>{scrollBehavior => (<Button onClick={() => handleClick(scrollBehavior)}>{scrollBehavior} overflow</Button>)}</For></HStack><Modal isOpen={isOpen()} onClose={() => setIsOpen(false)} scrollBehavior={scrollBehavior()}><Modal.Overlay /><Modal.Content p={4}><HStack justifyContent="space-between" mb={4}><Modal.Heading fontWeight="semibold">Title</Modal.Heading><Modal.CloseButton /></HStack><For each={Array(100).fill("")}>{() => <p>The content of the Modal.</p>}</For></Modal.Content></Modal></>);}
Accessibility
ARIA roles and attributes
- The
Modal.Contenthasaria-modalset totrue. - The
Modal.Contenthasaria-labelledbyset to theidof theModal.Heading, if any. - The
Modal.Contenthasaria-describedbyset to theidof theModal.Description, if any.
Keyboard support and focus management
- If the
trapFocusprop istrue, focus is locked within the modal. - If the
initialFocusSelectorprop is set, focus is moved to the matching element, otherwise focus moves to theModal.Content. - When the modal closes, focus returns to the trigger element unless another focusable element takes focus.
- If the
closeOnOverlayClickprop istrue, clicking on the overlay closes the modal. - When focus is within the
Modal.ContentandcloseOnEscprop istrue, pressing esc closes the modal. - If the
preventScrollprop istrue, scrolling is blocked on the elements behind the modal.
Theming
Use the ModalTheme type to override Modal styles and default props when extending Hope UI theme.
tsximport { extendTheme, ModalTheme } from "@hope-ui/core";const theme = extendTheme({components: {Modal: {// ...overrides} as ModalTheme,},});
tsximport { extendTheme, ModalTheme } from "@hope-ui/core";const theme = extendTheme({components: {Modal: {// ...overrides} as ModalTheme,},});
Component parts
| Name | Static selector | Description |
|---|---|---|
| root | hope-Modal-root | Root element |
| content | hope-Modal-content | Modal content |
| overlay | hope-Modal-overlay | Modal overlay/backdrop |
| heading | hope-Modal-heading | Modal heading |
| description | hope-Modal-description | Modal description |
Props
Modal
| Name | Type | Description | Default value |
|---|---|---|---|
| isOpen* | boolean | Whether the modal should be shown. | |
| onClose* | () => void | A function that will be called to close the modal. | |
| id | string | The id of the modal content. | |
| size | "xs" | "sm" | "md" | "lg" | "xl" | "full" | The size of the modal. | |
| isCentered | boolean | Whether the modal should be centered on screen. | |
| scrollBehavior | "inside" | "outside" | Defines how scrolling should happen when content overflows beyond the viewport. | |
| contentTransitionOptions | TransitionOptions | Options passed to the modal content transition. | |
| overlayTransitionOptions | TransitionOptions | Options passed to the overlay transition. | |
| closeOnOverlayClick | boolean | Whether the modal should close when the overlay is clicked. | true |
| closeOnEsc | boolean | Whether the modal should close when the user hit the Esc key. | true |
| preventScroll | boolean | Whether the scroll should be disabled on the body when the modal opens. | true |
| trapFocus | boolean | Whether the focus will be locked into the modal. | true |
| initialFocusSelector | string | A query selector to retrieve the element that should receive focus once the modal opens. | |
| restoreFocusSelector | string | A query selector to retrieve the element that should receive focus once the modal closes. | |
| onOverlayClick | () => void | A function that will be called when the overlay is clicked. | |
| onEscKeyDown | () => void | A function that will be called when the Esc key is pressed and focus is within the modal. |
Other components props
Modal.CloseButtonrenders a CloseButton and supports all of its props.Modal.Overlay,Modal.Content,Modal.HeadingandModal.Descriptionsupports commons Hope UI props (style props,sxandas).