简体   繁体   English

StencilJS | 将宿主样式应用到组件

[英]StencilJS | Apply host styles to component

I am trying to apply the styles from the website where a stencilJS component is included ... but don't know how.我正在尝试应用包含 stencilJS 组件的网站的样式......但不知道如何。

import { Component } from '@stencil/core';

@Component({
  tag: 'menu-component',
  styleUrl: 'menu-component.css',
  shadow: true
})

export class MyComponent {

 render() {
    return (
      <div>
        <h1>Hello World</h1>
        <p id="red">This is JSX!</p>
      </div>
    );
  }
}

The component is included like this:该组件包含如下:

<link rel="stylesheet" href="css/main.css" type="text/css" />
<script src='https://unpkg.com/component@0.0.2/dist/mycomponent.js'></script>
<my-component></my-component>

In my main.css file I have something like this:在我的 main.css 文件中,我有这样的东西:

#red{
    color: red;
}

I would like the style to be applied to the element from the stencil component.我希望将样式应用于模板组件中的元素。 Is this possible?这可能吗?

You can use css variables for this.您可以为此使用css 变量 Look at the following code samples:查看以下代码示例:

index.html索引.html

<my-component style="--text-color:red;"></my-component>

my-component.css我的组件.css

#red {
    color: var(--text-color, black);
}

In the component's styling you assign a CSS variable as value to the text color of the class [id="red"] .在组件的样式中,您将一个 CSS 变量作为值分配给类[id="red"]的文本颜色。 In your application, you're now able to change the color by setting the value of the variable.在您的应用程序中,您现在可以通过设置变量的值来更改颜色。

Your component has a "Shadow DOM", which serves the purpose of encapsulating everything, including styles in a separate DOM, so it pretty much exists to prevent you from overriding it's styles.您的组件有一个“Shadow DOM”,用于封装所有内容,包括单独 DOM 中的样式,因此它几乎存在以防止您覆盖它的样式。

Previously there were some "shadow piercing" CSS directives like /deep/ and ::shadow , but they have been deprecated and are no longer functional.以前有一些“阴影穿透”CSS 指令,如/deep/::shadow ,但它们已被弃用并且不再起作用。

So that's pretty much how it's supposed to be.所以这就是它应该的样子。

Now for workarounds:现在的解决方法:

  • create a shared css file and include it in both your component and your application - or创建一个共享的 css 文件并将其包含在您的组件和应用程序中 - 或者
  • set the style using javascript from your host application using the shadowRoot property:使用 shadowRoot 属性从主机应用程序中使用 javascript 设置样式:
var div = document.querySelector('#comp').shadowRoot.querySelector('div#red');
div.style['color'] = 'red';

You should be able to use the :host psuedo selector in your stylesheet to apply host level styles:您应该能够在样式表中使用:host伪选择器来应用主机级别的样式:

:host {
    style: value
}

You could easily bring in @stencil.sass for your style sheets, link here: https://github.com/ionic-team/stencil-sass/blob/master/readme.md你可以很容易地为你的样式表引入@stencil.sass ,链接在这里: https : //github.com/ionic-team/stencil-sass/blob/master/readme.md

This will give you greater functionality with your styles in stencil.这将为您在模板中的样式提供更大的功能。

EDIT:编辑:

I misunderstood and now see you want to manipulate outside of the component.我误解了,现在看到您想在组件外部进行操作。 You could supply a <slot /> element in your web component and add specifically styled elements from outside of the web components DOM.您可以在 Web 组件中提供一个<slot />元素,并从 Web 组件 DOM 外部添加特定样式的元素。 Link here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot链接在这里: https : //developer.mozilla.org/en-US/docs/Web/HTML/Element/slot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM