Installation
Install the package along with its peer dependencies:Overview
This package helps you:- Validate theme accessibility: Check if your custom themes meet WCAG contrast requirements
- Test text contrast: Ensure text colors have sufficient contrast against backgrounds
- Test UI control contrast: Verify UI elements meet the 3:1 contrast requirement
- Test data visualization contrast: Check data visualization color pairings
- Debug contrast issues: Identify which token pairs fail accessibility standards
Basic Usage
Checking Current Theme
Inspect the accessibility of your current theme:With Customization Provider
Use within a Theme or Customization Provider:API Reference
Text Contrast Functions
getContrastRatingsOfTokensWithTextContrastRequirements
Returns contrast ratings for all token pairs with text contrast requirements.tokens- Design tokens object from your theme
getNumberOfTextFailures
Counts how many token pairs fail WCAG AA text contrast requirements.ratings- Array of contrast ratings fromgetContrastRatingsOfTokensWithTextContrastRequirements
UI Control Contrast Functions
getContrastRatingsOfTokensWithUIControlContrastRequirements
Returns contrast ratings for all token pairs with UI control contrast requirements.tokens- Design tokens object from your theme
getNumberOfUIControlFailures
Counts how many token pairs fail UI control contrast requirements.ratings- Array of contrast ratings fromgetContrastRatingsOfTokensWithUIControlContrastRequirements
Data Visualization Contrast Functions
getContrastRatingsOfTokensWithDataVisualizationContrastRequirements
Returns contrast ratings for data visualization color pairs.tokens- Design tokens object from your theme
Helper Functions
flattenCategorizedTokens
Removes token categorization and returns a flat list.convertRawTokenJSONToArray
Converts raw design tokens object to an array.Use Cases
Custom Theme Validation
Validate a custom theme before using it in production:Development Dashboard
Build a development tool to monitor theme accessibility:CI/CD Integration
Add theme validation to your CI/CD pipeline:Understanding WCAG Contrast Requirements
Text Contrast (WCAG 2.1 Level AA)
- Normal text: 4.5:1 minimum contrast ratio
- Large text (18pt+ or 14pt+ bold): 3:1 minimum contrast ratio
UI Control Contrast
- UI components: 3:1 minimum contrast ratio against adjacent colors
- Graphical objects: 3:1 minimum contrast ratio
Rating Properties
aa: Meets WCAG Level AA for normal text (4.5:1)aaLarge: Meets WCAG Level AA for large text (3:1)aaa: Meets WCAG Level AAA for normal text (7:1)aaaLarge: Meets WCAG Level AAA for large text (4.5:1)
Best Practices
Do
- Validate custom themes before deploying to production
- Use these utils during theme development to catch issues early
- Set up automated testing in CI/CD for theme accessibility
- Review all failed pairs and understand why they fail
- Aim for WCAG AA compliance at minimum
Don’t
- Don’t ignore accessibility failures
- Don’t assume visual inspection is sufficient
- Don’t make exceptions for brand colors that fail contrast
- Don’t forget to test both text and UI control contrast
Troubleshooting
Theme Not Updating
If contrast checks don’t reflect theme changes:Unexpected Failures
If you see unexpected failures:- Check that token values are correct color values (hex, rgb, etc.)
- Verify token pairings are defined correctly in raw design tokens
- Ensure you’re using the correct function for the type of check
Version
Current version: 5.0.0Dependencies
This package uses:color-combos(1.0.12) - For calculating contrast ratioslodash(4.17.21) - Utility functions