import { For, createSignal, onCleanup, onSettled, type Accessor } from "solid-js";
import type { JSX } from "@solidjs/web";
import preview from "../../../.storybook/preview.js";
import { createMachine } from "@solid-primitives/state-machine";
import readme from "../README.md?raw";
import {
Button,
ButtonRow,
Container,
Section,
Separator,
StatRow,
colors,
font,
radii,
inputStyle,
} from "../../../.storybook/ui/index.js";
const meta = preview.meta({
title: "Reactivity/State Machine",
tags: ["autodocs"],
parameters: {
layout: "centered",
docs: {
description: {
component: readme,
},
},
},
});
export default meta;
const LIGHTS = [
{ key: "red" as const, color: "#ef4444" },
{ key: "yellow" as const, color: "#f59e0b" },
{ key: "green" as const, color: "#22c55e" },
];
export const TrafficLight = meta.story({
name: "Traffic light",
parameters: {
docs: {
description: {
story:
'Typed `to` constraints enforce valid transitions at compile time. TypeScript rejects `state.to.green()` from `red` because `to: "yellow"` is the only allowed transition.',
},
},
},
render: () => {
const state = createMachine<{
red: { to: "yellow" };
yellow: { to: "green" | "red" };
green: { to: "red" };
}>({
initial: "red",
states: {
red: () => undefined,
yellow: () => undefined,
green: () => undefined,
},
});
return (
{l => (
)}
);
},
});
export const LifecycleCounter = meta.story({
name: "Counter lifecycle",
parameters: {
docs: {
description: {
story:
"Signals and cleanups created inside a state callback are owned by that state. The `count` signal and `setInterval` live inside `running` — transitioning to `idle` disposes them automatically via `onCleanup`.",
},
},
},
render: () => {
const state = createMachine<{
idle: { to: "running" };
running: { value: { count: Accessor }; to: "idle" };
}>({
initial: "idle",
states: {
idle: () => undefined,
running() {
const [count, setCount] = createSignal(0);
const id = setInterval(() => setCount(c => c + 1), 100);
onCleanup(() => clearInterval(id));
return { count };
},
},
});
const count = () => {
const s = state();
return s.type === "running" ? s.value.count() : 0;
};
return (
{count()}
);
},
});
export const InlineEdit = meta.story({
name: "JSX as state value",
parameters: {
docs: {
description: {
story:
"`createMachine` works like a `` component when state callbacks return JSX. Each state owns its DOM — transitioning disposes the old view and mounts the fresh one.",
},
},
},
render: () => {
const [label, setLabel] = createSignal("Click Edit to change this text");
// Forward references so state callbacks can trigger external transitions.
// These are assigned after createMachine returns; closures capture the
// binding, not the value, so they resolve correctly at call time.
// oxlint-disable-next-line prefer-const
let toEdit!: () => void;
// oxlint-disable-next-line prefer-const
let toRead!: () => void;
const state = createMachine<{
reading: { value: JSX.Element; to: "editing" };
editing: { value: JSX.Element; to: "reading" };
}>({
initial: "reading",
states: {
reading() {
return (
{label()}
);
},
editing() {
let el!: HTMLInputElement;
const save = () => {
setLabel(el.value.trim() || label());
toRead();
};
return (
{
el = e;
onSettled(() => e.focus());
}}
value={label()}
onBlur={save}
onKeyDown={e => {
if (e.key === "Enter") save();
if (e.key === "Escape") toRead();
}}
style={{ ...inputStyle, flex: "1" }}
/>