[RFC] custom element support child component styles and style attribute settings #596
baiwusanyu-c
started this conversation in
RFC Discussions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
cesupport child component style tags and attribute settings core#7942Summary
This proposal suggests enhancing the capabilities of custom elements in Vue,
allowing child components of custom elements to attach styles and set attributes of the style tag.
We introduce an internal method called
useCEStyleAttrsto dynamically set attributes of the style tag.useCEStyleAttrsis a feature exclusive tocustom elements.v-bind() in cssto compile the runtime code.Basic example
foo.ce.vue
bar.ce.vue
Motivation
Most users do not manually define all child components as a
custom element, but use the child components as the child elements of acustom element.Currently
vuesupports this approach, but it does not handle the style of the child component. It also does not support setting attributes on thestyletag,such as
nonce, which does not comply with security standards.via
Detailed design
Style mounting of child components
We can add a context attribute
ceContextto the component instance, which contains methodsaddCEChildStyleandremoveCEChildStyles.Starting from the
custom elementcomponent, it is passed to the lower-level component. When the lower-level component is rendered,addCEChildStyleis called to pass the style of the component toshadowRootto create thestyletag.So users don't need to make many changes and can just write subcomponents as usual.
About hot update and style tag uninstallation of child components
In ordinary components,
stylewill eventually be compiled intocssfiles, but forcustom element,this will exist in the component code as the
stylesattribute of the component object.So for the child components of the
custom elementcomponent,the
styletag does not need to be uninstalled (normal components do not do this).But we still need the
removeCEChildStylesmethod, because when component hot updates occur we need to ensure that the oldstyleis correctly unloaded so that the newstyletag can be inserted.Regarding child components being used multiple times and repeated mounting of style tags
We add a new attribute
_childStylesSeton thecustom elementobject to record the styles of each of its child components._childStylesSetuses the component'scssas thekeyto avoid repeatedly mounting multiplestyletags when the same component is used multiple times incustom element.With one exception, when the component's
styleblock contains dynamic properties,the styles of each child component need to be mounted. Because the internal state of the component is independent,
the
styletag containing dynamic attributes is also independent.Dynamic style attribute support
This is a scenario that only exists under the
custom elementcomponent.In order to achieve style isolation, the
custom elementcomponent mounts the component'scssin theshadowRootas thestyletag,which prevents users from setting attributes on the
styletag.Consider using
useCEStyleAttrsto support this scenario. It is an internalapithat is invisible to users.Users only need to write property bindings on
stylejust like writing templates.Its basic implementation refers to
v-bind() in css.bar.vue
Compile the completed code
Drawbacks
stylesattribute, so all subcomponents must follow the suffix rules(but it can be improved through feat: added ce child component styles vitejs/vite-plugin-vue#284)teleport.teleportwill change the structure ofshadowRoot, so the style of the child component is still inshadowRootstyleattribute syntax is based onv-bind, but does not support binding objects like it does forstyleandclass, which requires further discussionstylehas attributes (scopedandlangwill be ignored),useCEStyleAttrsrelated code will be compiled and generated. But this problem may be able to be optimizedAlternatives
N/A
Adoption strategy
Awaiting further discussion
Beta Was this translation helpful? Give feedback.
All reactions