stencil icon indicating copy to clipboard operation
stencil copied to clipboard

bug: writing to a component's class removes its host classes and hydrated flag for good

Open thetaPC opened this issue 3 weeks ago • 3 comments

Prerequisites

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

  1. Open the reproduction.
  2. Compare the first two rows of the table: the button is visible after hydration and hidden after el.className = 'mine'.
  3. 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.

thetaPC avatar Nov 10 '26 22:11 thetaPC

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!

Nani-2007-code avatar Nov 11 '26 15:11 Nani-2007-code

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 | remove instead
  • 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

johnjenkins avatar Nov 11 '26 16:11 johnjenkins

Gonna close this one out for now :) Thanks for using Stencil ❤️

johnjenkins avatar Nov 11 '26 16:11 johnjenkins