Simple and elegant component-based UI library
npx @tessl/cli install tessl/npm-riot@10.0.0Riot.js is a simple and elegant component-based UI library that brings custom components to all modern browsers. It features a concise HTML-like syntax, one-way data flow, and high performance through pre-compiled expressions with minimal DOM manipulation.
npm install riotimport { mount, register, component } from "riot";For CommonJS:
const { mount, register, component } = require("riot");Compiler build (includes compilation capabilities):
import { mount, register, compile } from "riot+compiler";import { register, mount } from "riot";
// Register a component (typically done by the build process)
register("my-timer", {
css: "my-timer { display: block; }",
template: (template, expressionTypes, bindingTypes) =>
template("<p>Seconds: { state.time }</p>", [
// Template bindings would be here
]),
exports: {
onBeforeMount() {
this.state = { time: 0 };
this.timer = setInterval(() => {
this.update({ time: this.state.time + 1 });
}, 1000);
},
onUnmounted() {
clearInterval(this.timer);
}
}
});
// Mount the component
const components = mount("my-timer");Riot.js is built around several key concepts:
System for registering and managing component definitions in a global registry.
function register<Props, State>(
componentName: string,
wrapper: RiotComponentWrapper<RiotComponent<Props, State>>
): RegisteredComponentsMap;
function unregister(componentName: string): RegisteredComponentsMap;Core functionality for mounting components to DOM elements and managing their lifecycle.
function mount<Props, State>(
selector: string | HTMLElement,
initialProps?: Props,
componentName?: string
): RiotComponent<Props, State>[];
function unmount(
selector: string | HTMLElement,
keepRootElement?: boolean
): HTMLElement[];Direct component creation without global registration, useful for dynamic components.
function component<Props, State, Component extends RiotComponent>(
wrapper: RiotComponentWrapper<Component>
): (el: HTMLElement, initialProps?: Props, meta?: ComponentMeta) => Component;Component enhancement system for adding cross-cutting functionality to all components.
function install(plugin: ComponentEnhancer): InstalledPluginsSet;
function uninstall(plugin: ComponentEnhancer): InstalledPluginsSet;Lightweight component pattern for simple, stateless components without full lifecycle management.
function pure<InitialProps, Context, FactoryFunction>(
func: FactoryFunction
): FactoryFunction;Runtime compilation capabilities for compiling riot components from templates and URLs.
function compile(options?: CompileOptions): Promise<void>;
function compileFromString(string: string, options?: CompileOptions): CompilationResult;
function compileFromUrl(url: string, options?: CompileOptions): Promise<CompilationResult>;Helper functions, version information, and internal API access for advanced use cases.
function withTypes<Component>(component: Component): Component;
const version: string;
const __: InternalAPI;interface RiotComponent<Props = DefaultProps, State = DefaultState> {
readonly props: Props;
readonly root: HTMLElement;
readonly name?: string;
readonly slots: TagSlotData[];
state: State;
components?: RiotComponentsMap;
mount(
element: HTMLElement,
initialState?: State,
parentScope?: object
): RiotComponent<Props, State>;
update(
newState?: Partial<State>,
parentScope?: object
): RiotComponent<Props, State>;
unmount(keepRootElement?: boolean): RiotComponent<Props, State>;
$(selector: string): Element | null;
$$(selector: string): Element[];
shouldUpdate?(newProps: Props, oldProps: Props): boolean;
onBeforeMount?(props: Props, state: State): void;
onMounted?(props: Props, state: State): void;
onBeforeUpdate?(props: Props, state: State): void;
onUpdated?(props: Props, state: State): void;
onBeforeUnmount?(props: Props, state: State): void;
onUnmounted?(props: Props, state: State): void;
}
interface RiotComponentWrapper<Component> {
readonly css?: string | null;
readonly exports?: RiotComponentFactoryFunction<Component> | Component | null;
readonly name?: string | null;
template?(
template: TemplateFunction,
expressionTypes: ExpressionTypes,
bindingTypes: BindingTypes,
getComponent: GetComponentFunction
): TemplateChunk<Component> | null;
}
interface RiotPureComponent<Context = object> {
mount(element: HTMLElement, context?: Context): void;
update(context?: Context): void;
unmount(keepRootElement: boolean): void;
}
type DefaultProps = Record<PropertyKey, any>;
type DefaultState = Record<PropertyKey, any>;
type ComponentEnhancer = <Props, State>(
component: RiotComponent<Props, State>
) => RiotComponent<Props, State>;