UIElement Docs Version 0.12.2

🔗 🏗️ Building Components

Create lightweight, self-contained Web Components with built-in reactivity. UIElement lets you define custom elements that manage state efficiently, update the DOM automatically, and enhance server-rendered pages without an SPA framework.

🔗 Defining a Component

UIElement builds on Web Components, extending HTMLElement to provide built-in state management and reactive updates.

A UIElement creates components using the component() function:

js

component("my-component", {}, () => [
	// Component setup
]);

Every UIElement component must be registered with a valid custom element tag name (two or more words joined with -) as the first parameter.

🔗 Using the Custom Element in HTML

Once registered, the component can be used like any native HTML element:

html

<my-component>Content goes here</my-component>

🔗 Component Lifecycle

UIElement manages the Web Component lifecycle from creation to removal. Here's what happens.

🔗 Component Creation

In the constructor() reactive properties are initialized. You pass a second argument to the component() function to defines initial values for component states.

js

component("my-component", {
	count: 0, // Initial value of "count" signal
	isEven: el => () => !(el.count % 2), // Computed signal based on "count" signal
	value: asInteger(5), // Parse "value" attribute as integer defaulting to 5
	name: consume("display-name") // Consume "display-name" signal from closest context provider
}, () => [
	// Component setup
]);

In this example you see all four ways to define a reactive property:

Note: Property initialization runs before the element is attached to the DOM. You can't access not yet defined properties or child elements here.

🔗 Mounted in the DOM

Runs when the component is added to the page (connectedCallback()). This is where you:

UIElement expects you to return an array of partially applied functions to be executed during the setup phase. The order doesn't matter, as each function targets a specific element or event. So feel free to organize your code in a way that makes sense to you.

Each of these functions will return a cleanup function that will be executed during the disconnectedCallback() lifecycle method.

js

component("my-component", {
	count: 0,
}, el => [
	emit("update-count", el.count), // Emit custom event
	provide("count"), // Provide context
	first(".increment",
		on("click", () => { el.count++ }) // Add click event listener
	),
	first(".count",
		setText("count") // Apply effect to update text
	)
]);

🔗 Removed from the DOM

Runs when the component is removed (disconnectedCallback()). UIElement will run all cleanup functions returned by event listeners and effects during the setup phase (connectedCallback()). This will unsubscribe all signals the component is subscribed to, so you don't need to worry about memory leaks.

If you added event listeners outside the scope of your component or subscribed manually to external APIs, you need to return a cleanup function:

js

component("my-component", {}, el => [
	// Setup logic
	() => {
		const observer = new IntersectionObserver(([entry]) => {
			// Do something
		})
		observer.observe(el);

		// Cleanup logic
		return () => observer.disconnect();
	}
]);

🔗 Observed Attributes

UIElement automatically observes and converts attributes with an associated parser function in the init block and updates them whenever the attribute changes (attributeChangedCallback()).

🔗 Managing State with Signals

UIElement manages state using signals, which are atomic reactive states that trigger updates when they change. We use regular properties to access or update them:

js

console.log("count" in el); // Check if the signal exists
console.log(el.count); // Read the signal value
el.count = 42; // Update the signal value

🔗 Characteristics and Special Values

Signals in UIElement are of a static type and non-nullable. This allows to simplify the logic as you will never have to check the type or perform null-checks.

Because of the non-nullable nature of signals in UIElement, we need two special values that can be assigned to any signal type:

🔗 Initializing State from Attributes

The standard way to set initial state in UIElement is via server-rendered attributes on the component that needs it. No props drilling as in other frameworks. UIElements provides some bundled attribute parsers to convert attribute values to the desired type. And you can also define your own custom parsers.

js

component("my-component", {
	count: asInteger(), // Bundled parser: Convert '42' -> 42
	date: (_, v) => new Date(v), // Custom parser: '2025-02-14' -> Date object
}, () => [
	// Component setup
]);

Careful: Attributes may not be present on the element or parsing to the desired type may fail. To ensure non-nullability of signals, UIElement falls back to neutral defaults:

  • "" (empty string) for string
  • 0 for number
  • {} (empty object) for objects of any kind

🔗 Bundled Attribute Parsers

Function Description
asBoolean Converts "true" / "false" to a boolean (true / false). Also treats empty attributes (checked) as true.
asInteger() Converts a numeric string (e.g., "42") to an integer (42).
asNumber() Converts a numeric string (e.g., "3.14") to a floating-point number (3.14).
asString() Returns the attribute value as a string (unchanged).
asEnum([...]) Ensures the string matches one of the allowed values. Example: asEnum(["small", "medium", "large"]). If the value is not in the list, it defaults to the first option.
asJSON({...}) Parses a JSON string (e.g., '["a", "b", "c"]') into an array or object. If invalid, returns the fallback object.

The pre-defined parsers asInteger(), asNumber() and asString() allow to set a custom fallback value as parameter.

The asEnum() parser requires an array of valid values, while the first will be the fallback value for invalid results.

