Welcome! This readme explains the functionality and usage of the createWebComponentBaseClass
function for creating reusable web components in TypeScript.
This library is my pet project. It provides a base class for building web components with lit-html
, nanostores
and immer
. It simplifies the setup process by handling several boilerplate tasks:
- State management: Creates a
nanostores
'atom
with the provided default state usingimmer
'sproduce
for immutable updates. - Templating: Accepts a lit
TemplateResult
and updates the shadow DOM whenever it changes. - Basic setup: Extends
HTMLElement
, attaches a shadow DOM, and provides access to state and template.
npm install webcomponent-constructor
Create an object that defines the name, default state, and template for your web component.
import createWebComponentBaseClass from "webcomponent-constructor";
const configuration = {
name: "my-component",
defaultState: { count: 0 },
template: html`<h1>No event listeners here, just static HTML</h1>`,
};
const Base = createWebComponentBaseClass(configuration);
Use the createWebComponentBaseClass function with your configuration.
class YourOwnClass extends Base {
constructor() {
super()
this.state.subscribe((state => {
this.template.set(html`<button @click=${() => this.state.set(()=>({ count: state.count + 1 }))}>Click Me: ${state.count}</button>`)
}
// ^ ^ ^
// `@`-stuff is ok here since you have access to `this`.
// Just don't forget about arrow functions or binding `this` inside the listeners.
}
}
customElements.define(configuration.name, YourOwnClass);
- Into the HTML, and then declare your component there (
<my-component></my-component>
). - Or into some other script, and do your magic there.
You can further extend the generated class to add methods, lifecycle hooks, and additional functionality specific to your component.
- Simple and concise: Focuses on essential tasks, keeping the codebase clean and easy to understand.
- State management with
immer
: Enables immutable updates with a familiar API. The state is automatically subscribed to to re-rendeer on change. lit-html
integration: Renders templates efficiently usinglit
's virtual DOM. Technically, it's just anothernanostores
atom
, which is also subscribed to.- Open shadow DOM: Allows direct styling of component internals.
- Reduced boilerplate: Save time and effort by focusing on your component logic.
- Consistent structure: Provides a standardized base for all your web components.
- Improved maintainability: Code is easier to understand and extend.
I wanted my own Lit
with bells and whistles.
Sure.