Rasti Rasti v5.0.0

Rasti Rasti

HTML-first, composable user interfaces

const Counter = Component.create(({ model, html }) => html`
    <div>
        <div>Counter: ${model.count}</div>
        <button onClick=${() => model.count++}>Increment</button>
        <button onClick=${() => model.count--}>Decrement</button>
    </div>
`);

Counter.mount({ model : new Model({ count : 0 }) }, document.body);
Counter: 0
npm i rasti

Rasti is a library for building state-driven user interfaces from HTML-first components.

A component's template renders as HTML first. That markup is then hydrated, and from then on, when state changes, Rasti patches only the regions that changed.

This has two consequences:

  • Parsing and DOM creation are delegated to the browser's own parser.
  • Server rendering, static builds and browser rendering all follow the same path.

Underneath sits a small MVC core: a component is a view, models hold its state, and event emitters tie the two together.

Key Features

Template literals, components as tags

Templates are standard tagged template literals: no JSX, no compiler, no build step. A component is used inside another template as a tag, with props and children: <${Link} href="${href}">${label}</${Link}>. No registry, no custom elements.

Handlers in the markup

Event handlers are written as attributes: onClick=${() => this.increment()}. They are delegated from the component's root element, so re-renders never bind listeners and an inline handler binds nothing.

Updates in place

When state changes, Rasti re-evaluates the template and writes only the values that differ. The DOM is patched, not rebuilt, so focus, selection and input values persist.

State in models, or anything that emits

model, state and props are Rasti models, and a component re-renders when they change. Anything else that emits change works too, such as a Backbone model or a store of your own.

Real DOM, no wrapper

render().el is the element itself: no shadow DOM, no synthetic events, nothing between you and the node. Query it, style it, or hand it to another library.

Server rendering and hydration

Rendering is just toString: a component drops into any HTML string, anywhere JavaScript runs — const html = `<body>${new App({ model })}</body>` — and picks up the served markup in the browser without recreating it.

Typed

TypeScript declarations ship with the package, covering components, models and template interpolations.

Getting Started

Installing via npm

$ npm install rasti
import { Model, Component } from 'rasti';

Using native ES modules

No bundler needed: an import map points rasti to the ES module build, so imports read the same as with npm.

<script type="importmap">
    {
        "imports": {
            "rasti": "https://cdn.jsdelivr.net/npm/rasti/dist/rasti.esm.min.js"
        }
    }
</script>

<script type="module">
    import { Model, Component } from 'rasti';
</script>

The import map can also point to a copy of the file served from your own site, or to https://esm.run/rasti. During development, use rasti.esm.js for development warnings and detailed error messages.

Using the UMD build

For a classic <script> tag, the UMD build exposes the Rasti global object. It also loads as an AMD module, with RequireJS for example.

<script src="https://cdn.jsdelivr.net/npm/rasti/dist/rasti.umd.min.js"></script>
const { Model, Component } = Rasti;

The development build is rasti.umd.js.

Create a Component

// Define a Timer component that displays the number of seconds from the model.
const Timer = Component.create(({ model, html }) => html`
    <div>
        Seconds: <span>${model.seconds}</span>
    </div>
`);

// Create a model to store the seconds.
const model = new Model({ seconds : 0 });

// Mount the Timer component to the body and pass the model as an option.
Timer.mount({ model }, document.body);

// Increment the `seconds` property of the model every second.
// Only the text node inside the <span> gets updated on each render.
setInterval(() => model.seconds++, 1000);

Try it on CodePen

Adding sub components

// Define the routes for the navigation menu.
const routes = [
    { label : 'Home', href : '#' },
    { label : 'Faq', href : '#faq' },
    { label : 'Contact', href : '#contact' },
];

// Create a Link component for navigation items.
const Link = Component.create(({ props, html }) => html`
    <a href="${props.href}">
        ${props.renderChildren()}
    </a>
`);

// Create a Navigation component that renders Link components for each route.
// `html` builds a partial: a sub-template for lists and conditional blocks.
const Navigation = Component.create(({ props, html }) => html`
    <nav>
        ${props.routes.map(({ label, href }) => html`<${Link} key="${href}" href="${href}">${label}</${Link}>`)}
    </nav>
`);

