Skip to content
Frontend & UIChapter 42 / 57Intermediate6 min read

Design Tokens & UI Consistency

Design tokens aur UI consistency, semantic CSS variables, Tailwind theme, raw hex se parhez, spacing scale aur shared UI primitives taake har screen ek jaisi lage.

On this page

1. Yeh practice kya hai?

Design token ek naam wali design value hai: primary, surface, destructive, border. Component yeh nahi kehta ke "yeh #1e40af hai", woh kehta hai "yeh primary hai". Primary ka asal rang globals.css mein ek hi jagah tay hota hai.

Yeh wahi idea hai jo code mein named constants ka hai, bas design ke liye.

2. Kyun zaroori hai?

Asli kahani: brand ka rang badla. Codebase mein 140 jagah hex values thin, 80 files mein, aur teen thore thore alag neele. Do hafte lage, phir bhi kuch screens purane rang mein reh gayin. Dark mode lana to na mumkin tha, kyunki har component ne apna safed khud likha tha.

Doosri kahani: do developers ne alag alag modal banaye. Ek Escape par band hota tha, doosra nahi, aur keyboard users ek modal mein phans jate the.

Raw values aur ad hoc componentsTokens aur shared primitives
Rebrand matlab sau files mein search replaceRebrand matlab ek file mein token badlo
Dark mode ke liye har component dobaraTokens ki dark values, components wahi
Teen thore alag gray aur radiusEk scale, har step ka naam
Har developer ka apna modal aur buttonEk primitive, sab use karte hain
Accessibility har jagah alagFocus ring aur contrast primitive mein ek dafa sahi

3. Kab follow karein?

LevelKitna zarooriKyun
Learning projectTailwind ka default scale use karein, arbitrary values se bacheinScale ke andar rehna consistency ka pehla qadam hai
Production codeSemantic tokens aur primitives lazmi. Component mein hex review mein ruk jata haiRebrand, dark mode aur accessibility sab isi par khare hain
Senior levelToken naming, scales aur primitive inventory design karna aur document karnaTeam ko pata ho kaunsa primitive hai aur kya mana hai

4. Bura vs acha code

Bura: raw values aur apna button

tsx
export function DeleteButton({ onDelete }: DeleteButtonProps) {
  return (
    <button
      onClick={onDelete}
      className="rounded-[7px] bg-[#dc2626] px-[13px] py-[9px] text-[#ffffff] outline-none"
    >
      Delete
    </button>
  );
}

Hex rang, ajeeb spacing (13px), koi dark mode nahi, aur outline-none ne keyboard focus ring hi gayab kar di.

Acha: tokens ek jagah

css
@import "tailwindcss";

:root {
  --surface: oklch(0.99 0 0);
  --on-surface: oklch(0.21 0 0);
  --primary: oklch(0.52 0.2 262);
  --on-primary: oklch(0.99 0 0);
  --destructive: oklch(0.58 0.22 27);
  --on-destructive: oklch(0.99 0 0);
  --border: oklch(0.92 0 0);
}

.dark {
  --surface: oklch(0.18 0 0);
  --on-surface: oklch(0.96 0 0);
  --border: oklch(0.3 0 0);
}

@theme inline {
  --color-surface: var(--surface);
  --color-on-surface: var(--on-surface);
  --color-primary: var(--primary);
  --color-on-primary: var(--on-primary);
  --color-destructive: var(--destructive);
  --color-on-destructive: var(--on-destructive);
  --color-border: var(--border);
}

Class merge ke liye ek helper:

ts
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

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

Ab feature component sirf primitive use karta hai:

tsx
import { Button } from "@/components/ui/button";

export function DeleteButton({ onDelete }: DeleteButtonProps) {
  return (
    <Button variant="destructive" onClick={onDelete}>
      Delete
    </Button>
  );
}

Button primitive ke andar bg-destructive text-on-destructive rounded-md px-4 py-2 aur ek visible focus ring ek hi dafa likhe gaye hain.

Farq kya hai?

CheezBuraAcha
Rang#dc2626 component meindestructive token
Spacing13px, 9pxScale ke steps, px-4 py-2
Dark modeNa mumkinToken ki dark value
Focusoutline-none, ring gayabPrimitive mein visible ring
ReuseHar jagah naya buttonEk Button, variants ke saath

5. Kaise apply karein?

Tokens aur primitives set karna
Roles ke naam rakho: surface, primary, destructive, border, text tiers
globals.css mein CSS variables, light aur dark
Tailwind theme mein map karo
Spacing, radius, shadow, type aur motion ke fixed scales
components/ui mein primitives, feature components unhe use karein
Token groupMisaal
Colorsurface, on-surface, primary, destructive, border, muted
Spacing4 ya 8 ki rhythm: 4, 8, 12, 16, 24, 32
TypeEk scale, jaise 12, 14, 16, 18, 24, 32, aur do teen weights
Radius, shadow, z-indexHar ek ka chhota fixed scale, har step ka naam
MotionDurations jaise 150ms state, 200ms reveal, 300ms attention

Naya component banane se pehle components/ui dekhein. Milta julta primitive mil jaye to variant jorein, naya mat banayein. Har primitive loading, empty, error aur disabled states sambhale.

6. Exceptions aur common mistakes

Bohat si teams component mein seedha Tailwind palette use karti hain (bg-red-600) aur chhote projects ya prototypes mein yeh theek chalta hai. Semantic tokens ek layer barhate hain. Trade-off: shuru mein thora kaam, magar rebrand, dark mode aur multi-brand ke waqt yehi layer hafte bachati hai.

Charts ke data colors, brand logo ka SVG, ya marketing ki illustration token system se bahar ho sakte hain, magar charts ke liye bhi ek tay palette token set hona chahiye.

Galati: Tokens ko rang ke naam dena, jaise blue-brand

Why? Rebrand mein blue-brand ka rang hara ho gaya to naam jhoot bolne lagta hai.

Behtar: Role ke naam rakho: primary, accent, destructive, surface.

Galati: Arbitrary values jaise p-[13px] aur text-[#333]

Why? Har ek dafa ki value scale ko torti hai aur screens dheere dheere alag lagne lagti hain.

Behtar: Scale ke qareebi step ko use karo. Asal mein nayi value chahiye to token mein jorein.

Galati: Focus ring hata dena kyunki design mein achi nahi lagti

Why? Keyboard users ko pata hi nahi chalta ke woh kahan hain, aur yeh accessibility ka blocking masla hai.

Behtar: Ring ko token se style karo, hatao mat. Primitive mein ek dafa sahi karo.

7. Pehle aur baad mein kya seekhein?

Suggested order
Styling with Tailwind
Design Tokens & UI Consistency
Visual Hierarchy & Spacing
Motion & Design Iteration

Final quick revision

CheckHaan / Nahi
Component mein raw hexNahi
Tokens role ke naam seHaan
Light aur dark values ek jagahHaan
Arbitrary spacing valuesNahi, scale
Naya primitive banane se pehle components/ui dekhaHaan
Focus ring visibleHaan