LedgerUI.Components.Dropdown (LedgerUI v0.2.0)

Copy Markdown View Source

Dropdown menu: a button (or any trigger) that opens a floating menu of links and actions.

<.dropdown id="actions" label="Ações">
  <.dropdown_item phx-click="edit"><:icon><.icon name="edit" /></:icon>Editar</.dropdown_item>
  <.dropdown_separator />
  <.dropdown_item danger phx-click="delete">Excluir</.dropdown_item>
</.dropdown>

<.dropdown id="row" toggle_icon="dots" toggle_label="Mais ações" placement="bottom-end">
  <.dropdown_item href="#">Abrir</.dropdown_item>
</.dropdown>

Markup

The root is the lu-anc anchor; the menu is span.fl.p-<placement> holding the lu-pop box. Everything dynamic (open state, ARIA, positioning, keys, click outside, scroll following, flipping) is done by the hook LedgerUI.Dropdown (priv/static/ledger_ui/hooks/dropdown.js), which puts the menu in the top layer (popover="manual", position: fixed) under the trigger. The server renders the ARIA and the initial state: popover="manual" on the menu, role="menu" on the box, role="menuitem" (menuitemcheckbox/menuitemradio, separator) on the parts and, on the built-in triggers, aria-haspopup="menu", aria-expanded and aria-controls; the hook only flips aria-expanded and the open state. A custom :toggle slot receives those trigger attributes as its argument (<:toggle :let={t}><button {t}>...</button></:toggle>); when it does not spread them the hook falls back to setting aria-haspopup/aria-controls on that trigger. Options that differ from the defaults reach the hook as data-* attributes on the root (data-offset, data-hover, data-keep-submenus, data-close-on-select, data-on-open, data-on-close).

LedgerUI.open/close/toggle work with the dropdown id.

Additions to the design README

  • menu_style - inline style of the menu box (width:220px); anchor_class - extra classes of the root.
  • :overlay slot - extra content after the menu inside the anchor (a demo cursor).
  • dropdown_item accepts danger, selected and a :trailing slot.

Summary

Functions