|
| 1 | +# CxJS CSS Variables Design Document |
| 2 | + |
| 3 | +This document defines the naming conventions and structure for CSS custom properties in CxJS theming. |
| 4 | + |
| 5 | +## Implementation |
| 6 | + |
| 7 | +The CSS variables theming system is implemented in the `cx-theme-variables` package (`packages/cx-theme-variables/`). This package provides a base theme that can be customized at runtime by overriding CSS variables. |
| 8 | + |
| 9 | +## CSS Custom Properties Limitation |
| 10 | + |
| 11 | +**Important:** CSS custom properties have a fundamental limitation that affects sub-theming. When you define a derived variable on `:root` that references another variable: |
| 12 | + |
| 13 | +```scss |
| 14 | +:root { |
| 15 | + --cx-theme-primary-color: blue; |
| 16 | + --cx-primary-button-hover-background-color: color-mix(in srgb, var(--cx-theme-primary-color), white 20%); |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +And then override the base variable in a container: |
| 21 | + |
| 22 | +```scss |
| 23 | +.my-container { |
| 24 | + --cx-theme-primary-color: red; |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +The derived variable (`--cx-primary-button-hover-background-color`) **does NOT recalculate** inside `.my-container`. It still uses the `:root` computed value. |
| 29 | + |
| 30 | +**Consequence:** We cannot use intermediate component-level CSS variables for derived values. Instead, `color-mix()` calculations must be placed directly in the SCSS map overrides so they reference theme variables directly and recalculate properly in sub-themed containers. |
| 31 | + |
| 32 | +## Layers |
| 33 | + |
| 34 | +| Layer | Prefix | Purpose | |
| 35 | +|-------|--------|---------| |
| 36 | +| Theme | `--cx-theme-*` | High-level design tokens (colors, sizing, effects) | |
| 37 | +| Theme Defaults | `--cx-theme-default-*` | Base values for components (optional, for simple references) | |
| 38 | + |
| 39 | +**Note:** Component-level variables (`--cx-{component}-*`) are intentionally avoided for derived values due to the limitation above. Use `color-mix()` directly in SCSS maps instead. |
| 40 | + |
| 41 | +## Layer 1: Theme (`--cx-theme-*`) |
| 42 | + |
| 43 | +Minimal set of design tokens that define the theme's identity. |
| 44 | + |
| 45 | +```scss |
| 46 | +:root { |
| 47 | + // Primary colors |
| 48 | + --cx-theme-primary-color: #1976d2; |
| 49 | + |
| 50 | + // Status colors |
| 51 | + --cx-theme-accent-color: #ffc107; |
| 52 | + --cx-theme-danger-color: #d32f2f; |
| 53 | + |
| 54 | + // Text colors |
| 55 | + --cx-theme-color: rgba(0, 0, 0, 0.87); |
| 56 | + |
| 57 | + // Surface colors |
| 58 | + --cx-theme-background-color: white; |
| 59 | + --cx-theme-surface-color: white; |
| 60 | + |
| 61 | + // Border |
| 62 | + --cx-theme-border-color: lightgray; |
| 63 | + |
| 64 | + // Active state colors (black for light themes, white for dark themes) |
| 65 | + --cx-theme-active-state-color: black; |
| 66 | + --cx-theme-active-state-hover-amount: 8%; |
| 67 | + --cx-theme-active-state-pressed-amount: 12%; |
| 68 | + |
| 69 | + // Effects |
| 70 | + --cx-theme-box-shadow: ...; |
| 71 | + --cx-theme-focus-box-shadow: ...; |
| 72 | + --cx-theme-transition: all 0.2s ease; |
| 73 | + |
| 74 | + // Sizing |
| 75 | + --cx-theme-border-radius: 4px; |
| 76 | + --cx-theme-box-padding: 5px; |
| 77 | + --cx-theme-box-line-height: 24px; |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Layer 2: Theme Defaults (`--cx-theme-default-*`) |
| 82 | + |
| 83 | +Simple default values for components. Only use for direct references, not for values that will be used in further calculations. |
| 84 | + |
| 85 | +```scss |
| 86 | +:root { |
| 87 | + --cx-theme-default-button-background-color: color-mix(in srgb, var(--cx-theme-surface-color), var(--cx-theme-active-state-color) 4%); |
| 88 | + --cx-theme-default-button-border-color: var(--cx-theme-border-color); |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Using color-mix() in SCSS Maps |
| 93 | + |
| 94 | +Since we cannot use intermediate CSS variables for derived values, place `color-mix()` calculations directly in the SCSS map overrides: |
| 95 | + |
| 96 | +```scss |
| 97 | +$cx-button-state-style-map: cx-deep-map-merge( |
| 98 | + $cx-button-state-style-map, |
| 99 | + ( |
| 100 | + default: ( |
| 101 | + background-color: var(--cx-theme-default-button-background-color), |
| 102 | + border-color: var(--cx-theme-default-button-border-color), |
| 103 | + ), |
| 104 | + hover: ( |
| 105 | + // Calculate directly - references theme variable, will recalculate in sub-themed containers |
| 106 | + background-color: color-mix(in srgb, var(--cx-theme-default-button-background-color), var(--cx-theme-active-state-color) var(--cx-theme-active-state-hover-amount)), |
| 107 | + ), |
| 108 | + active: ( |
| 109 | + background-color: color-mix(in srgb, var(--cx-theme-default-button-background-color), var(--cx-theme-active-state-color) var(--cx-theme-active-state-pressed-amount)), |
| 110 | + ), |
| 111 | + ) |
| 112 | +); |
| 113 | + |
| 114 | +$cx-button-mods: cx-deep-map-merge( |
| 115 | + $cx-button-mods, |
| 116 | + ( |
| 117 | + primary: ( |
| 118 | + default: ( |
| 119 | + background-color: var(--cx-theme-primary-color), |
| 120 | + color: var(--cx-theme-background-color), |
| 121 | + border-color: color-mix(in srgb, var(--cx-theme-primary-color), var(--cx-theme-active-state-color) 20%), |
| 122 | + ), |
| 123 | + hover: ( |
| 124 | + background-color: color-mix(in srgb, var(--cx-theme-primary-color), var(--cx-theme-background-color) 20%), |
| 125 | + ), |
| 126 | + active: ( |
| 127 | + background-color: color-mix(in srgb, var(--cx-theme-primary-color), var(--cx-theme-active-state-color) 20%), |
| 128 | + ), |
| 129 | + ), |
| 130 | + ) |
| 131 | +); |
| 132 | +``` |
| 133 | + |
| 134 | +## File Structure |
| 135 | + |
| 136 | +``` |
| 137 | +packages/cx-theme-variables/src/ |
| 138 | +├── index.scss # Main entry |
| 139 | +├── variables.scss # Theme variables, @forward cx |
| 140 | +├── maps.scss # @use component files, map overrides |
| 141 | +├── widgets/ |
| 142 | +│ ├── Button.scss # Button map overrides with color-mix() |
| 143 | +│ ├── form/ |
| 144 | +│ │ └── ... |
| 145 | +│ ├── grid/ |
| 146 | +│ │ └── ... |
| 147 | +│ └── ... |
| 148 | +``` |
| 149 | + |
| 150 | +## Sub-theming |
| 151 | + |
| 152 | +Override theme variables in containers. Because `color-mix()` is in the SCSS maps (not intermediate CSS variables), derived colors recalculate correctly. |
| 153 | + |
| 154 | +**Toolbar with primary-colored buttons:** |
| 155 | +```scss |
| 156 | +.toolbar { |
| 157 | + --cx-theme-default-button-background-color: var(--cx-theme-primary-color); |
| 158 | + --cx-theme-default-button-border-color: transparent; |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +**Dark mode:** |
| 163 | +```scss |
| 164 | +.dark-mode { |
| 165 | + --cx-theme-primary-color: #90caf9; |
| 166 | + --cx-theme-surface-color: #1e1e1e; |
| 167 | + --cx-theme-background-color: #121212; |
| 168 | + --cx-theme-color: rgba(255, 255, 255, 0.87); |
| 169 | + --cx-theme-border-color: #424242; |
| 170 | + --cx-theme-active-state-color: white; // Lighten on hover instead of darken |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +## SCSS Variable Overrides |
| 175 | + |
| 176 | +Set SCSS `$cx-default-*` variables to `null` in the `@forward` block when they are controlled via CSS variables and maps: |
| 177 | + |
| 178 | +```scss |
| 179 | +@forward "cx/src/variables" with ( |
| 180 | + $cx-default-button-background-color: null !default, |
| 181 | + $cx-default-button-border-color: null !default, |
| 182 | + ... |
| 183 | +); |
| 184 | +``` |
| 185 | + |
| 186 | +## Guidelines |
| 187 | + |
| 188 | +1. **Minimize variables** - Only expose theme-level design tokens |
| 189 | +2. **No intermediate derived variables** - Use `color-mix()` directly in SCSS maps |
| 190 | +3. **Use active-state-color** - Avoid hardcoding `black` or `white` for state changes |
| 191 | +4. **Reference theme variables** - Always reference `--cx-theme-*` variables so sub-theming works |
| 192 | +5. **Test sub-theming** - Verify that overriding theme variables in containers produces expected results |
0 commit comments