signalizejs/component
Create reusable web components with minimum effort.
Installation
Required import map dependencies:
const { component } = await signalize.resolve('component');
API
component
This function is used to define a new web component.
To define a web component, you only need to call the component function with the name of the element. All other arguments are optional.
The simplest definition of a web component can look like this:
<script>
component('my-element');
</script>
<my-element />
This registers my-element as a custom element.
Web Component Options
The setup function receives the following arguments:
$el: The current component element.$: The current signalize instance.$refs: Returns an element or array of elements based on therefattribute. For example,$refs.fieldwould return the element withref="field".$parent: Returns the parent component. You can call it with$parent()or specify the parent’s name like$parent('custom-parent').$adopted: An asynchronous function called in the native web component’sadoptedCallbackhook.$connected: An asynchronous function called in the native web component’sconnectedCallbackhook.$disconnected: An asynchronous function called in the native web component’sdisconnectedCallbackhook.
component('my-element', {
/*
Props as an Array
All props are passed into `$data` and become publicly accessible.
Each property becomes a Signal.
Configured property names are included in the `observedAttributes` field.
This means that when a property attribute's value changes on an element,
the new value is automatically passed to the corresponding Signal.
For example, a prop named `"some-prop"` will be converted to a camelCase variable named `someProp`.
*/
props: ['some-prop'],
props: {
// key: default value
someProp: ''
},
shadow: {
// A native shadowRoot options
// https://developer.mozilla.org/en-US/docs/Web/API/Web_components#api.shadowroot
},
// Right before component constructor exits
setup({ $props, $el, $adopted, $connected, $disconnected }) {
const { someProp } = $props;
$adopted(() => {});
$connected(() => {});
$disconnected(() => {});
return { /* Public data added to prototype */ }
},
})
__CODE__