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.
| Need | Component family | Main interaction model |
|---|---|---|
| Focused task or confirmation | Modal and slide-over | Open and close with focus placed in the panel; Escape and click-away can dismiss |
| Anchored supporting content | Popover | Open from a trigger and close by light dismissal; placement can remain inside the viewport |
| Actions or account choices | Dropdown and user dropdown menu | Toggle a menu panel and dismiss it with Escape or an outside click |
| Site or application navigation | Navigation menu and vertical menu | Reveal links while keeping navigation semantics separate from command menus |
| Fast action search | Command palette and command dialog | Filter items as the user types and move a virtual selection with the keyboard |
| Brief control help | Tooltip | Show 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
sideandalignresolution 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_toggleJS. - 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
| Trigger | Consequence | Guard |
|---|---|---|
A dialog opens through client state but a LiveView patch removes its open state | The dialog closes while the page scroll lock remains | Preserve 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 open | The panel appears behind or outside the visible screen | Use the visual viewport and reposition on its scroll and resize events |
| A menu trigger loses its panel ID or ARIA relationship | Keyboard and assistive technology users cannot identify or control the panel | Keep trigger IDs, panel IDs, aria-controls, and expanded state as one contract |
| A command item is reordered during client filtering | LiveView patches can replace the wrong item state or break the active descendant | Hide unmatched items while the server keeps DOM order |
| A bottom drawer patch drops the hook-owned drag transform | The sheet jumps to its rest position while the user is dragging or after an update | Restore 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 scroll | The user cannot scroll the drawer content normally | Start body drags only at the scroll top and keep the handle path explicit |
| A slide-over receives an unknown runtime origin | Its transition or position selection can fail instead of showing a usable panel | Normalize runtime origins to the default direction before rendering |
| A nested button or link is placed inside a dropdown trigger slot | Browser control semantics split and the toggle can stop working | Put trigger content inside the component's built-in button without another interactive element |
Key files
| File | Why to read it |
|---|---|
lib/petal_components/modal.ex | Defines the general dialog contract and its LiveView open, close, focus, and dismissal behavior |
lib/petal_components/slide_over.ex | Shows how an edge-attached dialog adds origin, transitions, pinned actions, and bottom-drawer options |
lib/petal_components/popover.ex | Defines the anchored panel choices, including the native top-layer mode |
lib/petal_components/dropdown.ex | Defines the shared action-menu surface, placement vocabulary, and legacy placement mapping |
lib/petal_components/user_dropdown_menu.ex | Shows how account identity and custom account content use the dropdown surface |
lib/petal_components/menu.ex | Defines navigation-oriented lists with active pages, groups, and nested submenus |
lib/petal_components/navigation_menu.ex | Defines navigation-oriented flyouts and their hover or click behavior |
lib/petal_components/command.ex | Defines inline and dialog command palettes plus their accessible item model |
lib/petal_components/tooltip.ex | Defines the CSS-only hover and focus disclosure pattern |
| assets/…/petal_components.js | Shows the client coordination for positioning, command interaction, native dialogs, navigation menus, dropdown flipping, and drawers |
| README.md | Gives the package-level contract for component use and JavaScript hook registration |