Skip to content

bug: boolean attributes in form-associated components parsed wrongly #6319

Closed

Description

@annam002

Prerequisites

Stencil Version

4.35.1

Current Behavior

I am trying to upgrade to Stencil 4.35.1 from Stencil 4.23.0. In components that are form-associated the boolean properties now behave differently than before. They cannot be set to false anymore by using the property.
In a component like below, the checkbox cannot be set to disabled by the user of the web component anymore. This was working fine in Stencil 4.23.0.

The component:

@Component({
  tag: 'test-checkbox-form-associated',
  shadow: true,
  formAssociated: true,
})
export class TestCheckboxFormAssociated {
  @AttachInternals() internals: ElementInternals;

  @Prop() disabled: boolean | undefined = undefined;

  componentWillLoad() {
    if ('setFormValue' in this.internals) this.internals.setFormValue('');
  }

  render() {
    return (
      <label>
        <input type="checkbox" disabled={this.disabled} /> checkbox
      </label>
    );
  }
}

When using it with
<test-checkbox-form-associated disabled="false"></test-checkbox-form-associated>

this results in this HTML

<input type="checkbox" disabled="">

Expected Behavior

It works like before.

System Info

Stencil: 4.35.1 🦄
  TypeScript: 5.5.4
      Rollup: 4.34.9
      Parse5: 7.2.1
      jQuery: 4.0.0-pre
      Terser: 5.37.0

Steps to Reproduce

check out example repo and do npm run start

Code Reproduction URL

https://github.com/annam002/stencil-properties-bug

Additional Information

No response

Activity

  1. added
    Bug: Validated
    on Jun 26, 2025
  2. annam002 commented on Jun 27, 2025

    @annam002
    Author

    Was probably introduced with this PR: #6280

  3. danyball commented on Jun 27, 2025

    @danyball

    This is not a bug, its a feature! You can read that in the html standard. Also see #5461

  4. annam002 commented on Jun 27, 2025

    @annam002
    Author

    From what I understand, you changed the behaviour to be compliant to the HTML standard. But this is a breaking change: users of our components now have to change from setting disabled="false" to removing the disabled-attribute. And if you want to comply to the standard, why do you only do it to form-associated components? they now behave differently than non-form-associated ones, which is wildly confusing.

  5. christian-bromann commented on Jun 27, 2025

    @christian-bromann
    Member

    <test-checkbox-form-associated disabled="false"></test-checkbox-form-associated>

    Setting disabled="false" is the same as setting disabled="true". The fact that Stencil allowed to set negative boolean flags as properties has been a bug and we should clean this up.

  6. johnjenkins commented on Jun 27, 2025

    @johnjenkins
    Contributor

    Out of interest (genuine question) - if you removed the ability of Stencil to set bool=“false” how would one set a boolean (via attribute) whose default is “true” which does not necessarily reflect?
    I realise the standard says “you just can’t” but is that acceptable?
    Plus I think I’m right in saying that the standard doesn’t seem consistent regarding aria attributes?

  7. christian-bromann commented on Jun 27, 2025

    @christian-bromann
    Member

    how would one set a boolean (via attribute) whose default is “true” which does not necessarily reflect?

    I would advocate for a best practice where you would use a negation attribute, e.g.

    @Component({
      tag: 'my-component',
      styleUrl: 'my-component.css',
      shadow: true,
    })
    export class MyComponent {
      @Prop() myBool = true;
      // ...
    }

    to set it to false, you would just do <my-component no-my-bool />. This could be something that Stencil could do automatically.

    Thoughts?

  8. annam002 commented on Jun 28, 2025

    @annam002
    Author

    The point is that you cannot set the property to false like before and this is a breaking change. We cannot upgrade now because it would break things for our users.
    What I also don't understand: why was this behaviour only changed for form-associated components and not in general for all boolean properties?

  9. christian-bromann commented on Jun 30, 2025

    @christian-bromann
    Member

    @danyball any thoughts here?

  10. danyball commented on Jul 25, 2025

    @danyball

    Thanks Christian for pinging me.

    Regarding the handling for form-ass. elements: I would recommend to be as near to the standard as possible. Even if we think its weird. I am sure there was an discussion about that. If not, the standard should be changed. So for me its acceptable and Stencil implements the standard 1:1.

    The change itself is a breaking change. True. Maybe just release it as a major?!

    Not sure about not setting false booleans at all. This topic is not necessarily related to this one, right? Just jumped into the standard.. there is a warning about true/false on boolean attributes: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes "The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether."
    --> so I also would go this way and what @christian-bromann said. Having "booleanAttr=false" on a stencil element, stencil should remove this attr automatically. Makes sense to me.

  11. johnjenkins commented on Oct 10, 2025

    @johnjenkins
    Contributor

    @annam002 I'm going to close this one out as I don't think we're going to roll back this change (sorry - I know that's not what you're looking for).

    Removing stencil's weird behaviour around string "false" is probably the best way forward long-term but I do realise this is a breaking change so should not have been in a patch / minor release.

    Solutions / moving forward:

    1. Set false defaults to false not undefined:

    @Prop() disabled: boolean | undefined = undefined; > @Prop() disabled: boolean = false;

    1. If you want to recreate the old behaviour (i.e. if you have a boolean property that by default is true) use the @AttrDeserialize decorator:
    @Prop() disabled: boolean = true;
    
    @AttrDeserialize('disabled)
    handleBool(attr: string | null) {
     if (attr === 'false' || attr === null) return false;
     return true;
    }

    Now <my-thing disabled="false"> works as it used to

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

    Bug: ValidatedHelp Wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions