|
| 1 | +# Visual Parity: Lit and React Renderers |
| 2 | + |
| 3 | +This document describes the approach used to achieve visual parity between the Lit (Shadow DOM) and React (Light DOM) renderers. |
| 4 | + |
| 5 | +## Structural Mirroring |
| 6 | + |
| 7 | +### The Challenge |
| 8 | + |
| 9 | +Lit components use Shadow DOM, where each component has an encapsulated DOM tree with its own scoped styles. The typical structure is: |
| 10 | + |
| 11 | +``` |
| 12 | +#shadow-root |
| 13 | + <section class="theme-classes"> |
| 14 | + <slot></slot> ← children projected here |
| 15 | + </section> |
| 16 | +``` |
| 17 | + |
| 18 | +The shadow host element (the custom element itself) acts as the `:host` and can have its own styles. |
| 19 | + |
| 20 | +React uses Light DOM where everything exists in the global DOM. To achieve parity, we mirror Lit's two-element structure. |
| 21 | + |
| 22 | +### The Solution |
| 23 | + |
| 24 | +Each React component renders a wrapper div (representing `:host`) plus a section (the internal element): |
| 25 | + |
| 26 | +```tsx |
| 27 | +// React component structure |
| 28 | +<div className="a2ui-card"> {/* ← :host equivalent */} |
| 29 | + <section className="theme-classes"> {/* ← internal element */} |
| 30 | + {children} {/* ← ::slotted(*) equivalent */} |
| 31 | + </section> |
| 32 | +</div> |
| 33 | +``` |
| 34 | + |
| 35 | +This mirroring allows CSS selectors to target the same conceptual elements in both renderers. |
| 36 | + |
| 37 | +## CSS Selector Transformation |
| 38 | + |
| 39 | +### Shadow DOM to Light DOM |
| 40 | + |
| 41 | +Lit's Shadow DOM selectors need transformation for React's global CSS: |
| 42 | + |
| 43 | +| Lit (Shadow DOM) | React (Light DOM) | |
| 44 | +|-----------------------|------------------------------------------| |
| 45 | +| `:host` | `.a2ui-surface .a2ui-{component}` | |
| 46 | +| `section` | `.a2ui-surface .a2ui-{component} section`| |
| 47 | +| `::slotted(*)` | `.a2ui-surface .a2ui-{component} section > *` | |
| 48 | +| `element` (e.g., `h2`)| `:where(.a2ui-surface .a2ui-{component}) element` | |
| 49 | + |
| 50 | +### Example: Card Component |
| 51 | + |
| 52 | +Lit's card.ts static styles: |
| 53 | +```css |
| 54 | +:host { |
| 55 | + display: block; |
| 56 | + flex: var(--weight); |
| 57 | + min-height: 0; |
| 58 | + overflow: auto; |
| 59 | +} |
| 60 | + |
| 61 | +section { |
| 62 | + height: 100%; |
| 63 | + width: 100%; |
| 64 | + min-height: 0; |
| 65 | + overflow: auto; |
| 66 | +} |
| 67 | + |
| 68 | +section ::slotted(*) { |
| 69 | + height: 100%; |
| 70 | + width: 100%; |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +React's componentSpecificStyles equivalent: |
| 75 | +```css |
| 76 | +.a2ui-surface .a2ui-card { |
| 77 | + display: block; |
| 78 | + flex: var(--weight); |
| 79 | + min-height: 0; |
| 80 | + overflow: auto; |
| 81 | +} |
| 82 | + |
| 83 | +.a2ui-surface .a2ui-card section { |
| 84 | + height: 100%; |
| 85 | + width: 100%; |
| 86 | + min-height: 0; |
| 87 | + overflow: auto; |
| 88 | +} |
| 89 | + |
| 90 | +.a2ui-surface .a2ui-card section > * { |
| 91 | + height: 100%; |
| 92 | + width: 100%; |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## CSS Specificity Matching |
| 97 | + |
| 98 | +### The Problem |
| 99 | + |
| 100 | +Shadow DOM provides natural style encapsulation with low specificity. A selector like `h2` inside Shadow DOM has specificity `(0,0,0,1)`. |
| 101 | + |
| 102 | +In React's global CSS, we need contextual selectors to scope styles: |
| 103 | +```css |
| 104 | +.a2ui-surface .a2ui-text h2 { ... } |
| 105 | +``` |
| 106 | + |
| 107 | +This has specificity `(0,0,2,1)` — much higher than Lit's `(0,0,0,1)`. |
| 108 | + |
| 109 | +### Why It Matters |
| 110 | + |
| 111 | +Utility classes like `.typography-w-500` have specificity `(0,0,1,0)`. In Lit: |
| 112 | +- `h2 { font: inherit; }` = `(0,0,0,1)` — loses to utility class |
| 113 | +- `.typography-w-500` = `(0,0,1,0)` — **wins**, font-weight: 500 applied |
| 114 | + |
| 115 | +In React (without fix): |
| 116 | +- `.a2ui-surface .a2ui-text h2 { font: inherit; }` = `(0,0,2,1)` — **wins** |
| 117 | +- `.typography-w-500` = `(0,0,1,0)` — loses, font-weight reset to 400 |
| 118 | + |
| 119 | +### The Solution: `:where()` |
| 120 | + |
| 121 | +The `:where()` pseudo-class has zero specificity contribution. Wrapping contextual selectors in `:where()` matches Lit's low specificity: |
| 122 | + |
| 123 | +```css |
| 124 | +/* Before: specificity (0,0,2,1) — too high */ |
| 125 | +.a2ui-surface .a2ui-text h1, |
| 126 | +.a2ui-surface .a2ui-text h2 { ... } |
| 127 | + |
| 128 | +/* After: specificity (0,0,0,1) — matches Lit */ |
| 129 | +:where(.a2ui-surface .a2ui-text) h1, |
| 130 | +:where(.a2ui-surface .a2ui-text) h2 { ... } |
| 131 | +``` |
| 132 | + |
| 133 | +Now utility classes can override element resets, just like in Lit. |
| 134 | + |
| 135 | +### When to Use `:where()` |
| 136 | + |
| 137 | +Use `:where()` when the Lit component has element selectors that should be overridable by utility classes: |
| 138 | + |
| 139 | +- **Use `:where()`**: Element resets like `h1, h2 { font: inherit; }` |
| 140 | +- **Don't use `:where()`**: Structural styles on `:host` or `section` that define component behavior |
| 141 | + |
| 142 | +## File Organization |
| 143 | + |
| 144 | +- **`src/styles/index.ts`**: Contains `structuralStyles` (from Lit) and `componentSpecificStyles` (React-specific) |
| 145 | +- **Component files**: Render the mirrored structure with appropriate class names |
| 146 | +- **`injectStyles()`**: Injects both structural and component-specific styles into the document |
| 147 | + |
| 148 | +## Testing Parity |
| 149 | + |
| 150 | +The `renderers/visual-parity` directory contains side-by-side comparisons: |
| 151 | +1. Load the same fixture in both Lit and React |
| 152 | +2. Compare rendered output visually and via computed styles |
| 153 | +3. Use browser DevTools to verify CSS specificity matches |
0 commit comments