import { c, html } from"atomico";functioncomponent() {returnhtml`<host shadowDom> ...my content </host>`;}classVanillaElementextendsHTMLElement {constructor() {super();console.log("create"); }connectedCallback() {console.log("mount"); }disconnectedCallback() {console.log("mount"); }attributeChangedCallback() {console.log("my-attr update"); }staticgetobservedAttributes() {return ["my-attr"]; }// ⚠️ not native but valid within Atomico.// this is just an example the ideal is to // have a shared reference of the CSSStyleSheetstaticgetstyles(){constsheet=newCSSStyleSheet();sheet.replace('a { color: blue; }');return sheet; }}constComponent=c( component, VanillaElement );
component: function that declares the webcomponent for Atomico.
VanillaElement: class that will be extended by Atomico to create Component, Atomico will not break the life cycle of the component, allowing them to interact freely.
Classes internal to Atomico
classes produced by the Atomico function c, these can be extended between components, example:
Consider the following effects when using this inheritance model:
The render will be rewritten.
The props are inherited, Atomico will reuse the previously declared props.
Styles are inherited. Atomico will merge the stylesheets.
Inheritance outside of Atomico
The c function creates an optimized standard custom element, which can be extended to modify its behavior, be:
Adding methods.
Creating or replacing style sheets.
Creando nuevas propiedades.
Suppose we have a MyButton product of the function c, we can extend this component to modify its appearance without the need to completely rewrite it, example:
import { css } from"atomico";import { MyButton } from"./src/my-button/my-button.js";classMyNewButtonextendsMyButton {static styles = [/** * super.styles allows to load the previous styles * this static property is created internally by atomico */ super.styles,/** * In the following way we are associated with a new * styleSheet to our customElement */css` :host { --button-background: teal; } `, ];}
The benefit of this inheritance is to simplify the modification of the appearance of a component created with Atomico, since it avoids its rewriting.