---
title: "Data Attribute API"
description: "Use data attributes to configure component variants, sizes, and states directly in HTML markup."
slug: "data-attribute-api"
---

import { DocPage, PageHeader, SkillPanel, SourceFiles, SourceNote, StatesSection, CodeBlock, Demo, DemoCode, ChangelogEntries, ArchBody, StatsClaim, StatsCards, PageOverlay, DocText, DocLink, H2, SkillField, SkillText, SkillRow, SkillGrid, Variant, ApiPill, Dim, CodeWindow, CodeCard, TableCard } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="data-attribute-api" title="Data Attribute API" introStyle="max-width:38rem;">
  In React or Solid, you configure components with props. In defuss-shadcn, data attributes are your props. Every component uses a single CSS class to declare <em>what</em> it is, and <code>data-\*</code> attributes to declare <em>which version</em> - variant, size, orientation, and behavior.
</PageHeader>

<H2>Props → Data Attributes</H2>

<p class="text-sm text-muted-foreground mb-5">If you came from a component framework, this mapping should feel familiar.</p>

<div class="grid grid-cols-auto gap-4 mb-10">
  <CodeWindow lang="markup" title="React / Solid (props)" column code={"<Button\n  variant=\"destructive\"\n  size=\"lg\"\n>\n  Delete\n</Button>"} />
  <CodeWindow lang="markup" title="defuss-shadcn (data attributes)" column code={"<button class=\"btn\"\n  data-variant=\"destructive\"\n  data-size=\"lg\"\n>\n  Delete\n</button>"} />
</div>

<div class="sep" />

<H2>How it works</H2>

<p class="text-sm text-muted-foreground mb-5">Every component has exactly two layers in its markup: a class and its data attributes.</p>

<div style="display:grid;grid-template-columns:auto 1fr;gap:0.75rem 1.5rem;margin-bottom:1.5rem;align-items:baseline;">
  <code style="font-size:0.8125rem;font-weight:600;">class</code>
  <span class="text-sm text-muted-foreground">Identifies <strong>what</strong> the element is - <code>.btn</code>, <code>.badge</code>, <code>.card</code>, <code>.dialog</code>. One class per component. This is the base selector in CSS.</span>
  <code style="font-size:0.8125rem;font-weight:600;">data-variant</code>
  <span class="text-sm text-muted-foreground">Selects the visual style - <code>default</code>, <code>outline</code>, <code>ghost</code>, <code>destructive</code>, etc. Think of it as the component's "skin."</span>
  <code style="font-size:0.8125rem;font-weight:600;">data-size</code>
  <span class="text-sm text-muted-foreground">Selects the size - <code>sm</code>, <code>lg</code>, <code>icon</code>, etc. Controls height, padding, and font-size.</span>
  <code style="font-size:0.8125rem;font-weight:600;">data-\*</code>
  <span class="text-sm text-muted-foreground">Additional axes like <code>data-orientation</code>, <code>data-side</code>, <code>data-position</code> - each controls one independent dimension. Compose freely.</span>
</div>

<p class="text-sm text-muted-foreground mb-8">The CSS combines these with attribute selectors: <code>.btn\[data-variant="outline"\]</code>. No modifier classes, no BEM suffixes, no utility chains.</p>

<div class="sep" />

<H2>Why data attributes instead of modifier classes?</H2>

<p class="text-sm text-muted-foreground mb-5">Traditional systems like Bootstrap and BEM use modifier classes: <code>.btn-primary</code>, <code>.btn--lg</code>, <code>.btn-outline-danger</code>. Here's why we don't.</p>

<div class="grid grid-cols-auto gap-4 mb-10">
  <CodeWindow lang="html" title="Modifier classes" column code={"<button class=\"btn btn-destructive btn-lg\">\n  Delete\n</button>\n\n<style>\n  /* Conflicting specificity */\n  .btn-primary { ... }\n  .btn-lg { ... }\n  .btn-primary.btn-lg { ... }\n</style>"} />
  <CodeWindow lang="html" title="Data attributes" column code={"<button class=\"btn\"\n  data-variant=\"destructive\"\n  data-size=\"lg\">\n  Delete\n</button>\n\n<style>\n  /* Flat specificity - no combos needed */\n  .btn[data-variant=\"destructive\"] { ... }\n  .btn[data-size=\"lg\"] { ... }\n</style>"} />
</div>

<div style="display:grid;grid-template-columns:auto 1fr;gap:0.5rem 1rem;margin-bottom:2.5rem;align-items:baseline;">
  <span class="def-term">Flat specificity</span>
  <span class="text-sm text-muted-foreground">All variant/size selectors have equal weight. No specificity wars, no <code>!important</code>.</span>
  <span class="def-term">Independent axes</span>
  <span class="text-sm text-muted-foreground">Variant and size combine freely without needing combinatorial class names like <code>.btn-outline-lg</code>.</span>
  <span class="def-term">Uniform API</span>
  <span class="text-sm text-muted-foreground">Every component follows the same pattern. Learn it once, apply everywhere. Easy for AI to generate.</span>
  <span class="def-term">Clean class attribute</span>
  <span class="text-sm text-muted-foreground">The <code>class</code> attribute stays a single token - <code>class="btn"</code> - not a long chain of modifiers.</span>
</div>

<div class="sep" />

<H2>Styling attributes</H2>

<p class="text-sm text-muted-foreground mb-5">These are set in your HTML and read by CSS. They control the visual appearance of components.</p>

<h3 style="font-size:1rem;font-weight:500;margin:0 0 0.5rem;">
  <code>data-variant</code>
</h3>

<p class="text-sm text-muted-foreground mb-3">The most common attribute. Controls the visual style of a component.</p>

```html example component="button"
<div style="display: flex; gap: 0.75rem; flex-wrap: wrap; justify-content: center">
  <button class="btn" data-variant="default">Default</button>
  <button class="btn" data-variant="secondary">Secondary</button>
  <button class="btn" data-variant="outline">Outline</button>
  <button class="btn" data-variant="ghost">Ghost</button>
  <button class="btn" data-variant="destructive">Destructive</button>
  <button class="btn" data-variant="link">Link</button>
</div>
```

<div class="code-card" style="margin-bottom:1.5rem;">
  <div style="padding:0.625rem 1rem;background:var(--muted);border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;">
    <span class="mono-meta">data-variant values by component</span>
  </div>
  <div style="padding:1rem 1.25rem;font-size:0.8125rem;overflow-x:auto;">
    <table class="table" data-variant="simple">
      <thead>
        <tr>
          <th class="whitespace-nowrap">Component</th>
          <th>Values</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <code>.btn</code>
          </td>
          <td><code>default</code> · <code>secondary</code> · <code>outline</code> · <code>ghost</code> · <code>destructive</code> · <code>link</code></td>
        </tr>
        <tr>
          <td>
            <code>.badge</code>
          </td>
          <td><code>default</code> · <code>secondary</code> · <code>outline</code></td>
        </tr>
        <tr>
          <td>
            <code>.tag</code>
          </td>
          <td><code>default</code> · <code>secondary</code> · <code>outline</code></td>
        </tr>
        <tr>
          <td>
            <code>.alert</code>
          </td>
          <td><code>default</code> · <code>destructive</code></td>
        </tr>
        <tr>
          <td>
            <code>.toggle</code>
          </td>
          <td><code>default</code> · <code>outline</code></td>
        </tr>
        <tr>
          <td>
            <code>.toggle-group</code>
          </td>
          <td><code>default</code> · <code>outline</code></td>
        </tr>
        <tr>
          <td>
            <code>.toast</code>
          </td>
          <td><code>default</code> · <code>destructive</code> · <code>success</code> · <code>warning</code> · <code>info</code></td>
        </tr>
        <tr>
          <td>
            <code>.link</code>
          </td>
          <td><code>default</code> · <code>muted</code></td>
        </tr>
        <tr>
          <td>
            <code>.tab-list</code>
          </td>
          <td><code>default</code> · <code>line</code></td>
        </tr>
        <tr>
          <td>
            <code>.list</code>
          </td>
          <td><code>default</code> · <code>none</code> · <code>icon</code></td>
        </tr>
        <tr>
          <td>
            <code>.dropdown-item</code>
          </td>
          <td><code>default</code> · <code>destructive</code></td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

<h3 style="font-size:1rem;font-weight:500;margin:0 0 0.5rem;">
  <code>data-size</code>
