<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-tree-item/ in the magma monorepo (usage/*.md and the
  component's JSDoc). Regenerated on every stencil build (scripts/component-docs.ts).
-->

# mds-tree-item

> `@maggioli-design-system/magma` 2.15.0. Shadow DOM: style it only through the public CSS custom properties and the parts below.

The `<mds-tree-item>` web component is the node primitive of the Magma Design System tree view: it represents a single labeled, expandable/collapsible entry inside an [`<mds-tree>`](../mds-tree/AGENTS.md) and is also the recursive container for its own descendants. It replaces the role of an `<li>` within a nested disclosure list.

#### Semantic Behavior

- **Compound child only**: It must be a direct slot child of `<mds-tree>` (first-level nodes) or of another `<mds-tree-item>` (nested nodes). It is not used standalone, and the default slot is reserved for further `<mds-tree-item>` children - not arbitrary content.
- **Parent-driven defaults**: The parent `<mds-tree>` writes `toggle`, `togglePosition`, `appearance` and `truncate` onto its descendant items when it loads and onto the items that arrive later (overwriting their own values), and `expanded` when it is set; `actions` is inherited from the nearest ancestor that sets it. These props are therefore orchestrated by the tree rather than set per-item. First-level items receive `depth = 0`.
- **Self-managed expansion**: Clicking the toggle or label flips `expanded`, swaps the toggle icon, and animates the children container; on collapse it emits `mdsTreeItemCollapse`. Both buttons carry `aria-expanded` on an item with children (or `async`); a leaf has none. The item is a `listitem`, its children a nested `list`, and the children of a closed branch are out of the tab order.
- **Asynchronous expansion**: While `async` is set, an expand click does not open the node - it shows a spinner and emits `mdsTreeItemExpand` so the application can lazy-load children; the application then calls the public `expand()` method to finalize opening. Without `async`, opening emits no event.
- **Bubbled events**: `mdsTreeItemExpand` and `mdsTreeItemCollapse` both carry `{ element }` (the host item) so the tree or application can react to node lifecycle.
- **Leaf vs. branch**: It detects whether it contains nested items (again whenever they change, so a branch loaded on demand gets its toggle) and whether it has slotted actions, adjusting the connector/branch rendering and actions visibility accordingly.
- **Public method**: `expand()` opens the node and clears the awaiting state (used to resolve async loads).

#### Properties & Visual Configurations

- **`toggle`**: Picks the disclosure affordance - `chevron` (default arrow) or `folder` (closed/open folder icons that swap as the user opens and closes the node). Normally inherited from the parent tree for visual consistency.
- **`actions`**: Controls when slotted `action` controls appear - `auto` reveals them on hover, `visible` keeps them always shown. Place those controls in the named `action` slot.
- **`async`**: Enable when a node's children are loaded on demand; pair it with a listener on `mdsTreeItemExpand` and a call to `expand()` once data is ready.
- **`expanded`**: Mutable open/closed state; set it directly to pre-open a node, but expect the parent tree to override it when the whole tree expands or collapses.
- **`truncate`**: Governs label overflow (`word`, `all`, `none`); `all` enables multi-line clamping. Usually inherited from the tree.
- **`icon`**: Optional leading icon rendered inside the label button, independent of the toggle affordance.
- **`depth`**: Internal/parent-assigned; drives branch-line decoration and should not normally be set by consumers.

## Read next

- [`examples.md`](examples.md) - correct usage, from the most common case
- [`mistakes.md`](mistakes.md) - the wrong forms, each with the right one
- [Storybook](https://magma-design-system.github.io/magma/storybook/?path=/docs/ui-tree-tree-item--docs) - the live examples, in the published Storybook of `main`
- [`conventions.md`](../../../../agents/conventions.md), [`variants.md`](../../../../agents/variants.md), [`mistakes.md`](../../../../agents/mistakes.md) - the rules shared by every component

## Props

| Prop | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `actions` | `actions` | `"auto" \| "visible" \| undefined` | - | Show actions on the tree item on hover or by default. |
| `appearance` | `appearance` | `"depth" \| "none" \| undefined` | - | Reflects the parent tree appearance (set by mds-tree); drives the depth/none layout without :host-context |
| `async` | `async` | `boolean \| undefined` | - | Loads the children on demand: a click on the closed item shows a spinner and emits `mdsTreeItemExpand` instead of opening it; call `expand()` once the children are in place |
| `depth` | `depth` | `number \| undefined` | - | Specifies the tree branch depth. Internal: used by CSS to remove branch lines on first-level elements. |
| `expanded` | `expanded` | `boolean \| undefined` | - | Specifies if the tree is expanded. |
| `icon` | `icon` | `string \| undefined` | - | The icon displayed in the button |
| `label` | `label` | `string` | - | Specifies the label of the tree item |
| `toggle` | `toggle` | `"chevron" \| "folder" \| undefined` | - | Specifies the icon of the element |
| `togglePosition` | `toggle-position` | `"left" \| "right" \| undefined` | - | Reflects the parent tree toggle icon position (set by mds-tree); drives the toggle-icon layout without :host-context |
| `truncate` | `truncate` | `"all" \| "none" \| "word" \| undefined` | `'word'` | Truncate the text of the element on one single line. |

## Events

| Event | Detail | Description |
| --- | --- | --- |
| `mdsTreeItemCollapse` | `MdsTreeItemEventDetail` | Emits when the item closes, once its children have finished hiding |
| `mdsTreeItemExpand` | `MdsTreeItemEventDetail` | Emits when the user opens an `async` item, to load its children: opening any other item emits nothing |

## Methods

| Method | Signature | Description |
| --- | --- | --- |
| `expand` | `expand() => Promise<void>` | Expands the tree item, revealing its children. |
| `refreshActions` | `refreshActions() => Promise<void>` | `internal` Re-resolves the effective actions; called by mds-tree when its own actions changes so items that inherit it stay in sync. |

## Slots

| Slot | Description |
| --- | --- |
| (default) | Add `mds-tree-item` element/s. |
| `action` | Add `mds-button`, `mds-icon` or other types component and HTML element/s. |

## Parts

| Part | Description |
| --- | --- |
| `actions-container` | Selects the wrapper of the container of the actions. |
| `actions-list` | Selects the container of the actions. |

## CSS custom properties

None is public: its 30 custom properties are internal and may change in any release. Style it with its props, its parts and the semantic roles.

## Dependencies

Uses: `mds-button`.