The asJSON() parser requires a fallback object as parameter as {} probably won't match the type you're expecting.

🔗 Accessing Sub-elements

Before adding event listeners, applying effects, or passing states to sub-elements, you need to select them using a function for element selection:

Function Description
first(selector, ...fns) Selects the first matching element inside the component and applies the given setup functions.
all(selector, ...fns) Selects all matching elements inside the component and applies the given setup functions.

js

// Select the first ".increment" button and apply effects on it
first(".increment",
	// Fx functions
)

// Select all <button> elements and apply effects on them
all("button",
	// Fx functions
)

The first() function expects the matched element to be present at connection time. If not, it will silently ignore the call.

On the other hand, the all() function creates a dynamic array of elements that will be updated whenever the matching elements are added or removed from the component's DOM branch. UIElement will apply the given setup functions to added elements and run the cleanup functions on removed elements.

Tip: The all() function is more flexible but also more resource-intensive than first(). Prefer first() when targeting a single element known to be present at connection time.

🔗 Adding Event Listeners

Event listeners allow to respond to user interactions. They are the cause of changes in the component's state.

js

component("my-component", {
	active: 0,
	value: ''
}, (el) => [
	all("button",
		on("click", (e) => {
			// Set "active" signal to value of data-index attribute of button
			const index = parseInt(e.target.dataset['index'], 10);
			el.active = Number.isInteger(index) ? index : 0;
		})
	),
	first("input",
		on("change", (e) => {
			// Set "value" signal to value of input element
			el.value = e.target.value;
		})
	)
]

🔗 Synchronizing State with Effects

Effects automatically update the DOM when signals change, avoiding manual DOM manipulation.

🔗 Applying Effects

Apply one or multiple effects in the setup function (for component itself) or in element selector functions:

js

() => [
	// On the component itself
	setAttribute("open", "isOpen"), // Set "open" attribute according to "isOpen" signal

	// On first element matching ".count"
	first(".count",
		setText("count"), // Update text content according to "count" signal
		toggleClass("even", "isEven") // Toggle "even" class according to "isEven" signal
	)
];

Again, the order of effects is not important. Feel free to apply them in any order that suits your needs.

🔗 Bundled Effects

Function Description
setText() Updates text content with a string signal value (while preserving comment nodes).
setProperty() Updates a given property with any signal value.*
setAttribute() Updates a given attribute with a string signal value.
toggleAttribute() Toggles a given boolean attribute with a boolean signal value.
toggleClass() Toggles a given CSS class with a boolean signal value.
setStyle() Updates a given CSS property with a string signal value.
dangerouslySetInnerHTML() Sets HTML content with a string signal value.
insertOrRemoveElement() Inserts (positive integer) or removes (negative integer) elements with a number signal value.

Tip: TypeScript will check whether a value of a given type is assignable to a certain element type. You might have to pass a type hint for the queried element type. Prefer setProperty() over setAttribute() for increased type safety. Setting string attributes is possible for all elements, but will have an effect only on some.

🔗 Simplifying Effect Notation

For effects that take two arguments, the second argument can be omitted if the signal key matches the targeted property name, attribute, class, or style property.

When signal key matches property name:

js

first(".count", toggleClass("even"))

Here, toggleClass("even") automatically uses the "even" signal.

🔗 Using Local Signals for Protected State

Local signals are useful for storing state that should not be exposed to the outside world. They can be used to manage internal state within a component:

js

component("my-component", {}, () => {
	const count = state(0);
	const double = count.map(v => v * 2);
	return [
		first(".increment", on("click", () => {
			count.update(v => ++v);
		})),
		first(".count", setText(count)),
		first(".double", setText(double))
	];
});

Outside components cannot access the count or double signals.

🔗 Using Functions for Ad-hoc Derived State

Instead of a signal key or a local signal, you can pass a function that derives a value dynamically:

js

component("my-component", {
	count: 0
}, el => {
	const double = computed(() => el.count * 2);
	return [
		first(".count", toggleClass("even", () => !(el.count % 2)))),
		first(".double", setText(() => String(double.get())))
	];
});

When to use

  • Use a signal key or a local signal when the state is part of the component's public interface or internally reused.
  • Use a function to derive a value on the fly when it is needed only in this one place.

Ad-hoc derived state is more efficient than the overhead of a memoized computed signal for simple functions like converting to a string or boolean, formatting a value or performing a calculation.

🔗 Efficient & Fine-Grained Updates

Unlike some frameworks that re-render entire components, UIElement updates only what changes:

🔗 Single Component Example: MySlider

Bringing all of the above together, you are now ready to build your own components like this slider with prev / next buttons and dot indicators, demonstrating single-component reactivity.

Slides

Slide 1

Hello, World!

Slide 2

Number of rows:

3

Number of columns:

3

Row
Sum

Slide 3

Rate
Source Code

Loading...

🔗 Next Steps

Now that you understand the basics, explore: