[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
-
ce
support 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
useCEStyleAttrs
to dynamically set attributes of the style tag.useCEStyleAttrs
is a feature exclusive tocustom elements
.v-bind() in css
to 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
vue
supports this approach, but it does not handle the style of the child component. It also does not support setting attributes on thestyle
tag,such as
nonce
, which does not comply with security standards.via
Detailed design
Style mounting of child components
We can add a context attribute
ceContext
to the component instance, which contains methodsaddCEChildStyle
andremoveCEChildStyles
.Starting from the
custom element
component, it is passed to the lower-level component. When the lower-level component is rendered,addCEChildStyle
is called to pass the style of the component toshadowRoot
to create thestyle
tag.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,
style
will eventually be compiled intocss
files, but forcustom element
,this will exist in the component code as the
styles
attribute of the component object.So for the child components of the
custom element
component,the
style
tag does not need to be uninstalled (normal components do not do this).But we still need the
removeCEChildStyles
method, because when component hot updates occur we need to ensure that the oldstyle
is correctly unloaded so that the newstyle
tag can be inserted.Regarding child components being used multiple times and repeated mounting of style tags
We add a new attribute
_childStylesSet
on thecustom element
object to record the styles of each of its child components._childStylesSet
uses the component'scss
as thekey
to avoid repeatedly mounting multiplestyle
tags when the same component is used multiple times incustom element
.With one exception, when the component's
style
block contains dynamic properties,the styles of each child component need to be mounted. Because the internal state of the component is independent,
the
style
tag containing dynamic attributes is also independent.Dynamic style attribute support
This is a scenario that only exists under the
custom element
component.In order to achieve style isolation, the
custom element
component mounts the component'scss
in theshadowRoot
as thestyle
tag,which prevents users from setting attributes on the
style
tag.Consider using
useCEStyleAttrs
to support this scenario. It is an internalapi
that is invisible to users.Users only need to write property bindings on
style
just like writing templates.Its basic implementation refers to
v-bind() in css
.bar.vue
Compile the completed code
Drawbacks
styles
attribute, 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
.teleport
will change the structure ofshadowRoot
, so the style of the child component is still inshadowRoot
style
attribute syntax is based onv-bind
, but does not support binding objects like it does forstyle
andclass
, which requires further discussionstyle
has attributes (scoped
andlang
will be ignored),useCEStyleAttrs
related 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