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: none

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: none

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: none. onOpenChange: none

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

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;
}
TokenDefault
--sf-overlay-guttervar(--sf-space-4)
--sf-popover-min-width12rem
--sf-popover-max-widthmin(22rem, 100vw - 2 * var(--sf-overlay-gutter))
--sf-popover-max-heightmin(22rem, 70dvh)
--sf-popover-offsetvar(--sf-space-1)
--sf-radius-popovervar(--sf-radius-control)
--sf-shadow-popover0 8px 24px rgb(0 0 0 / 16%)
--sf-menu-description-colorvar(--sf-color-muted)
--sf-menu-description-size0.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.