A symbolic name of the StylesGears.stylesAttachedCallback. Use it to declare the method.
class Foo extends HTMLElement {
[stylesAttachedCallback](): void {
// method body
}
}
A default version of stylesAdvanced decorator with automatically detected options from StylesDecoratorOptions.
A decorator to add styles to a web component. There are four ways to do it.
Using an URL
instance, you can define the path to the CSS file you want to
use for your web component. A <link>
tag is created in the ShadowRoot
and the file is loaded; when the loading is over, the styles are applied to
the component. All styles are scoped, so the result is equal to othe
approaches.
This approach brings some disadvantages:
Considering all the facts, it may be not a good idea to use this approach in production; on the other hand, it would be a great addition to the development process along with the native ES modules because it does not require any building process.
import styles, {stylesAttachedCallback} from '@corpuscule/styles';
@styles(new URL('styles.css', import.meta.url))
class StyledComponent extends HTMLElement {
public connectedCallback() {
this.shadowRoot!.innerHTML = '<div hidden class="foo">Bar</div>';
}
private [stylesAttachedCallback]() {
this.shadowRoot!.querySelector('.foo')!.hidden = false;
}
}
customElements.define('styled-component', StyledComponent);
This approach is used if the element is an URL
instance.
The Constructible Stylesheets proposal allows attaching styles directly to a
component ShadowRoot
as if a browser defined it. No intermediate <style>
tag is required.
There are two approaches this function supports.
If you provide a CSSStyleSheet
object as an element of the pathsOrStyles
array, it will just be adopted.
This approach will be used whether the adoptedStyleSheets
is enabled or not. Providing CSSStyleSheet
object is enough.
If you provide a string as an element of the pathsOrStyles
array, the new
CSSStyleSheet
object will be created and adopted.
This approach will be used if adoptedStyleSheets is enabled.
ShadyCSS
provides support for a Constructible Stylesheets proposal, so
this approach does not differ a lot from the previous one; just the polyfill
is used.
This approach is used if the element is a string and the shadyCSS is enabled.
If no approach described above works, the fallback to a <style>
tag in the
top of ShadowRoot
will be used.
import styles, {stylesAttachedCallback} from '@corpuscule/styles';
import css from './styles.css';
@styles(css)
class StyledComponent extends HTMLElement {
public connectedCallback() {
this.shadowRoot!.innerHTML = '<div class="foo">Bar</div>';
}
}
customElements.define('styled-component', StyledComponent);
an array with paths to the CSS files (as URL
instances), CSSStyleSheet
objects or strings with CSS code.
an object that contains options to change the default behavior of the decorator.
Generated using TypeDoc
This module provides tools to add styles to a web component.
import styles, {stylesAttachedCallback} from '@corpuscule/styles';