Skip to main content
rule CX Codex

data-attributes — Claude Code rule: what it does, how to install

data-attributes is a Claude Code rule for styling component states and targeting nested parts with data-state and data-slot.

Apache-2.0 ★ 141 updated 2026-11-17

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/data-attributes.md
Open on GitHub

What data-attributes does

The data-attributes rule standardizes component state styling with attributes such as data-state, data-disabled, and data-loading. It avoids separate className props for open, closed, selected, or loading states.

Reusable parts receive data-slot values in kebab-case, such as search-input or submit-button. Parent selectors can target those slots, while Tailwind arbitrary variants handle classes like data-[state=open]:fade-in and [&_[data-slot=button]]:w-full.

Variants and sizes stay as props. The class examples require Tailwind support for arbitrary variants.

What it helps with

  • style open and closed component states
  • target nested component parts from a parent
  • mark selected, disabled, or loading states
  • separate state attributes from variants and sizes

Example prompts for the agent

  • Rewrite Dialog so its visual state uses data-state with Tailwind variants.
  • Add data-slot to CheckboxGroup and style the containing FieldSet when that group is nested inside it.
  • Update Button so loading and disabled become data attributes while variants and sizes remain props.

How to install data-attributes

Manually: put the files in place

.claude/rules/data-attributes.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about data-attributes

How do I install data-attributes in Claude Code?

Add the rule file at .claude/rules/data-attributes.md. This rule does not require a separate command.

What does data-attributes do?

It defines patterns for styling component states through data-state and related attributes, and for identifying nested parts with data-slot. Variants and sizes remain props.

What does data-attributes need to work?

The class examples require Tailwind with arbitrary variant support. Selectors such as data-[state=open]:fade-in depend on it.

Source text

data-attributes.md · Apache-2.0
---
title: Data Attributes for Styling
impact: LOW
impactDescription: Clean state-based styling patterns
tags: data-attributes, data-state, data-slot, styling, tailwind, css
---

## Data Attributes for Component Styling

Use `data-state` and `data-slot` attributes to create flexible, maintainable component APIs.

### Use data-state for Visual States

**Never expose separate className props for different states.** Use `data-state` attributes:

```tsx
const Dialog = ({ className, ...props }: DialogProps) => {
  const [isOpen, setIsOpen] = useState(false);
  return (
    <div
      data-state={isOpen ? "open" : "closed"}
      className={cn("transition-all", className)}
      {...props}
    />
  );
};
```

**Consumer styles from outside:**

```tsx
<Dialog className="data-[state=open]:opacity-100 data-[state=closed]:opacity-0" />
```

### Common State Patterns

```tsx
// Open/closed
<Accordion data-state={isOpen ? 'open' : 'closed'} />

// Selected
<Tab data-state={isSelected ? 'active' : 'inactive'} />

// Disabled
<Button data-disabled={isDisabled} disabled={isDisabled} />

// Loading
<Button data-loading={isLoading} />

// Orientation
<Slider data-orientation="horizontal" />

// Side/position
<Tooltip data-side="top" />
```

### Tailwind Integration

```tsx
<Dialog
  className={cn(
    "rounded-lg border p-4",
    "data-[state=open]:animate-in data-[state=open]:fade-in",
    "data-[state=closed]:animate-out data-[state=closed]:fade-out"
  )}
/>
```

### Use data-slot for Component Identification

Give components stable identifiers for parent targeting:

```tsx
function CheckboxGroup({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="checkbox-group"
      className={cn("flex flex-col gap-2", className)}
      {...props}
    />
  );
}

function FieldSet({ className, ...props }: React.ComponentProps<"fieldset">) {
  return (
    <fieldset
      data-slot="field-set"
      className={cn(
        "flex flex-col gap-6",
        "has-[>[data-slot=checkbox-group]]:gap-3",
        className
      )}
      {...props}
    />
  );
}
```

### Parent-Aware Styling

```tsx
<form className="[&_[data-slot=button]]:w-full">
  <Button>Submit</Button>
</form>
```

### Naming Conventions

**Incorrect:**

```tsx
data-slot="input"           // Too generic
data-slot="blueButton"      // Includes styling
data-slot="div-wrapper"     // Implementation detail
```

**Correct:**

```tsx
data-slot="search-input"
data-slot="navigation-menu"
data-slot="error-message"
data-slot="submit-button"
```

### When to Use Each

| Pattern      | Use For                                              |
| ------------ | ---------------------------------------------------- |
| `data-state` | Visual states (open/closed, active, loading)         |
| `data-slot`  | Component identification, parent-child targeting     |
| `props`      | Variants (primary, secondary), sizes, event handlers |

### Combined Example

```tsx
const Button = ({
  variant = "primary",
  size = "md",
  loading,
  disabled,
  className,
  ...props
}: ButtonProps) => {
  return (
    <button
      data-slot="button"
      data-loading={loading}
      data-disabled={disabled}
      className={cn(buttonVariants({ variant, size }), className)}
      disabled={disabled}
      {...props}
    />
  );
};

// Usage
<form className="[&_[data-slot=button]]:w-full">
  <Button loading={isLoading} className="data-[loading=true]:opacity-50">
    Submit
  </Button>
</form>;
```

### Rules

1. **Use `data-state` instead of separate className props** for states
2. **Add `data-slot` to reusable components** for targeting
3. **Use kebab-case** for `data-slot` values
4. **Prefer Tailwind arbitrary variants** over custom CSS
5. **Never rely on class names** for parent-child targeting

© maxtrigify · Apache-2.0 · maxtrigify/Otto

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set18

The whole set →
design-tokens design-tokens is a Claude Code rule that replaces hardcoded colors with semantic tokens. Light and dark themes then change variable values instead of component classes. CX Codex +1 ★ 141repo stars composition The composition rule for Claude Code guides the breakdown of complex UI components into small, focused parts. It serves when building component libraries or flexible interfaces. CX Codex +1 ★ 141repo stars overview overview is a Claude Code rule for designing accessible, composable UI components. It comes into play while shaping component APIs and shared patterns. CX Codex +1 ★ 141repo stars types The types rule for Claude Code standardizes component prop definitions in TypeScript. It provides a framework for building flexible, type-safe interfaces across React projects. CX Codex +1 ★ 141repo stars principles The principles rule for Claude Code establishes six core standards for component design. It guides agents during component creation, refactoring, and evaluation. CX Codex +1 ★ 141repo stars as-child as-child is a Claude Code rule for merging a component’s props and handlers into a custom child without an extra DOM wrapper. It applies to composable React triggers. CX Codex +1 ★ 141repo stars documentation documentation is a Claude Code rule that standardizes UI component documentation. It enforces consistent sections for props, runnable examples, and accessibility guidelines. CX Codex +1 ★ 141repo stars state state is a Claude Code rule for controlled and uncontrolled React component state. It applies while designing component APIs and state props. CX Codex +1 ★ 141repo stars

Project sponsors

Companies that keep this analytics open