The Modal component is designed to block out the screen and focus the user on the content inside the Modal.
It provides only the backdrop and a scrollable container for your content. If your content should have a background color, be sure to add it yourself.
Any time the Modal is in the DOM, it will take over the screen. You make it go away by removing it from the DOM.
<script>
let showMyModal = false
</script>
<button on:click={() => showMyModal = true}>Show Modal</button>
{#if showMyModal}
<Modal on:escape={() => showMyModal = false}>
This is a modal dialog!
</Modal>
{/if}
<style>
:global(.modal-container) { background: white; }
</style>
on:escape
- dispatched when the user does something that would normally dismiss a modal, such as clicking in the darkened backdrop or pressing the Escape key. You should listen for this and dismiss your modal. If you do not listen for this event, users will only be able to escape by some other means provided within your content. Be careful not to trap them!
opaque: boolean
(defaultfalse
) - Make the backdrop completely opaque so that it cannot be seen through. This is a more jarring experience for users but sometimes you don't want background content to be visible at all.containerClass: string
(default''
) - A class to be added to the scrollable container inside the backdrop. This container will always have the.modal-container
class regardless of this prop.escapable: boolean
(defaulttrue
) - Let screen readers know that they can escape the modal by pressing the Escape key. If you are not listening foron:escape
then you should set thisfalse
to avoid giving screen reader users false information.hidefocus: boolean
(defaulttrue
) - Focus must be moved to the modal when it opens, in order to maintain accessibility. This component avoids making the focus visible to users by default as it might be visually disconcerting. So on mount, focus is moved to an invisible element above your content. If you prefer focus to be visible to users, set this tofalse
and focus will automatically be moved to the first element in your content that has a tabindex and therefore be visible to users. This prop is overridden if you provide aninitialfocus
.hidefocuslabel: string|undefined
(defaultfocus is above modal dialog, start tabbing
) - The message that will be read out to users using a screen reader when the focus is on the invisible element created by thehidefocus
prop.initialfocus: string|undefined
(defaultundefined
) - A selector to be used to locate and set focus on an element in your content, on mount. Use if you want focus on a specific element when the modal opens. Overrideshidefocus
.returnfocusto: HTMLElement|undefined
(default element that had focus was when modal opened) - Use to control where focus moves when the modal closes. If not provided, component automatically saves the element that had focus on mount. Usually it's the button that triggered the modal.usePortal: HTMLElement|undefined
(defaultundefined
) - An optional bind for specifying theHTMLElement
to use with theportal
action.includeselector: string|undefined
(defaultundefined
) - If you expect any popup menus to be added to the body, we need to know that they are considered to be part of the focus lock, or else the modal will be dismissed when the user clicks inside. Use commas to include multiple selectors.
The FocusLock component is for creating accessible modal dialogs that do not have a darkened backdrop. It shares the (escapable
, hidefocus
, hidefocuslabel
, initialfocus
, returnfocusto
, and includeselector
) props and the behaviors with Modal because the Modal component uses a FocusLock itself. In additon it exports a className
prop for passing the component a custom CSS class name to use.
Any time the FocusLock is in the DOM, it will lock screen readers inside it. You release them by removing it from the DOM. Regular users are NOT trapped and may interact with other screen elements. When they do, the escape
event will be fired, allowing you to remove the dialog.
FocusLocks can be nested. The user will be trapped inside the deepest FocusLock present in the DOM. When it goes away, they'll be trapped inside the previous FocusLock, and so on.
on:escape
- dispatched when the user does something to dismiss the dialog, such as pressing the Escape key or clicking outside the dialog area. You should listen for this and remove the FocusLock. If you do not listen for this event, users will only be able to escape by some other means provided within your content. Be careful not to trap them!
<script>
let showMyDialog = false
</script>
<button on:click={() => showMyDialog = true}>Show Dialog</button>
{#if showMyDialog}
<FocusLock on:escape={() => showMyDialog = false}>
Screen readers cannot escape this!
<button on:click={() => showMyDialog = false}>Collapse Dialog</button>
</FocusLock>
{/if}
escapable: boolean
(defaulttrue
) - Controls whether the focus lock can be easily escaped by pressing the Escape key or clicking the invisible focus (seehidefocus
). If you set thisfalse
you must provide a way to dismiss the lock inside your content or screen readers will be trapped.hidefocus: boolean
(defaulttrue
) - Focus must be moved to the modal when it opens, in order to maintain accessibility. This component avoids making the focus visible to users by default as it might be visually disconcerting. So on mount, focus is moved to an invisible element above your content. If you prefer focus to be visible to users, set this tofalse
and focus will automatically be moved to the first element in your content that has a tabindex and therefore be visible to users. This prop is overridden if you provide aninitialfocus
.hidefocuslabel: string
(defaultfocus is above modal dialog, start tabbing
) - The message that will be read out to users using a screen reader when the focus is on the invisible element created by thehidefocus
prop.initialfocus: string
(defaultundefined
) - A selector to be used to locate and set focus on an element in your content, on mount. Use if you want focus on a specific element when the lock mounts. Overrideshidefocus
.returnfocusto: HTMLElement
(default element that had focus was when focuslock mounted) - Use to control where focus moves when the lock ends. If not provided, component automatically saves the element that had focus on mount. Usually it's the button that triggered the dialog.