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

fix(custom-element): reflect prop default value #10537

Closed
wants to merge 7 commits into from

Conversation

edison1105
Copy link
Member

close #9006

Copy link

github-actions bot commented Mar 17, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 90.8 kB (+70 B) 34.5 kB (+29 B) 31.1 kB (+30 B)
vue.global.prod.js 148 kB (+70 B) 53.8 kB (+33 B) 48.1 kB (+35 B)

Usages

Name Size Gzip Brotli
createApp 50.8 kB (+25 B) 19.9 kB (+15 B) 18.1 kB (+14 B)
createSSRApp 54.2 kB (+25 B) 21.2 kB (+15 B) 19.3 kB (+14 B)
defineCustomElement 53.2 kB (+70 B) 20.6 kB (+26 B) 18.8 kB (+51 B)
overall 64.6 kB (+25 B) 24.9 kB (+16 B) 22.6 kB (+25 B)

@mgibas
Copy link

mgibas commented Mar 19, 2024

I’m pretty sure if you’ll set an attribute (setProp-> reflect attr) inside of the constructor, real browser will throw an exception :(

@edison1105 edison1105 marked this pull request as draft March 19, 2024 03:53
@edison1105 edison1105 marked this pull request as ready for review March 19, 2024 09:45
@edison1105 edison1105 changed the title fix(custom-element): handle props default value on initial render fix(custom-element): reflect prop default value Apr 21, 2024
@edison1105
Copy link
Member Author

/ecosystem-ci run

@vue-bot
Copy link
Contributor

vue-bot commented Apr 21, 2024

📝 Ran ecosystem CI: Open

suite result latest scheduled
language-tools success success
nuxt failure success
pinia success success
primevue success success
quasar success success
radix-vue success success
router success success
test-utils success success
vant success success
vite-plugin-vue success success
vitepress success success
vue-i18n success success
vue-macros success success
vuetify failure failure
vueuse success success
vue-simple-compiler success success

@skirtles-code
Copy link
Contributor

I think we may need to make a distinction between properties and attributes here.

The original issue was specifically about properties, not attributes. I think having default prop values reflected in the properties of the element make sense, but I'm not so sure about the attributes.

I've had a look through some resources about custom elements, but I can't find anything that describes what's expected for property defaults. There's a recommendation that calls to setAttribute should also set the property, and vice versa, but that isn't the scenario we're in here. A default is an implicit value, it isn't being set by the consumer of the element.

For native elements, a default value for a property doesn't lead to an attribute being set. For example, consider a <textarea>. The default value for rows is 2, which is reflected in the property, but there isn't a corresponding attribute for that default. Here's a Playground to illustrate that: example. I would expect the same from a custom element.

I suspect exposing the defaults as attributes could cause problems for SSR too. The hydration process won't be expecting those extra attributes to be present. I haven't actually verified whether this is problem in practice, as there's a lot of stuff that needs setting up to test it, but it isn't just Vue's hydration process that might be impacted, it could also impact hydration in other frameworks that are trying to use a Vue-based custom element.

@yyx990803
Copy link
Member

Superseded by 63689ed

@skirtles-code that seems to be a separate topic from this PR - the current behavior of Vue custom elements always reflect declared props as attribute if it has a value, so this PR (and by extension 63689ed) just aligns default values with that behavior.

@yyx990803 yyx990803 closed this Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default prop value is not reflected in custom element property
6 participants