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 / transient-overlays

Transient overlays

Transient overlays are short-lived surfaces that add context or actions without taking the user to a new page. Petal Components uses dialogs for focused work, slide-overs for edge-attached work, popovers and menus for nearby actions, hover cards for rich previews, and tooltips for brief labels. This page is a child of overlays-and-menus. Use menus-and-command-navigation for the wider command-navigation model.

Overlay model

Surface roles

Each surface type has a different user task. That task sets the expected trigger, focus model, placement, and dismissal behavior.

SurfacePrimary purposeTriggerSurface relationship
ModalGeneral focused work or a decision that can be abandonedExternal controlCentered over the page
Alert dialogA choice where leaving without an answer is unclear or unsafeExternal control or built-in triggerCentered over the page
Slide-overSupporting work or detail without a full page changeExternal controlAttached to a viewport edge
PopoverInteractive local contentComponent-owned buttonAnchored to its trigger
Hover cardA rich preview or related contentHover or focusAnchored to its trigger
TooltipA short, non-interactive label or hintHover or focusVisually attached to its trigger
DropdownA short command or navigation listComponent-owned buttonAnchored to its trigger
Context menuCommands for a region at the point of requestRight-click, long press, or keyboardAnchored to the pointer or trigger region

Use a modal for forms and detail panes. Use an alert dialog only when the user must make an explicit choice. Use a popover or hover card for interactive local content. Use a tooltip only when the content is a brief aid and not an action.

References: lib/…/modal.ex• L1-12 lib/…/alert_dialog.ex• L7-31 lib/…/slide_over.ex• L1-5 lib/…/popover.ex• L16-19 lib/…/hover_card.ex• L1-6 lib/…/tooltip.ex• L4-5 lib/…/dropdown.ex• L1-3 lib/…/context_menu.ex• L6-16

Shared lifecycle

An overlay starts with a trigger request. It then becomes visible, receives its position and focus state, accepts user input, and follows one of its dismissal paths. Dialog surfaces also prevent page scrolling while open. A close event may update the LiveView state after the visual surface starts to leave.

The main lifecycle is shared by the surfaces, but the trigger and close rules differ by role.

flowchart LR
    A[Trigger request] --> B[Surface opens]
    B --> C[Placement and focus]
    C --> D[User interaction]
    D --> E[Close intent]
    E --> F[Surface hides]
    E --> G[Optional LiveView event]

Opening and closing transitions are visual. They must not change which content belongs to the surface or replace the application action that caused the close. Use the matching show and hide command for modal and slide-over pairs.

References: lib/…/modal.ex• L69-82 lib/…/modal.ex• L128-182 lib/…/slide_over.ex• L158-204 lib/…/slide_over.ex• L250-318 assets/…/petal_components.js• L2507-2524

Trigger contracts

Dialog triggers

Modal and slide-over triggers are external controls. The trigger must address the same surface id that the show command opens. The close action must use the matching hide command. If closing changes LiveView state, the owning view handles close_modal or close_slide_over and decides what state or route follows.

Alert dialogs can also render a trigger beside the dialog. That trigger is linked to the dialog by the component, so callers do not need to repeat the id. An external trigger can still open the dialog through its open command.

References: lib/…/modal.ex• L20-55 lib/…/modal.ex• L128-159 lib/…/slide_over.ex• L70-113 lib/…/slide_over.ex• L250-318 lib/…/alert_dialog.ex• L128-172 lib/…/alert_dialog.ex• L258-278

Anchored triggers

Popover and dropdown triggers belong to the component. Each component renders a native button and places the supplied trigger content inside it. Pass text, an icon, or an avatar to the trigger slot. Do not put another button or link inside that slot because nested interactive controls break the toggle contract.

The popover trigger names its panel and exposes its expanded state in default mode. The dropdown trigger carries the marker used to relate the menu to its panel for placement. Context-menu triggers use a focusable region instead of a button because the region itself is the right-click target.

References: lib/…/popover.ex• L30-33 lib/…/popover.ex• L127-181 lib/…/dropdown.ex• L87-165 lib/…/context_menu.ex• L86-142

Hover and tooltip triggers

Hover-card content wraps the supplied trigger and opens when the pointer rests on it or focus enters it. The trigger must remain useful without the card because the card is enrichment, not the only path to the information. A link or button gives keyboard and touch users that path.

Tooltip content also wraps the supplied trigger. The trigger must have its own accessible name. For assistive technology to announce the tooltip, connect the focusable trigger's aria-describedby value to the tooltip id.

References: lib/…/hover_card.ex• L34-44 lib/…/hover_card.ex• L80-103 lib/…/tooltip.ex• L31-45 lib/…/tooltip.ex• L64-83

Focus behavior

Dialog focus

When a modal or slide-over opens, focus moves into the surface. The dialog title provides the accessible name, and a slide-over can add a description. Keep a usable close path when the visible header or close button is hidden.

Alert dialogs focus the cancel action first. This makes the least destructive choice the default. Escape follows the cancel path, while backdrop clicks do not close an alert dialog. The native dialog behavior also contains focus and restores it to the opener when the alert dialog closes.

Bottom slide-over drawers keep the same dialog focus and keyboard contract as other slide-overs. Their drag behavior is an optional pointer enhancement, so keyboard and screen-reader users can open, use, and close the drawer without the drag handle.

References: lib/…/modal.ex• L84-123 lib/…/modal.ex• L159-182 lib/…/slide_over.ex• L174-235 lib/…/slide_over.ex• L25-43 lib/…/alert_dialog.ex• L21-28 assets/…/petal_components.js• L2520-2524 assets/…/petal_components.js• L2652-2686

Anchored focus

The default popover keeps its trigger as a keyboard-accessible button. Escape closes the panel and returns focus to that trigger. Its click-away path closes the panel without moving focus into the panel. Top-layer popovers use the browser's popover behavior for open and light dismiss, while the positioning hook keeps the panel beside its trigger.

Dropdowns close on Escape or an outside click. They keep the trigger as the main keyboard control and do not provide menu-item arrow navigation by themselves. Context menus provide that navigation: keyboard opening focuses the first enabled item, arrows move through enabled items, and Escape or Tab closes the menu and returns focus to the trigger region.

References: lib/…/popover.ex• L89-181 lib/…/dropdown.ex• L303-333 lib/…/context_menu.ex• L53-63 assets/…/petal_components.js• L8356-8371 assets/…/petal_components.js• L8503-8552 assets/…/petal_components.js• L8578-8597

Preview focus

Hover cards open on focus-within, so focus can move from the trigger into the card's controls. They do not trap focus. Moving focus away closes the card, and Escape is not a close path. Use a popover when Escape or explicit light dismissal is required.

Tooltips never move focus into the bubble. They appear while the trigger is hovered or focused, then disappear when neither state remains. Tooltip content cannot receive pointer input, so interactive content belongs in a popover or hover card.

References: lib/…/hover_card.ex• L80-109 lib/…/tooltip.ex• L4-5 lib/…/tooltip.ex• L31-45 assets/default.css• L2774-2795

Placement behavior

Dialog placement

Modals center their surface inside a full-viewport layer. Slide-overs attach to the left, right, top, or bottom edge. Their content area can scroll while the header and optional footer remain available for orientation and actions.

Bottom slide-overs are drawers. They can show a handle, support drag-to-dismiss, and rest at viewport-height snap points. A drag closes through the same path as Escape, the close button, and click-away. Other slide-over origins do not use this drawer interaction.

The drawer can also scale and round a marked page wrapper behind it. This option is off by default because it changes the positioning context for fixed page elements and can cause a full-page repaint. Use it only when the page wrapper can safely accept that change.

References: lib/…/modal.ex• L115-123 lib/…/slide_over.ex• L13-64 lib/…/slide_over.ex• L142-146 lib/…/slide_over.ex• L174-238 lib/…/slide_over.ex• L369-430 assets/…/petal_components.js• L7899-8055

Anchored placement

Popover placement names a side and optional alignment: top, bottom, left, or right with start or end alignment. Top-layer popovers escape overflow-clipped ancestors. They flip to the opposite side when needed, stay within the visible viewport, and limit their height when space is short.

Dropdown placement uses side and align. With no side, it opens below and measures the available space on each open. It flips above only when it does not fit below and there is more room above. Explicit sides skip this measurement. placement and direction remain supported as older names for the same choices.

Context menus open near the pointer or near the trigger region for keyboard use. They flip away from a viewport edge and limit their height so the menu remains usable. The position check uses the visual viewport, which accounts for a mobile keyboard and pinch zoom.

References: lib/…/popover.ex• L35-53 lib/…/popover.ex• L55-64 lib/…/dropdown.ex• L5-31 lib/…/dropdown.ex• L63-85 lib/…/context_menu.ex• L129-142 assets/…/petal_components.js• L1944-1961 assets/…/petal_components.js• L1963-1968 assets/…/petal_components.js• L2080-2099 assets/…/petal_components.js• L2905-2990 assets/…/petal_components.js• L8622-8667

Preview placement

Hover cards use a fixed side with optional start or end alignment. They do not flip or shift to avoid an edge, so choose a placement with enough room. Tooltips use a fixed top, bottom, left, or right placement and can show an arrow back to the trigger.

References: lib/…/hover_card.ex• L23-32 lib/…/hover_card.ex• L121-134 lib/…/tooltip.ex• L47-58 assets/default.css• L2784-2809

Dismissal behavior

Light dismissal

Modal and slide-over surfaces support a close button, click-away, and Escape by default. Callers can disable click-away or Escape when leaving the surface would be unsafe or confusing. A hidden modal header must not remove every usable close path.

Popover and dropdown surfaces close on click-away and Escape. The default popover implements these paths locally, while a top-layer popover uses the browser's light-dismiss behavior. Tooltips close when hover and focus both leave. Hover cards close when hover and focus leave. Alert dialogs ignore backdrop clicks and require cancel or confirm; Escape selects cancel.

References: lib/…/modal.ex• L30-45 lib/…/slide_over.ex• L87-95 lib/…/slide_over.ex• L195-203 lib/…/popover.ex• L89-117 lib/…/alert_dialog.ex• L89-111 lib/…/hover_card.ex• L46-60 assets/default.css• L2777-2783 assets/…/petal_components.js• L2520-2524

Context menus close when a tap completes outside the menu. A drag, a two-finger gesture, or a scroll does not count as an outside tap. Scrolling the page closes an open context menu because its pointer position is no longer reliable. Selecting an item closes the menu after the item's own action has been queued.

The context menu's long-press path opens after a short hold and cancels when the pointer moves. Disabled mode removes the menu contract, so the browser's native context menu remains available.

References: lib/…/context_menu.ex• L53-71 assets/…/petal_components.js• L8316-8342 assets/…/petal_components.js• L8373-8385 assets/…/petal_components.js• L8473-8501

Close propagation

The visual close and the application close are separate concerns. on_cancel and on_close can add client actions, while modal and slide-over hide commands can push a targeted close event to the LiveView that owns the state. Keep the target stable when a surface lives inside a component. Otherwise the surface can disappear while the owning state remains open.

Bottom-drawer drag dismissal uses the same close command as the other slide-over paths. This keeps one server event, close_slide_over, for a close caused by a gesture or by a control.

References: lib/…/modal.ex• L47-55 lib/…/modal.ex• L75-80 lib/…/modal.ex• L132-137 lib/…/slide_over.ex• L107-113 lib/…/slide_over.ex• L158-195 lib/…/slide_over.ex• L250-318 assets/…/petal_components.js• L8044-8055

Change checklist

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

  • If a dialog id, trigger command, or close event changes, update the matching show and hide bindings and any LiveView event target.
  • If the trigger or panel structure changes, preserve the popover aria-controls relationship and the dropdown trigger marker used for placement.
  • If a focusable control becomes part of a tooltip, move that content to a popover or hover card and keep the control's own accessible name.
  • If an anchored panel gains a new placement, update both its placement contract and its viewport collision behavior.
  • If a bottom drawer changes its snap points or close path, keep its visual rest positions and the single close_slide_over event aligned.
  • If a bottom drawer uses background scaling, mark the intended page wrapper with data-pc-drawer-wrapper and check the behavior of fixed descendants.
  • If a context-menu item changes its enabled state or role, preserve the arrow-navigation set and the item's native activation behavior.
  • If a modal or slide-over footer changes its form boundary, preserve the link between the footer action and the form it submits.

Important failure modes

TriggerConsequenceGuard
A button or link is nested inside a popover or dropdown trigger slotThe browser gets invalid nested interactive controls and the toggle can failPut only trigger content in the slot and style the component-owned button
A popover panel id does not match its trigger relationshipThe trigger cannot open or describe the intended panelKeep the panel id, aria-controls, and top-layer popovertarget aligned
A modal or slide-over close event targets the wrong LiveViewThe surface hides but the owning state does not changeUse the close target for the LiveView or component that owns the surface
A long tooltip or hover card is used for an interactive task without a usable triggerPointer or touch users can miss the content and keyboard users may not reach itUse a popover or a focusable trigger with an independent path to the content
A dropdown is placed at the bottom of a short viewport without the bundled hookAutomatic placement cannot flip and the menu can extend off-screenRegister the bundled hooks or choose an explicit side
A hover card is placed near an edgeIts static placement can be clippedChoose a side with room or use a collision-aware popover
A title is omitted from a dialog that has no other accessible nameAssistive technology cannot identify the focused surface wellProvide a title or explicit accessible label and keep a usable close path
A bottom drawer changes snap points without matching its initial snapThe drawer can open at a rest position the user cannot reach againKeep initial_snap within the normalized snap-point set
Background scaling is enabled without the marked page wrapperThe drawer cannot apply the intended page effect or fixed page elements can behave unexpectedlyAdd data-pc-drawer-wrapper to the page wrapper and use scaling only for simple page layouts
A context-menu outside press is treated as a tap during a dragScrolling or pinch gestures close the menu unexpectedlyRequire an outside press and release with no movement or multi-touch

Key files

FileWhy to read it
lib/petal_components/modal.exDefines the general centered-dialog contract, close paths, accessible name, focus-on-open behavior, and LiveView close event.
lib/petal_components/alert_dialog.exDefines the explicit-choice dialog and its cancel-first focus and no-backdrop-dismissal contract.
lib/petal_components/slide_over.exDefines edge origins, descriptions, pinned footer behavior, bottom-drawer options, snap points, optional page scaling, and the close event.
lib/petal_components/popover.exDefines the interactive anchored panel, trigger relationship, placement choices, and default versus top-layer behavior.
lib/petal_components/hover_card.exDefines hover and focus preview behavior, delays, static placement, and the independent-trigger requirement.
lib/petal_components/tooltip.exDefines the non-interactive hover and focus label and the aria-describedby contract.
lib/petal_components/dropdown.exDefines the menu trigger, menu roles, side and alignment vocabulary, and legacy placement names.
lib/petal_components/context_menu.exDefines pointer, touch, and keyboard opening plus the menu item contract.
assets/…/petal_components.jsDefines anchored collision checks, visual-viewport tracking, alert-dialog state, dropdown measurement, context-menu navigation, and bottom-drawer gestures.
assets/default.cssShows the shared visual rules for centered dialogs, edge panels, anchored surfaces, tooltip visibility, and hover-card timing.