</h3>

<p class="text-sm text-muted-foreground mb-3">Controls height, padding, and sometimes font-size.</p>

```html example component="button"
<div style="display: flex; gap: 0.75rem; flex-wrap: wrap; align-items: center; justify-content: center">
  <button class="btn" data-size="xs">Extra Small</button>
  <button class="btn" data-size="sm">Small</button>
  <button class="btn">Default</button>
  <button class="btn" data-size="lg">Large</button>
</div>
```

<div class="code-card" style="margin-bottom:1.5rem;">
  <div style="padding:0.625rem 1rem;background:var(--muted);border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;">
    <span class="mono-meta">data-size values by component</span>
  </div>
  <div style="padding:1rem 1.25rem;font-size:0.8125rem;overflow-x:auto;">
    <table class="table" data-variant="simple">
      <thead>
        <tr>
          <th class="whitespace-nowrap">Component</th>
          <th>Values</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <code>.btn</code>
          </td>
          <td><code>xs</code> · <code>sm</code> · default · <code>lg</code> · <code>icon</code> · <code>icon-sm</code> · <code>icon-lg</code></td>
        </tr>
        <tr>
          <td>
            <code>.input</code>
          </td>
          <td><code>sm</code> · default · <code>lg</code></td>
        </tr>
        <tr>
          <td>
            <code>.select</code>
          </td>
          <td><code>sm</code> · default · <code>lg</code></td>
        </tr>
        <tr>
          <td>
            <code>.dialog</code>
          </td>
          <td><code>sm</code> · default · <code>lg</code> · <code>xl</code> · <code>full</code></td>
        </tr>
        <tr>
          <td>
            <code>.toggle</code>
          </td>
          <td><code>sm</code> · default · <code>lg</code></td>
        </tr>
        <tr>
          <td>
            <code>.toggle-group</code>
          </td>
          <td><code>sm</code> · default · <code>lg</code></td>
        </tr>
        <tr>
          <td>
            <code>.avatar</code>
          </td>
          <td><code>sm</code> · default · <code>lg</code></td>
        </tr>
        <tr>
          <td>
            <code>.spinner</code>
          </td>
          <td><code>sm</code> · default · <code>lg</code></td>
        </tr>
        <tr>
          <td>
            <code>.switch</code>
          </td>
          <td><code>sm</code> · default</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

<h3 style="font-size:1rem;font-weight:500;margin:0 0 0.5rem;">Other styling attributes</h3>

<p class="text-sm text-muted-foreground mb-3">Some components have additional axes beyond variant and size.</p>

<div class="code-card" style="margin-bottom:2.5rem;">
  <div style="padding:0.625rem 1rem;background:var(--muted);border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;">
    <span class="mono-meta">additional styling data attributes</span>
  </div>
  <div style="padding:1rem 1.25rem;font-size:0.8125rem;overflow-x:auto;">
    <table class="table" data-variant="simple">
      <thead>
        <tr>
          <th class="whitespace-nowrap">Attribute</th>
          <th>Component</th>
          <th>Values</th>
          <th>Purpose</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <code>data-orientation</code>
          </td>
          <td><code>.btn-group</code> · <code>.sep</code> · <code>.toolbar</code></td>
          <td><code>horizontal</code> · <code>vertical</code></td>
          <td>Layout direction</td>
        </tr>
        <tr>
          <td>
            <code>data-side</code>
          </td>
          <td><code>.sheet</code> (dialog)</td>
          <td><code>left</code> · <code>right</code> · <code>top</code> · <code>bottom</code></td>
          <td>Slide-in direction</td>
        </tr>
        <tr>
          <td>
            <code>data-position</code>
          </td>
          <td>
            <code>.toast-container</code>
          </td>
          <td><code>top-left</code> · <code>top-right</code> · <code>top-center</code> · <code>bottom-left</code> · <code>bottom-right</code> · <code>bottom-center</code></td>
          <td>Viewport placement</td>
        </tr>
        <tr>
          <td>
            <code>data-ratio</code>
          </td>
          <td>
            <code>.image</code>
          </td>
          <td><code>1/1</code> · <code>4/3</code> · <code>16/9</code> · <code>21/9</code></td>
          <td>Aspect ratio</td>
        </tr>
        <tr>
          <td>
            <code>data-layout</code>
          </td>
          <td>
            <code>.descriptions</code>
          </td>
          <td><code>horizontal</code> · <code>vertical</code></td>
          <td>Term/definition layout</td>
        </tr>
        <tr>
          <td>
            <code>data-trend</code>
          </td>
          <td>
            <code>.stat-trend</code>
          </td>
          <td><code>up</code> · <code>down</code></td>
          <td>Trend indicator color</td>
        </tr>
        <tr>
          <td>
            <code>data-spacing</code>
          </td>
          <td>
            <code>.toggle-group</code>
          </td>
          <td>(boolean)</td>
          <td>Adds gap between toggles</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

