-
Notifications
You must be signed in to change notification settings - Fork 104
How to define style within a web component? #12
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
Comments
Playing around with this awesome wrapper, I'm finding it impossible to attach any style to my registered vue converted webcomponent.
How can I define ANY style on my shadow DOM (from FILE 2) with this wrapper? File 1:
File 2:
|
I found a really hacky solution. Reasoning: The reasoning for this solution is styles are handled by webpack. They are not accessible on the Vue element itself, thus it can't be solved with this wrapper correct? Implementation Create a custom component to render/import style tags since the compiler won't let us File 1:
File two is our
This will import our styles into our webcomponent, making them scoped. |
I slightly simplified @gnardecky's custom style component like so:
|
I was able to enable shadow mode with webpaack v4 and vue-loader 15 (without vue-cli v3), docs // webpack.config.js
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
exclude: path.resolve(__dirname, 'node_modules'),
options: {
shadowMode: true // vue-loader v15.0.9+
}
},
{
test: /\.css$/,
use: [
{
loader: 'vue-style-loader',
options: {
shadowMode: true
}
},
'css-loader',
// don't use `style-loader`
],
},
]
] Now i can use vue SFC as normal <template>
<div class="card">
<!-- add html here -->
</div>
</template>
<script>
// add script
</script>
<style>
/* no need to use CSS Modules or Scoped CSS */
@import "~bootstrap/dist/css/bootstrap.css";
.card {
background: red
}
</style> You also need to load import //app.js
import CustomElement from './CustomElement.vue?shadow' Now vue-loader injects |
@ankurk91 i tried the solution you told but still goint to head? can you provide more details please? |
@ankurk91 yay, i got it... now wondering how to make it with <style lang="scss"> |
@victorhramos hi, can you share some details why it wasn't working for you and now it works? |
@maciekkus here one working example https://github.com/victorhramos/vuew-shadowdom |
@victorhramos thanks. |
is there any good workaround to use properly scoped scss? |
Good initiative this project, I was unfortunately unable to use with bootstrap-vue :( |
I wonder like @victorhramos why css dosen’t work in case <style lang=“scss”>. ↓my code.
|
Also having trouble extracting Further debugging shows that the styles from stand-alone |
@ankurk91 Do you have any reference to the |
The undocumented |
Thanks to @andreasvirkus for offering the very counterintuitive solution of moving to external style files. I was tearing my hair our trying to figure out what I was doing wrong and switching to standalone files solved the problem. Does anyone understand why this works? Seems like the source of the scss/css shouldn't matter to webpack... FYI I'm using scss without issue using CLI 3. |
No, I've yet to solve this properly :/ |
Any progress on this one? Seem to be an issue for more people.. |
Did you remark the the typo in the "alias-import" 😉 |
This problem made me switch to https://github.com/karol-f/vue-custom-element |
@FranzSkuffka Are you adding css to the component via the "shadowCss" option? Or does it work for you to use the <style> tag in the SFC? |
@FranzSkuffka never mind, vue-custom-element is working for me too :) |
How did you add the styles using |
After I adding following code
After I removing the |
sure just write <style lang="scss">
@import "path/to/css";
</style> |
i found the :root{--xxx} in web components are not working? are there any one resolve this problem? |
When I'm trying to publish multiple web components using the vue cli command vue-cli-service build --target wc --inline-vue --name my-element src/*.vue. it is building perfectly fine but external dependencies like CSS and javascript file is not importing in the build. Can anyone help me with how to build web components with all external dependencies? import all CSS from node_modules as a dependency. |
Not working |
I have tried this method as well but still not working. could you please provide me solution with third-party library as well. |
it is not working. Do you know if you have working repo? |
@rajeevverma076 We are working on a solution that might solve your issue, but currently there is no support for Vue CLI |
I tried to use this plugin to write a UI library, but I found that the styles defined in the component did not take effect, and the template tag in the .vue file could not write style in it, and how to handle it more elegantly.
The text was updated successfully, but these errors were encountered: