Modal
Modal dialogs interrupt a user by isolating information that needs to be acted upon. They are often triggered by an action a user took, and should be used to inform, confirm or complete a task.
How to use Modal
- Use modal dialogs to request information critical for a user to continue their current process
- Use modal dialogs to warn users about a potentially destructive action they are taking
- Use modal dialogs consistently across use cases
- Do not use modal dialogs for nonessential information that is not related to the current user flow
- Avoid modal dialogs that interrupt high-stake processes such as checkout flows
Import
import { Modal } from '@contentful/f36-components';// orimport { Modal } from '@contentful/f36-modal';
Code examples
The Modal component can be configured in a number of different ways. Use this guidance to determine when to use certain variations:
Basic example
Modal position
Modal sizes
Content recommendations
- Practice progressive disclosure. Eliminate extra details to keep copy concise and actionable
- Focus on the objective of the modal dialog. Start each sentence with the objective when possible
- To enable users to navigate quickly, write concise sentences that are easy to understand at a glance
- Limit the number of concepts in each sentence. Write simple sentences for all audiences
- To keep UX copy actionable use present tense for user interactions
- Avoid “never,” “always” and other absolutes
- Use contractions when possible
- Avoid exclamation marks
Accessibility
- When the modal opens, focus is trapped within it.
- When the modal opens, focus is automatically set to the close button, 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.
Props
- Name
children(required)
ReactNode | RenderModal - Name
isShown(required)
DescriptionWhen true, the dialog is shown.
falsetrue - Name
onClose(required)
DescriptionFunction that will be run when the modal is requested to be closed, prior to actually closing.
(event: MouseEvent<Element, MouseEvent> | KeyboardEvent<Element>) => void - Name
allowHeightOverflow
DescriptionAre modals higher than viewport allowed
Defaultfalsetruefalse - Name
aria
DescriptionAdditional aria attributes
Aria - Name
className
DescriptionCSS class to be appended to the root element
string - Name
initialFocusRef
DescriptionOptional property to set initial focus
RefObject<HTMLElement> - Name
modalContentProps
DescriptionOptional props to override ModalContent behaviour
Partial<ModalContentProps> - Name
modalHeaderProps
DescriptionOptional props to override ModalHeader behaviour
Partial<ModalHeaderProps> - Name
onAfterOpen
DescriptionFunction that will be run after the modal has opened.
OnAfterOpenCallback - Name
position
DescriptionIndicating if modal is centered or linked to the top
Default"center""top"center - Name
shouldCloseOnEscapePress
DescriptionBoolean indicating if pressing the esc key should close the overlay.
Defaultfalsetruetrue - Name
shouldCloseOnOverlayClick
DescriptionBoolean indicating if clicking the overlay should close the overlay.
Defaultfalsetruetrue - Name
size
DescriptionSize of the modal window
Defaultstringnumbermedium - Name
testId
DescriptionA [data-test-id] attribute used for testing purposes
Defaultstringcf-ui-modal - Name
title
DescriptionModal title that is used in header
string - Name
topOffset
DescriptionTop offset if position is 'top'
Defaultstringnumber50px