<div class="sep" />

<H2>Behavioral attributes</H2>

<p class="text-sm text-muted-foreground mb-5">These wire up JavaScript interactions. They follow the pattern <code>data-\{component\}-trigger</code> and <code>data-\{component\}-close</code> - connecting a button to the element it controls.</p>

<CodeWindow lang="html" title="trigger / close pattern" mb="1.5rem" code={"<!-- The trigger: its value is the ID of the target -->\n<button class=\"btn\" data-dialog-trigger=\"confirm\">\n  Open Dialog\n</button>\n\n<!-- The target: matched by ID -->\n<dialog id=\"confirm\" class=\"dialog\">\n  <div class=\"dialog-content\">\n    <p>Are you sure?</p>\n    <!-- The close: placed inside, closes the nearest parent -->\n    <button class=\"btn\" data-dialog-close>Cancel</button>\n  </div>\n</dialog>"} />

<div class="code-card" style="margin-bottom:1.5rem;">
  <div style="padding:0.625rem 1rem;background:var(--muted);border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;">
    <span class="mono-meta">all behavioral data attributes</span>
  </div>
  <div style="padding:1rem 1.25rem;font-size:0.8125rem;overflow-x:auto;">
    <table class="table" data-variant="simple">
      <thead>
        <tr>
          <th class="whitespace-nowrap">Attribute</th>
          <th>Component</th>
          <th>Purpose</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <code>data-dialog-trigger</code>
          </td>
          <td>Dialog</td>
          <td>Opens the <code>&lt;dialog&gt;</code> matching the attribute value</td>
        </tr>
        <tr>
          <td>
            <code>data-dialog-close</code>
          </td>
          <td>Dialog</td>
          <td>Closes the parent dialog</td>
        </tr>
        <tr>
          <td>
            <code>data-alert-dialog-trigger</code>
          </td>
          <td>Alert Dialog</td>
          <td>Opens an alert dialog by ID</td>
        </tr>
        <tr>
          <td>
            <code>data-alert-dialog-close</code>
          </td>
          <td>Alert Dialog</td>
          <td>Closes the parent alert dialog</td>
        </tr>
        <tr>
          <td>
            <code>data-sheet-trigger</code>
          </td>
          <td>Sheet</td>
          <td>Opens a sheet (drawer) by ID</td>
        </tr>
        <tr>
          <td>
            <code>data-sheet-close</code>
          </td>
          <td>Sheet</td>
          <td>Closes the parent sheet</td>
        </tr>
        <tr>
          <td>
            <code>data-dropdown-trigger</code>
          </td>
          <td>Dropdown</td>
          <td>Toggles a popover dropdown menu</td>
        </tr>
        <tr>
          <td>
            <code>data-action</code>
          </td>
          <td>Number Input</td>
          <td><code>increment</code> / <code>decrement</code> buttons</td>
        </tr>
        <tr>
          <td>
            <code>data-type</code>
          </td>
          <td>Accordion · Toggle Group</td>
          <td><code>single</code> or <code>multiple</code> selection mode</td>
        </tr>
        <tr>
          <td>
            <code>data-collapsible</code>
          </td>
          <td>Accordion</td>
          <td>Allows all panels to be closed</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

<div class="sep" />

<H2>State attributes</H2>

<p class="text-sm text-muted-foreground mb-5">These are set and removed by JavaScript at runtime to represent UI state. CSS reads them to apply state-specific styles. You don't write these in markup - the JS manages them.</p>

