home / skills / gentleman-programming / gentleman-skills / tailwind-4

tailwind-4 skill

/curated/tailwind-4

This skill helps you apply Tailwind 4 patterns and safe practices, guiding cn usage, theme handling, and avoiding var() in class names.

npx playbooks add skill gentleman-programming/gentleman-skills --skill tailwind-4

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.6 KB
---
name: tailwind-4
description: >
  Tailwind CSS 4 patterns and best practices.
  Trigger: When styling with Tailwind - cn(), theme variables, no var() in className.
license: Apache-2.0
metadata:
  author: gentleman-programming
  version: "1.1"
---

## Styling Decision Tree

```
Tailwind class exists?  → className="..."
Dynamic value?          → style={{ width: `${x}%` }}
Conditional styles?     → cn("base", condition && "variant")
Static only?            → className="..." (no cn() needed)
Library can't use class?→ style prop with var() constants
```

## Critical Rules

### Never Use var() in className

```typescript
// ❌ NEVER: var() in className
<div className="bg-[var(--color-primary)]" />
<div className="text-[var(--text-color)]" />

// ✅ ALWAYS: Use Tailwind semantic classes
<div className="bg-primary" />
<div className="text-slate-400" />
```

### Never Use Hex Colors

```typescript
// ❌ NEVER: Hex colors in className
<p className="text-[#ffffff]" />
<div className="bg-[#1e293b]" />

// ✅ ALWAYS: Use Tailwind color classes
<p className="text-white" />
<div className="bg-slate-800" />
```

## The cn() Utility

```typescript
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}
```

### When to Use cn()

```typescript
// ✅ Conditional classes
<div className={cn("base-class", isActive && "active-class")} />

// ✅ Merging with potential conflicts
<button className={cn("px-4 py-2", className)} />  // className might override

// ✅ Multiple conditions
<div className={cn(
  "rounded-lg border",
  variant === "primary" && "bg-blue-500 text-white",
  variant === "secondary" && "bg-gray-200 text-gray-800",
  disabled && "opacity-50 cursor-not-allowed"
)} />
```

### When NOT to Use cn()

```typescript
// ❌ Static classes - unnecessary wrapper
<div className={cn("flex items-center gap-2")} />

// ✅ Just use className directly
<div className="flex items-center gap-2" />
```

## Style Constants for Charts/Libraries

When libraries don't accept className (like Recharts):

```typescript
// ✅ Constants with var() - ONLY for library props
const CHART_COLORS = {
  primary: "var(--color-primary)",
  secondary: "var(--color-secondary)",
  text: "var(--color-text)",
  gridLine: "var(--color-border)",
};

// Usage with Recharts (can't use className)
<XAxis tick={{ fill: CHART_COLORS.text }} />
<CartesianGrid stroke={CHART_COLORS.gridLine} />
```

## Dynamic Values

```typescript
// ✅ style prop for truly dynamic values
<div style={{ width: `${percentage}%` }} />
<div style={{ opacity: isVisible ? 1 : 0 }} />

// ✅ CSS custom properties for theming
<div style={{ "--progress": `${value}%` } as React.CSSProperties} />
```

## Common Patterns

### Flexbox

```typescript
<div className="flex items-center justify-between gap-4" />
<div className="flex flex-col gap-2" />
<div className="inline-flex items-center" />
```

### Grid

```typescript
<div className="grid grid-cols-3 gap-4" />
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6" />
```

### Spacing

```typescript
// Padding
<div className="p-4" />           // All sides
<div className="px-4 py-2" />     // Horizontal, vertical
<div className="pt-4 pb-2" />     // Top, bottom

// Margin
<div className="m-4" />
<div className="mx-auto" />       // Center horizontally
<div className="mt-8 mb-4" />
```

### Typography

```typescript
<h1 className="text-2xl font-bold text-white" />
<p className="text-sm text-slate-400" />
<span className="text-xs font-medium uppercase tracking-wide" />
```

### Borders & Shadows

```typescript
<div className="rounded-lg border border-slate-700" />
<div className="rounded-full shadow-lg" />
<div className="ring-2 ring-blue-500 ring-offset-2" />
```

### States

```typescript
<button className="hover:bg-blue-600 focus:ring-2 active:scale-95" />
<input className="focus:border-blue-500 focus:outline-none" />
<div className="group-hover:opacity-100" />
```

### Responsive

```typescript
<div className="w-full md:w-1/2 lg:w-1/3" />
<div className="hidden md:block" />
<div className="text-sm md:text-base lg:text-lg" />
```

### Dark Mode

```typescript
<div className="bg-white dark:bg-slate-900" />
<p className="text-gray-900 dark:text-white" />
```

## Arbitrary Values (Escape Hatch)

```typescript
// ✅ OK for one-off values not in design system
<div className="w-[327px]" />
<div className="top-[117px]" />
<div className="grid-cols-[1fr_2fr_1fr]" />

// ❌ Don't use for colors - use theme instead
<div className="bg-[#1e293b]" />  // NO
```

## Keywords
tailwind, css, styling, cn, utility classes, responsive

Overview

This skill documents Tailwind CSS 4 patterns and practical rules for consistent styling. It focuses on when to use className vs style, the cn() utility for conditional and merged classes, and strict rules against var() and hex colors inside className. The goal is predictable, theme-driven UI across components and libraries that don’t accept className.

How this skill works

It inspects common styling scenarios and prescribes the right approach: use plain className for static classes, cn() for conditional or merging needs, style prop for truly dynamic values, and CSS custom properties only for library props that don't accept classes. It enforces theme tokens and Tailwind color classes instead of inline var() or hex values in class strings.

When to use it

  • Use className="..." for static Tailwind classes with no runtime conditions.
  • Use cn(...) when classes are conditional or you must merge external className values.
  • Use style={{ ... }} for dynamic numeric values (percentages, pixel values, opacity).
  • Use CSS var() only in JS constants passed to libraries that don’t accept className.
  • Use arbitrary values for one-off layout needs, but never for colors — use theme tokens.

Best practices

  • Never place var() or hex color literals inside className; map to theme tokens (e.g., bg-primary).
  • Keep cn() for runtime logic and potential conflicts; avoid wrapping static class lists.
  • Define chart/library color constants with var(...) and pass via props when className is unsupported.
  • Prefer Tailwind semantic color classes and responsive utilities instead of inline values.
  • Use style prop for truly dynamic values and CSS custom properties for component-level theming.

Example use cases

  • Button with variants and disabled state: className={cn('px-4 py-2', variant==='primary' && 'bg-blue-500', disabled && 'opacity-50')}.
  • Responsive grid layout: className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6'.
  • Chart library colors: const COLORS = { text: 'var(--color-text)', grid: 'var(--color-border)' } and pass to Recharts.
  • Dynamic width bar: <div style={{ width: `${percentage}%` }} /> for percent-driven sizing.
  • One-off fixed width: className='w-[327px]' used sparingly for design exceptions.

FAQ

Why not use var() inside className?

Tailwind class strings must map to utility names and compiled rules; var() breaks purging and theming consistency. Use theme tokens (bg-primary) or pass var() via JS props for libraries.

When should I use cn() instead of className directly?

Use cn() when classes depend on runtime conditions, when merging external className props, or when resolving conflicting utilities. For static lists, prefer plain className for clarity and performance.