An <sp-underlay>
provides a visual layer between overlay content and the rest of your application. It is commonly used with modal dialogs and other overlay elements to create a visual separation and prevent interaction with the background content while the overlay is active.
yarn add @spectrum-web-components/underlay
Import the side effectful registration of <sp-underlay>
via:
import '@spectrum-web-components/underlay/sp-underlay.js';
When looking to leverage the Underlay
base class as a type and/or for extension purposes, do so via:
import { Underlay } from '@spectrum-web-components/underlay';
When using an <sp-underlay>
with overlay content, place it as a sibling element before your overlay content.
<style>
sp-underlay:not([open]) + sp-dialog {
display: none;
}
sp-underlay + sp-dialog {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 1;
background: var(--spectrum-gray-100);
}
</style>
<sp-button
onclick="
console.log(this.nextElementSibling);
this.nextElementSibling.open = true;
"
>
Open dialog with underlay element
</sp-button>
<sp-underlay></sp-underlay>
<sp-dialog size="s">
<h1 slot="heading">Hello, I'm an overlay!</h1>
<p>Enjoy your day...</p>
<sp-button
slot="button"
onclick="
this.parentElement.previousElementSibling.open = false;
"
>
Close
</sp-button>
</sp-dialog>
To ensure proper layering of your overlay content with the underlay, use appropriate CSS:
<style>
/* Hide overlay content when underlay is closed */
sp-underlay:not([open]) + sp-dialog {
display: none;
}
/* Position overlay content above the underlay */
sp-underlay + sp-dialog {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 1;
}
</style>
The <sp-underlay>
element helps create an accessible modal experience by:
- Providing visual separation between modal content and the rest of the page
- Supporting proper focus management when used with modal dialogs
- Helping communicate the modal state to assistive technologies