Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

CSS and shadow-root with custom web components made from multiple component files #2336

Unanswered
jtlindsey asked this question in Q&A
Discussion options

How do i apply style.css to components shadow-root?

I'm working on adopting vue incrementally into a legacy non-vue application via custom web components. With large components I prefer to extract the logic into multiple files instead of one large .vue file. The problem I'm running into is some of my styles from my style.css aren't being applied to the shadow-root.

Here is a minimum reproduction based on modified default vite/vue scaffold. The structure of the web component is made up like this:

# DemoTest.ce.vue
...
<template>
 <div id="app">
 <VueVite />
 <HelloWorld msg="Vite + Vue" />
 </div>
</template>

-- VueVite.vue # style.css not applied even if i rename to VueVite.ce.vue
-- HelloWorld.vue # style.css not applied even if i rename to HelloWorld.ce.vue

I export DemoTest.ce.vue for consumption as <demo-hello></demo-hello>. The idea is that once the legacy application is ~95% vue, we'll make the jump to full vue and instead of refactoring DemoTest.ce.vue, HelloWorld.vue, and VueVite.vue, I'll only need to refactor/rename DemoTest.ce.vue (and main.ts).

The only work around I can think of is to rename all .vue sub components into .ce.vue, export them all as individual custom web components and apply styles to each of them via <style></style> in each component file. There are a few issues with that including, still not knowing how to use style.css with sub-components, and will make consuming the custom components in another application more verbose/complicated. i.e. the html for minimum example would be:

<body>
 <vue-vite></vue-vite>
 <hello-world></hello-world>
 ... or 3, 4, etc more tags that might make up a large component
</body>

instead of :

<body>
 <demo-hello></demo-hello>
</body>

Is there a better way or work around where I can have my style.css file apply styles to the custom web component and the components it consumes?

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /