# Web Component Base [](https://www.npmjs.com/package/web-component-base) [](https://www.npmjs.com/package/web-component-base) [](https://www.npmjs.com/package/web-component-base) [](#library-size) 🤷♂️ zero-dependency, 🤏 tiny JS base class for creating reactive custom elements easily ✨  When you extend the `WebComponent` class for your component, you only have to define the `template` and `properties`. Any change in any property value will automatically cause just the component UI to render. The result is a reactive UI on property changes. [View on CodePen ↗](https://codepen.io/ayoayco-the-styleful/pen/ZEwoNOz?editors=1010) ## Table of Contents 1. [Project Status](#project-status) 1. [Installation](#installation) 1. [Import via unpkg](#import-via-unpkg) 1. [Installation via npm](#installation-via-npm) 1. [Exports](#exports) 1. [Main Exports](#main-exports) 1. [Utilities](#utilities) 1. [Usage](#usage) 1. [`template` vs `render()`](#template-vs-render) 1. [Prop access](#prop-access) 1. [Alternatives](#alternatives) 1. [Just the Templating](#just-the-templating) 1. [Quick Start Example](#quick-start-example) 1. [Life-Cycle Hooks](#life-cycle-hooks) 1. [`onInit`](#oninit) - the component is connected to the DOM, before view is initialized 1. [`afterViewInit`](#afterviewinit) - after the view is first initialized 1. [`onDestroy`](#ondestroy) - the component is disconnected from the DOM 1. [`onChanges`](#onchanges) - every time an attribute value changes 1. [Library Size](#library-size) ## Project Status It is ready for majority of cases people use custom elements for. If you have a cool project built on **WebComponent.io** we'd love to know! Please [open an issue](https://github.com/ayoayco/web-component-base/issues/new) or reach out to [Ayo](https://ayco.io/@ayo) if you want to be featured on the project website :) For building some advanced interactions, we have a few issues that are still open: [#24 smart diffing](https://github.com/ayoayco/web-component-base/issues/24), [#15 memoization](https://github.com/ayoayco/web-component-base/issues/15), [#4 attachEffect improvements](https://github.com/ayoayco/web-component-base/issues/4) In the mean time, if you have some complex needs, we recommend using the `WebComponent` base class with a more mature rendering approach like `lit-html`... and here's a demo for that: [View on CodePen ↗](https://codepen.io/ayoayco-the-styleful/pen/ZEwNJBR?editors=1010). ...or you can even [use just parts](#just-the-templating) of it for your own base class. ## Installation The library is distributed as complete ECMAScript Modules (ESM) and published on [NPM](https://ayco.io/n/web-component-base). Please file an issue in our [issue tracker](https://ayco.io/gh/web-component-base/issues) for problems or requests regarding our distribution. ### Use on the browser via unpkg (no bundlers needed!) Import using [unpkg](https://unpkg.com/web-component-base) in your vanilla JS component. You can replace the version `@latest` in the URL with specific versions. We will use this in the rest of our [usage examples](#usage). ```js import { WebComponent } from "https://unpkg.com/web-component-base@latest/index.js" ``` ### Installation via npm Usable for projects with bundlers or using import maps pointing to the specific files downloaded in `node_modules/web-component-base`. ```bash npm i web-component-base ``` ## Exports You can import everything separately, or in a single file each for the main exports and utilities. ### Main Exports ```js // all in a single file import { WebComponent, html, attachEffect } from "web-component-base"; // in separate files import { WebComponent } from "web-component-base/WebComponent.js"; import { html } from "web-component-base/html.js"; import { attachEffect } from "web-component-base/attach-effect.js"; ``` ### Utilities ```js // in a single file import { serialize, deserialize, getCamelCase, getKebabCase, createElement } from "web-component-base/utils"; // or separate files import { serialize } from "web-component-base/utils/serialize.js"; import { createElement } from "web-component-base/utils/create-element.js"; // etc... ``` ## Usage In your component class: ```js // HelloWorld.mjs import { WebComponent } from "https://unpkg.com/web-component-base@latest/index.js"; class HelloWorld extends WebComponent { static props ={ myName: 'World', emotion: 'sad' } get template() { return `
## `template` vs `render()`
This mental model attempts to reduce the cognitive complexity of authoring components:
1. The `template` is a read-only property (initialized with a `get` keyword) that represents *how* the component view is rendered.
1. There is a `render()` method that triggers a view render.
1. This `render()` method is *automatically* called under the hood every time an attribute value changed.
1. You can *optionally* call this `render()` method at any point to trigger a render if you need (eg, if you have private unobserved properties that need to manually trigger a render)
1. Overriding the `render()` function for handling a custom `template` is also possible. Here's an example of using `lit-html`: [View on CodePen ↗](https://codepen.io/ayoayco-the-styleful/pen/ZEwNJBR?editors=1010)
## Prop Access
The `props` property of the `WebComponent` interface is provided for easy read/write access to a camelCase counterpart of *any* observed attribute.
```js
class HelloWorld extends WebComponent {
static props = {
myProp: 'World'
}
get template() {
return html`