@pyreon/storybook 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present Vit Bokisch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,200 @@
1
+ # @pyreon/storybook
2
+
3
+ Storybook renderer for Pyreon components. Mount, render, and interact with Pyreon components in Storybook.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ bun add @pyreon/storybook
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ Configure Storybook to use the Pyreon renderer:
14
+
15
+ ```ts
16
+ // .storybook/main.ts
17
+ export default {
18
+ stories: ["../src/**/*.stories.ts"],
19
+ framework: "@pyreon/storybook",
20
+ }
21
+ ```
22
+
23
+ ## Quick Start
24
+
25
+ ```tsx
26
+ import type { Meta, StoryObj } from "@pyreon/storybook"
27
+ import { Button } from "./Button"
28
+
29
+ const meta = {
30
+ component: Button,
31
+ args: { label: "Click me" },
32
+ } satisfies Meta<typeof Button>
33
+
34
+ export default meta
35
+ type Story = StoryObj<typeof meta>
36
+
37
+ export const Primary: Story = {
38
+ args: { variant: "primary" },
39
+ }
40
+
41
+ export const AllVariants: Story = {
42
+ render: (args) => (
43
+ <div style="display: flex; gap: 8px;">
44
+ <Button {...args} variant="primary" />
45
+ <Button {...args} variant="secondary" />
46
+ </div>
47
+ ),
48
+ }
49
+ ```
50
+
51
+ ## API
52
+
53
+ ### `renderToCanvas(context, canvasElement)`
54
+
55
+ Core renderer called by Storybook to display stories. Handles cleanup of previous renders, error display, and mounting the VNode tree.
56
+
57
+ | Parameter | Type | Description |
58
+ | --- | --- | --- |
59
+ | `context.storyFn` | `() => VNodeChild` | Function that produces the story VNode |
60
+ | `context.showMain` | `() => void` | Show the main canvas |
61
+ | `context.showError` | `(error) => void` | Show an error panel |
62
+ | `context.forceRemount` | `boolean` | Whether to force a full remount |
63
+ | `canvasElement` | `HTMLElement` | DOM element to render into |
64
+
65
+ This function is used internally by the Storybook framework preset. You typically do not call it directly.
66
+
67
+ ### `defaultRender(component, args)`
68
+
69
+ Default render implementation. Called when no custom `render` function is provided on the story or meta.
70
+
71
+ | Parameter | Type | Description |
72
+ | --- | --- | --- |
73
+ | `component` | `ComponentFn` | Pyreon component function |
74
+ | `args` | `Record<string, unknown>` | Story args passed as props |
75
+
76
+ **Returns:** `VNodeChild` — result of `h(component, args)`
77
+
78
+ ### `Meta<TComponent>`
79
+
80
+ Type for the default export of a story file. Provides type inference for args based on the component's props.
81
+
82
+ | Property | Type | Description |
83
+ | --- | --- | --- |
84
+ | `component` | `TComponent` | The component to document |
85
+ | `title` | `string` | Display title in sidebar |
86
+ | `decorators` | `DecoratorFn[]` | Decorators for all stories in the file |
87
+ | `args` | `Partial<InferProps<TComponent>>` | Default args |
88
+ | `argTypes` | `Record<string, unknown>` | Arg type definitions for Controls |
89
+ | `parameters` | `Record<string, unknown>` | Story parameters (backgrounds, viewport) |
90
+ | `tags` | `string[]` | Tags for filtering (e.g. `"autodocs"`) |
91
+ | `render` | `(args, context) => VNodeChild` | Default render function |
92
+ | `excludeStories` | `string \| string[] \| RegExp` | Exclude named exports |
93
+ | `includeStories` | `string \| string[] \| RegExp` | Include only named exports |
94
+
95
+ ### `StoryObj<TMeta>`
96
+
97
+ Type for individual story exports. Args are merged with `Meta.args`.
98
+
99
+ | Property | Type | Description |
100
+ | --- | --- | --- |
101
+ | `args` | `Partial<MetaArgs>` | Args for this story |
102
+ | `argTypes` | `Record<string, unknown>` | Arg type overrides |
103
+ | `decorators` | `DecoratorFn[]` | Story-specific decorators |
104
+ | `parameters` | `Record<string, unknown>` | Story parameters |
105
+ | `tags` | `string[]` | Story tags |
106
+ | `render` | `(args, context) => VNodeChild` | Custom render for this story |
107
+ | `name` | `string` | Display name override |
108
+ | `play` | `(context) => Promise<void> \| void` | Interaction test function |
109
+
110
+ ### `DecoratorFn<TArgs>`
111
+
112
+ Decorator function type for wrapping stories.
113
+
114
+ ```tsx
115
+ const withTheme: DecoratorFn<{ label: string }> = (storyFn, context) => (
116
+ <div class="dark-theme">{storyFn(context.args, context)}</div>
117
+ )
118
+ ```
119
+
120
+ ### `StoryFn<TArgs>` / `StoryContext<TArgs>` / `InferProps<T>`
121
+
122
+ | Type | Description |
123
+ | --- | --- |
124
+ | `StoryFn<TArgs>` | `(args, context) => VNodeChild` |
125
+ | `StoryContext<TArgs>` | `{ args, argTypes, globals, id, kind, name, viewMode }` |
126
+ | `InferProps<T>` | Extract props type from a `ComponentFn<P>` |
127
+
128
+ ## Patterns
129
+
130
+ ### Decorators
131
+
132
+ Wrap stories with providers, themes, or layout containers.
133
+
134
+ ```tsx
135
+ const meta = {
136
+ component: Button,
137
+ decorators: [
138
+ (storyFn, context) => (
139
+ <div style="padding: 20px; background: #f5f5f5;">
140
+ {storyFn(context.args, context)}
141
+ </div>
142
+ ),
143
+ ],
144
+ } satisfies Meta<typeof Button>
145
+ ```
146
+
147
+ ### Interaction Tests
148
+
149
+ Use the `play` function for automated interaction testing.
150
+
151
+ ```ts
152
+ export const Clickable: Story = {
153
+ args: { label: "Click me" },
154
+ play: async ({ canvasElement, step }) => {
155
+ await step("click the button", async () => {
156
+ const button = canvasElement.querySelector("button")!
157
+ button.click()
158
+ })
159
+ },
160
+ }
161
+ ```
162
+
163
+ ### Reactive Stories
164
+
165
+ Use signals directly in stories to demonstrate interactive behavior.
166
+
167
+ ```tsx
168
+ import { signal, effect } from "@pyreon/storybook"
169
+
170
+ export const Interactive: Story = {
171
+ render: (args) => {
172
+ const count = signal(0)
173
+ return (
174
+ <div>
175
+ <p>{() => `Count: ${count()}`}</p>
176
+ <button onClick={() => count.update(n => n + 1)}>Increment</button>
177
+ </div>
178
+ )
179
+ },
180
+ }
181
+ ```
182
+
183
+ ## Re-exports
184
+
185
+ The following are re-exported for convenience in story files:
186
+
187
+ **From `@pyreon/core`:** `h`, `Fragment`
188
+
189
+ **From `@pyreon/reactivity`:** `signal`, `computed`, `effect`
190
+
191
+ **From `@pyreon/runtime-dom`:** `mount`
192
+
193
+ **Types from `@pyreon/core`:** `ComponentFn`, `Props`, `VNode`, `VNodeChild`
194
+
195
+ ## Gotchas
196
+
197
+ - Previous renders are automatically cleaned up (unmounted) before a new story is displayed. The cleanup state is tracked per canvas element via a `WeakMap`.
198
+ - If a story's render function throws, the error is caught and displayed via Storybook's error panel rather than crashing the UI.
199
+ - `defaultRender` simply calls `h(component, args)`. If your component needs children or special setup, provide a custom `render` function.
200
+ - The `play` function receives `canvasElement` for DOM queries and a `step` helper for organizing interaction sequences.