Tokenizing Elevation Values for Consistent Depth
Establishing a predictable depth hierarchy requires decoupling visual representation from implementation logic. By abstracting z-index stacking contexts, box-shadow definitions, and transform-based parallax into a unified token registry, engineering teams eliminate visual drift across component libraries. This architectural approach builds directly on the principles outlined in Design System Token Fundamentals & Naming Conventions, ensuring semantic clarity, predictable cascade behavior, and scalable CSS architecture.
Precise Implementation Workflow
- Define a Base Elevation Scale (0–6) Map primitive shadow values to CSS custom properties in a centralized registry. Use a consistent mathematical progression for blur, spread, and opacity to maintain optical balance.
/* tokens/primitives.css */
:root {
--elevation-0: 0 0 0 0 rgba(0, 0, 0, 0);
--elevation-1: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
--elevation-2: 0 4px 6px rgba(0, 0, 0, 0.08), 0 2px 4px rgba(0, 0, 0, 0.04);
--elevation-3: 0 10px 15px rgba(0, 0, 0, 0.1), 0 4px 6px rgba(0, 0, 0, 0.05);
--elevation-4: 0 20px 25px rgba(0, 0, 0, 0.12), 0 10px 10px rgba(0, 0, 0, 0.06);
--elevation-5: 0 25px 50px rgba(0, 0, 0, 0.18), 0 12px 12px rgba(0, 0, 0, 0.08);
--elevation-6: 0 35px 60px rgba(0, 0, 0, 0.22), 0 15px 15px rgba(0, 0, 0, 0.1);
}
- Alias Primitives to Semantic Tokens Decouple implementation from usage. Map scale values to contextual roles consumed by components.
/* tokens/semantic.css */
:root {
--elevation-surface: var(--elevation-1);
--elevation-card: var(--elevation-2);
--elevation-overlay: var(--elevation-3);
--elevation-modal: var(--elevation-4);
--elevation-toast: var(--elevation-5);
--elevation-dragging: var(--elevation-6);
}
- Implement Fallback Chains & Compute Values
Use CSS
calc()for dynamic adjustments (e.g., dark mode inversion) or a compiler like Style Dictionary to generate platform-specific outputs. Enforce strict fallback chains to prevent unstyled states.
.component-elevated {
box-shadow: var(--elevation-card, var(--elevation-2));
}
-
Standardize Blur, Spread, and Opacity Ratios Integrate with Elevation & Shadow Tokens to lock shadow geometry across responsive breakpoints. Configure your token compiler to output breakpoint-specific overrides where optical scaling is required.
-
Validate Token Resolution in Theme Modes Run automated visual regression baselines and computed style assertions. Ensure
prefers-color-schemeor data-attribute themes correctly invert shadow opacity without breaking stacking contexts.
CI Pipeline Debugging & Diagnostic Workflows
When elevation tokens fail during CI builds, apply this diagnostic matrix to isolate and resolve pipeline bottlenecks.
| Symptom | Root Cause | Resolution |
|---|---|---|
Build fails with undefined CSS variable or broken alias chains |
Token generation script outputs malformed JSON or misses recursive alias resolution in the compiler AST. | Execute npm run tokens:build -- --dry-run to inspect the intermediate AST. Verify all alias chains terminate at primitive values. Enforce strict JSON schema validation (ajv-cli validate -s schema.json -d tokens.json) before compilation. |
| Visual regression flags depth mismatches despite passing unit tests | Browser-specific shadow rendering differences (WebKit vs Blink) or missing @supports fallbacks for complex box-shadow stacks. |
Inject computed style snapshots into CI artifacts. Use Playwright/Cypress getComputedStyle() assertions to validate exact box-shadow strings against token baselines. Add @supports (box-shadow: 0 0 0) guards for legacy environments. |
| Token drift detected in PR reviews | Manual overrides in component CSS bypassing the token registry (e.g., inline z-index: 9999 or hardcoded shadows). |
Enforce Stylelint rules at lint time. Configure declaration-property-value-disallowed-list to block raw box-shadow, z-index, and transform values outside the token layer. Fail PRs on violation. |
CI Log Pattern Example:
[ERROR] Stylelint: Unexpected hardcoded value 'z-index: 9999' (disallowed-list)
[WARN] Style Dictionary: Alias chain '--elevation-modal' -> '--elevation-7' unresolved.
[INFO] Running dry-run compilation...
[SUCCESS] Token AST validated. 42 semantic aliases resolved.
Migration Patterns & Resolution Strategies
Migrating legacy elevation implementations requires a phased, automated approach to prevent regression and maintain developer velocity.
- Audit
Extract all hardcoded
box-shadow,z-index, andtranslateZvalues using AST parsing. Run a PostCSS or Stylelint plugin across the codebase to generate an inventory report.
// audit-report.json
{
"components/Modal.css": ["box-shadow: 0 10px 30px rgba(0,0,0,0.15)", "z-index: 1000"],
"components/Card.tsx": ["boxShadow: '0 4px 12px rgba(0,0,0,0.08)'"]
}
-
Map Create a translation matrix matching legacy values to the nearest semantic elevation token. Use perceptual distance algorithms or manual design review to align legacy shadows with the new scale.
-
Refactor Apply codemods to replace inline values with
var(--elevation-*)references. Preserve legacy fallbacks during the transition period using CSS@layeror feature queries.
# Example jscodeshift command
npx jscodeshift -t ./codemods/elevation-tokenizer.js src/ --extensions=tsx,css
-
Verify Run parallel builds with legacy and tokenized outputs. Compare computed styles using a token diff CLI or visual regression suite to ensure zero optical regression.
-
Deprecate Remove legacy shadow definitions and enforce token-only usage via CI lint gates and pre-commit hooks (
husky+lint-staged). Archive the translation matrix for historical reference.
Cross-Environment Validation & Maintenance
Maintain elevation consistency by integrating token validation into pre-commit hooks and CI pipelines. Use token diffing tools to detect unauthorized scale modifications and enforce semantic naming conventions. Document resolution patterns for edge cases, including high-DPI display scaling, prefers-reduced-motion constraints, and forced-colors mode compliance. Regularly audit token consumption metrics to identify unused or duplicated elevation references, ensuring the registry remains lean, accessible, and predictable across all delivery channels.