<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-filter-item/usage/ in the magma monorepo.
-->

# mds-filter-item - mistakes

> The API of this component: [`AGENTS.md`](AGENTS.md).

Common incorrect uses of `<mds-filter-item>`. Each entry pairs the wrong form with the right one and a one-line reason. System-wide rules (boolean-as-string, shadow piercing, Tailwind color utilities, raw native event listening) live in [`agents/mistakes.md`](../../../../agents/mistakes.md) - they apply here too but are not repeated.

#### Do Not Use `<mds-filter-item>` Outside `<mds-filter>`

`<mds-filter-item>` is a compound child component. Without its parent it has no selection management, no auto-scroll, and no `mdsFilterChange` aggregation. Always wrap it in `<mds-filter>`.

```html
<!-- INCORRECT -->
<div class="my-filters">
  <mds-filter-item label="Attivo" value="active"></mds-filter-item>
  <mds-filter-item label="Archiviato" value="archived"></mds-filter-item>
</div>

<!-- CORRECT -->
<mds-filter label="Stato">
  <mds-filter-item label="Attivo" value="active"></mds-filter-item>
  <mds-filter-item label="Archiviato" value="archived"></mds-filter-item>
</mds-filter>
```

#### Do Not Set `selected="false"` to Deselect

`selected` is a boolean prop: remove the attribute entirely to deselect, as for every boolean. The runtime happens to read the string `"false"` as `false`, but the attribute stays in the markup until the component renders, and HTML, `[selected]` selectors and `hasAttribute()` read a present attribute as set.

```html
<!-- INCORRECT -->
<mds-filter label="Stato">
  <mds-filter-item label="Attivo" value="active" selected="false"></mds-filter-item>
</mds-filter>

<!-- CORRECT -->
<mds-filter label="Stato">
  <mds-filter-item label="Attivo" value="active"></mds-filter-item>
</mds-filter>
```

#### Do Not Omit `value`

`value` is the machine-readable token the parent aggregates into `mdsFilterChange`. Without it the parent's event payload is empty or meaningless for consumers - always provide a distinct, meaningful value.

```html
<!-- INCORRECT -->
<mds-filter label="Priorita">
  <mds-filter-item label="Alta"></mds-filter-item>
  <mds-filter-item label="Bassa"></mds-filter-item>
</mds-filter>

<!-- CORRECT -->
<mds-filter label="Priorita">
  <mds-filter-item label="Alta" value="high"></mds-filter-item>
  <mds-filter-item label="Bassa" value="low"></mds-filter-item>
</mds-filter>
```

#### Do Not Leave the Count Out of an `aria-label` of Your Own

The item names itself after its label and its count. An `aria-label` written on the host replaces that name as it is, and the count the item shows is visible text too: a name without it fails WCAG 2.5.3 (Label in Name), and a speech-input user saying the number does not reach the item. Put the count in the name, or leave the name to the item.

```html
<!-- INCORRECT -->
<mds-filter label="Vista">
  <mds-filter-item aria-label="Vista a griglia" icon="mi/baseline/grid-view" value="grid" count="3"></mds-filter-item>
</mds-filter>

<!-- CORRECT -->
<mds-filter label="Vista">
  <mds-filter-item aria-label="Vista a griglia, 3 risultati" icon="mi/baseline/grid-view" value="grid" count="3"></mds-filter-item>
</mds-filter>
```

#### Do Not Slot Content Inside `<mds-filter-item>`

`<mds-filter-item>` has no publicly documented default or named slots. Icon, label, and count are all props. Slotting arbitrary HTML has no effect - use the dedicated props instead.

```html
<!-- INCORRECT -->
<mds-filter label="Formato">
  <mds-filter-item value="pdf">
    <mds-icon name="mi/baseline/picture-as-pdf"></mds-icon>
    PDF
  </mds-filter-item>
</mds-filter>

<!-- CORRECT -->
<mds-filter label="Formato">
  <mds-filter-item icon="mi/baseline/picture-as-pdf" label="PDF" value="pdf"></mds-filter-item>
</mds-filter>
```

#### Do Not Listen for Native `click` Instead of `mdsFilterItemSelect`

The component handles its own toggle internally and emits `mdsFilterItemSelect` when selection changes. A native `click` listener added before the component has loaded runs before the internal toggle (listeners run in the order they were added), so it reads the old `selected` state.

```html
<!-- INCORRECT -->
<mds-filter label="Stato">
  <mds-filter-item label="Attivo" value="active"></mds-filter-item>
</mds-filter>
<script>
  document.querySelector('mds-filter-item[value="active"]').addEventListener('click', (e) => {
    console.log('selected?', e.target.selected); // state not yet updated
  });
</script>

<!-- CORRECT -->
<mds-filter label="Stato">
  <mds-filter-item label="Attivo" value="active"></mds-filter-item>
</mds-filter>
<script>
  document.querySelector('mds-filter-item[value="active"]').addEventListener('mdsFilterItemSelect', (e) => {
    console.log('selected:', e.detail.selected);
  });
</script>
```

#### Do Not Customize via Undocumented Shadow Parts or Internal Selectors

`<mds-filter-item>` has no public CSS custom property and no shadow part: the count badge follows the `selected` state, and the bar is styled through the parent's public `--mds-filter-*` properties. Targeting shadow internals via `::part()`, `>>>`, or other selectors couples code to the implementation and will break on minor releases.

```css
/* INCORRECT */
mds-filter-item >>> .count {
  border-radius: 4px;
}
mds-filter-item::part(count) {
  background: red;
}

/* CORRECT */
mds-filter {
  --mds-filter-items-background: rgb(var(--magma-wash-strong));
  --mds-filter-items-background-active: rgb(var(--magma-accent-surface));
}
```
