/*{ "order": 6, "description": "The Component base class for building custom elements with tosijs: light-DOM by default, automatic slot composition, initAttributes, form association." }*/ /*# # Web-Components **tosijs** provides the abstract `Component` class to make defining custom-elements easier. - `Component` leverages the [elements](/elements/) proxy and [css](/css/) to make defining elements very efficient. - `Component` makes it easy to create custom-elements with no shadowDOM but with slotting behavior so your elements are lighter weight and easier to style. - It solves friction points like allowing element tagNames to be changed on-the-fly to avoid registry clashes. - It allows you to deploy `Component` classes as zero dependency [blueprints](/blueprint-loader/) functions. ## Component To define a custom-element you can subclass `Component`, simply add the properties and methods you want, with some help from `Component` itself, and then simply export your new class's `elementCreator()` which is a function that defines your new component's element and produces instances of it as needed. ``` import {Component} from 'tosijs' class ToolBar extends Component { static preferredTagName = 'tool-bar' static shadowStyleSpec = { ':host': { display: 'flex', gap: '10px', }, } } export const toolBar = ToolBar.elementCreator() ``` > **Note**: Custom elements default to `display: inline`, which often causes them to > appear dimensionless. Unless you want this (e.g., for content-holder elements), > set an explicit `display` value (e.g., `block`, `inline-block`, `flex`) in your > `:host` styles. This component is just a structural element. By default a `Component` subclass will comprise itself and a ``. You can change this by giving your subclass its own `content` template. `static preferredTagName` sets the desired tag name for the custom element. If omitted, it is derived from the class name (e.g. `ToolBar` → `tool-bar`), but this does not survive minification. `elementCreator()` returns an `ElementCreator` function that creates instances of the element. See [elements](/elements/) for more information on `ElementCreator` functions. ### Component properties #### content: Element | Element[] | () => Element | () => Element[] | null Here's a simple example of a custom-element that simply produces a `