-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Boolean attributes on Web Components #9230
Comments
Hey @nickdima This behavior is described in Docs (old jsx gotchas) here. So basically, if You pass a prop without a value, it defaults to "True" Quick edit: Digging deeper, this behavior (React Docs also point that) is standard behavior of html5 boolean attributes. Simply put, when you have an input element: the presence of |
Well yeah, but React is doing the contrary in this case, id doesn't render |
Thanks for the report @nickdima! Current React just passes attributes through if the element is a custom element. So if you pass You can verify that's exepcted behavior with: var element = document.createElement('some-tag');
element.setAttribute('somebool', true);
// <some-tag somebool="true"></some-tag> According to the HTML5 spec on boolean attributes
So as a workaround you should be able to do I'm not sure if that will accomplish what you want, as it probably depends on how AMP is reading these attributes. Attributes like React can potentially be smarter about this and coerce |
Thanks @aweary! The workaround |
I don't remember all the history here, but I think we assume that custom elements are string-typed, not boolean-typed. For all string-typed attributes we just coerce to strings (even for boolean values). So like |
@spicyj Is there any reason to assume booleans should be coerced to strings? Are there any cases where If we leave the current behavior then we should definitely document how to render boolean attributes. |
ping @spicyj, what do you think about ^? |
I think there may be an issue here because if the boolean is I guess the distinction to be drawn here is between enumerated attributes (which expect "true"/"false") and boolean attributes. Some examples of enumerated attributes are things like Taking a bigger step back, I think switching to passing properties to custom elements would be really nice as it sort of sidesteps some of these issues. But it doesn't resolve what to do in the AMP case if AMP requires an attribute instead of a property :\ |
I looked at what Preact does here and I think their solution is pretty decent. If the value is a boolean it will add or remove the attribute. If the value is a string it will set the attribute to the string. Example: let val;
val = true;
<x-foo bool={val}></x-foo>
// output: <x-foo bool="true"></x-foo>
val = false;
<x-foo bool={val}></x-foo>
// output: <x-foo></x-foo>
val = "true";
<x-foo bool={val}></x-foo>
// output: <x-foo bool="true"></x-foo>
val = "false";
<x-foo bool={val}></x-foo>
// output: <x-foo bool="false"></x-foo> This seems like it would allow you to support boolean attributes ( |
For TypeScript users, I noticed that TypeScript complains if I pass an empty string as the value. So I'm getting around that by passing <Helmet>
<script
async={undefined}
custom-element="amp-bodymovin-animation"
src="https://cdn.ampproject.org/v0/amp-bodymovin-animation-0.1.js"
/>
</Helmet> This renders as |
I think boolean attributes should be allowed as they were declared. take a look: |
Yall broke working code. I should be able to pass false in a component property and not throw an error like it used to, very annoying. |
So...hold up. You're telling me there's no way to get React to call |
Issue is still open +1 |
hey, so it looks like the fix was merged and removed latter on. the problem is still there. passing |
@gaearon Can I work on this as a first contribution? |
I think you'll find that I found that |
Has this been fixed in the |
It's still adding attributes with the stringified value: https://codesandbox.io/s/web-components-boolean-props-lgbhp0?file=/src/index.js |
Re: #9230 (comment) val = true;
<x-foo bool={val}></x-foo>
// output: <x-foo bool="true"></x-foo> @robdodson Wouldn't this still be problematic with the initial case of For enumerated attributes like |
Following facebook#9230 (comment) except that `foo={true}` renders an empty string. See facebook#9230 (comment) for rationale.
Following facebook#9230 (comment) except that `foo={true}` renders an empty string. See facebook#9230 (comment) for rationale.
Following facebook#9230 (comment) except that `foo={true}` renders an empty string. See facebook#9230 (comment) for rationale.
* Log unexpected warnings when testing with ReactDOMServerIntegrationTestUtils * Add test Following #9230 (comment) except that `foo={true}` renders an empty string. See #9230 (comment) for rationale. * Match Preact behavior for boolean props on custom elements * Poke CircleCI
When setting boolean attributes on Web Components
<x-search somebool name={this.props.name} />
they get rendered as attribute/value pairs instead
<x-search somebool="true" name={this.props.name}>
This causes problems with some AMP components, for eg.
<amp-iframe />
which has aresizable
attribute that gets rendered asresizable="true"
. This results in non valid AMP content and developers crying with blood tears on their keyboards :)Is there any reason for the current behaviour? Will changing this break something else?
The text was updated successfully, but these errors were encountered: