Introducing a new way to review your PRs.

Read the announcement

petalframework / petal_components

Phoenix + Live View HEEX Components

View on GitHub

24 documents · Updated Sep 2, 2026

docs / overlays-and-menus

Overlays, menus, and commands

Petal Components provides an interactive disclosure layer for short-lived content and navigation choices. Server-rendered HEEx components define the structure and accessible state. LiveView JS commands and optional browser hooks then manage opening, closing, focus, keyboard input, and placement without changing the meaning of the content.

Disclosure layer

The disclosure layer separates content by how it should interrupt or extend the current page. Full overlays hold the user's attention. Anchored panels stay with a trigger. Menus expose actions or routes. Tooltips add brief context without becoming a task surface.

NeedComponent familyMain interaction model
Focused task or confirmationModal and slide-overOpen and close with focus placed in the panel; Escape and click-away can dismiss
Anchored supporting contentPopoverOpen from a trigger and close by light dismissal; placement can remain inside the viewport
Actions or account choicesDropdown and user dropdown menuToggle a menu panel and dismiss it with Escape or an outside click
Site or application navigationNavigation menu and vertical menuReveal links while keeping navigation semantics separate from command menus
Fast action searchCommand palette and command dialogFilter items as the user types and move a virtual selection with the keyboard
Brief control helpTooltipShow on pointer hover or keyboard focus

The two child pages give the detailed guidance for these groups: Transient overlays covers modal, slide-over, popover, and tooltip behavior. Menus and command navigation covers dropdowns, user menus, navigation menus, and command palettes.

Server-rendered structure

The HEEx components provide the stable markup that the browser and LiveView can understand before any client behavior runs. They connect triggers to panels with IDs and ARIA relationships, mark dialogs and menus with their roles, and keep links and buttons as the real action targets.

The component layer also expresses the product choice behind each disclosure type. A modal or slide-over is a dialog with an optional title, description, close control, and action footer. A bottom slide-over can also act as a drawer with a handle, snap positions, and pointer drag dismissal. A dropdown is a menu of links or buttons with a placement choice that can be above, below, or beside its trigger. A user dropdown menu reuses that menu surface for either a compact account trigger or a sidebar identity row. A vertical menu represents navigation data with active-page state and nested sections. Nested sections use a button to reveal a submenu, and callers can add JS to the toggle without replacing the built-in reveal and focus behavior. A command item remains a link or button, so navigation and LiveView events keep their normal behavior.

References: lib/…/modal.ex• L20-125 lib/…/slide_over.ex• L7-188 lib/…/dropdown.ex• L47-167 lib/…/menu.ex• L142-277 lib/…/menu.ex• L345-400 lib/…/user_dropdown_menu.ex• L7-136 lib/…/command.ex• L32-135

Client coordination

LiveView JS commands handle the common open and close path for server-rendered surfaces. They show or hide the panel, apply the transition, lock page scrolling for full overlays, and place focus in the newly opened content. Close paths can also push an event back to the owning LiveView, so a client-side dismissal can lead to a server state change.

JavaScript hooks take ownership only where the browser needs ongoing client state. They observe native popover and dialog state, restore client-owned state after a LiveView patch, filter command items without a server round trip, measure panels against the visible viewport, and add pointer behavior to bottom drawers. The server remains the source of markup and item order. The hook preserves the user's current interaction around that markup.

This diagram shows how server-rendered disclosure markup moves through shared commands or specialized hooks before it produces visible and interactive state.

flowchart LR
    A[HEEx trigger and panel] --> B{Disclosure type}
    B -->|Modal or slide-over| C[LiveView JS open and close]
    B -->|Bottom drawer| D[LiveView JS and drawer hook]
    B -->|Anchored popover| E[Native popover or LiveView JS]
    B -->|Dropdown or navigation menu| F[LiveView JS or menu hook]
    B -->|Command dialog| G[Native dialog and command hooks]
    C --> H[Visibility, focus, scroll lock]
    D --> I[Drag, snap, and dismissal]
    E --> J[Dismissal and viewport position]
    F --> K[Keyboard, pointer, and panel state]
    G --> L[Search, highlight, focus trap]
    H --> M[LiveView events and patches]
    I --> M
    J --> M
    K --> M
    L --> M

The bundled hooks are registered through the library's hook export when a LiveView app uses the package JavaScript. The tooltip is the exception: it uses CSS hover and focus state and does not need a hook.

References: lib/…/modal.ex• L75-181 lib/…/slide_over.ex• L155-279 lib/…/dropdown.ex• L107-165 lib/…/popover.ex• L89-181 lib/…/tooltip.ex• L4-84 assets/…/petal_components.js• L1971-2187 assets/…/petal_components.js• L2190-2486 assets/…/petal_components.js• L2771-2886 assets/…/petal_components.js• L2905-2991 assets/…/petal_components.js• L7761-8070 assets/…/petal_components.js• L9919-9934

Focus and keyboard model

Focus follows the disclosure's role. Full overlays place focus in the panel and keep the page behind it inactive. Anchored panels return focus to their trigger when Escape closes them. Command dialogs focus the search field, while arrow keys move a virtual highlight and Enter activates the highlighted real link or button.

Dismissal belongs to the surface that owns the interaction. Escape and click-away close most dialogs and menus. A command dialog also closes when an item runs unless that item opts to stay open. A bottom drawer adds pointer drag as an enhancement, while keyboard and screen-reader users retain dialog focus and Escape behavior. Navigation menus add hover and focus behavior for desktop navigation, while touch use is driven by an explicit click path. Vertical navigation submenus open from their buttons and focus the first child.

The markup keeps accessible state aligned with these actions. Triggers expose expanded state and controls. Dialogs name their content. Command inputs use the combobox pattern, and command items expose a selected descendant instead of moving keyboard focus away from the input. The drawer handle is decorative, so the drag behavior does not create a second keyboard control.

References: lib/…/modal.ex• L84-111 lib/…/slide_over.ex• L168-227 lib/…/dropdown.ex• L123-165 lib/…/menu.ex• L239-275 lib/…/navigation_menu.ex• L7-20 lib/…/command.ex• L17-25 lib/…/command.ex• L219-251 assets/…/petal_components.js• L2194-2357 assets/…/petal_components.js• L2771-2886 assets/…/petal_components.js• L7747-8055

Viewport positioning

Anchored panels use the trigger as their reference point and treat the visible viewport as the usable area. Popovers can move to the opposite side, clamp their cross-axis position, and scroll their own content when the panel is too large. Dropdowns use the same downward-first decision and can flip upward when a lower trigger has more room above it.

Dropdown placement has two separate choices: side selects above, below, left, or right, and align lines up the panel on the other axis. With no explicit side, the dropdown hook measures on open and during viewport changes. An explicit side answers the main placement question in the server-rendered markup and skips that vertical flip hook. Older placement and direction names still map to the newer vocabulary.

Navigation flyouts use a lighter adjustment: they keep their panel within the horizontal viewport. Explicit placement remains available when the surrounding layout already knows the correct side. The user menu passes that placement choice through to the dropdown so a sidebar account row can open above or beside the sidebar.

Positioning is separate from disclosure state. The server chooses the intended placement and renders the panel. A hook measures after the panel opens and updates the client position as the page scrolls, resizes, or the mobile visual viewport changes.

References: lib/…/popover.ex• L14-64 lib/…/dropdown.ex• L5-31 lib/…/dropdown.ex• L277-388 lib/…/user_dropdown_menu.ex• L21-62 lib/…/navigation_menu.ex• L21-35 assets/…/petal_components.js• L1937-2187 assets/…/petal_components.js• L2905-2991

LiveView patch boundaries

LiveView patches can replace attributes or child nodes while a disclosure remains open. The coordination layer therefore treats server markup and client interaction state as separate concerns. Hooks reapply filtering or position after an update, and the command dialog restores its open state and focus when a patch would otherwise remove the client-only dialog state.

This boundary matters most for overlays that lock the page or own focus. Every close route must release the scroll lock exactly once. A removed open dialog must also release it because removal does not produce the native close event. When a bottom drawer uses drag, snap, or background-scale behavior, its hook must restore its position after a patch so server updates do not move the sheet away from the user's finger. Its optional background scale follows the drawer's visibility and is removed when the drawer closes or is destroyed.

