Component · top layer
Popover and Menu
A panel beside its button, in the top layer so nothing clips it. The menu adds the keyboard of an action menu.
Chosen:
With descriptions
A quieter line under an item's words. Its name and typeahead are the words; screen readers hear the line after them.
Chosen:
Inside a box that clips
The box is 3.5rem tall with hidden overflow.
This panel is still whole.
At the screen edge
Asked to open towards the end, it flips.
Astro
---
import Menu from '@subfloor/astro/Menu.astro'
import Popover from '@subfloor/astro/Popover.astro'
---
<Menu id="project-actions" label="Actions" items={[
{ label: 'Edit', value: 'edit' },
{ label: 'Share', value: 'share', description: 'Anyone with the link' },
{ label: 'Archive', value: 'archive', disabled: true },
{ label: 'Settings', href: '/settings/' },
'separator',
{ label: 'Delete', value: 'delete', danger: true },
]} />
<!-- placement: "bottom-start" (default) | "bottom-end" | "top-start" | "top-end" -->
<Popover id="filters" label="Filters" placement="bottom-end">
…
</Popover>
<script>
document.getElementById('project-actions')
?.addEventListener('sf:select', (event) => console.log(event.detail.value))
</script>React
'use client'
import { Menu, Popover } from '@subfloor/react'
<Menu
label="Actions"
menuLabel="Project actions"
items={[
{ label: 'Edit', onSelect: edit },
{ label: 'Move', onSelect: move, description: 'To another project' },
{ label: 'Settings', href: '/settings' },
'separator',
{ label: 'Delete', onSelect: remove, danger: true },
]}
renderLink={(props) => <Link {...props} />}
/>
// Controlled: set open in onOpenChange. If open stays true after the user
// closed it, it stays closed until open changes (as with Dialog).
<Popover label="Filters" open={open} onOpenChange={(next) => setOpen(next)}>
…
</Popover>Uncontrolled, with onOpenChange.
Chosen: . onOpenChange:
Markup
The button names itself with anchor-name, the panel points at it with position-anchor; the components set both.
<button type="button" popovertarget="actions" aria-haspopup="menu"
aria-controls="actions" style="anchor-name: --sf-anchor-actions">Actions</button>
<div id="actions" popover="auto" role="menu" aria-label="Actions"
class="sf-popover sf-menu" data-sf-menu data-placement="bottom-start"
style="position-anchor: --sf-anchor-actions">
<button type="button" class="sf-menu__item" role="menuitem" tabindex="-1"
data-value="edit">Edit</button>
<button type="button" class="sf-menu__item" role="menuitem" tabindex="-1"
data-value="share" aria-describedby="actions-share-description">
<span class="sf-menu__label">Share</span>
<span class="sf-menu__description" id="actions-share-description"
aria-hidden="true">Anyone with the link</span>
</button>
<a class="sf-menu__item" role="menuitem" tabindex="-1" href="/settings/">Settings</a>
<hr class="sf-menu__separator" role="separator">
<button type="button" class="sf-menu__item" role="menuitem" tabindex="-1"
data-danger>Delete</button>
</div>Behaviour
- The panel is in the top layer: an
overflowortransformon the button's parents cannot clip it. - It opens beside its button and flips to the other side at a screen edge (CSS anchor positioning). It keeps its natural width, so at a side edge it lines up with the other end of the button, and where it fits beside neither end it is in the middle of the screen. Too tall for either side, it gets shorter and scrolls inside. The script measures the larger room, above or below the button, again as the page scrolls; without it, the panel is at most about half the screen tall. Browsers without anchor positioning (Safari before 26) show it as a sheet along the bottom.
- “Start” and “end” follow the direction of the panel’s own part of the page: in a right-to-left part,
bottom-startlines up with the button’s right edge. To place a panel yourself, set itsinset. - An outside press or Escape closes it; the button closes it again. Escape gives focus back to the button, an outside press leaves it where you pressed. No scroll lock: it is not a modal.
- The menu: Enter, Space, ↓ or a click open it on the first item, ↑ on the last; ↓ ↑ wrap, Home End jump, letters jump to an item; Enter or Space chooses and gives focus back to the button; Tab closes and moves on. Disabled items are skipped. A choice dispatches
sf:select. - An item's
descriptionis a quieter line under its words, in.sf-menu__description: hidden from the item's name and linked byaria-describedby, so screen readers read it after the words. Typeahead matches the words, in.sf-menu__label, and they are the value when there is nodata-value. The line is quieter by--sf-menu-description-colorand--sf-menu-description-size; in a danger item it keeps the danger colour, so only the size sets it apart. In React, an item'siconis an element (an<svg>or a<span>), not bare text, so it stands beside both lines. sf:open-changereports each change with a reason:escape,outside,toggle,select,taborapi.- A React popover opened before React hydrates stays open and is reported once. A menu opened before its script runs gets focus on its first item, unless you have moved focus away from its button.
Without JavaScript
The button opens and closes the panel (popovertarget), an outside press and Escape close it, and CSS places it. The menu's arrow keys, typeahead and aria-expanded need the script; Tab walks through its items instead.
Customisation
.compact-menu {
--sf-popover-min-width: 9rem;
--sf-radius-popover: 4px;
}| Token | Default |
|---|---|
--sf-overlay-gutter | var(--sf-space-4) |
--sf-popover-min-width | 12rem |
--sf-popover-max-width | min(22rem, 100vw - 2 * var(--sf-overlay-gutter)) |
--sf-popover-max-height | min(22rem, 70dvh) |
--sf-popover-offset | var(--sf-space-1) |
--sf-radius-popover | var(--sf-radius-control) |
--sf-shadow-popover | 0 8px 24px rgb(0 0 0 / 16%) |
--sf-menu-description-color | var(--sf-color-muted) |
--sf-menu-description-size | 0.875em |
--sf-color-danger | #b91c1c |
Background
Content 1. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 2. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 3. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 4. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 5. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 6. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 7. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 8. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 9. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 10. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 11. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 12. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.