Skip to content
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

Setting boolean, hyphenated attribute on a vue custom-element to null does not remove the attribute in the HTML document #12139

Closed
WIStudent opened this issue Oct 10, 2024 · 0 comments · Fixed by #12143
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. scope: custom elements

Comments

@WIStudent
Copy link

Vue version

3.5.11

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-bayiek?file=src%2FApp.vue

Steps to reproduce

Run the StackBlitz reproduction project.

It tries multiple ways to set boolean attributes on vue custom-elements and native custom-elements:

<vue-custom-element
  :foo="booleanValue"
  :foo-bar="booleanValue"
></vue-custom-element>

<native-custom-element
  :foo="booleanValue"
  :foo-bar="booleanValue"
></native-custom-element>

<vue-custom-element
  :foo="booleanValue ? '' : null"
  :foo-bar="booleanValue ? '' : null"
></vue-custom-element>

<native-custom-element
  :foo="booleanValue ? '' : null"
  :foo-bar="booleanValue ? '' : null"
></native-custom-element>

What is expected?

 <vue-custom-element
    :foo="booleanValue ? '' : null"
    :foo-bar="booleanValue ? '' : null"
  ></vue-custom-element>

should set or remove both non-hyphenated and hyphenated attributes in the HTML document accordingly.

What is actually happening?

 <vue-custom-element
    :foo="booleanValue ? '' : null"
    :foo-bar="booleanValue ? '' : null"
  ></vue-custom-element>

updates the non-hyphenated attribute correctly, but the hyphenated attribute is always set to foo-bar="" in the HTML document, which is interpreted as true.

<vue-custom-element
  :foo="booleanValue"
  :foo-bar="booleanValue"
></vue-custom-element>

sets both attributes correctly on vue custom-elements, but

<native-custom-element
  :foo="booleanValue"
  :foo-bar="booleanValue"
></native-custom-element>

sets both attributes to either "true" or "false" in the HTML document, which is always interpreted as true by the native custom-element because the attribute is present.

This is problematic because now we need to differentiate between custom-elements created with vue and native custom elements. According to Vue's documentation, attributes that are set to null or undefined should be removed in the HTML document. So I would expect

 <vue-custom-element
    :foo="booleanValue ? '' : null"
    :foo-bar="booleanValue ? '' : null"
  ></vue-custom-element>

to create

<vue-custom-element></vue-custom-element>

in the HTML document when booleanValue is false.

System Info

No response

Any additional comments?

No response

@edison1105 edison1105 added 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. scope: custom elements labels Oct 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. scope: custom elements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants