Getting Started
Installation
Section titled “Installation”Install the package using your preferred package manager:
npm install statecharts.shpnpm add statecharts.shbun add statecharts.shYour First Statechart
Section titled “Your First Statechart”Create a simple toggle statechart:
import { chart } from "statecharts.sh";
const toggle = chart({ context: {}, initial: "inactive", states: { inactive: { on: { TOGGLE: "active" }, }, active: { on: { TOGGLE: "inactive" }, }, },});Starting an Instance
Section titled “Starting an Instance”A chart is a blueprint. To run it, create an instance:
const instance = toggle.start();
console.log(instance.state.value); // "inactive"
instance.send("TOGGLE");console.log(instance.state.value); // "active"Subscribing to State Changes
Section titled “Subscribing to State Changes”React to state transitions:
instance.subscribe((state) => { console.log("Current state:", state.value);});
instance.send("TOGGLE"); // logs: "Current state: inactive"Using Context
Section titled “Using Context”Store data alongside state:
const counter = chart({ context: { count: 0 }, initial: "idle", states: { idle: { on: { INCREMENT: { actions: (ctx) => ({ count: ctx.count + 1 }), }, DECREMENT: { actions: (ctx) => ({ count: ctx.count - 1 }), }, }, }, },});
const instance = counter.start();instance.send("INCREMENT");console.log(instance.state.context.count); // 1Next Steps
Section titled “Next Steps”- Learn how to use statecharts with React
- Explore the API Reference for full documentation
- See Examples for real-world patterns