Created by @uppercod with reference to @jouni's work.
This webcomponent is an alternative to ::part and allows to inject CSS outside the shadowDOM into the shadowDOM of the parent who uses this webcomponent, consider:
- The CSS must be inside an at-rule rule
@media <tagName | namespace>. - The reading is static, if you want to execute dynamic effects, declare customProperty inside the
at-mediareferenced by this webcomponent. - Safe, as the rules are injected via
insertRule. - This component must be nested within shadowDOM depth 0, example
#shadow-root > inject-style. - The search for css rules is limited by the
hostjust like::part.
<style>
@media my-example {
button {
background: black;
border-radius: 100vh;
padding: 0.5rem 1rem;
color: white;
border: none;
}
}
</style>
<my-example>
#shadow-root
<!-- inject-style will inject the css into `@media my-example` -->
<inject-style></inject-style>
</my-example>the css inside the at-rule @media my-example will exist inside my-example only if they share the tagName or a custom namespace.
npm i @atomico/inject-styleimport { c } from "atomico";
import "@atomico/inject-style";
function component() {
return (
<host shadowDom>
<inject-style></inject-style>
...DOM
</host>
);
}
customElements.define("my-component", c(component));Remember in Atomico you can also use Constructors to instantiate the webcomponent, example <InheritStyle/>.
import "@atomico/inject-style";
class Component extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
}
connectedCallback() {
this.shadowRoot.innerHTML = `
<inject-style></inject-style>
...DOM
`;
}
}| Property | Type | Description |
|---|---|---|
| namespace | String | Permite añadir un namespace adicional a la catura de at-rule |