import { createSignal, For } from "solid-js"; import preview from "../../../.storybook/preview.js"; import { combineProps, combineHandlers, partitionProps } from "@solid-primitives/props"; import readme from "../README.md?raw"; import { Container, Card, EventLog, Button, ButtonRow, StatRow, BoolRow, Section, } from "../../../.storybook/ui/index.js"; import { colors, font, radii } from "../../../.storybook/ui/tokens.js"; const statusColors = { ok: { bg: "#dcfce7", fg: "#16a34a" }, warn: { bg: "#fef9c3", fg: "#ca8a04" }, error: { bg: "#fee2e2", fg: "#dc2626" }, } as const; type StatusKey = keyof typeof statusColors; type StatusProps = { status: StatusKey; label: string; [key: string]: unknown }; const StatusBadge = (props: StatusProps) => { const [own, html] = partitionProps(props, k => k === "status" || k === "label"); const status = () => (own as StatusProps).status; return ( {(own as StatusProps).label} ); }; const FILTER_DEMO_KEYS = [ "id", "class", "style", "data-id", "data-testid", "aria-label", "aria-hidden", "onClick", ] as const; const meta = preview.meta({ title: "Utilities/Props", tags: ["autodocs"], parameters: { layout: "centered", docs: { description: { component: readme } }, }, }); export default meta; export const HandlerChaining = meta.story({ name: "Handler chaining", parameters: { docs: { description: { story: "All `on*` handlers across sources are chained left-to-right — spread `{...combined}` onto a native element and every source's handler fires without any manual wiring.", }, }, }, render: () => { const [log, setLog] = createSignal<{ label: string; time: string }[]>([]); const push = (msg: string) => setLog(e => [{ label: msg, time: new Date().toLocaleTimeString("en", { hour12: false }) }, ...e].slice( 0, 5, ), ); const combined = combineProps( { onClick: () => push("base onClick") }, { onClick: () => push("extra onClick") }, ); return ( ); }, }); export const StyleAndClassMerge = meta.story({ name: "Class & style merging", parameters: { docs: { description: { story: "`class` values are joined with a space. `style` objects are shallow-merged with later sources overriding the same key — string styles are converted to objects first.", }, }, }, render: () => { const sourceA = { class: "btn", style: { background: colors.primary, color: "white", "border-radius": radii.md }, }; const sourceB = { class: "btn-large", style: { padding: "0.75rem 1.5rem", "font-weight": "700" }, }; const combined = combineProps(sourceA, sourceB); return (
"{combined.class as string}"
)}> {([k, v]) => }
); }, }); export const NullHandlerSkip = meta.story({ name: "Null handler skipping", parameters: { docs: { description: { story: "`combineHandlers` silently skips `null`, `undefined`, and `false` — toggle the extra handler off and click: no errors, only the base handler fires.", }, }, }, render: () => { const [extraOn, setExtraOn] = createSignal(true); const [log, setLog] = createSignal<{ label: string; time: string }[]>([]); const push = (msg: string) => setLog(e => [{ label: msg, time: new Date().toLocaleTimeString("en", { hour12: false }) }, ...e].slice( 0, 5, ), ); const base = () => push("base handler"); const extra = () => push("extra handler"); return ( ); }, }); export const DynamicFilter = meta.story({ name: "Dynamic prefix filter", parameters: { docs: { description: { story: "`filterProps` evaluates its predicate lazily per property read. Any signal inside the predicate is tracked — switching the prefix below reactively changes which keys pass through.", }, }, }, render: () => { const [prefix, setPrefix] = createSignal("data-"); return ( {(["data-", "aria-", "on"] as const).map(p => ( ))} {key => } ); }, }); export const OwnVsDomSplit = meta.story({ name: "Own vs DOM split", parameters: { docs: { description: { story: "`partitionProps` splits props into two lazy views. `status` and `label` are consumed by `StatusBadge`; all other keys (`title`, `id`, `aria-label`) pass through to the underlying ``. Hover a badge to see the forwarded `title` tooltip.", }, }, }, render: () => { const [status, setStatus] = createSignal("ok"); return ( {(Object.keys(statusColors) as StatusKey[]).map(s => ( ))}
); }, });