# Accordion

> A Layout widget for organizing content into collapsible sections

Accordion widgets are [Layout Fields](https://golemui.com/dx/widgets-reference/layout-fields/overview.md) that allow users to toggle the visibility of content sections. They are ideal for grouping related fields and saving vertical space in complex forms.


```ts title="accordion.ts"

export default [
  gui.layouts.accordion([
    {
      label: 'Personal Information',
      uid: 'personal',
      children: [
        gui.inputs.textInput('name', {
          label: 'Full Name',
        }),
      ],
    },
    {
      label: 'Account Settings',
      uid: 'account',
      children: [
        gui.inputs.textInput('email', {
          label: 'Email Address',
        }),
      ],
    },
  ], {
    defaultOpen: {
      personal: true,
    },
    uid: 'accordion_basic',
  }),
];
```

_Live demo on the docs page. Form definition: [accordion.ts](https://golemui.com/template/widgets-dx/accordion/accordion.ts)._

## Props

Use these `props` to customize your Accordion widget.

| prop          | type    | description                                                                                                                                                  |
| ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `sections`    | array   | **Required**. Array of section definitions: `{ label: string, uid: string }`. Each `uid` must match the `uid` of one of the accordion's children.            |
| `defaultOpen` | object  | Map of section `uid` to boolean controlling which sections start expanded (e.g., `{ "section_1": true }`). Defaults to all collapsed                         |
| `renderMode`  | string  | `'all'` keeps every section mounted in the DOM and toggles visibility; `'activeOnly'` mounts a section's children only while it is open. Defaults to `'all'` |
| `singleOpen`  | boolean | If `true`, only one section can be open at a time — opening a section closes the others. Defaults to `false`                                                 |

### Single Open

Use the `singleOpen` property to ensure that opening one section automatically closes others.


```ts title="accordion.ts"

export default [
  gui.layouts.accordion([
    {
      label: 'First Section',
      uid: 'sec1',
      children: [
        gui.inputs.textInput('f1', {
          label: 'Field 1',
        }),
      ],
    },
    {
      label: 'Second Section',
      uid: 'sec2',
      children: [
        gui.inputs.textInput('f2', {
          label: 'Field 2',
        }),
      ],
    },
  ], {
    singleOpen: true,
    uid: 'accordion_single',
  }),
];
```

_Live demo on the docs page. Form definition: [accordion-single.ts](https://golemui.com/template/widgets-dx/accordion/accordion-single.ts)._

### Default Open

Use `defaultOpen` to pre-expand sections on initial render. The keys are section `uid`s; sections whose key is `true` start open.


```ts title="accordion.ts"

export default [
  gui.layouts.accordion([
    {
      label: 'Personal Information',
      uid: 'section1',
      children: [
        gui.layouts.grid([
          gui.inputs.textInput('shoppingCart.firstName'),
        ]),
      ],
    },
    {
      label: 'Shipping Address',
      uid: 'section2',
      children: [
        gui.layouts.grid([
          gui.inputs.textInput('shoppingCart.shippingAddress'),
        ]),
      ],
    },
    {
      label: 'Billing Address',
      uid: 'section3',
      children: [
        gui.layouts.grid([
          gui.inputs.textInput('shoppingCart.billingAddress'),
        ]),
      ],
    },
  ], {
    defaultOpen: {
      section1: true,
      section2: true,
    },
    uid: 'tab5',
  }),
];
```

_Live demo on the docs page. Form definition: [accordion-default-open.ts](https://golemui.com/template/widgets-dx/accordion/accordion-default-open.ts)._

### Render Mode

Use `renderMode` to control when section children are mounted. The default `'all'` keeps every section in the DOM and hides closed ones, which preserves child state and form values when collapsing. Use `'activeOnly'` to mount a section's children only while it is open — useful when sections contain expensive widgets, side effects on mount, or media you don't want loaded up front. Inspect the rendered DOM as you toggle sections to see the difference.


```ts title="accordion.ts"

export default [
  gui.layouts.accordion([
    {
      label: 'Personal Information',
      uid: 'section1',
      children: [
        gui.layouts.grid([
          gui.inputs.textInput('shoppingCart.firstName'),
        ]),
      ],
    },
    {
      label: 'Shipping Address',
      uid: 'section2',
      children: [
        gui.layouts.grid([
          gui.inputs.textInput('shoppingCart.shippingAddress'),
        ]),
      ],
    },
    {
      label: 'Billing Address',
      uid: 'section3',
      children: [
        gui.layouts.grid([
          gui.inputs.textInput('shoppingCart.billingAddress'),
        ]),
      ],
    },
  ], {
    renderMode: 'activeOnly',
    defaultOpen: {
      section1: true,
    },
    uid: 'accordion_render_mode',
  }),
];
```

_Live demo on the docs page. Form definition: [accordion-render-mode.ts](https://golemui.com/template/widgets-dx/accordion/accordion-render-mode.ts)._

## Styling

Accordions can be styled as explained in the [Styling Guide](https://golemui.com/dx/styling/overview.md).

### CSS Variables

Following you will find a list with the CSS Variables used to style the accordion.

| CSS Variable           | Description                               |
| ---------------------- | ----------------------------------------- |
| `--gui-bg-default`     | Background color for sections and buttons |
| `--gui-border-default` | Border color for sections and buttons     |
| `--gui-text-default`   | Text color for labels                     |
| `--gui-space-3`        | Padding inside sections and buttons       |

### Anatomy

This is the anatomy of the Accordion Widget in case you want to use your CSS styles.


```html title="accordion-anatomy.html"
<gui-accordion-layout class="gui-accordion gui-field">
  <gui-accordion id="accordion_uid" multiple="">
    <gui-accordion-item open="">
      <details open="">
        <summary id="accordion_button_accordion_uid_personal">Personal Information</summary>
        <section
          class="gui-widget"
          role="region"
          id="accordion_section_accordion_uid_personal"
          aria-labelledby="accordion_button_accordion_uid_personal"
        >
          <!-- Child widget: Full Name -->
        </section>
      </details>
    </gui-accordion-item>
    <gui-accordion-item>
      <details>
        <summary id="accordion_button_accordion_uid_account">Account Settings</summary>
        <section
          class="gui-widget"
          role="region"
          id="accordion_section_accordion_uid_account"
          aria-labelledby="accordion_button_accordion_uid_account"
        >
          <!-- Child widget: Email Address -->
        </section>
      </details>
    </gui-accordion-item>
  </gui-accordion>
</gui-accordion-layout>
```
