An `<sp-overlay>` element is used to decorate content that you would like to present to your visitors as "overlaid" on the rest of the application. This includes dialogs (modal and not), pickers, tooltips, context menus, et al.
npm install @spectrum-web-components/overlayAn element is used to decorate content that you would like to present to your visitors as "overlaid" on the rest of the application. This includes dialogs (modal and not), pickers, tooltips, context menus, et al.


``zsh`
yarn add @spectrum-web-components/overlay
Import the side effectful registration of as follows:
`ts`
import '@spectrum-web-components/overlay/sp-overlay.js';
When looking to leverage the Overlay base class as a type and/or for extension purposes, do so via:
`ts`
import { Overlay } from '@spectrum-web-components/overlay';
By leveraging the trigger attribute to pass an ID reference to another element within the same DOM tree, your overlay will be positioned in relation to this element. When the ID reference is followed by an @ symbol and interaction type, like click, hover, or longpress, the overlay will bind itself to the referenced element via the DOM events associated with that interaction.
`html
But, it really could be anything. Really.
Clicking opens this popover...
I'm a tooltip and I'm triggered by hovering over the button!
`
When a element is opened, it will pass that state to its direct children elements as the property open, which it will set to true. Elements should react to this by initiating any transition between closed and open that they see fit. Similarly, open will be set to false when the element is closed.
An Overlay that is delayed will wait until a warm-up period of 1000ms has completed before opening. Once the warmup period has completed, all subsequent Overlays will open immediately. When no Overlays are opened, a cool down period of 1000ms will begin. Once the cool down has completed, the next Overlay to be opened will be subject to the warm-up period if provided that option.
`html
`
When an Overlay is notImmediatelyCloseable that means that the first interaction that would lead to the closure of the Overlay in question will be ignored. This is useful when working with non-"click" mouse interactions, like contextmenu, where the trigger event (e.g. contextmenu) occurs _before_ an event that would close said overlay (e.g. pointerup).
`html-live
Right click anywhere in bounded rectangle to open the menu
`
The offset property accepts either a single number, to define the offset of the Overlay along the main axis from the trigger, or 2-tuple, to define the offset along the main axis and the cross axis. This option has no effect when there is no trigger element.
`html
An offset of 50px is applied to the overlay.
Clicking opens this popover...
`
A placement of "auto-start" | "auto-end" | "top" | "bottom" | "right" | "left" | "top-start" | "top-end" | "bottom-start" | "bottom-end" | "right-start" | "right-end" | "left-start" | "left-end" will instruct the Overlay where to place itself in relationship to the trigger element.
`html
The overlay is placed to the right of the trigger.
Clicking opens this popover...
`
Some Overlays will always be passed focus (e.g. modal or page Overlays). When this is not true, the receivesFocus option will inform the API whether to attempt to pass focus into the Overlay once it is open. 'true' will pass focus, 'false' will not (when possible), and "auto" (the default), will make a decision based on the type of the Overlay.
`html
Clicking opens this popover but does not receive focus
this.dispatchEvent(
new Event('close', {
bubbles: true,
composed: true,
})
);
"
>
Sign in
`
#### trigger
The trigger attribute accepts a string ID reference for the trigger element combined with the interaction type.
The format is "elementId@interaction", where:
- elementId is the ID of the HTML element to use as the triggerinteraction
- is required and can be click, hover, or longpress
Examples:
`html
`
#### triggerElement
The triggerElement property accepts an HTMLElement or a VirtualTrigger from which to position the Overlay.
- You can import the VirtualTrigger class from the overlay package to create a virtual trigger that can be used to position an Overlay. This is useful when you want to position an Overlay relative to a point on the screen that is not an element in the DOM, like the mouse cursor.
#### type
The type of an Overlay outlines a number of things about the interaction model within which it works:
'modal' Overlays create a modal context that traps focus within the content and prevents interaction with the rest of the page. The overlay manages focus trapping and accessibility features like aria-modal="true" to ensure proper screen reader behavior.
They should be used when you need to ensure that the user has interacted with the content of the Overlay before continuing with their work. This is commonly used for dialogs that require a user to confirm or cancel an action before continuing.
` I am a modal type overlay.html`
this.dispatchEvent(
new Event('close', {
bubbles: true,
composed: true,
})
);
"
>
Sign in
'page' Overlays behave similarly to 'modal' Overlays by creating a modal context and trapping focus, but they will not be allowed to close via the "light dismiss" algorithm (e.g. the Escape key).
A page overlay could be used for a full-screen menu on a mobile website. When the user clicks on the menu button, the entire screen is covered with the menu options.
` I am a page type overlay.html`
mode="fullscreenTakeover"
cancel-label="Close"
>
'hint' Overlays are much like tooltips so they are not just ephemeral, but they are delivered primarily as a visual helper and exist outside of the tab order. In this way, be sure _not_ to place interactive content within this type of Overlay.
This overlay type does not accept focus and does not interfere with the user's interaction with the rest of the page.
`html`
I am a hint type overlay. I am not interactive and will close when the
user interacts with the page.
'auto' Overlays provide a place for content that is ephemeral _and_ interactive. These Overlays can accept focus and remain open while interacting with their content. They will close when focus moves outside the overlay or when clicking elsewhere on the page.
`html`
My slider in overlay element:
'manual' Overlays act much like 'auto' Overlays, but do not close when losing focus or interacting with other parts of the page.
Note: When a 'manual' Overlay is at the top of the "overlay stack", it will still respond to the Escape key and close.
`html`
Chat Window
When fully open the element will dispatch the sp-opened event, and when fully closed the sp-closed event will be dispatched. Both of these events are of type:
`ts`
type OverlayStateEvent = Event & {
overlay: Overlay;
};
The overlay value in this case will hold a reference to the actual that is opening or closing to trigger this event. Remember that some element (like those creates via the imperative API) can be transiently available in the DOM, so if you choose to build a cache of Overlay elements to some end, be sure to leverage a weak reference so that the can be garbage collected as desired by the browser.
#### When it is "fully" open or closed?
"Fully" in this context means that all CSS transitions that have dispatched transitionrun events on the direct children of the element have successfully dispatched their transitionend or transitioncancel event. Keep in mind the following:
- transition* events bubble; this means that while transition events on light DOM content of those direct children will be heard, those events will not be taken into accounttransition*
- events are not composed; this means that transition events on shadow DOM content of the direct children will not propagate to a level in the DOM where they can be heard
This means that in both cases, if the transition is meant to be a part of the opening or closing of the overlay in question you will need to re-dispatch the transitionrun, transitionend, and transitioncancel events from that transition from the closest direct child of the .
#### Action bar system
`html`
type="auto"
placement="right-start"
>
>
type="auto"
placement="right-start"
>
>
type="auto"
placement="right-start"
>
>
#### API
`ts`
?delayed=${boolean}
offset=${Number | [Number, Number]}
placement=${Placement}
receives-focus=${'true' | 'false' | 'auto' (default)
trigger=${string | ${string}@${string}}
.triggerElement=${HTMLElement | VirtualTrigger}
.triggerInteraction=${'click' | 'longpress' | 'hover'}
type=${'auto' | 'hint' | 'manual' | 'modal' | 'page'}
?allow-outside-click=${boolean}
>
##### API value interactions
When a triggerElement is present (via trigger attribute or direct property setting), the following configurations apply:
+ triggerElement
+ offset + triggerElement
without triggerElement
specified
• Content itself
• Application
Common in modal/page overlays for full-screen content
##### Deprecated Properties
> ⚠️ Deprecation Notice: The allow-outside-click property is deprecated and will be removed in a future version.
The allow-outside-click property allows clicks outside the overlay to close it. We do not recommend using this property for accessibility reasons as it can cause unexpected behavior and accessibility issues. When set to true, it configures the focus trap to allow outside clicks, which may interfere with proper focus management and user expectations.
` This overlay can be closed by clicking outsidehtml`
Alternative approaches: Instead of using allow-outside-click, consider implementing explicit close buttons or using the type="modal" or type="page" overlay types which provide better accessibility and user experience.
#### Styling
element will use the
#### Top layer over complex CSS
There are some complex CSS values that have not yet been covered by the positioning API that the element leverages to associate overlaid content with their trigger elements. In specific, properties like filter, when applied to a trigger element within which lives the related content to be overlaid, are not currently supported by the relationship created herein. If support for this is something that you and the work you are addressing would require, we'd love to hear from you in an issue. We'd be particularly interested in speaking with you if you were interested in contributing support/testing to ensure this capability for all consumers of the library.
#### Fallback support
While the is widely supported by browsers, the popover attribute is still quite new. When leveraged in browsers that do not yet support the popover attribute, there may be additional intervention required to ensure your content is delivered to your visitors as expected.
##### Complex layered
When an overlay is placed within a page with complex layering, the content therein can fall behind other content in the z-index stack. The following example is somewhat contrived but, imagine a toolbar next to a properties panel. If the toolbar has a lower z-index than the properties panel, any overlaid content (tooltips, etc.) within that toolbar will display underneath any content in the properties panel with which it may share pixels.
`html`
type="hint"
placement="bottom-start"
>
I can be partially blocked when [popover] is not available
Properly managed z-index values will support working around this issue while browsers work to adopt the popover attribute. In this demo, you can achieve the same output by sharing one z-index between the various pieces of content, removing z-index values altogether, or raising the .complex-layered-holder element to a higher z-index than the .complex-layered-blocker element.
##### Contained
CSS Containment gives a developer direct control over how the internals of one element affect the paint and layout of the internals of other elements on the same page. While leveraging some of its values can offer performance gains, they can interrupt the delivery of your overlaid content.
`html`
I can be blocked when [popover] is not available
You could just _remove_ the contain rule. But, if you are not OK with simply removing the contain value, you still have options. If you would like to continue to leverage contain, you can place your "contained" content separately from your overlaid content, like so:
`html`
type="hint"
placement="bottom-start"
>
accepts an ID reference via the trigger attribute to relate it to interactions and positioning in the DOM. To fulfill this reference the two elements need to be in the same DOM tree. However, alternatively accepts a triggerElement _property_ that opens even more flexibility in addressing this situation.
##### Clip pathed
clip-path can also restrict how content in an element is surfaced at paint time. When overlaid content should display outside of the clip-path, without the popover attribute, that content could be _clipped_.
`html`
type="hint"
placement="bottom-start"
>
I can be blocked when [popover] is not available
Here, again, working with your content needs (whether or not you want to leverage clip-path) or DOM structure (not colocating clipped and non-clipped content) will allow you to avoid this issue:
`html`
type="hint"
placement="bottom-start"
>
##### Non-overflowing, relative containers with z-index in Safari
Under very specific conditions, WebKit will incorrectly clip fixed-position content.
WebKit clips position: fixed elements within containers that have all of:
1. position: relativeoverflow: clip
2. or overflow: hiddenz-index
3. greater than zero
If you notice overlay clipping _only_ in Safari, this is likely the culprit. The solution is to break up the conditions into separate elements to avoid triggering WebKit's bug. For example, leaving relative positioning and z-index on the outermost container while creating an inner container that enforces the overflow rules.
#### Nested overlays
When nesting multiple overlays, it is important to ensure that the nested overlays are actually nested in the HTML as well, otherwise it will not be accessible.
` This is the outer modal content. Press ESC to close it.html`
Open Outer Modal
Outer Dialog
variant="primary"
aria-haspopup="dialog"
>
Open Inner Modal
type="auto"
trigger="innerTrigger@click"
>
Inner Dialog
This is the inner modal content. Press ESC to
close this first, then the outer modal.
#### Focus management
The overlay manages focus based on its type:
- For modal and page types, focus is always trapped within the overlayauto
- For and manual types, focus behavior is controlled by the receives-focus attributehint
- For type, focus remains on the trigger element
Example of proper focus management:
`html
Open Settings
dismissable
underlay
aria-labelledby="settings-heading"
>
Settings
#### Keyboard navigation
Key
Action
ESC
Closes overlays in reverse order of opening
TAB/Shift+TAB
Navigates through focusable elements within modal/page overlays
Arrow keys
Navigate through menu items in menu overlays
ENTER/SPACE
Activates buttons and controls
#### Screen reader considerations
- Use
aria-haspopup on trigger elements to indicate the type of overlay
- Provide descriptive labels using aria-label or aria-labelledby
- Use proper heading structure within overlays
- Ensure error messages are announced using aria-liveExample of a tooltip with proper screen reader support:
`html
Click for more information about this feature
``