import { createSignal, For } from "solid-js"; import preview from "../../../.storybook/preview.js"; import { MatchTag, MatchValue } from "@solid-primitives/match"; import readme from "../README.md?raw"; import { Button, Container, Card, Section, StatRow, BoolRow, Badge, colors, font, radii, } from "../../../.storybook/ui/index.js"; const meta = preview.meta({ title: "Control Flow/Match", tags: ["autodocs"], parameters: { layout: "centered", docs: { description: { component: readme, }, }, }, }); export default meta; type Shape = | { type: "circle"; radius: number } | { type: "rect"; width: number; height: number } | { type: "triangle"; base: number; height: number }; const SHAPES: Shape[] = [ { type: "circle", radius: 40 }, { type: "rect", width: 80, height: 50 }, { type: "triangle", base: 70, height: 60 }, ]; export const DiscriminatedUnion = meta.story({ name: "Discriminated union (default tag)", parameters: { docs: { description: { story: "`` dispatches on the `type` field by default. Each branch receives a narrowed **accessor** — reading `v().radius` is only possible inside the `circle` branch.", }, }, }, render: () => { const [shape, setShape] = createSignal(null); const area = () => { const s = shape(); if (!s) return null; if (s.type === "circle") return (Math.PI * s.radius ** 2).toFixed(1); if (s.type === "rect") return (s.width * s.height).toFixed(1); return (0.5 * s.base * s.height).toFixed(1); }; return (

Shape inspector

{/* Shape selector */}
{s => ( )}
{/* MatchTag output */} (
Circle
radius: {v().radius}px
), rect: v => (
Rectangle
{v().width} × {v().height}px
), triangle: v => (
Triangle
base {v().base}px · height {v().height}px
), }} fallback={ No shape selected } />

Each branch accessor is narrowed — v().radius only exists inside{" "} circle.

); }, }); type Notification = | { kind: "info"; message: string } | { kind: "warning"; message: string; code: number } | { kind: "error"; message: string; code: number; fatal: boolean }; const NOTIFS: Notification[] = [ { kind: "info", message: "Deployment succeeded." }, { kind: "warning", message: "Disk usage above 80%.", code: 1001 }, { kind: "error", message: "Out of memory.", code: 5002, fatal: true }, ]; const BADGE_VARIANT = { info: "info", warning: "warning", error: "error", } as const; export const CustomTagField = meta.story({ name: "Custom tag field", parameters: { docs: { description: { story: 'Pass `tag="kind"` when the discriminant field isn\'t named `type`. The `case` keys and accessor shape update accordingly.', }, }, }, render: () => { const [notif, setNotif] = createSignal(null); return (

Notification viewer

{n => ( )}
(
info {v().message}
), warning: v => (
warning #{v().code} {v().message}
), error: v => (
error #{v().code} {v().message}
), }} fallback={ No notification } />

The discriminant is kind, set via tag="kind".

); }, }); type MediaEvent = { type: "play" } | { type: "pause" } | { type: "seek"; position: number } | { type: "end" }; const ALL_EVENTS: MediaEvent[] = [ { type: "play" }, { type: "pause" }, { type: "seek", position: 42 }, { type: "end" }, ]; export const PartialMatch = meta.story({ name: "Partial match with fallback", parameters: { docs: { description: { story: "Add `partial` when you only handle a subset of union members. Unhandled cases fall through to `fallback` — identical runtime behavior, but the TypeScript type of `case` switches from required to optional keys.", }, }, }, render: () => { const [evt, setEvt] = createSignal(null); return (

Media event handler

Only play and pause are handled; others fall through.

{e => ( )}
(
▶ Playing
), pause: () => (
⏸ Paused
), }} fallback={ {evt() ? `Unhandled: "${evt()!.type}"` : "No event"} } />

partial is TypeScript-only — runtime behavior is unchanged.

); }, }); type Status = "idle" | "loading" | "success" | "error"; const STATUSES: Status[] = ["idle", "loading", "success", "error"]; const STATUS_COLOR: Record = { idle: colors.muted, loading: "#6366f1", success: colors.success, error: "#ef4444", }; const STATUS_LABEL: Record = { idle: "Idle", loading: "Loading…", success: "Done", error: "Failed", }; export const LiteralUnion = meta.story({ name: "Literal union (MatchValue)", parameters: { docs: { description: { story: "`` matches plain string/number literals — no tag field required. Each branch receives the matched value directly.", }, }, }, render: () => { const [status, setStatus] = createSignal("idle"); return (

Request status

{/* Status indicator */}
( Waiting ), loading: () => ( Fetching data… ), success: () => ( Data loaded successfully ), error: () => ( Request failed ), }} />
{s => ( )}

No object needed — MatchValue matches "idle" | "loading" | …{" "} directly.

); }, });