Modal
appears on top of the main content and moves the system into a special mode requiring user interaction. It blocks interaction with the main content until an action is taken.
Modal dialogs are often triggered by an action a user took, and should be used to inform, confirm or complete a task. Content behind a modal dialog is inert, meaning that users cannot interact with it.
Use Modal
:
- to request information critical for a user to continue their current process
- to warn users about a potentially destructive action they are taking
Don't use Modal
:
- for nonessential information that is not related to the current user flow
- for interrupting high-stake processes such as checkout flows
Import
import { Modal } from '@contentful/f36-components';// orimport { Modal } from '@contentful/f36-modal';
Example
Basic
Changing sizes
There are 4 different sizes for modals: small
, medium
, large
, fullWidth
. Pick a size based on the amount of content you have to present in them.
You can also pass an arbitary width in pixels, but we recommend to choose of the predefined values.
Choosing a fullWidth modal
As a rule of thumb, fullWidth
modals shouldn’t be used frequently. Use them modals specially to present imagery or when you need to present a layout with columns in it. Never use fullWidth
modals for a single column layout as the length of the line will affect readability.
Setting initial focus
When the modal opens, focus is automatically set to the close button but you have a possibility to set an initial focus to a specific element of your modal dialog.
Using Modal.Controls
Using Modal.Controls
is optional and you may have a modal without control buttons. When you have control buttons, follow the following rules:
- always use
Button
component withsize="small"
- your primary action button should be aligned to the right
- there should be only one primary action
- ideally, 1 secondary button should be enough when paired with a primary one. It’s allowed, however, to add a second one for another action.
Using ModalLauncher
When you want to open a modal from a function (outside of React component code), use ModalLauncher.
Props (API reference)
- 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
Content guidelines
For general guidelines around content and how to better write copy, consult this UX writing page . Find below a few suggestions on how to elaborate good content for modals:
Titles
Make them brief and easily inteligible, following this structure: verb + noun
✅ Do:
- Create content type
- Delete entry?
- Add to release
❌ Don't:
- Do you want to add this page to your release?
- Permanently delete this entry?
- Are you sure you want to delete this entry?
Body
The body is the main content of a modal. Therefore, try not to get chatty and keep it to the information that is necessary for users to accomplish their tasks. For more guidance on how to write good content for a modal, refer to this UX writing document.
Buttons label
A good way to think about buttons labels is to mentalize what users will be thinking when pressing the button. Actions should be clear and have a verb + noun structure that describes the action.
Buttons in modals should be short and sweet and shouldn’t require a lot of effort from users to understand what’s going to happen.
As a rule of thumb, users shouldn’t feel unclear of the outcomes of clicking on a specific button.
✅ Do: Add field
, Remove page
, Create content type
, Add page to release
❌ Don't: Discard
, New page
, New content type
, Go
, Add
Avoid vague actions such as Remove
(without saying what’s going to be removed) or Add
.
Another scenario to avoid is to create lengthy buttons and with more that one action being described.
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.
- By default, clicking on the overlay closes the modal.
- By default, pressing
Esc
closes the modal. - Scrolling is blocked on the elements behind the modal.