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
Remove attribute when set to null/undefined/false #486
Conversation
I wonder if now would be a good time to actually introduce a Boolean modifier? |
Yeah, not in this PR though I want Caleb to be able to decide which bits to merge and which not to Boolean modifier is about parsing stuff, Boolean attr are a HTML spec thing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if now would be a good time to actually introduce a Boolean modifier?
Yeah, not in this PR though
I want Caleb to be able to decide which bits to merge and which not to
Boolean modifier is about parsing stuff, Boolean attr are a HTML spec thing.
Yeah for sure. Separate PR. |
@richgcook what do you think of this implementation? Do we still think it's worth checking against null/undefined/false? Or is just "null" enough? |
Personally I think "null" is enough but I guess it should be any falsy value? |
Any falsy value except |
🤪 |
I think it's sensible to make a stand and say "you have to use the |
I agree Vue removes the value when false but Alpine is not Vue. |
@SimoTod only caveat is this one
|
Btw the tests aren't 100% representative since a removed DOM Node attribute, when accessed will return |
@HugoDF https://codepen.io/SimoTod/pen/WNQgvqo I'd be tempted to leave the current behaviour for boolean attributes and just change the last else to 'if null remove it else set value as attribute` |
Hey @HugoDF, thanks for this. Yes I think we shoud just skip the "boolean" attribute thing for attribute removal right? And just mirror Vue: If undefined, null, or falsy, remove it - right? |
Regarding the boolean stuff, is there any reason to just not be strict about <div :turd-sandwich="true">
// => <div turd-sandwich>
<div :turd-sandwich="'true'">
// => <div turd-sandwich="true">
<div :turd-sandwich="false">
// => <div>
<div :turd-sandwich="scrumptious">
// => <div turd-sandwich="scrumptious"> |
Ah, sorry I didn't read @SimoTod's comment - yeah, keeping boolean attribute detection if that's how vue does it applying this new removal behavior otherwise is great: remove on undefined, null, or falsey |
Yeah @adamwathan, like:
Curious how this would apply to Boolean attributes like "selected" or "disabled" though. |
Boolean attributes behave the same with or without a value, right? |
No that's the problem |
Yeah so we never had this, up until now Boolean attributes when enabled would be set to |
Ok cool so I'll keep the Boolean attribute logic and add a check for undefined, false in addition to null |
Yeah sorry, I meant having the attribute there with a value isn't a problem |
Ah yes @ryangjchandler although I can half see the "bug report" coming in for it 😂😂😂 |
True, but our version is actually following the standard.
If we decide to go with Adam's suggestion, I believe we need to use the empty string since there's no way to programmatically set an attribute without a value. I still slightly prefer to keep boolean attributes as they are but the other solution is fine if we are happy with that as long as it's consistent and not half-half like vue where true becomes the string 'true' but false remove the attribute (and 0, falsy value, adds the string '0'). :) Apart from that detail, PR looks good, well done 👍 |
Ok I'll fix it up so that:
This won't change too much and also gives us the option to remove Boolean attribute detection at some point (but not required) |
Add test for removal on regular attr add custom attribute test
fix test that was relying on 'false'default stringification improve existing attr removal tests
Ok changes are done @SimoTod @calebporzio @adamwathan thanks for the feedback One for @ryangjchandler re "boolean" modifier, we should also have a "string" modifier so that if someone (for whatever reason) wants to bind attributes to |
null
null
, undefined
, false
null
, undefined
, false
Yeah agree with that (I think) |
Thanks so much, @HugoDF and everyone else who weighed in! |
Thanks @calebporzio Did we end up going with |
Closes #485 #280
Todo:
Feedback requests:
''
now they'll be set to whatevervalue
is, which shouldn't make too much of a difference (eg. none of the tests are failing) cc @SimoTod @ryangjchandlerIf we go with Vue's approach we could ditch the whole "Boolean HTML attribute" detection