dile-popover
Web Component that opens a panel next to a trigger element to show rich content: extra details, a compact form or secondary actions. It covers the middle ground between a tooltip, meant for short descriptions, and a modal, which takes the user's whole attention.
dile-popover is a preset of dile-menu-overlay: it extends it and only changes some defaults. Positioning, the opened property, Escape, click outside, exclusivity, focus handling and the overlay-* events are the ones of dile-menu-overlay, whose page documents the full API. It doesn't use the native HTML popover attribute.
Installation
npm i @dile/ui
Usage
Import the component.
import '@dile/ui/components/popover/popover.js';
Use the component with a trigger and a content slot.
<dile-popover>
<button slot="trigger" type="button">Details</button>
<div slot="content" id="details">
The order ships in 2 working days.
</div>
</dile-popover>
What the preset changes
Compared with dile-menu-overlay, dile-popover:
- Has a wider panel with padding, a maximum height with scroll, and its own
--dile-popover-*custom properties. - Sets
triggerAriaby default, so the trigger getsaria-expanded(andaria-controlswhen the content has anid). - Adds the
interactiveflag for content with controls.
Properties
The popover adds one property. All the dile-menu-overlay properties are available too: opened, horizontalAlign, verticalAlign, moveTop, moveLeft, closeOnClickInside, disableCloseOnClickOutside, allowMultiple, triggerAria, panelRole, panelLabel, focusOnOpen and closeOnFocusOut.
| Property | Type | Default | Description |
|---|---|---|---|
interactive |
Boolean |
false |
For content with controls (forms, buttons). Sets panelRole="dialog" and focusOnOpen="first", unless you give those properties other values. The trigger then gets aria-haspopup="dialog". |
triggerAria |
Boolean |
true |
Inherited from dile-menu-overlay, enabled by default in the popover. |
Methods
Inherited from dile-menu-overlay: open(), close() and toggle().
Events
The popover has no events of its own. It dispatches the dile-menu-overlay events:
- overlay-opened: dispatched when the popover opens.
- overlay-ready: dispatched when the content is displayed and can receive the focus.
- overlay-closed: dispatched when the popover closes.
Slots
- trigger: the element that opens the popover. Use a focusable element, like a
<button>, so it works with the keyboard. - content: the popover content. Give it an
idto getaria-controlson the trigger.
CSS Custom Properties
Each property falls back to the dile-menu-overlay one, and then to the popover default.
| Custom property | Description | Default |
|---|---|---|
| --dile-popover-width | Panel width | --dile-menu-overlay-width or 340px |
| --dile-popover-max-width | Panel max width | --dile-menu-overlay-max-width or calc(100vw - 25px) |
| --dile-popover-max-height | Panel max height (the content scrolls) | calc(100vh - 20px) |
| --dile-popover-padding | Panel padding | --dile-menu-overlay-padding or 1rem |
| --dile-popover-background-color | Panel background color | --dile-menu-overlay-background-color or #fff |
| --dile-popover-color | Panel text color | --dile-menu-overlay-color or #303030 |
| --dile-popover-border-radius | Panel border radius | --dile-menu-overlay-border-radius or 8px |
| --dile-popover-box-shadow | Panel shadow | --dile-menu-overlay-box-shadow or 0 4px 20px rgba(0, 0, 0, 0.25) |
| --dile-popover-z-index | Panel z-index | --dile-menu-overlay-z-index or 999 |
The transition is configured with --dile-menu-overlay-transition.
Accessibility
- Informative content (the default): the focus stays on the trigger and the panel has no role. Tab moves from the trigger into the content, because the panel comes right after the trigger.
- Interactive content (
interactive): the panel is a non-modaldialogand the focus moves to its first focusable element. UsepanelLabelto give it an accessible name. - Escape closes the popover. When it closes with the focus inside, the focus returns to the trigger.
- Click outside is never the only way to close it: Escape and the trigger always work, also with
disableCloseOnClickOutside. - Leaving the panel with Tab keeps it opened. Set
closeOnFocusOutto close it when the focus moves to another element of the page.
dile-popover demos
Informative content
Standard shipping
Orders placed before 2 PM ship the same day and arrive in 2 to 3 working days. Read the shipping policy.
<script type="module">
import '@dile/ui/components/popover/popover.js';
</script>
<dile-popover>
<button slot="trigger" type="button">Shipping details</button>
<div slot="content" id="shipping-details">
<p style="margin-top: 0;"><strong>Standard shipping</strong></p>
<p style="margin-bottom: 0;">Orders placed before 2 PM ship the same day and arrive in 2 to 3 working days. <a href="#informative-content">Read the shipping policy</a>.</p>
</div>
</dile-popover>
Form
With interactive, the panel is a dialog and the focus moves to the first field.
<dile-popover interactive panelLabel="Rename file" id="renamePopover">
<button slot="trigger" type="button">Rename</button>
<form slot="content" id="rename-form" onsubmit="event.preventDefault(); document.getElementById('renamePopover').close();">
<label for="rename-input" style="display: block; margin-bottom: 0.5rem;">File name</label>
<input id="rename-input" value="report.pdf" style="width: 100%; box-sizing: border-box; margin-bottom: 1rem;">
<button type="submit">Save</button>
<button type="button" onclick="document.getElementById('renamePopover').close()">Cancel</button>
</form>
</dile-popover>
Click outside disabled and several popovers opened
disableCloseOnClickOutside and allowMultiple keep these popovers opened until the user closes them with Escape or their trigger.
<div style="display: flex; gap: 1rem; flex-wrap: wrap;">
<dile-popover disableCloseOnClickOutside allowMultiple>
<button slot="trigger" type="button">Note 1</button>
<div slot="content">First note. It stays opened.</div>
</dile-popover>
<dile-popover disableCloseOnClickOutside allowMultiple>
<button slot="trigger" type="button">Note 2</button>
<div slot="content">Second note. Open both at the same time.</div>
</dile-popover>
</div>
Declarative state
The app opens and closes the popover by setting its opened property. The buttons are outside the popover, so they stop the propagation of their clicks: otherwise a click on "Set opened = true" while it is opened would count as a click outside and close it. The state shown follows the overlay-* events, so it also changes with Escape, a click outside or the trigger.
opened property.<div style="display: flex; gap: 1rem; flex-wrap: wrap; align-items: center;">
<dile-popover id="declarativePopover">
<button slot="trigger" type="button">Help</button>
<div slot="content" id="declarative-help">Opened from the app through the <code>opened</code> property.</div>
</dile-popover>
<button type="button" id="declarativeOpen">Set opened = true</button>
<button type="button" id="declarativeClose">Set opened = false</button>
<span>opened: <strong id="declarativeState">false</strong></span>
</div>
<script type="module">
const popover = document.getElementById('declarativePopover');
const state = document.getElementById('declarativeState');
const showState = () => { state.textContent = String(popover.opened); };
popover.addEventListener('overlay-opened', showState);
popover.addEventListener('overlay-closed', showState);
document.getElementById('declarativeOpen').addEventListener('click', (e) => {
e.stopPropagation();
popover.opened = true;
});
document.getElementById('declarativeClose').addEventListener('click', (e) => {
e.stopPropagation();
popover.opened = false;
});
</script>
Styled
<style>
.styled-popover {
--dile-popover-width: 240px;
--dile-popover-background-color: #303030;
--dile-popover-color: #fff;
--dile-popover-border-radius: 0;
}
</style>
<dile-popover class="styled-popover" verticalAlign="top">
<button slot="trigger" type="button">Styled popover</button>
<div slot="content">A dark popover opened above its trigger.</div>
</dile-popover>
dile-components