Examples

This is the accordion content that can be expanded or collapsed.

It supports any content inside including other components.

Built-in trigger

With a trigger snippet the component renders its own header. The trigger carries aria-expanded and aria-controls pointing at the panel it opens, so assistive technology can move straight to the region the trigger governs.

Delivered in 3–5 business days.

Explicit panelId

Pass panelId when something outside the component needs to reference the panel by a known id; the trigger's aria-controls follows it.

Free returns within 30 days.

Disabled trigger

disabled locks the built-in trigger: clicks and Enter/Space no longer toggle, it leaves the tab order, and aria-disabled="true" is set.

This content stays collapsed — the trigger above is disabled.

Stable trigger id

The trigger's id is generated per instance and never changes, even when panelId is assigned later, so the panel's aria-labelledby and anything else holding a reference to the trigger stay valid.

Two-year manufacturer warranty.

Inside a grid container

A display: grid parent stretches its items into an auto-sized row by default. The panel needs overflow: hidden to animate grid-template-rows, but overflow other than visible also drops an element's automatic minimum size to zero — so as a grid item the panel is squeezed down to whatever height the grid happens to hand it, disconnected from what its own content actually needs, rather than growing to fit. Depending on the surrounding layout that can mean a sliver, or nothing at all. The children are still in the DOM and correctly rendered; only the box around them is wrong. align-self: start on the panel opts it out of that stretch so it always sizes from its own expanded content instead.

This paragraph must stay visible once the accordion above is expanded, even though its direct parent is a grid container rather than a block one.

A second paragraph of the same content keeps the panel's expanded height comfortably taller than the 300px grid parent, proving the fix reads real content height rather than coincidentally fitting inside a squeezed row.

A third paragraph pads the expanded height further still, so a regression that shrinks the row back down is unmistakably a missing few hundred pixels, not a rounding difference.

A fourth paragraph, for the same reason: the more of this content a broken build hides, the less plausible it is that a future change could pass this check by accident.

A fifth and final paragraph brings the panel's true content height well past the grid parent's own 300px, so the parent's overflow-y: auto is the thing that scrolls once the fix is in place — not the panel silently discarding the rest.


Documentation

An expandable/collapsible container that uses CSS grid row animation for smooth open/close transitions. When expand is true, the content is visible (grid-template-rows: 1fr); when false, it collapses to 0fr. Transition duration/easing is controlled via --accordion-transition. Render children content using the default children snippet. Pass an optional trigger snippet to render a built-in clickable/keyboard-accessible toggle header.

Usage

<script>
  import { Accordion } from '@juspay/svelte-ui-components';
  let open = $state(false);
</script>

<Accordion bind:expand={open}>
  {#snippet trigger({ expanded })}
    <span>{expanded ? 'Collapse' : 'Expand'}</span>
  {/snippet}
  <p>Expandable content here</p>
</Accordion>

Props

Prop Type Required Default Description
expand boolean No false Controls whether the accordion content is expanded (visible) or collapsed (hidden). Supports two-way binding (bind:expand).
classes string No - CSS class string applied to the accordion content wrapper element. Useful for theming — define classes with CSS variable overrides and pass them to create variant styles.
triggerClasses string No - CSS class string applied to the accordion-trigger wrapper div. Use to style the trigger area independently of the content panel.
testId string No - Value written to data-pw on the accordion content wrapper. Enables Playwright locators (page.getByTestId(testId)).
disabled boolean No false Disables the built-in trigger: clicks and Enter/Space no longer toggle, the trigger is removed from the tab order (tabindex="-1"), and aria-disabled="true" is emitted. The trigger also gets a disabled class for styling. expand is still honoured, so a disabled accordion can be shown open or closed under external (controlled) state.
panelId string No generated id for the collapsible panel, which the built-in trigger references via aria-controls. Defaults to a generated per-instance id, so the trigger and panel are linked without any caller involvement. Supply one only when something else needs to reference the panel by a known id.
lazy boolean No false Mounts children only when the panel is first expanded, then keeps them mounted (so closing still animates real content and keeps its state). For long lists of mostly-closed panels, where rendering every closed body up front is the cost.

Events

Event Payload Description
ontoggle (expanded: boolean) => void Fired after the trigger toggles expand. Receives the new expanded state.

Snippets

Svelte 5 Snippet props — pass content blocks to the component.

Snippet Parameters Description
children (none) Content rendered inside the accordion panel (the collapsible region).
trigger { expanded: boolean } When provided, renders a keyboard-accessible toggle header (role="button", tabindex="0", aria-expanded, and aria-controls pointing at the panel it opens). When disabled is set, the trigger keeps role="button" but drops to tabindex="-1" and stops toggling — see the disabled prop. The expanded parameter reflects the current open/closed state so you can render different UI for each state.

Accessibility

  • The built-in trigger is role="button" with tabindex="0" and toggles on Enter or Space.
  • aria-expanded reflects the panel's real open state, and aria-controls references the panel's id — the two elements are siblings rather than nested, so without that reference assistive technology has no way to reach the region the trigger governs. The id is generated per instance, so two accordions on one page never collide.
  • The panel is role="region" with aria-labelledby pointing at the trigger, so screen readers announce the expanded content as a landmark named by its header. The trigger's id is generated per instance and never changes, even if panelId is assigned later.
  • With lazy, a collapsed panel is also inert: it is clipped to zero height, so its links, buttons and fields leave the tab order and the accessibility tree until it opens. Without lazy (the default), the panel is only clipped, matching the pre-lazy behaviour.
  • The trigger's accessible name is computed from its content. An icon-only trigger snippet therefore has no name: include text (visually hidden is fine) or put an aria-label on the icon element, which name-from-content picks up.

CSS Variables

Variable Default Description
--accordion-trigger-cursor pointer Cursor style for the trigger element.
--accordion-trigger-disabled-cursor not-allowed Cursor style for the trigger element when disabled is true.
--accordion-transition 0.2s ease-out Transition value for the grid-template-rows animation.
--accordion-align-self start align-self for the panel when it is a grid/flex item of a consumer's own container (see #550). Override to stretch if you want the panel stretched to the container's row/column instead.

Web Component

Tag: <sui-accordion>

<sui-accordion expand trigger-classes="my-trigger" test-id="my-accordion">
  <p>Expandable content here</p>
</sui-accordion>

ontoggle is a JS-property callback only (accordion.ontoggle = (expanded) => ...) and does not dispatch a DOM event: toggle is already HTMLElement's own native event name, and adding a second, synthetic one under it would double-deliver to any accordion.addEventListener('toggle', ...) listener.

Props (HTML Attributes)

Attribute Maps to Prop Type Description
expand expand Boolean Reflects the expanded state.
classes classes String CSS classes for the content wrapper.
trigger-classes triggerClasses String CSS classes for the trigger wrapper.
test-id testId String Sets data-pw on the content wrapper.
panel-id panelId String Sets the panel's id (referenced by the trigger's aria-controls).
lazy lazy Boolean Renders the default slot only once the panel first opens.

Slots

Slot Name Maps to Snippet Description
(default) children Content rendered inside the accordion body.

Svelte-only: trigger (receives { expanded: boolean }) takes argument, so it cannot be expressed as a named slot: a Web Component <slot> projects markup, it does not forward Svelte snippet parameters, so the argument above would be silently dropped. Use the Svelte component directly when you need this.

Native rendering

native uses real details/summary markup, including prehydration interaction. nativeAttrs and summaryAttrs forward native metadata; element is a bindable details reference. classes and triggerClasses apply to details and summary respectively.