// Create a Main component that includes the Navigation and displays the current route's label as the title.
class Main extends Component {
    template() {
        return this.html`
            <main>
                <${Navigation} routes="${this.props.routes}" />
                <section>
                    <h1>${this.currentRoute().label}</h1>
                </section>
            </main>
        `;
    }

    currentRoute() {
        return this.props.routes.find(({ href }) => href === (this.model.location || '#'));
    }
}

// Initialize a model to store the current location.
const model = new Model({ location : document.location.hash });

// Update the model's location state when the browser's history changes.
window.addEventListener('popstate', () => model.location = document.location.hash);

// Mount the Main component to the body, passing the routes and model as options.
Main.mount({ routes, model }, document.body);

Try it on CodePen

Handling events and state

// Create a Filter component that keeps its query in internal state.
class Filter extends Component {
    onCreate() {
        // Initialize the component's state.
        this.state = new Model({ query : '' });
    }

    template() {
        return this.html`
            <div>
                <input placeholder="Filter…" onInput=${(ev) => this.search(ev.target.value)} />
                <ul>${this.matches().map((item) => this.html`<li>${item}</li>`)}</ul>
            </div>
        `;
    }

    search(query) {
        this.state.query = query;
    }

    matches() {
        const query = this.state.query.toLowerCase();
        return this.props.items.filter((item) => item.toLowerCase().includes(query));
    }
}

// Mount the Filter component to the body and pass the items as a prop.
Filter.mount({ items : ['Model', 'View', 'Component', 'Emitter'] }, document.body);

// Event listeners are delegated from the component's root element.
// The DOM is patched, not rebuilt: the input keeps its focus and value while the list updates.

Try it on CodePen

Writing components

A component renders from its template() method, which returns a partial built with html (an alias of partial). For short presentational components like Timer, Link and Navigation, pass Component.create a template function: it becomes template() and receives the component as its argument. When the component has methods or state, as Main and Filter do, write a subclass that defines template() next to them. create also takes a tagged template, the main form up to Rasti 4: its interpolations are defined once, with the declaration, so dynamic values have to be functions, which pays off in a few specific cases. The three forms, how they differ and when to use each are in Writing components.

Scaffolding a New Project

The fastest way to start a real-world Rasti project is create-rasti, the official scaffolding tool. It generates a ready-to-use Rasti + Vite setup with optional server-side rendering, routing, styling, and icon components.

# Interactive setup
npm create rasti

# Non-interactive single-page app
npm create rasti my-app

# Server-side rendering with routing and Tailwind CSS
npm create rasti my-app --ssr --router --tailwind

Available options include:

  • Rendering — Single-page app (default), server-side rendering (--ssr), or static pre-rendering (--static).
  • Styling — Plain CSS (default), Tailwind CSS (--tailwind), or CSSFUN with light/dark theme support (--cssfun).
  • Routing (--router) — A small universal router built on path-to-regexp.
  • Icons (--icons) — Generate Rasti components from popular SVG icon sets (heroicons, akar-icons, feathericon, pixelarticons, and more).

See the create-rasti repository for the full list of templates and options.

Example

To see how Rasti's API and architecture come together in a small app, explore the sample TODO application in the example folder of the Rasti GitHub repository. It's a concise, self-contained reference for understanding how models, views, and components fit together in a simple application. Try it live here.

To scaffold a real-world project, use create-rasti.

Documentation

  • Writing components — the three forms and which to use, interpolations, containers and the root partial.
  • TypeScript — typing components, models and template interpolations.
  • API documentation — every Rasti class, method and option.
  • Architecture overview — how the pieces fit together: components, the render engine, recycling and SSR.
  • Agent skill — the API, patterns and best practices for coding agents. Install it with npx skills add 8tentaculos/rasti, or copy node_modules/rasti/skills/rasti into your agent's skills folder.

TypeScript

Rasti ships with TypeScript declarations out of the box. The types are bundled in the package and resolved automatically. How to type components, models and template interpolations is in the TypeScript guide.

Changelog

Release history and migration notes for major versions are in CHANGELOG.md.

How this website is built

This website is built with Rasti and CSSFUN, using the Static scaffold provided by create-rasti.

The Static template writes each public route as HTML that can be hosted on services such as GitHub Pages. This combines fast static delivery and SEO-friendly pages with fluid SPA navigation once Rasti hydrates the markup in the browser.

The home page and documentation are generated directly from the Markdown in the Rasti repository, keeping the published website aligned with the project source.