Skip to content

bug: empty attribute is ignored on a boolean | string prop in dist-custom-elements (regression in 4.38.0) #6963

Closed

Description

@kmosher

Prerequisites

  • I have read the Contributing Guidelines.
  • I agree to follow the Code of Conduct.
  • I have searched for existing issues that already report this problem, without success.

Stencil Version

4.45.2 (regressed in 4.38.0; 4.37.1 works)

Current Behavior

In a dist-custom-elements build, setting an attribute to the empty string (<my-cmp union> in markup, or el.setAttribute('union', '')) on a prop typed boolean | string leaves the prop at its default false. The attribute is silently ignored.

This is the same symptom as #6411, which #6413 fixed for props typed exactly boolean. A union-typed prop compiles to MEMBER_FLAGS.Any, so it skips the boolean coercion #6413 added. It then reaches this check in proxyComponent's attributeChangedCallback (src/runtime/proxy-component.ts):

if (!isSpuriousBooleanRemoval && newValue != this[propName] && (!propDesc.get || !!propDesc.set)) {
  this[propName] = newValue;
}

'' != false is false, so the write is skipped. The loose comparison came from #6404, which wanted null and undefined to count as equal. As a side effect, it also makes '' equal to false.

The lazy (www/dist) build of the same component is not affected.

Expected Behavior

As in 4.37.1, the attribute sets the prop to '', so the component sees the attribute as present.

System Info

      System: node 22.23.2
    Platform: darwin (27.0.0)
   CPU Model: Apple M5 Pro (18 cpus)
     Stencil: 4.45.2
  TypeScript: 5.8.3
      Rollup: 4.44.0

Observed in Chromium (Playwright 1.62.1).

Steps to Reproduce

  1. Build a component with a boolean | string prop using the dist-custom-elements output target:

    @Component({ tag: 'my-cmp', shadow: true })
    export class MyCmp {
      @Prop() bool: boolean = false;
      @Prop() union: boolean | string = false;
      render() {
        return <span>bool={JSON.stringify(this.bool)} union={JSON.stringify(this.union)}</span>;
      }
    }
  2. Define it from dist/components and set both attributes to "":

    <my-cmp bool union></my-cmp>
    <script type="module">
      import { defineCustomElementMyCmp } from '/dist/components/index.js';
      defineCustomElementMyCmp();
    </script>
  3. Read the props:

    @stencil/core bool union
    4.37.1 true ""
    4.38.0 false false
    4.45.2 true false

    Calling setAttribute('union', '') after definition gives the same results. Setting the property directly (el.union = '') works on every version.

Code Reproduction URL

https://github.com/kmosher/stencil-union-boolean-attr-repro

Additional Information

Changing that comparison back to newValue !== this[propName] in @stencil/core/internal/client/index.js fixes the reproduction on 4.45.2. To keep #6404's intent, the check could treat only null and undefined as equal, for example:

const unchanged = newValue === this[propName] || (newValue == null && this[propName] == null);

The loose comparison may also drop other writes on Any props, such as '0' over a current value of false or 0. We haven't tested those.

We hit this in a design system where many props are typed boolean | string so that they accept bare attributes. On 4.38 and later, every bare attribute on those props stops working in the custom-elements build.

Activity

  1. visualkhh commented on Nov 1, 2026

    @visualkhh

    Hit the same class on my side — empty-string attributes silently becoming false — and the loose != in the quoted check is exactly the shape to look at.

    Browser ground truth (Chromium). Presence is truth for booleans, no coercion games:

    • setAttribute('disabled', '') → disabled === true, value stays ""
    • hidden="until-found" round-trips verbatim (enumerated, not boolean)

    So '' must never read back as the false default. What I use (@dooboostore/simple-web-component) handles this with a strict presence rule instead of a loose comparison: only the literal strings 'false'/'0' coerce to false, everything else present — including '' — is true. Its own test pins it: setAttribute('open', '') → true, then 'false' → false, '0' → false. No union-type special path exists to skip, so there's no MEMBER_FLAGS.Any-shaped hole for '' to fall through.

    For the Stencil fix, the same shape applies: the boolean | string branch needs the boolean-coercion path #6413 added (presence → true for ''), not the loose newValue != this[propName] gate — that comparison is what equates '' with false today. A strict == null-style null/undefined check would keep #6404's intent without swallowing empty strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions