Vue RFC for boolean attribute shorthand

After migrating to Vue 3, I was disappointed to discover a breaking departure from Vue 2 attribute behavior.

Previously, false would result in the removal of an attribute:

Template
<m-dialog :open=”showDialog”>

DOM if true
<m-dialog open&…


This content originally appeared on DEV Community and was authored by Jordan Brennan

After migrating to Vue 3, I was disappointed to discover a breaking departure from Vue 2 attribute behavior.

Previously, false would result in the removal of an attribute:

Template
<m-dialog :open="showDialog">

DOM if true
<m-dialog open>

DOM if false
<m-dialog>

This was simple and intuitive, but no longer works in all cases.

Whether template expression or the output of a method or computed, the value used for setting an unknown boolean attribute has to be refactored to produce null instead of false:

<m-dialog :open="showDialog || null">

Vue has its reasons for the breaking change, which include the much-needed removal of some special behavior for three specific HTML attributes.

I have my reasons why I think Vue over-corrected.

Vue of course wins here, so that's what has led to a proposal to add a new ? shorthand binding option to get consistent boolean attribute behavior:

Template
<m-dialog ?open="showDialog">

DOM if true
<m-dialog open>

DOM if false
<m-dialog>

You can read the RFC here and if you like it you can upvote here.

For Vue apps with Custom Elements and/or custom HTML, this is especially important because migrating to Vue 3 may break these components.


This content originally appeared on DEV Community and was authored by Jordan Brennan


Print Share Comment Cite Upload Translate Updates
APA

Jordan Brennan | Sciencx (2021-11-05T21:07:17+00:00) Vue RFC for boolean attribute shorthand. Retrieved from https://www.scien.cx/2021/11/05/vue-rfc-for-boolean-attribute-shorthand/

MLA
" » Vue RFC for boolean attribute shorthand." Jordan Brennan | Sciencx - Friday November 5, 2021, https://www.scien.cx/2021/11/05/vue-rfc-for-boolean-attribute-shorthand/
HARVARD
Jordan Brennan | Sciencx Friday November 5, 2021 » Vue RFC for boolean attribute shorthand., viewed ,<https://www.scien.cx/2021/11/05/vue-rfc-for-boolean-attribute-shorthand/>
VANCOUVER
Jordan Brennan | Sciencx - » Vue RFC for boolean attribute shorthand. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/05/vue-rfc-for-boolean-attribute-shorthand/
CHICAGO
" » Vue RFC for boolean attribute shorthand." Jordan Brennan | Sciencx - Accessed . https://www.scien.cx/2021/11/05/vue-rfc-for-boolean-attribute-shorthand/
IEEE
" » Vue RFC for boolean attribute shorthand." Jordan Brennan | Sciencx [Online]. Available: https://www.scien.cx/2021/11/05/vue-rfc-for-boolean-attribute-shorthand/. [Accessed: ]
rf:citation
» Vue RFC for boolean attribute shorthand | Jordan Brennan | Sciencx | https://www.scien.cx/2021/11/05/vue-rfc-for-boolean-attribute-shorthand/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.