What is lit?
The lit npm package is a simple and fast library for building web components and web applications. It allows developers to create reusable, encapsulated, and interactive elements using modern JavaScript and Web Component standards. Lit is designed to be lightweight and efficient, focusing on speed and minimal overhead.
What are lit's main functionalities?
Creating Web Components
This feature allows developers to create custom web components using the LitElement base class. The example demonstrates defining a new element with a simple style and rendering method.
import { LitElement, html, css } from 'lit';
class MyElement extends LitElement {
static styles = css`p { color: blue; }`;
render() {
return html`<p>Hello, world!</p>`;
}
}
customElements.define('my-element', MyElement);
Reactive Properties
This feature showcases Lit's reactive property system. The example includes a counter component that updates its display whenever the 'count' property changes.
import { LitElement, html, css, property } from 'lit';
class MyCounter extends LitElement {
@property({ type: Number }) count = 0;
render() {
return html`
<button @click=${this._increment}>Increment</button>
<span>${this.count}</span>
`;
}
_increment() {
this.count++;
}
}
customElements.define('my-counter', MyCounter);
Styling Components
This feature demonstrates how to apply CSS styles to a Lit component. The example shows defining styles for the component itself and its internal elements.
import { LitElement, html, css } from 'lit';
class StyledElement extends LitElement {
static styles = css`
:host {
display: block;
border: 1px solid black;
padding: 16px;
max-width: 200px;
}
.highlight {
color: red;
}
`;
render() {
return html`<div class='highlight'>Styled Content</div>`;
}
}
customElements.define('styled-element', StyledElement);
Other packages similar to lit
react
React is a popular library for building user interfaces. It focuses on a component-based architecture similar to Lit but uses a virtual DOM for updates, which differs from Lit's direct DOM manipulation approach.
vue
Vue is a progressive framework for building UIs. Like Lit, it emphasizes component-based development and reactivity. However, Vue offers a more comprehensive framework experience with options for routing and state management.
svelte
Svelte is a compiler that generates efficient JavaScript code from component declarations. Unlike Lit, which runs in the browser, Svelte shifts much of the work to compile time, resulting in smaller bundles and faster runtime execution.
Lit.js
Lit is a micro-literate programming library for Javascript inspired by docco.
Lit was created for the Apres project but is
released separately for easy integration with any project wanting to display
annotated Javascript code. Lit features:
- Pure Javascript implementation, no shell-outs.
- Runs in browser, or via node.js.
- Can be loaded as an AMD module via requirejs, a CommonJS module via node.js,
or via a simple
<script>
include. - Table-free, simple, and readable html structure.
- Multiple css styles.
- Pluggable comment and code parsers.
- MIT License.
Installation
To use Lit on the command-line, use npm install lit
, use the global option
to be able to run it anywhere.
Examples
CLI
The command-line script litjs
is provided to create annotated web pages from
Javascript source files.
Create annotated web page from JS source
Usage: litjs [infile]
Options:
-t, --template HTML template file name, defaults to columns.html
-o, --out Output file, defaults to stdout
--title Title for output page, defaults to input file name
--style, -s Page style, one of "columns", "callouts", or "stacked" [default: "columns"]