Modal offers a more focused view on something that the user may need to action. It can also offer further information that may be important, or relevant, to something in-page.
The component consists of Modal.Content and optional Modal.CloseButton.
Default appearance of the Close button floating over the content.
$inline property would push the content section under the Close button section.
functionExample(){
const[open, setOpen]=React.useState(false);
consttoggleOpen=(event)=>{
event.stopPropagation();
setOpen(!open);
}
return(
<>
{open &&(
<ModalonClose={toggleOpen}>
<Modal.CloseButton$inline/>
<Modal.Content$padding={5}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis aliquet diam mi, ut condimentum turpis sodales varius. Aliquam vitae quam sed augue tempor euismod. Praesent quis cursus lorem. Etiam lobortis luctus commodo. Nulla porta justo sed quam gravida finibus. Nullam scelerisque lorem in vehicula tristique. Etiam ut nibh non urna sagittis accumsan. Nunc eget felis pellentesque nunc vulputate convallis vel maximus magna.
</Modal.Content>
</Modal>
)}
<Buttonaria-label="Open modal with Close button example"onClick={toggleOpen}>Open</Button>
</>
);
}
Accessibility
The Modal component will force focus to its close button when first opened.
This ensures keyboard-only and screenreader users can access it. If used without the close button, focus will be forced onto the first interactive element within it.
There are some use-cases where this may not be appropriate. Please refer to the W3C Dialog Modal Design Pattern for more details.
Returning Focus
Returning focus to a Button used to open the Modal is left to the concern of the consumer. Below is an example of how to achieve this desired outcome.