Solarite

Solarite is a small (8KB min+gzip), fast, compilation-free JavaScript library for creating elements and web components. Features:

To use, import one of these pre-bundled es6 modules into your project:

Or get Solarite from GitHub or NPM:

This project is currently in BETA stage and not yet recommended for production code. Tip: A JetBrains IDE like WebStorm, PhpStorm, or IDEA will syntax highlight the html template strings.

Concepts

Regular Elements

The r() function can create elements. Pass any object with a render() function as the first argument. This object can optionally have additional properties and methods, which become bound to the resulting element. When render() is called, only the changed nodes will be updated.

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

Web Components

Solarite can also create web components. In this minimal example, we make a new class called MyComponent which extends from HTMLElement like any other web component. We provide a render() function to set its html, and a constructor to call it when a new instance is created.

All browsers require web component tag names to have at least one dash in the middle.

Alternatively, instead of instantiating the element in JavaScript, we could can instantiate the element directly from html:

JavaScript veterans will realize that other than the r() function, this is highly similar to one might create vanilla JavaScript web components. This is by design!

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. These functions are only supported for web components and not regular elements.

Rendering

The r function, when used as a tagged template literal , converts the html and embedded expressions into a Solarite Template. This is a data structure used by Solarite to store processed html and expressions. The call to r(this) then renders that Template as an element's attributes and children. You can think of this like assigning to the browser's built-in this.outerHTML property, except updates are much faster because only the changed elements are replaced, instead of all nodes.

Unlike other frameworks, Solarite does not re-render automatically when data changes, so you should call the render() function manually. This is a deliberate design choice to reduce unexpected side effects, since in some cases you may want to update internal data without rendering.

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, as seen in this example:

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().

Note that by default, r() will render expressions as text, with escaped html entities. To render as html, wrap a variable in the r() function:

Folder icon comes from Google.

These types of objects can be returned by in expressions with r 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 created by r-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.

Note that attributes can also be assigned to the root element, such as class="big" on the <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:

Id's that have values matching built-in HTMLElement attribute names such as title or disabled are not allowed.

Events

To intercept events, set the value of an event attribute like onclick to a function. Alternatively, set the value to an array where the first item is a function and subsequent items are arguments to that function.

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.

Two-Way Binding

Form elements can update the properties that provide their values if an event attribute such as oninput is assigned a function to perform the update:

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

Loops

As previously seen, loops can be written with JavaScript's Array.map() function:

When we change an element or add another element to the items list, calling render() only redraws the changed or new element. The other list items are not modified.

Note that nested template literals must also have the r prefix. Otherwise they'll be rendered as escaped text instead of HTML elements.

Scoped Styles

Html with style elements will be rewritten so that the :host selector applies to the root element. This allows an element to specify styles that will apply only to itself and its children, while still inheriting styles from the rest of the document.

These local, "scoped" styles are implemented by:

  1. Adding a data-style attribute to the root element with a unique, incrementing id value for each instance.

  2. Replacing any :host selectors inside the style with element-name[data-style="1"]. For example the :host selector below becomes fancy-text[data-style="1"].

Note that if shadow-dom is used, the element will not replace the :host selector in styles, as browsers natively support the :host selector when inside shadow DOM.

Child Components

When one web component is embedded within another, its attributes and children are passed as arguments to the constructor:

Calling render() on a parent component will call render() on child components if the attributes passed to the child component have changed. The new attributes will be passed as an object to the child component's render() function.

In the above code, we alternatively could've created the <notes-item> element via the new keyword, but doing so would cause all NotesItem components to be recreated on every render.

 

The r() function

The r() function renders templates and elements. There are multiple ways to use the r() function:

Extending Other DOM Elements.

Suppose you want to use a custom component for each <tr> in a <table>. Html won't allow you to put just any element as a child of table or tbody. In this case you can make your web component inherit from the browser's built in <tr> element, by passing it as the third argument to customElements.define:

Manual DOM Ops

You can perform manual DOM operations on your elements in these cases:

  1. Modify any attributes that are not created by expressions, on any nodes not created by expressions.

  2. Add/remove nodes that:

    1. Are not created by an expression

    2. Are not directly before or after an expression that creates nodes.

    3. Do not have any attributes created by expressions.

  3. Modify any node, as long as you restore its previous position and attributes before render() is called again.

This example creates a list inside a div element and demonstrates which manual DOM operations are allowed.

 

The Solarite Class (Experimental)

Instead of inheriting from HTMLElement, you can inherit from the Solarite class, which will add a little bit of magic to your web component:

  1. render() is automatically called when the element is added to the DOM, via a connectedCallback() function in the Solarite parent class.

  2. customElements.define() is automatically called when an element is instantiated via new. It defines the element name based on the class name, by converting the class name to a tag name with dashes, because browsers require all custom elements to have at least one dash within the name. If it can't find at least one place to put a dash, it will append -element to the end.

If you want the component to have a tag name that's different than the name derived from the class name, you can pass a different name to define():

How it works

Suppose you're looping over an array of 10 objects and printing them to a list or table:

When render() is called:

  1. Solarite's r() function gets the array of raw strings created by tasks.map() using a template literal function.

  2. It then creates a hash of the values of each item in the array.

  3. Then it compares those hashes with the hashes from the last time rendering happened, and only update elements and attributes given values that have changed.

Upcoming Features

These are possible features to come:

  1. Optional shadow DOM support

  2. Optional JSX support

  3. Option to render automatically when properties change, without calling render()

  4. Faster rendering performance.