Solarite

Solarite makes native web components fast to update, with no build step and no signals. You write plain JavaScript and call render() when your data changes; Solarite then patches only the DOM that actually changed. It's tiny (12KB min+gzip) and runs straight in the browser as a standard ES module.

Key Features

Installation

Quick Start

Import the module directly from a CDN:

Or install via NPM:

Development Tips

For the best development experience, use an IDE like WebStorm or VS Code with a Lit-html extension for syntax highlighting of HTML template strings. Solarite's included Solarite.d.ts provides auto-completion and type checking for all core APIs.

Performance

Solarite is the faster than almost every well known framework, according to the js-framework-benchmark. It's score of 1.10 indicates being 10% slower than vanilla js (hand-coding everything). Benchmarks were run on a Ryzen 7 3700X with 16GB ram on Kubuntu 26.04.

js-framework-benchmark

Core Concepts

Web Components

Solarite enhances web components with efficient and minimal re-rendering of elements when your data changes. This approach minimizes DOM operations and improves performance.

In this minimal example, we create a class called MyComponent which extends from HTMLElement (the standard way to create web components). We add a render() method to define its HTML content, and call it from the constructor when a new instance is created.

Important: All browsers require web component tag names to contain at least one dash (e.g., my-component, not mycomponent). This is a standard requirement for custom elements.

We can alternatively instantiate the element directly from html:

Note that we call .define() to register the <my-component> tag name with the browser. Internally, this calls the browser's customElements.define() function. Browsers can only use web components that have been defined.

If you don't call .define() and instead create an instance via new, the tag is defined automatically using the class name converted to kebab-case. But this auto-define can't happen if the browser first meets the element as a tag name in html, so in that case you must call .define() yourself.

Since these are just regular web components, they can define the connectedCallback() and disconnectedCallback() methods that will be called when they're added and removed from the DOM, respectively.

Rendering

How Rendering Works

Use the h function as a tagged template literal to convert HTML strings and embedded expressions into a Solarite Template. This data structure efficiently stores processed HTML and expressions for optimal rendering.

When you call h(this) followed by a template string, it renders that Template as the element's attributes and children. This is similar to assigning to the browser's built-in this.outerHTML property, but with a crucial difference: Solarite's updates are much faster because only the changed elements are replaced, not all nodes.

When an element is first added to the DOM, the render() function is called automatically. But only if it hasn't already been previously called manually.

Manual Rendering

Unlike many frameworks, Solarite does not automatically re-render when data changes. Instead, you must call the render() function manually when you want to update the DOM. This is a deliberate design choice that:

  1. Gives you complete control over when rendering occurs. You can update data without triggering a render.

  2. Reduces unexpected side effects, making behavior more predictable.

This approach is particularly useful in performance-critical applications where you need precise control over when DOM updates occur.

Wrapping the web component's html in its tag name is optional. But without it you then must set any attributes on your web component manually:

If you do wrap the web component's html in its tag, that tag name must exactly match the tag name passed to customElements.define().

SVG

Use the svg tagged-template prefix for SVG markup. The resulting template can be embedded in a normal h template. Use svg for dynamically generated SVG child fragments too, such as shapes created in a loop.

By default, expressions render as text. So raw SVG markup in a string expression is escaped and displayed as text. Put SVG markup in an svg tagged template instead.

For reusable SVG icons, store the whole icon as an svg template:

These types of values can be used in expressions within h tagged template literals:

  1. strings and numbers.

  2. boolean true, which will be rendered as 'true'

  3. false, null, and undefined, which will be rendered as empty string.

  4. Solarite Templates, which can be created by h-tagged template literals.

  5. DOM Nodes, including other web components.

  6. Arrays of any of the above.

  7. Functions that return any of the above.

Attributes

Dynamic attributes can be specified by inserting expressions inside a tag. An expression can be part or all of an attribute value, or a string specifying multiple whole attributes. For example:

Expressions can also toggle the presence of an attribute. In the last div above, if isEditable is false, null, or undefined, the contenteditable attribute will be removed.

You can also specify multiple attributes at once using an object, where the keys are attribute names and the values are attribute values:

In the example above, all attributes from the this.attrs object are applied to the button element. If a value is undefined, false, or null, the attribute will be skipped or removed if it was previously set.

Note that attributes can also be assigned to the root element, such as class="big" on the <object-attribute-demo> tag above.

Id's

Any element in the html with an id or data-id attribute is automatically bound to a property with the same name on the class instance. But this only happens after render() is first called:

Don't use an id that collides with a built-in HTMLElement property (like title or style), a class method, or a field that already holds a non-element value. Solarite throws rather than silently clobbering it.

Events

To capture events, set an event attribute like onclick to a function. Alternatively, use an array where the first item is the function and subsequent items are its arguments.

Event binding with an array containing a function and its arguments is slightly faster, since the function isn't recreated when render() is called, and it doesn't need to be unbound and rebound. But the performance difference is usually negligible.

