Skip to content

Regression: boolean attributes are no longer removed if value is false if attr name contains a dash #3790

Closed
@rejhgadellaa

Description

@rejhgadellaa
  • Check if updating to the latest Preact version resolves the issue

Describe the bug

Preact 10.11.1 introduced the following change:

Fix falsy data attributes not working
https://github.com/preactjs/preact/releases/tag/10.11.1

Related issue #3717 and PR #3720

To Reproduce

With preact 10.11.0, the following snippet:

<div data-has-toolbar={hasToolbar} />

would result in either a <div /> with or without the attribute:

<div data-has-toolbar="true" />
<div />

but starting in 10.11.1, it results in:

<div data-has-toolbar="true" />
<div data-has-toolbar="false" />

Expected behavior

I was relying on the old behaviour for style rules so, at least in my use-case, this change could be considered a regression. The following CSS selector now always applies:

div[data-has-toolbar] { /* ... */ }

where previously it would only apply if the prop's value was true.

The old behaviour feels more like HTML spec for boolean attributes/properties, but I'm not sure this applies to data- attributes?

For example, I think element.dataset.hasToolbar will always return a string where boolean properties-derived-from-attributes like element.required and -.disabled will actually return a boolean in javascript.

I can also see how it's worthwhile following React's behaviour here. They've actually recently fixed an issue and adopted Preact's behaviour for boolean attributes without dashes: facebook/react#9230

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