# Collapsible

A single section that opens and closes, built on native details and summary without JavaScript. Good for show more content.

- Category: Navigation
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/collapsible.json
- CLI: `npx markasui@latest add collapsible`

## When to use

Hide secondary content behind one toggle, such as show more details, advanced settings, or a long description.

- Use one Collapsible per independent section; use Accordion for a list of related sections
- Write the title as a clear label of the hidden content, not just "More"
- Do not hide required form fields or critical warnings inside a closed Collapsible

## Code

### Blade

Save at `resources/views/components/markas/collapsible.blade.php`. No extra dependencies.

```blade
@props([
    'title' => '',
    'open' => false,
])

@php
    $root = 'group w-full';
    $summary = 'flex cursor-pointer list-none items-center justify-between gap-4 rounded-md py-2 text-left text-sm font-medium text-ink transition-colors duration-150 ease-out hover:text-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none [&::-webkit-details-marker]:hidden';
    $icon = 'size-4 shrink-0 text-muted transition-transform duration-200 ease-out group-open:rotate-180 motion-reduce:transition-none';
    $content = 'pt-2 pb-2 text-sm text-muted';
@endphp

<details wire:ignore.self {{ $attributes->class([$root]) }} @if ($open) open @endif>
    <summary class="{{ $summary }}">
        <span>{{ $title }}</span>
        <svg class="{{ $icon }}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6" /></svg>
    </summary>
    <div class="{{ $content }}">{{ $slot }}</div>
</details>
```

```blade
<x-markas.collapsible title="Show order details">
    Shipped from Jakarta on 4 November. Tracking number JKT-2210-884.
</x-markas.collapsible>
```

No Alpine.js needed. Use the `title` slot for rich content: `<x-slot:title>Show <strong>3</strong> more</x-slot:title>`.

### React

Save at `src/components/markas/Collapsible.tsx`. No extra dependencies.

```tsx
import type { ComponentProps, ReactNode } from "react";

const root = "group w-full";
const summary =
  "flex cursor-pointer list-none items-center justify-between gap-4 rounded-md py-2 text-left text-sm font-medium text-ink transition-colors duration-150 ease-out hover:text-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none [&::-webkit-details-marker]:hidden";
const icon =
  "size-4 shrink-0 text-muted transition-transform duration-200 ease-out group-open:rotate-180 motion-reduce:transition-none";
const content = "pt-2 pb-2 text-sm text-muted";
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

export type CollapsibleProps = Omit<ComponentProps<"details">, "title"> & {
  title: ReactNode;
  open?: boolean;
};

export function Collapsible({
  title,
  open = false,
  className,
  children,
  ...props
}: CollapsibleProps) {
  return (
    <details className={cx(root, className)} open={open} {...props}>
      <summary className={summary}>
        <span>{title}</span>
        <svg
          className={icon}
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          strokeWidth="1.5"
          strokeLinecap="round"
          strokeLinejoin="round"
          aria-hidden="true"
        >
          <path d="m6 9 6 6 6-6" />
        </svg>
      </summary>
      <div className={content}>{children}</div>
    </details>
  );
}
```

```tsx
import { Collapsible } from "@/components/markas/Collapsible";

<Collapsible title="Show order details" open>
  Shipped from Jakarta on 4 November. Tracking number JKT-2210-884.
</Collapsible>;
```

### Vue

Save at `src/components/markas/Collapsible.vue`. No extra dependencies.

```vue
<script setup lang="ts">
const props = withDefaults(
  defineProps<{
    title?: string;
    open?: boolean;
  }>(),
  { title: "", open: false },
);

const root = "group w-full";
const summary =
  "flex cursor-pointer list-none items-center justify-between gap-4 rounded-md py-2 text-left text-sm font-medium text-ink transition-colors duration-150 ease-out hover:text-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none [&::-webkit-details-marker]:hidden";
const icon =
  "size-4 shrink-0 text-muted transition-transform duration-200 ease-out group-open:rotate-180 motion-reduce:transition-none";
const content = "pt-2 pb-2 text-sm text-muted";
</script>

<template>
  <details :class="root" :open="props.open">
    <summary :class="summary">
      <span
        ><slot name="title">{{ props.title }}</slot></span
      >
      <svg
        :class="icon"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="1.5"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
      >
        <path d="m6 9 6 6 6-6" />
      </svg>
    </summary>
    <div :class="content"><slot /></div>
  </details>
</template>
```

```vue
<template>
  <Collapsible title="Show order details">
    Shipped from Jakarta on 4 November. Tracking number JKT-2210-884.
  </Collapsible>
</template>
```

### Svelte

Save at `src/lib/components/markas/Collapsible.svelte`. No extra dependencies.

```svelte
<script lang="ts">
  import type { Snippet } from "svelte";
  import type { HTMLDetailsAttributes } from "svelte/elements";

  const root = "group w-full";
  const summary =
    "flex cursor-pointer list-none items-center justify-between gap-4 rounded-md py-2 text-left text-sm font-medium text-ink transition-colors duration-150 ease-out hover:text-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none [&::-webkit-details-marker]:hidden";
  const icon =
    "size-4 shrink-0 text-muted transition-transform duration-200 ease-out group-open:rotate-180 motion-reduce:transition-none";
  const content = "pt-2 pb-2 text-sm text-muted";

  type Props = Omit<HTMLDetailsAttributes, "title" | "open"> & {
    /** Summary text, or a snippet for rich content. */
    title?: string | Snippet;
    open?: boolean;
    children?: Snippet;
  };

  let {
    title = "",
    open = $bindable(false),
    class: className,
    children,
    ...rest
  }: Props = $props();
</script>

<details class={[root, className]} bind:open {...rest}>
  <summary class={summary}>
    <span
      >{#if typeof title === "function"}{@render title()}{:else}{title}{/if}</span
    >
    <svg
      class={icon}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="1.5"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
    >
      <path d="m6 9 6 6 6-6" />
    </svg>
  </summary>
  <div class={content}>{@render children?.()}</div>
</details>
```

```svelte
<script lang="ts">
  import Collapsible from "$lib/components/markas/Collapsible.svelte";

  let open = $state(false);
</script>

<Collapsible title="Show order details" bind:open>
  Shipped from Jakarta on 4 November. Tracking number JKT-2210-884.
</Collapsible>
```

For rich content pass a snippet instead of a string: `{#snippet title()}Show <strong>3</strong> more{/snippet}`.

### Astro

Save at `src/components/markas/Collapsible.astro`. No extra dependencies.

```astro
---
import type { HTMLAttributes } from "astro/types";

type Props = Omit<HTMLAttributes<"details">, "title"> & {
  /** Summary text. A title slot overrides it for rich content. */
  title?: string;
  open?: boolean;
};

const root = "group w-full";
const summary =
  "flex cursor-pointer list-none items-center justify-between gap-4 rounded-md py-2 text-left text-sm font-medium text-ink transition-colors duration-150 ease-out hover:text-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none [&::-webkit-details-marker]:hidden";
const icon =
  "size-4 shrink-0 text-muted transition-transform duration-200 ease-out group-open:rotate-180 motion-reduce:transition-none";
const content = "pt-2 pb-2 text-sm text-muted";

const { title = "", open = false, class: className, ...rest } = Astro.props;
---

<details class:list={[root, className]} open={open} {...rest}>
  <summary class={summary}>
    <span><slot name="title">{title}</slot></span>
    <svg
      class={icon}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="1.5"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
    >
      <path d="m6 9 6 6 6-6"></path>
    </svg>
  </summary>
  <div class={content}><slot /></div>
</details>
```

```astro
---
import Collapsible from "@/components/markas/Collapsible.astro";
---

<Collapsible title="Show order details">
  Shipped from Jakarta on 4 November. Tracking number JKT-2210-884.
</Collapsible>
```

No JavaScript is shipped. Use the `title` slot for rich content: `<Fragment slot="title">Show <strong>3</strong> more</Fragment>`.

### Angular

Save at `src/app/markas/collapsible.component.ts`. No extra dependencies.

```ts
import {
  booleanAttribute,
  ChangeDetectionStrategy,
  Component,
  input,
} from "@angular/core";

const root = "group w-full";
const summary =
  "flex cursor-pointer list-none items-center justify-between gap-4 rounded-md py-2 text-left text-sm font-medium text-ink transition-colors duration-150 ease-out hover:text-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary motion-reduce:transition-none [&::-webkit-details-marker]:hidden";
const icon =
  "size-4 shrink-0 text-muted transition-transform duration-200 ease-out group-open:rotate-180 motion-reduce:transition-none";
const content = "pt-2 pb-2 text-sm text-muted";

/**
 * Attribute selector on the native details: id, class, and the toggle event stay on the
 * element itself. The title input is cleared from the host so it does not become a tooltip.
 * An element with the `title` attribute (ng-container works) replaces the title text.
 */
@Component({
  selector: "details[markas-collapsible]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { "[class]": "root", "[attr.title]": "null", "[open]": "open()" },
  template: `
    <summary [class]="summary">
      <span
        ><ng-content select="[title]">{{ title() }}</ng-content></span
      >
      <svg
        [class]="icon"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="1.5"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
      >
        <path d="m6 9 6 6 6-6" />
      </svg>
    </summary>
    <div [class]="content"><ng-content /></div>
  `,
})
export class Collapsible {
  readonly title = input("");
  /** Starting state. Listen to the native (toggle) event for changes. */
  readonly open = input(false, { transform: booleanAttribute });

  protected readonly root = root;
  protected readonly summary = summary;
  protected readonly icon = icon;
  protected readonly content = content;
}
```

```ts
import { Component } from "@angular/core";
import { Collapsible } from "./markas/collapsible.component";

@Component({
  selector: "app-order",
  imports: [Collapsible],
  template: `
    <details markas-collapsible title="Show order details">
      Shipped from Jakarta on 4 November. Tracking number JKT-2210-884.
    </details>
  `,
})
export class Order {}
```

The component sits on the native `details`, so `open`, `class`, and `(toggle)` work directly. For rich content add `<ng-container title>Show <strong>3</strong> more</ng-container>`.

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `title` | `string / slot / ReactNode / snippet` | - | Text of the summary row. Blade, Vue, and Astro also accept a title slot, Svelte a title snippet, and Angular an element with the title attribute (an ng-container works) for rich content. |
| `open` | `boolean` | false | Starts the section open. Svelte supports bind:open. |
| `children / slot` | `ReactNode / slot` | - | Content shown when the section is open. |

## Accessibility

- Uses native details and summary, so the summary is a focusable disclosure button announced as expanded or collapsed.
- Enter and Space toggle the section; no JavaScript is needed.
- The chevron icon is decorative and hidden from assistive technology.

## Notes

- Built on `details` and `summary`, so it works without JavaScript and the browser handles keyboard and screen reader state.
- `open` only sets the starting state. Listen to the native `toggle` event to react to changes.
- For several related sections where only one should be open, use Accordion instead.