<div class="code-card" style="margin-bottom:2.5rem;">
  <div style="padding:0.625rem 1rem;background:var(--muted);border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;">
    <span class="mono-meta">JS-managed state attributes</span>
  </div>
  <div style="padding:1rem 1.25rem;font-size:0.8125rem;overflow-x:auto;">
    <table class="table" data-variant="simple">
      <thead>
        <tr>
          <th class="whitespace-nowrap">Attribute</th>
          <th>Component</th>
          <th>Purpose</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <code>data-highlighted</code>
          </td>
          <td>Dropdown · Combobox</td>
          <td>Marks the currently keyboard-highlighted item</td>
        </tr>
        <tr>
          <td>
            <code>data-error</code>
          </td>
          <td>Avatar</td>
          <td>Set when image fails to load, triggers fallback display</td>
        </tr>
        <tr>
          <td>
            <code>data-placeholder</code>
          </td>
          <td>Combobox</td>
          <td>Present when no selection has been made</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

<div class="sep" />

<H2>ARIA attributes as styling hooks</H2>

<p class="text-sm text-muted-foreground mb-5">Where the browser natively provides state APIs - like <code>aria-selected</code>, <code>aria-expanded</code>, or <code>disabled</code> - we style against those directly. No data attributes are needed because the browser already exposes the state.</p>

<CodeWindow lang="css" title="native state as CSS selectors" mb="1.5rem" code={"/* Tab is selected - use ARIA, not a data attribute */\n.tab-trigger[aria-selected=\"true\"] {\n  color: var(--foreground);\n  border-color: var(--primary);\n}\n\n/* Toggle is pressed - native ARIA attribute */\n.toggle[aria-pressed=\"true\"] {\n  background: var(--accent);\n}\n\n/* Button is disabled - native HTML attribute */\n.btn:disabled {\n  opacity: 0.5;\n  cursor: not-allowed;\n}"} />

<p class="text-sm text-muted-foreground mb-8"><strong>Rule of thumb:</strong> if the browser has a native attribute for a state (<code>disabled</code>, <code>aria-selected</code>, <code>aria-expanded</code>, <code>open</code>), we style against that. Data attributes are only used when no native equivalent exists. </p>

<div class="sep" />

<H2>The pattern at a glance</H2>

<p class="text-sm text-muted-foreground mb-5">Every component follows this structure. Once you learn one, you know them all.</p>

<CodeWindow lang="text" title="the universal component pattern" mb="1.5rem" code={"<element\n  class=\"{component}\"             ← what it is\n  data-variant=\"{variant}\"        ← visual style\n  data-size=\"{size}\"              ← dimensions\n  data-{axis}=\"{value}\"           ← other config\n  data-{component}-trigger=\"{id}\" ← JS wiring\n  aria-*/disabled/open            ← native state\n>\n  content\n</element>"} />

<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(12rem, 1fr));gap:1rem;margin-bottom:2.5rem;">
  <div class="code-card-col">
    <div class="code-card-head">
      <span class="mono-meta">button</span>
    </div>
    <pre style="border-radius:0;border:none;margin:0;background:transparent;flex:1;overflow:hidden;font-size:0.75rem;"><code class="language-markup">{"<button class=\"btn\"\n  data-variant=\"outline\"\n  data-size=\"sm\">\n  Edit\n</button>"}</code></pre>
  </div>
  <div class="code-card-col">
    <div class="code-card-head">
      <span class="mono-meta">badge</span>
    </div>
    <pre style="border-radius:0;border:none;margin:0;background:transparent;flex:1;overflow:hidden;font-size:0.75rem;"><code class="language-markup">{"<span class=\"badge\"\n  data-variant=\"secondary\">\n  New\n</span>\n"}</code></pre>
  </div>
  <div class="code-card-col">
    <div class="code-card-head">
      <span class="mono-meta">sheet</span>
    </div>
    <pre style="border-radius:0;border:none;margin:0;background:transparent;flex:1;overflow:hidden;font-size:0.75rem;"><code class="language-markup">{"<dialog class=\"sheet\"\n  data-side=\"left\"\n  id=\"nav\">\n  ...\n</dialog>"}</code></pre>
  </div>
</div>

<p class="text-sm text-muted-foreground mb-8">Each component's <code>component-skill.md</code> lists the complete set of supported data attributes, variants, sizes, and ARIA requirements.</p>

</DocPage>
