@efflore/ui-element
Version:
UIElement - minimal reactive framework based on Web Components
561 lines (510 loc) โข 20.2 kB
HTML
<html>
<head>
<title>UIElement Docs โ API Reference</title>
<link rel="stylesheet" href="assets/css/global.css">
<link rel="stylesheet" href="assets/css/okaidia.css">
<link rel="stylesheet" href="assets/css/components.css">
<script type="module" src="assets/js/main.min.js"></script>
</head>
<body>
<header class="content-grid">
<h1 class="content">UIElement Docs <small>Version 0.8.5</small></h1>
<nav class="breakout">
<ol>
<li>
<a href="index.html">
<span class="icon">๐</span>
<strong>Introduction</strong>
<small>Overview and key benefits of UIElement</small>
</a>
</li>
<li>
<a href="installation-setup.html">
<span class="icon">โ๏ธ</span>
<strong>Installation & Setup</strong>
<small>How to install and set up the library</small>
</a>
</li>
<li>
<a href="core-concepts.html">
<span class="icon">๐งฉ</span>
<strong>Core Concepts</strong>
<small>Learn about signals, state, and reactivity</small>
</a>
</li>
<li>
<a href="detailed-walkthrough.html">
<span class="icon">๐</span>
<strong>Detailed Walkthrough</strong>
<small>Step-by-step guide to creating components</small>
</a>
</li>
<li>
<a href="best-practices-patterns.html">
<span class="icon">๐ก</span>
<strong>Best Practices & Patterns</strong>
<small>Tips for effective and scalable usage</small>
</a>
</li>
<li>
<a href="advanced-topics.html">
<span class="icon">๐</span>
<strong>Advanced Topics</strong>
<small>Diving deeper into contexts and performance</small>
</a>
</li>
<li>
<a href="examples-recipes.html">
<span class="icon">๐งช</span>
<strong>Examples & Recipes</strong>
<small>Sample components and practical use cases</small>
</a>
</li>
<li>
<a href="troubleshooting-faqs.html">
<span class="icon">โ</span>
<strong>Troubleshooting & FAQs</strong>
<small>Common issues and frequently asked questions</small>
</a>
</li>
<li class="active">
<a href="api-reference.html">
<span class="icon">๐</span>
<strong>API Reference</strong>
<small>Detailed documentation of classes and methods</small>
</a>
</li>
<li>
<a href="contributing-development.html">
<span class="icon">๐ค</span>
<strong>Contributing & Development</strong>
<small>How to contribute and set up the dev environment</small>
</a>
</li>
<li>
<a href="changelog-versioning.html">
<span class="icon">๐</span>
<strong>Changelog & Versioning</strong>
<small>Track changes and understand versioning</small>
</a>
</li>
<li>
<a href="licensing-credits.html">
<span class="icon">โ๏ธ</span>
<strong>Licensing & Credits</strong>
<small>License details and acknowledgments</small>
</a>
</li>
</ol>
</nav>
</header>
<main>
<section class="hero">
<h1>๐ API Reference</h1>
<p class="lead">
Explore the full API of UIElement, the lightweight library for creating reactive Web Components. This guide provides a detailed overview of constructors, lifecycle callbacks, state management, attribute parsing, events, auto-effects, and helper functions to help you get the most out of your components.
</p>
</section>
<!-- Class Overview -->
<section>
<h2>UIElement Base Class</h2>
<h3>1.1 Constructor and Lifecycle Callbacks</h3>
<!-- Constructor -->
<details>
<summary><strong>constructor()</strong>: Initializes the component</summary>
<p>
The constructor initializes the instance of the `UIElement` class, extending `HTMLElement`. It is recommended to perform only simple initializations here, as the component is not yet connected to the DOM.
</p>
</details>
<!-- connectedCallback -->
<details>
<summary><strong>connectedCallback()</strong>: Called when the element is connected to the DOM</summary>
<p>
Invoked when the custom element is inserted into the DOM. Use this lifecycle method for setting up component state, bindings, and effects. This is where you access child elements, attributes, and context.
</p>
</details>
<!-- disconnectedCallback -->
<details>
<summary><strong>disconnectedCallback()</strong>: Called when the element is removed from the DOM</summary>
<p>
Triggered when the custom element is removed from the DOM. Clean up any event listeners or subscriptions added in `connectedCallback()` to avoid memory leaks.
</p>
</details>
<!-- attributeChangedCallback -->
<details>
<summary><strong>attributeChangedCallback(name, oldValue, newValue)</strong>: Responds to attribute changes</summary>
<p>
Invoked when one of the observed attributes is added, removed, or changed. Use this method to handle changes in attributes that should trigger corresponding changes in signals or the DOM.
</p>
<ul>
<li><code>name</code>: The name of the changed attribute.</li>
<li><code>oldValue</code>: The previous value of the attribute.</li>
<li><code>newValue</code>: The new value of the attribute.</li>
</ul>
</details>
<h3>1.2 Static Properties & Methods</h3>
<!-- observedAttributes -->
<details>
<summary><strong>observedAttributes</strong>: string[]</summary>
<p>
A static getter that returns an array of attribute names for the element to observe. When these attributes change, `attributeChangedCallback()` is called.
</p>
</details>
<!-- attributeMap -->
<details>
<summary><strong>attributeMap</strong>: { [key: string]: (value: string | null) => any }</summary>
<p>
An object defining how observed attributes are parsed into signals. The key is the attribute name, and the value is a parser function that transforms the attribute's value into an appropriate signal type.
</p>
</details>
<!-- providedContexts -->
<details>
<summary><strong>providedContexts</strong>: string[]</summary>
<p>
A static property that returns an array of context keys provided by the component. Contexts are state values shared between components in the same tree.
</p>
</details>
<!-- consumedContexts -->
<details>
<summary><strong>consumedContexts</strong>: string[]</summary>
<p>
A static property that returns an array of context keys consumed by the component. When the component is connected, it requests these contexts from its ancestors.
</p>
</details>
<!-- define -->
<details>
<summary><strong>define()</strong>: Registers the custom element</summary>
<p>
Registers the component with a custom tag name in the `CustomElementRegistry`. It ensures that the tag name is valid and not already defined.
</p>
<ul>
<li><code>define(tag: string)</code>: Registers the component with the provided tag name.</li>
</ul>
</details>
<h3>1.3 Instance Properties & Methods</h3>
<!-- self -->
<details>
<summary><strong>self</strong>: `HTMLElement`</summary>
<p>
Refers to the component instance itself. It is useful for adding effects or accessing the component as a target for state synchronization.
</p>
</details>
<!-- root -->
<details>
<summary><strong>root</strong>: `HTMLElement | ShadowRoot`</summary>
<p>
Refers to the root of the component, which could be either the Light DOM or the Shadow DOM, depending on how the component is set up.
</p>
</details>
<!-- has() -->
<details>
<summary><strong>has(key)</strong>: Checks if a signal exists</summary>
<p>
Checks whether a signal with the specified key exists within the component's state.
</p>
<ul>
<li><code>key</code>: The key of the signal to check for.</li>
<li>**Returns**: <code>boolean</code> - <code>true</code> if the signal exists, <code>false</code> otherwise.</li>
</ul>
</details>
<!-- get() -->
<details>
<summary><strong>get(key)</strong>: Retrieves the value of a signal</summary>
<p>
Retrieves the current value of a signal within the component.
</p>
<ul>
<li><code>key</code>: The key of the signal to retrieve.</li>
<li>**Returns**: The current value of the signal.</li>
</ul>
</details>
<!-- set() -->
<details>
<summary><strong>set(key, value)</strong>: Creates or updates a signal</summary>
<p>
Sets the value of a signal within the component. The value can either be a primitive or a function that receives the old value.
</p>
<ul>
<li><code>key</code>: The key of the signal to set or update.</li>
<li><code>value</code>: The new value for the signal, or a function that receives the old value.</li>
</ul>
</details>
<!-- delete() -->
<details>
<summary><strong>delete(key)</strong>: Deletes a signal</summary>
<p>
Removes the signal with the specified key from the component's state.
</p>
<ul>
<li><code>key</code>: The key of the signal to delete.</li>
</ul>
</details>
<!-- signal() -->
<details>
<summary><strong>signal(key, value)</strong>: Initializes a new signal</summary>
<p>
Initializes a new signal within the component, setting its initial value.
</p>
<ul>
<li><code>key</code>: The key for the new signal.</li>
<li><code>value</code>: The initial value for the signal.</li>
</ul>
</details>
<!-- first() -->
<details>
<summary><strong>first(selector)</strong>: Selects the first matching child element</summary>
<p>
Uses `querySelector()` to select the first child element that matches the provided selector.
</p>
<ul>
<li><code>selector</code>: The CSS selector to match elements.</li>
<li>**Returns**: The first matching child element.</li>
</ul>
</details>
<!-- all() -->
<details>
<summary><strong>all(selector)</strong>: Selects all matching child elements</summary>
<p>
Uses `querySelectorAll()` to select all child elements that match the provided selector.
</p>
<ul>
<li><code>selector</code>: The CSS selector to match elements.</li>
<li>**Returns**: A NodeList of all matching child elements.</li>
</ul>
</details>
</section>
<section>
<h2>2. Attribute Parser Functions</h2>
<!-- parse() -->
<details>
<summary><strong>parse(value, type)</strong>: Parses a value into a specific type</summary>
<p>
Converts the given string value into the appropriate type based on the provided parser function.
</p>
<ul>
<li><code>value</code>: The string to be parsed.</li>
<li><code>type</code>: The type to convert the string to (e.g., boolean, number).</li>
<li>**Returns**: The parsed value in the correct type.</li>
</ul>
</details>
<!-- asBoolean() -->
<details>
<summary><strong>asBoolean(value)</strong>: Parses a string into a boolean</summary>
<p>
Converts the string value to `true` if it is not `null`, otherwise `false`.
</p>
<ul>
<li><code>value</code>: The string value to be parsed.</li>
<li>**Returns**: `true` if the value is not `null`, `false` otherwise.</li>
</ul>
</details>
<!-- asInteger() -->
<details>
<summary><strong>asInteger(value)</strong>: Parses a string into an integer</summary>
<p>
Converts the string value into an integer. If the value is not a finite number, defaults to `0`.
</p>
<ul>
<li><code>value</code>: The string value to be parsed.</li>
<li>**Returns**: The parsed integer value or `0` if not finite.</li>
</ul>
</details>
<!-- asNumber() -->
<details>
<summary><strong>asNumber(value)</strong>: Parses a string into a number</summary>
<p>
Converts the string value into a number. If parsing fails, returns `NaN`.
</p>
<ul>
<li><code>value</code>: The string value to be parsed.</li>
<li>**Returns**: The parsed number or `NaN` if parsing fails.</li>
</ul>
</details>
<!-- asString() -->
<details>
<summary><strong>asString(value)</strong>: Returns the value as a string</summary>
<p>
Returns the value unchanged as a string.
</p>
<ul>
<li><code>value</code>: The string value to be returned.</li>
<li>**Returns**: The original string value.</li>
</ul>
</details>
<!-- asJSON() -->
<details>
<summary><strong>asJSON(value)</strong>: Parses a string into a JSON object</summary>
<p>
Converts the string value into a JavaScript object using `JSON.parse()`.
</p>
<ul>
<li><code>value</code>: The string value to be parsed as JSON.</li>
<li>**Returns**: The parsed JavaScript object.</li>
</ul>
</details>
</section>
<section>
<h2>3. State Passing Function</h2>
<!-- pass() -->
<details>
<summary><strong>pass(state)</strong>: Passes signals to another component</summary>
<p>
Transfers one or more signals from the current component to another target component. Useful for sharing state between parent and child components.
</p>
<ul>
<li><code>state</code>: An object containing key-value pairs of signal names and their respective values or functions.</li>
</ul>
</details>
</section>
<section>
<h2>4. Event Functions</h2>
<!-- on() -->
<details>
<summary><strong>on(event, callback)</strong>: Attaches an event listener</summary>
<p>
Adds an event listener to the current component or a target element. The callback function is executed when the event is triggered.
</p>
<ul>
<li><code>event</code>: The name of the event to listen for (e.g., `click`, `input`).</li>
<li><code>callback</code>: The function to execute when the event is triggered.</li>
</ul>
</details>
<!-- off() -->
<details>
<summary><strong>off(event, callback)</strong>: Removes an event listener</summary>
<p>
Removes an event listener from the current component or a target element.
</p>
<ul>
<li><code>event</code>: The name of the event to stop listening for.</li>
<li><code>callback</code>: The function that was originally passed to `on()`.</li>
</ul>
</details>
<!-- emit() -->
<details>
<summary><strong>emit(event, detail)</strong>: Dispatches a custom event</summary>
<p>
Creates and dispatches a custom event from the component. Useful for communicating state changes to parent components or triggering custom event handlers.
</p>
<ul>
<li><code>event</code>: The name of the event to dispatch.</li>
<li><code>detail</code>: An object containing data to pass with the event.</li>
</ul>
</details>
</section>
<section>
<h2>5. Auto-Effect Functions</h2>
<!-- setText() -->
<details>
<summary><strong>setText(signal)</strong>: Synchronizes text content with a signal</summary>
<p>
Binds a signal to the text content of an element within the component. Automatically updates the text when the signal changes.
</p>
<ul>
<li><code>signal</code>: The signal key or function to bind to the text content.</li>
</ul>
</details>
<!-- setProperty() -->
<details>
<summary><strong>setProperty(name, signal)</strong>: Synchronizes a property with a signal</summary>
<p>
Binds a signal to a property on the component, automatically updating the property whenever the signal changes.
</p>
<ul>
<li><code>name</code>: The name of the property to bind.</li>
<li><code>signal</code>: The signal key or function to bind to the property.</li>
</ul>
</details>
<!-- setAttribute() -->
<details>
<summary><strong>setAttribute(name, signal)</strong>: Synchronizes an attribute with a signal</summary>
<p>
Binds a signal to an attribute on the component, updating the attribute whenever the signal changes.
</p>
<ul>
<li><code>name</code>: The name of the attribute to bind.</li>
<li><code>signal</code>: The signal key or function to bind to the attribute.</li>
</ul>
</details>
<!-- toggleAttribute() -->
<details>
<summary><strong>toggleAttribute(name, signal)</strong>: Toggles an attribute based on a signal's truthiness</summary>
<p>
Toggles the presence of an attribute on the component based on the truthiness of a signal. Adds or removes the attribute whenever the signal changes.
</p>
<ul>
<li><code>name</code>: The name of the attribute to toggle.</li>
<li><code>signal</code>: The signal key or function to evaluate for toggling.</li>
</ul>
</details>
<!-- toggleClass() -->
<details>
<summary><strong>toggleClass(name, signal)</strong>: Toggles a class based on a signal's truthiness</summary>
<p>
Adds or removes a class from the component based on the truthiness of a signal.
</p>
<ul>
<li><code>name</code>: The class name to toggle.</li>
<li><code>signal</code>: The signal key or function to evaluate for toggling.</li>
</ul>
</details>
<!-- setStyle() -->
<details>
<summary><strong>setStyle(name, signal)</strong>: Synchronizes a CSS style with a signal</summary>
<p>
Binds a signal to a CSS style on the component, updating the style whenever the signal changes.
</p>
<ul>
<li><code>name</code>: The name of the CSS style property to bind.</li>
<li><code>signal</code>: The signal key or function to bind to the style.</li>
</ul>
</details>
</section>
<section>
<h2>6. Reactivity and Helper Functions</h2>
<!-- derive() -->
<details>
<summary><strong>derive(fn)</strong>: Creates a derived signal from other signals</summary>
<p>
Creates a signal that depends on one or more existing signals, recomputing its value whenever any of the dependencies change.
</p>
<ul>
<li><code>fn</code>: A function that returns the derived value, computed based on other signals.</li>
</ul>
</details>
<!-- effect() -->
<details>
<summary><strong>effect(fn)</strong>: Creates a reactive effect</summary>
<p>
Sets up a custom effect that runs whenever the signals used within the effect callback change. The effect can perform DOM manipulations, fetch data, or trigger other side effects.
</p>
<ul>
<li><code>fn</code>: The effect callback function that executes when any of its dependencies change.</li>
</ul>
</details>
<!-- maybe() -->
<details>
<summary><strong>maybe(condition, callback)</strong>: Conditionally runs a callback based on a signal</summary>
<p>
Conditionally executes a callback function based on the truthiness of a given signal or condition.
</p>
<ul>
<li><code>condition</code>: A signal or function whose truthiness determines whether the callback runs.</li>
<li><code>callback</code>: The function to execute if the condition is true.</li>
</ul>
</details>
<!-- log() -->
<details>
<summary><strong>log(signal)</strong>: Logs signal changes to the console</summary>
<p>
Monitors the value of a signal and logs its changes to the console. Useful for debugging signal state transitions within a component.
</p>
<ul>
<li><code>signal</code>: The key or function for the signal to log.</li>
</ul>
</details>
</section>
</main>
</body>
</html>