PetalComponents.Collapsible (petal_components v4.15.3)

Copy Markdown View Source

One disclosure region: a trigger row that shows and hides the content under it.

<.collapsible>
  <:trigger>Advanced options</:trigger>
  <p>Timeouts, retries and the webhook signing secret.</p>
</.collapsible>

<.collapsible open={@show_details} id="api-keys">
  <:trigger>API keys (3)</:trigger>
  <ul>...</ul>
</.collapsible>

Collapsible or accordion?

This is one region with no neighbours: nothing closes when it opens, and there is no list of entries. When you have a set of sections where opening one should close the others, that is PetalComponents.Accordion.accordion/1.

State

Toggling is client-side Phoenix.LiveView.JS, so an open/close costs no round trip. The open attr sets the state the server renders, which means LiveView can drive it too: re-render with a changed assign and the region follows. Pass extra commands through on_toggle (analytics, a phx-click to persist the state) and they run before the component's own.

Animation

The height animation is the grid-template-rows: 0fr -> 1fr trick, so content of any height animates without measuring anything in JavaScript. Under prefers-reduced-motion: reduce the transition is dropped and the region snaps open, fully legible either way.

Accessibility

The trigger is a real <button type="button">, so Enter and Space work for free and focus stays put across toggles. It carries aria-expanded and aria-controls pointing at the content region, which is role="region" labelled by the trigger. While collapsed the content is inert, so it is out of the tab order and out of the accessibility tree instead of being an invisible trap. disabled uses the button's native disabled attribute.

Summary

Functions

A single disclosure region.

The JS command that toggles a collapsible by id.

Functions

collapsible(assigns)

A single disclosure region.

<.collapsible open>
  <:trigger>What is included</:trigger>
  Everything in Pro, plus SSO.
</.collapsible>

Attributes

  • id (:string) - container id; autogenerated when omitted. Defaults to nil.
  • class (:any) - CSS class. Defaults to nil.
  • open (:boolean) - render expanded; server-controlled open state. Defaults to false.
  • disabled (:boolean) - disables the trigger button. Defaults to false.
  • on_toggle (Phoenix.LiveView.JS) - additional JS commands composed onto the toggle, run before the component's own. Defaults to %Phoenix.LiveView.JS{ops: []}.
  • Global attributes are accepted. any extra HTML attributes for the root.

Slots

  • trigger (required) - the always-visible row; rendered inside a button. Accepts attributes:
    • class (:any)
  • inner_block (required) - the collapsible content.

toggle_collapsible(id, on_toggle \\ %JS{})

The JS command that toggles a collapsible by id.

Exposed so a button elsewhere on the page can drive the same region:

<.button phx-click={PetalComponents.Collapsible.toggle_collapsible("api-keys")}>
  Toggle
</.button>