Lit styleMap: custom property with $ in name is dropped on initial render, set on update
Comments · 0
Be the first to comment.
Bug reportby SceneRecap AI
Repro steps
- 1.Render an element with styleMap({ '--$bar-fill-ratio': '0.5' }) next to a control using styleMap({ '--bar-fill-ratio': '0.5' }).
- 2.Read getPropertyValue('--$bar-fill-ratio') on the bug element after the initial render.
- 3.Click 'Re-render with value 0.8 (triggers styleMap update path)'.
- 4.Read getPropertyValue('--$bar-fill-ratio') again after the update.
Expected
styleMap sets '--$bar-fill-ratio' on the initial render, so getPropertyValue returns "0.5" and the bar fills to 0.5. This matches the control '--bar-fill-ratio'. Initial render and update target the same custom property.
Actual
On initial render getPropertyValue('--$bar-fill-ratio') returns "" and the bug bar shows '0 / 1'. The declaration '--$bar-fill-ratio: 0.5;' is written unescaped to the style attribute and the CSS parser drops it. After the re-render, the update path calls style.setProperty('--$bar-fill-ratio', '0.8'). That call is accepted, and the value reads "0.8".
Evidence
Issue markdown (copy-paste ready)
## Summary
styleMap does not escape custom CSS property names on initial render. A name like `--$bar-fill-ratio` is written raw into the `style` attribute and dropped by the CSS parser. Later updates use `style.setProperty`, which accepts the name. Initial render and updates therefore behave differently.
## Environment
- lit@3 (lit-html 3.3.3)
- Chromium 151.0.7922.34, macOS, 1280x720
- Related: github.com/lit/lit/issues/5446
## Steps to reproduce
1. Render `styleMap({ '--$bar-fill-ratio': '0.5' })` next to a control `styleMap({ '--bar-fill-ratio': '0.5' })` (1.0s).
2. Read `getPropertyValue('--$bar-fill-ratio')` (1.0s).
3. Click **Re-render with value 0.8 (triggers styleMap update path)** (10.8s).
4. Read the value again (13.0s).
## Expected
The initial render sets `--$bar-fill-ratio` to `0.5`, the same as the control. Initial render and update target the same property.
## Actual
- At 1.0s the bug panel reads `""` and the bar shows `0 / 1`. The declaration `--$bar-fill-ratio: 0.5;` is dropped because `$` is not a valid CSS ident character.
- At 13.0s, after the update, the value reads `"0.8"` because `style.setProperty('--$bar-fill-ratio', '0.8')` is accepted by the CSSOM.
## Control
The `--bar-fill-ratio` control works on both paths: it reads `"0.5"` at 1.0s and `"0.8"` at 13.0s.
## Suggested area
The styleMap initial render path that serializes property names into the style attribute. Consider escaping custom property names, for example with `CSS.escape`, or using `setProperty` on first render so both paths agree.
*Recorded with SceneRecap.*This report was written by SceneRecap from a screen recording. Try SceneRecap free
Turn a screen recording into a bug report like this one.
Repro steps, expected vs actual and the console and network evidence, each with a timestamp. On every plan, Free included.
Try SceneRecap free →