bug: writing to a component's class removes its host classes and hydrated flag for good
Prerequisites
- [x] I have read the Contributing Guidelines.
- [x] I agree to follow the Code of Conduct.
- [x] I have searched for existing issues that already report this problem, without success.
Stencil Version
4.45.2
Current Behavior
When code outside Stencil writes to a component's class after it renders, the host loses every class Stencil set on it, including the hydrated flag. With the default hydrated flag, the component stays visibility: hidden. The next render only adds the classes that changed in that render, so the rest never come back.
Measured with ion-button from Ionic Framework 9.0.10 (Stencil 4.45.2) in Chromium and WebKit:
| Step | class |
visibility |
|---|---|---|
| After hydration | md button button-solid ion-activatable ion-focusable hydrated |
visible |
el.className = 'mine' |
mine |
hidden |
el.color = 'danger' (re-render) |
mine ion-color ion-color-danger |
hidden |
React 19 makes this write whenever a re-render changes className on a custom element, so a Stencil component rendered directly by React 19 disappears.
Expected Behavior
Classes Stencil sets on the host, and the hydrated flag, survive a write to class from outside, or come back on the next render, alongside the classes the caller set.
System Info
Steps to Reproduce
- Open the reproduction.
- Compare the first two rows of the table: the button is visible after hydration and hidden after
el.className = 'mine'. - The third row shows the re-render adding back only
ion-color ion-color-danger.
Code Reproduction URL
https://stackblitz.com/edit/stackblitz-starters-ylbupzmj?file=index.html
Additional Information
@stencil/react-output-target already works around this inside its wrappers by merging className into the element's current classes (stenciljs/output-targets#485). Plain DOM code and custom elements rendered directly by React 19 don't go through the wrappers. Found while triaging ionic-team/ionic-framework#30107.
Hi! I would like to work on fixing this issue. After inspecting src/runtime/vdom/set-accessor.ts and src/runtime/update-component.ts, I noticed that setAccessor returns early when oldValue === newValue and skips adding classes already present in oldClasses, while ddHydratedFlag is only applied during initial load. I plan to ensure host classes and the hydrated class are restored on host updates even when external DOM code overwrites className, and add unit tests in src/runtime/vdom/test/set-accessor.spec.ts. Let me know if you have any feedback on this approach!
hey @thetaPC - thanks for raising.
To clarify, the issue is el.className = 'mine' wipes away Stencil classes?
I think, that's just native behaviour and Stencil shouldn't probably mess with it. Diffing against the DOM on every render would add cost to every component and stop outside code from ever removing a class.
Some associated thoughts:
- Callers should be encouraged to use
classList.add | removeinstead - Component authors ideally shouldn't use class 'sprouting' - better to use custom states or use attribute reflection as css hooks
- Component authors can set
hydratedFlag: { selector: 'attribute' }(in stencil.config) for more robust hydrated state
Gonna close this one out for now :) Thanks for using Stencil ❤️