# Collapsible

## Usage

`Collapsible` folds one content region behind a trigger: the trigger stays visible and a click expands or collapses the content underneath. It belongs in settings panels, FAQ pages, and detail views — places where the page should stay easy to scan while still letting people drill into what they care about. Several collapsibles side by side form a plain accordion, each managing its own state.

The shortest form gives a label and the content:

```vue
<script setup lang="ts">
import { Collapsible } from '@astryx-vue/core/Collapsible'
</script>

<template>
  <Collapsible trigger="Shipping address">
    <p>Orders leave the warehouse within one business day, and the tracking number is emailed as soon as the parcel is scanned.</p>
  </Collapsible>
</template>
```

A collapsible starts expanded. Pass `:default-is-open="false"` when the content should stay hidden until someone asks for it. In that mode the component owns the state: clicking the trigger flips the content and reports the new state through the `openChange` event.

When something outside the component decides, lift the state with `v-model:is-open`. The component then renders exactly what the bound value says, and a click is only a request to change it:

```vue
<script setup lang="ts">
import { Collapsible } from '@astryx-vue/core/Collapsible'
import { ref } from 'vue'

const isOpen = ref(false)
</script>

<template>
  <Collapsible v-model:is-open="isOpen" trigger="Release notes">
    <p>Version 4.2 adds inline comments to shared reports.</p>
  </Collapsible>
</template>
```

The trigger is normally a plain text label passed to the `trigger` prop. When the label needs a heading, an icon, or extra controls, use the `trigger` slot instead; it replaces the prop completely. Expanding and collapsing play a real transition: the content region's height, padding, and opacity change together, and under `prefers-reduced-motion: reduce` the change lands immediately without the movement.

The trigger and the content are linked with `aria-expanded` and `aria-controls`, so a screen reader can move from the button straight to the region it controls. While collapsed the region stays the same element — `aria-controls` always points at it — but it is set to `display: none`, which keeps it out of the tab order and out of the accessibility tree.

`chevronPosition` decides which side of the label carries the disclosure chevron. The default `end` follows the label and points down while collapsed, up while expanded. Set it to `start` to lead the label with an arrow that points into the content while collapsed — mirrored in right-to-left layouts — and turns down when expanded, matching the rows of a file tree.

`isDisabled` dims the trigger, stops it responding to clicks, marks it `aria-disabled`, and takes it out of the tab order. It never collapses content that is already open; it only stops further changes.

### Anatomy

- **Container**: The root that contains one trigger and its controlled content and, in a divided group, paints the item divider.
- **Trigger**: The always-visible button that toggles the content. Shows a label and a chevron indicator.
- **Chevron**: Animated disclosure arrow. It follows the label by default; chevronPosition="start" moves it ahead of the label, points inward when collapsed (mirrored under RTL), and turns down when expanded.
- **Content**: The area that hides or reveals when the trigger is clicked.
- **Group container**: The CollapsibleGroup wrapper rendered when dividers are enabled. It contains the coordinated items and carries their density.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Name what the section holds. The label is the button's accessible name, so a vague "More" or "Details" leaves screen reader users guessing. |
| Do | Collapse content that is long or only occasionally needed. A single short paragraph is better shown directly than hidden behind a click. |
| Do | Lift the state with `v-model:isOpen` when something outside the component decides, so the parent stays the single source of truth. |
| Do | Use `chevronPosition="start"` for nested rows and lists, where the arrow leaning into the content matches the file-tree convention, and keep the default `end` for standalone sections. |
| Do | Keep the trigger to its label. Put actions inside the content region or on their own row so a click is never ambiguous. |
| Don't | Hide critical or required content behind a collapsible. Many people will never open it, and the information is lost. |
| Don't | Nest collapsibles more than two levels deep; the content becomes hard to find and navigate. |
| Don't | Put the outcome of an action or an error inside a collapsible. Show that kind of message directly, or use a banner. |

## Examples

### Controlled

Manage the open section from parent state, so something other than a click can move it: a URL parameter, a form jumping to the step that failed validation, or the Previous/Next controls shown here.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Card } from '@astryx-vue/core/Card'
import { Collapsible, CollapsibleGroup } from '@astryx-vue/core/Collapsible'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { computed, ref } from 'vue'

const steps = ['profile', 'security', 'billing'] as const
const open = ref<(typeof steps)[number]>('profile')
const index = computed(() => steps.indexOf(open.value))

function previous() {
  open.value = steps[Math.max(0, index.value - 1)]
}
function next() {
  open.value = steps[Math.min(steps.length - 1, index.value + 1)]
}
</script>

<template>
  <Stack :gap="3" :max-width="440">
    <Stack align="center" direction="horizontal" :gap="2">
      <Button label="Previous" variant="secondary" size="sm" :is-disabled="index <= 0" @click="previous" />
      <Button label="Next" variant="secondary" size="sm" :is-disabled="index >= steps.length - 1" @click="next" />
      <Text type="supporting">
        Step {{ index + 1 }} of {{ steps.length }}
      </Text>
    </Stack>
    <CollapsibleGroup v-model:value="open" type="single">
      <Stack :gap="2">
        <Card>
          <Collapsible trigger="Profile" value="profile">
            <Text type="body" color="secondary">
              Name, email, and photo. Changes save as you type; the email address needs confirmation from the new inbox before it takes effect.
            </Text>
          </Collapsible>
        </Card>
        <Card>
          <Collapsible trigger="Security" value="security">
            <Text type="body" color="secondary">
              Two-factor authentication, active sessions, and recent login history. Signing out of a session revokes its token immediately.
            </Text>
          </Collapsible>
        </Card>
        <Card>
          <Collapsible trigger="Billing" value="billing">
            <Text type="body" color="secondary">
              Invoices, payment method, and plan. Invoices stay downloadable for seven years, including after a downgrade.
            </Text>
          </Collapsible>
        </Card>
      </Stack>
    </CollapsibleGroup>
  </Stack>
</template>
```

### FAQ

FAQ built with hasDividers: row hairlines and density padding with no custom CSS. Questions use body semibold so a list of questions reads as rows rather than a stack of headings.

```vue
<script setup lang="ts">
import { Collapsible, CollapsibleGroup } from '@astryx-vue/core/Collapsible'
import { Link } from '@astryx-vue/core/Link'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Stack :gap="3" :max-width="560">
    <Text type="label">
      Billing and plans
    </Text>
    <CollapsibleGroup type="single" has-dividers default-value="proration">
      <Collapsible value="proration">
        <template #trigger>
          <Text type="body" weight="semibold">
            How is my bill prorated when I change plans mid-cycle?
          </Text>
        </template>
        <Stack :gap="2">
          <Text type="body" color="secondary">
            Upgrades take effect immediately and we charge the difference for the days remaining in the cycle. Downgrades take effect at the next renewal, so you keep the higher tier until the period you already paid for runs out.
          </Text>
          <Text type="body" color="secondary">
            Seat changes work the same way: added seats are prorated to the day, removed seats free up at renewal.
          </Text>
        </Stack>
      </Collapsible>
      <Collapsible value="username">
        <template #trigger>
          <Text type="body" weight="semibold">
            Can I change my username?
          </Text>
        </template>
        <Text type="body" color="secondary">
          Once every 30 days, from your profile settings. Old links keep working — we redirect them for a year.
        </Text>
      </Collapsible>
      <Collapsible value="cancel">
        <template #trigger>
          <Text type="body" weight="semibold">
            What happens to my data if I cancel?
          </Text>
        </template>
        <Stack :gap="2">
          <Text type="body" color="secondary">
            Your workspace goes read-only at the end of the billing period. Nothing is deleted for 30 days, so reactivating inside that window restores everything exactly as it was.
          </Text>
          <Text type="body" color="secondary">
            After 30 days the workspace and its backups are permanently removed. Export first if you want a copy — an export covers documents, comments, and version history.
          </Text>
          <Link href="#export" is-standalone has-underline>
            How to export your workspace
          </Link>
        </Stack>
      </Collapsible>
      <Collapsible value="discounts">
        <template #trigger>
          <Text type="body" weight="semibold">
            Do you offer discounts for non-profits or education?
          </Text>
        </template>
        <Text type="body" color="secondary">
          Yes — 50% off any paid plan for registered non-profits and accredited schools. Email your documentation and we usually apply it within two business days.
        </Text>
      </Collapsible>
    </CollapsibleGroup>
  </Stack>
</template>
```

### Multiple mode

Several sections open at once, for comparing across them. One Collapsible per Card, so each trigger keeps its own large type: it is the heading of its surface, not a row in a list.

```vue
<script setup lang="ts">
import { Card } from '@astryx-vue/core/Card'
import { Collapsible, CollapsibleGroup } from '@astryx-vue/core/Collapsible'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <CollapsibleGroup type="multiple" :default-value="['features', 'pricing']">
    <Stack :gap="2" :max-width="440">
      <Card>
        <Collapsible trigger="Features" value="features">
          <Text type="body" color="secondary">
            Real-time collaboration, full version history, and granular permissions. Every plan includes unlimited documents and unlimited guests — seats are counted for editors only.
          </Text>
        </Collapsible>
      </Card>
      <Card>
        <Collapsible trigger="Pricing" value="pricing">
          <Stack :gap="2">
            <Text type="body" color="secondary">
              Free for up to 5 editors. Pro is $12 per editor per month billed annually, or $15 month to month.
            </Text>
            <Text type="supporting">
              Non-profit and education pricing is 50% off any paid plan.
            </Text>
          </Stack>
        </Collapsible>
      </Card>
      <Card>
        <Collapsible trigger="Integrations" value="integrations">
          <Text type="body" color="secondary">
            Slack, GitHub, Jira, Figma, and 40 more through pre-built connectors, plus a REST API and outbound webhooks for anything not on the list.
          </Text>
        </Collapsible>
      </Card>
    </Stack>
  </CollapsibleGroup>
</template>
```

### Single mode

Only one section open at a time, so a single body of content competes for attention. Use defaultValue to pre-expand whichever section a first-time reader needs.

```vue
<script setup lang="ts">
import { Collapsible, CollapsibleGroup } from '@astryx-vue/core/Collapsible'
import { Section } from '@astryx-vue/core/Section'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <CollapsibleGroup type="single" default-value="general">
    <Stack :gap="2" :max-width="440">
      <Section>
        <Collapsible trigger="General" value="general">
          <Text type="body" color="secondary">
            Language, time zone, and date format. Time zone drives every timestamp in the product, including exports and scheduled reports.
          </Text>
        </Collapsible>
      </Section>
      <Section>
        <Collapsible trigger="Privacy" value="privacy">
          <Text type="body" color="secondary">
            Control who can see your profile and activity. Turning off discovery also removes you from @-mention autocomplete for people outside your workspace.
          </Text>
        </Collapsible>
      </Section>
      <Section>
        <Collapsible trigger="Notifications" value="notifications">
          <Text type="body" color="secondary">
            Choose what you are notified about and how it reaches you. Mentions always come through; everything else can be batched into a daily digest.
          </Text>
        </Collapsible>
      </Section>
    </Stack>
  </CollapsibleGroup>
</template>
```

### With dividers

A flat list on the page background with hand-placed Dividers, for detail panels and sidebars where cards would add too much weight.

```vue
<script setup lang="ts">
import { Collapsible } from '@astryx-vue/core/Collapsible'
import { Divider } from '@astryx-vue/core/Divider'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Stack :gap="3" :max-width="480">
    <Collapsible value="deployment">
      <template #trigger>
        <Text type="body" weight="semibold">
          Deployment details
        </Text>
      </template>
      <Stack :gap="1">
        <Text type="body" color="secondary">
          Build 4,182 shipped to production from <code>main</code>. 847 modules compiled with no warnings, and the bundle came in at 142 KB gzipped — 3 KB under the budget.
        </Text>
        <Text type="supporting">
          Deployed by Sarah Chen · April 18, 3:42 PM · 2m 14s
        </Text>
      </Stack>
    </Collapsible>
    <Divider />
    <Collapsible value="environment">
      <template #trigger>
        <Text type="body" weight="semibold">
          Environment variables
        </Text>
      </template>
      <Stack :gap="1">
        <Text type="body" color="secondary">
          12 variables configured across production and preview. Secrets are encrypted at rest with AES-256 and are never printed in build logs.
        </Text>
        <Text type="supporting">
          Last updated March 30 by the deploy bot
        </Text>
      </Stack>
    </Collapsible>
    <Divider />
    <Collapsible value="rollback">
      <template #trigger>
        <Text type="body" weight="semibold">
          Rollback
        </Text>
      </template>
      <Stack :gap="1">
        <Text type="body" color="secondary">
          The previous build stays warm for 24 hours, so a rollback swaps traffic back in a few seconds without a rebuild. After that window a rollback redeploys from source and takes about two minutes.
        </Text>
        <Text type="supporting">
          Previous build 4,181 · expires in 19h
        </Text>
      </Stack>
    </Collapsible>
  </Stack>
</template>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `trigger` | `string` | - | Plain-text label for the trigger. Use the `trigger` slot instead when the label needs a heading, an icon, or extra controls; the slot replaces this prop. |
| `v-model:isOpen` | `boolean` | - | Controlled open state. While bound, the component renders exactly this value and a click only requests an update; leave it unbound to let the component manage its own state. |
| `defaultIsOpen` | `boolean` | `true` | Whether the content starts expanded while `isOpen` is unbound. |
| `isDisabled` | `boolean` | `false` | Dims the trigger, marks it `aria-disabled`, and takes it out of the tab order. Clicks no longer toggle the content, and content that is already open stays open. |
| `chevronPosition` | `CollapsibleChevronPosition` | `'end'` | Logical position of the disclosure chevron. `end` (default) follows the label, pointing down while collapsed and up while expanded; `start` leads the label, pointing into the content while collapsed (mirrored under RTL) and down while expanded. |

## Slots

| Name | Description |
| --- | --- |
| `default` | The content hidden while collapsed and revealed while expanded. |
| `trigger` | The trigger label. Accepts headings, icons, and other markup, and replaces the `trigger` prop. |

## Events and template ref

| Name | Signature | Description |
| --- | --- | --- |
| `openChange` | `(isOpen: boolean) => void` | Emitted when the trigger asks for a new open state. In uncontrolled usage it reports the change; in controlled usage it is the request to update the bound value. |
| `update:isOpen` | `(isOpen: boolean) => void` | Listened to automatically by `v-model:isOpen`; you rarely handle it yourself. |
| `element` | `HTMLDivElement \| null` | The root element, exposed through a template ref. |

## Theming

```css
.my-collapsible {
  --astryx-collapsible-trigger-foreground: #171717;
  --astryx-collapsible-content-foreground: #525252;
}
```

- `--astryx-collapsible-trigger-foreground`: trigger label color.
- `--astryx-collapsible-content-foreground`: content text color.
- `--astryx-collapsible-pressed-overlay`: overlay painted on the trigger while it is held down, and only then.
- `--astryx-collapsible-focus`: keyboard focus outline color.
- `--astryx-spacing-1`, `--astryx-text-large-size`, `--astryx-text-body-size`: the padding and both type sizes reuse the global tokens.

The neutral theme adapts to light and dark mode on its own.

## Native attributes

The root element is a `<div>`.

The following go to the root element:

- HTML attributes.
- `aria-*` attributes.
- `data-*` attributes.
- `class`.
- `style`.

The trigger is a `<button type="button">` inside the root element; its `aria-expanded`, `aria-controls`, `aria-disabled`, and `tabindex` are maintained by the component. Disabling never sets the native `disabled` attribute, so assistive technology keeps perceiving the trigger.

## Accessibility

### Trigger label

- Criterion: 1.4.3 Contrast (Minimum)
- Check: 4.5:1
- States: Rest, Pointer down

The trigger text must have at least 4.5:1 contrast with the surface behind it. For Pointer down, measure against the pressed overlay the trigger row paints while it is pressed.