References: assets/…/petal_components.js• L2031-2038 assets/…/petal_components.js• L2415-2455 assets/…/petal_components.js• L7806-7823 assets/…/petal_components.js• L7827-7837 assets/…/petal_components.js• L7913-7921 assets/…/petal_components.js• L8415-8425 lib/…/slide_over.ex• L155-197 lib/…/slide_over.ex• L369-409

Change checklist

When these files or sections are changed, remember to consider these:

  • When a dialog's markup or close path changes, keep its title, ARIA dialog state, focus entry, click-away behavior, Escape behavior, and LiveView close event aligned.
  • When a trigger or panel ID changes, update the ARIA controls relationship and every JS selector or dispatched event that targets the surface.
  • When a dropdown's placement vocabulary changes, keep side and align resolution aligned with the server classes, the auto-flip hook, and user-menu callers. Preserve the legacy mapping if existing consumers use it.
  • When a bottom drawer's snap, drag, or background options change, keep the server data attributes aligned with the hook's restore, settle, dismissal, and wrapper-scale behavior.
  • When a vertical-menu submenu changes, keep its stable container and generated submenu and icon IDs aligned with the toggle command, initial active state, focus target, and caller-supplied on_toggle JS.
  • When command item markup changes, preserve the input-to-listbox relationship and the item attributes used for filtering, virtual selection, disabled state, and Enter activation.
  • When adding or removing a hook, update the bundled hook export and the app registration contract. CSS-only tooltips do not need this step.

Important failure modes

TriggerConsequenceGuard
A dialog opens through client state but a LiveView patch removes its open stateThe dialog closes while the page scroll lock remainsPreserve open state across the patch and release the lock from the native close path
A dropdown or popover panel is measured against the layout viewport while the mobile keyboard is openThe panel appears behind or outside the visible screenUse the visual viewport and reposition on its scroll and resize events
A menu trigger loses its panel ID or ARIA relationshipKeyboard and assistive technology users cannot identify or control the panelKeep trigger IDs, panel IDs, aria-controls, and expanded state as one contract
A command item is reordered during client filteringLiveView patches can replace the wrong item state or break the active descendantHide unmatched items while the server keeps DOM order
A bottom drawer patch drops the hook-owned drag transformThe sheet jumps to its rest position while the user is dragging or after an updateRestore the current drawer offset after patches and avoid resetting it during an active drag
A drawer drag claims a gesture while its content can still scrollThe user cannot scroll the drawer content normallyStart body drags only at the scroll top and keep the handle path explicit
A slide-over receives an unknown runtime originIts transition or position selection can fail instead of showing a usable panelNormalize runtime origins to the default direction before rendering
A nested button or link is placed inside a dropdown trigger slotBrowser control semantics split and the toggle can stop workingPut trigger content inside the component's built-in button without another interactive element

Key files

FileWhy to read it
lib/petal_components/modal.exDefines the general dialog contract and its LiveView open, close, focus, and dismissal behavior
lib/petal_components/slide_over.exShows how an edge-attached dialog adds origin, transitions, pinned actions, and bottom-drawer options
lib/petal_components/popover.exDefines the anchored panel choices, including the native top-layer mode
lib/petal_components/dropdown.exDefines the shared action-menu surface, placement vocabulary, and legacy placement mapping
lib/petal_components/user_dropdown_menu.exShows how account identity and custom account content use the dropdown surface
lib/petal_components/menu.exDefines navigation-oriented lists with active pages, groups, and nested submenus
lib/petal_components/navigation_menu.exDefines navigation-oriented flyouts and their hover or click behavior
lib/petal_components/command.exDefines inline and dialog command palettes plus their accessible item model
lib/petal_components/tooltip.exDefines the CSS-only hover and focus disclosure pattern
assets/…/petal_components.jsShows the client coordination for positioning, command interaction, native dialogs, navigation menus, dropdown flipping, and drawers
README.mdGives the package-level contract for component use and JavaScript hook registration