Make sure to put your events inside ${...} expressions, because classic events can't reference variables in the current scope.

Event Delegation

For components that render many event handlers, like a data grid with buttons on every row, pass eventDelegation: true as a render option:

Your templates don't change at all. Internally, instead of calling addEventListener on every element, Solarite listens once per event type at the document level and finds handlers by walking up from the event target. Creating 10,000 rows with two handlers each then costs zero listener registrations, which makes large lists noticeably faster to create and clear, especially on phones.

Only events that bubble are delegated (click, input, keydown, and the like); focus, blur, scroll and other non-bubbling events automatically keep regular listeners. Pass an array like eventDelegation: ['click', 'input'] to delegate only specific events.

Two caveats, both rare in practice: delegated handlers run when the event reaches the document, so a manually added addEventListener on an ancestor element fires before them rather than after, and stopPropagation() called from such a manual listener prevents delegated handlers from running. Handlers see the correct event.currentTarget either way.

Two-Way Binding

Two-way binding connects your component's data to form elements, keeping them in sync automatically.

Basic Two-Way Binding

Form elements update properties when an event like oninput is assigned a function to handle the change:

<input>, <select>, <textarea>, and elements with the contenteditable attribute can all use the value attribute to set their value on render. Likewise so can any custom web component that defines a value property.

Shorthand Two-Way Binding

Solarite also provides a shortcut for two-way binding using array syntax: value=${[this, 'count']}:

  1. When render() is called, the input's value is set to this.count

  2. When a user types in the input, an input event listener updates this.count with the new value.

Optionally add an oninput=${this.render} attribute to trigger re-rendering when the value changes.

Form Element Types

When a bound value is read back from an element, Solarite converts it to the most appropriate JavaScript type. Bind to the value attribute for most elements, and to the checked attribute for checkboxes and radio buttons.

ElementBind toProperty type read back
<input> (text, password, email, etc.)valueString
<input type="checkbox">checkedBoolean
<input type="radio">checkedString (the selected radio's value)
<input type="number">, type="range"valueNumber (NaN when empty)
<input type="date">, time, datetime-localvalueDate object (null when empty)
<input type="file">valueArray of File objects
<select>valueString
<select multiple>valueArray of Strings
<textarea>valueString
contenteditable elementvalueString (the element's innerHTML)
Custom component with a value propertyvalueWhatever type the component's value holds

For a radio group, put the same checked=${[this, 'prop']} binding on every radio in the group. Each radio is checked when its value matches the bound property. Clicking a radio writes its value back to the property:

For a <select multiple>, bind an array. Each option whose value is in the array is selected, and the selected options' values are written back as an array of strings:

Loops

The most common way to render lists is with JavaScript's Array.map() function:

Efficient List Updates

When you push a new plant and call render(), Solarite appends a single <span> instead of rebuilding the whole row. Only the changed elements are touched.

Important: Nested template literals must also have the h prefix, or they'll be rendered as escaped text. Try removing the h before `<span ...>` to see what happens.

Memoized List Items

Normally each list item runs its .map() callback to build a template, and then Solarite compares that template against the live DOM to find what changed. For very long lists, h.memo() skips both steps for items that haven't changed, similar to Vue's v-memo or Lit's guard(). Give it the loop item, the values its html depends on, and a function that builds its template:

The second argument is the list of values the item's html depends on. Pass one value, or an array of them. h.memo() compares these values against the previous render with === (and shallowly, if it's an array). As long as they all stay the same, h.memo() returns the item's previous template without calling your build function, and the list diff reuses that item's DOM untouched. As soon as one of those values changes, your build function runs again and the item re-renders normally. The same item object must not appear twice in one list.

Keyed Lists

By default, Solarite matches list items to existing DOM nodes by position, rewriting each changed row in place. That's the fastest option when rows hold no state of their own. But when rows contain form inputs, focus, animations, or components with internal state, add a key attribute so DOM nodes follow their data instead:

With keys, reordering the rows array moves the existing DOM nodes (using the fewest possible moves), removing a row removes exactly its node, and rows with new keys always get newly created nodes. Anything the user typed into a row's <input> travels with the row.

Rules for key:

h.memo() and keys compose: memo skips rebuilding unchanged rows' templates, while keys control node identity and movement.

Scoped Styles

Solarite provides a powerful scoped styling system that allows components to define styles that apply only to themselves and their children. Unlike Shadow DOM, this allows styles to be inherited from the rest of the document.

When you include a <style> element in your component template, Solarite automatically scopes those styles to your component instance. This prevents style leakage and conflicts with other elements.

Internally, scoped styles become:

  1. A data-style attribute on the root element, with a number that increments for each instance of the component.

  2. :host selectors rewritten to the tag name plus that identifier: fancy-text[data-style="1"].

A style tag with the global attribute defines the style only once in the document head, instead of for every instance of a component. This improves rendering performance with many instances. Unlike regular styles, global styles cannot have expressions within them.

Slots

Slots let you pass HTML content from a parent into specific locations within a child component. This is useful for reusable layouts like cards, modals, or tabs.

Basic Slots

Use the <slot> element to define where children should be rendered:

Named Slots

To use multiple slots, give them a name attribute. Assign children to these slots using the slot attribute:

Elements without a slot attribute go into the unnamed (default) slot. Multiple elements can be assigned to the same slot; they appear in the order they are provided.

Slotless Components

If a component has no <slot> elements, any provided children are appended to the end of the component by default.

 

Child Components

Solarite makes it easy to compose complex UIs by combining smaller, reusable components.

Passing Data to Child Components

When one web component is embedded within the html of another, its attributes are automatically passed as arguments to the constructor:

Attribute Name Conversion

Since HTML attributes are case-insensitive, Solarite automatically converts dash-case (kebab-case) attribute names to camelCase when passing them to component constructors. For example, the font-size attribute becomes the fontSize property of the first argument passed to the constructor and to the render() function.

Component Rendering Hierarchy

When a parent component renders:

  1. Its render() function executes, typically calling h() to update itself and its children.

  2. For each child web component (whether a Solarite component or otherwise), h() then calls that child's render() method, if it exists.

  3. The child receives its attributes as an object (first argument) and a changed boolean (second argument).

  4. The child then decides whether to call its own h() function to update.

In the example above, creating <notes-item> via new instead of its tag name is discouraged, as it would cause the component to be recreated on every render:

Functions

h()

The h() function handles template creation, DOM updates, and element instantiation:

toEl()

The toEl() function converts a string or a template created via the h function into a DOM element. It enforces these rules:

 

Advanced Techniques

Extending Native HTML Elements

HTML has strict rules about which elements can be children of certain container elements. For example, a <table> can only have specific children like <tr>, <thead>, etc.

If you want to create a custom component to use in these restricted contexts (like a custom <tr> element), you can extend the appropriate native HTML element instead of the generic HTMLElement.

To do this, pass {extends: 'tr'} as the third argument to customElements.define. This is standard, vanilla JavaScript and is not specific to Solarite.

Manual DOM Operations

While Solarite handles most updates automatically, you can perform manual DOM operations in these scenarios:

  1. Static Attributes: Modify attributes not created by expressions.

  2. Static Nodes: Add or remove nodes not created by expressions, and not directly adjacent to node-creating expressions.

  3. Temporary Changes: Modify any node if you restore its original state before the next render().

This example demonstrates these rules:

Non-Component Elements

The toEl() function (discussed above) can also be given an object with a render() method to toEl(). Properties and methods of the object become bound to the resulting element.

If you want multiple instances of such an element, the code above can be wrapped in a function:

This is an experimental feature and is likely to change in the future.

How Solarite Works

Understanding how Solarite works internally can help you write more efficient components and debug issues more effectively.

Efficient Rendering Algorithm

Consider this example where we're rendering a list of tasks:

When you call render(), Solarite performs these steps:

  1. Template Parsing: The h() function pairs the template literal's static html with its ${...} expression values in a lightweight Template object. The static html is parsed only once, no matter how many items or renders use it: each unique template gets a cached "Shell" of expression-free DOM nodes, plus precomputed paths to where the expressions belong. Whitespace-only text between table tags is dropped since browsers never render it.

  2. Instantiation: New elements are created by cloning the Shell's nodes, then resolving all expression locations in the clone with a single precomputed resolve program that visits each target node once.

  3. Positional Diffing: On re-render, list items are compared positionally against the previous render's items. Expression values are compared by identity (===), so unchanged items are skipped with no hashing or string building. Items created from the same template html are rewritten in place, updating only the expressions whose values changed.

  4. Minimal DOM Updates: A lone primitive expression renders as a bare text node and updates via nodeValue, with no wrapper objects. Attributes are written only when their value changes. Event handlers register one listener per element; re-renders just swap the function it calls. Removed list items are pooled and reused by later renders instead of being rebuilt.

DOM Diffing

List reconciliation uses a positional two-pointer diff: matching prefix and suffix items are kept, the aligned middle is rewritten in place, and leftovers are removed or batch-inserted with direct DOM operations. When expressions contain raw DOM nodes, Solarite instead falls back to WebReflection/udomdiff to rearrange them with minimal DOM manipulations.

Examples

This is the time example from Lit.js implemented with Solarite:

Upcoming Features

Solarite is actively being developed with several exciting features planned for future releases:

  1. Shadow DOM Support: Optional integration with the browser's native Shadow DOM for true encapsulation of styles and DOM.

  2. JSX Support: Alternative syntax for those who prefer JSX over template literals.

  3. Automatic Rendering: An opt-in feature to automatically re-render components when watched properties change, eliminating the need to manually call render().

  4. Performance Optimizations: Continued improvements to rendering speed and efficiency.

Stay tuned for updates on these features by following the GitHub repository Star.