@gtkx/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.
Files changed (66) hide show
  1. package/dist/actions-panel.d.ts +7 -0
  2. package/dist/actions-panel.d.ts.map +1 -0
  3. package/dist/actions-panel.js +11 -0
  4. package/dist/actions-panel.js.map +1 -0
  5. package/dist/actions.d.ts +35 -0
  6. package/dist/actions.d.ts.map +1 -0
  7. package/dist/actions.js +161 -0
  8. package/dist/actions.js.map +1 -0
  9. package/dist/catalog.d.ts +71 -0
  10. package/dist/catalog.d.ts.map +1 -0
  11. package/dist/catalog.js +158 -0
  12. package/dist/catalog.js.map +1 -0
  13. package/dist/compose.d.ts +27 -0
  14. package/dist/compose.d.ts.map +1 -0
  15. package/dist/compose.js +67 -0
  16. package/dist/compose.js.map +1 -0
  17. package/dist/config.d.ts +13 -0
  18. package/dist/config.d.ts.map +1 -0
  19. package/dist/config.js +4 -0
  20. package/dist/config.js.map +1 -0
  21. package/dist/controls.d.ts +10 -0
  22. package/dist/controls.d.ts.map +1 -0
  23. package/dist/controls.js +94 -0
  24. package/dist/controls.js.map +1 -0
  25. package/dist/explorer-navigator.d.ts +11 -0
  26. package/dist/explorer-navigator.d.ts.map +1 -0
  27. package/dist/explorer-navigator.js +25 -0
  28. package/dist/explorer-navigator.js.map +1 -0
  29. package/dist/explorer-preview.d.ts +8 -0
  30. package/dist/explorer-preview.d.ts.map +1 -0
  31. package/dist/explorer-preview.js +89 -0
  32. package/dist/explorer-preview.js.map +1 -0
  33. package/dist/explorer-view.d.ts +15 -0
  34. package/dist/explorer-view.d.ts.map +1 -0
  35. package/dist/explorer-view.js +24 -0
  36. package/dist/explorer-view.js.map +1 -0
  37. package/dist/explorer.d.ts +22 -0
  38. package/dist/explorer.d.ts.map +1 -0
  39. package/dist/explorer.js +23 -0
  40. package/dist/explorer.js.map +1 -0
  41. package/dist/index.d.ts +5 -0
  42. package/dist/index.d.ts.map +1 -0
  43. package/dist/index.js +3 -0
  44. package/dist/index.js.map +1 -0
  45. package/dist/types.d.ts +143 -0
  46. package/dist/types.d.ts.map +1 -0
  47. package/dist/types.js +2 -0
  48. package/dist/types.js.map +1 -0
  49. package/dist/upstream.d.ts +14 -0
  50. package/dist/upstream.d.ts.map +1 -0
  51. package/dist/upstream.js +5 -0
  52. package/dist/upstream.js.map +1 -0
  53. package/package.json +86 -0
  54. package/src/actions-panel.tsx +32 -0
  55. package/src/actions.ts +234 -0
  56. package/src/catalog.ts +263 -0
  57. package/src/compose.ts +94 -0
  58. package/src/config.ts +14 -0
  59. package/src/controls.tsx +206 -0
  60. package/src/explorer-navigator.tsx +120 -0
  61. package/src/explorer-preview.tsx +207 -0
  62. package/src/explorer-view.tsx +93 -0
  63. package/src/explorer.ts +44 -0
  64. package/src/index.ts +16 -0
  65. package/src/types.ts +205 -0
  66. package/src/upstream.ts +24 -0
@@ -0,0 +1,143 @@
1
+ import type * as Gtk from "@gtkx/gi/gtk";
2
+ import type { ComponentType, ReactNode } from "react";
3
+ import type { ComponentAnnotations, GlobalTypes, Renderer, ArgTypes as StorybookArgTypes, StoryContext as StorybookContext } from "storybook/internal/types";
4
+ /** Named values supplied to a story, including component props and custom render args. */
5
+ type Args = Record<string, unknown>;
6
+ /** Storybook renderer types for React content rendered as native GTK widgets. */
7
+ type NativeRenderer<TArgs extends object = Args> = Renderer & {
8
+ /** React component receiving the story's args. */
9
+ component: ComponentType<TArgs>;
10
+ /** React content returned by story rendering. */
11
+ storyResult: ReactNode;
12
+ /** Native widget type representing the preview canvas. */
13
+ canvasElement: Gtk.Widget;
14
+ };
15
+ /** Explicit argument metadata used to configure native controls and named actions. */
16
+ type ArgTypes<TArgs extends object = Args> = Partial<StorybookArgTypes<TArgs>>;
17
+ /** Composed story identity, args, and annotations available to render functions and decorators. */
18
+ type StoryContext<TArgs extends object = Args> = Pick<StorybookContext<NativeRenderer<TArgs>, TArgs>, "id" | "name" | "title" | "args" | "initialArgs" | "argTypes" | "viewMode"> & {
19
+ /** Parameters merged from preview, component metadata, and the story. */
20
+ parameters: Args;
21
+ /** Global values after applying preview defaults and metadata or story overrides. */
22
+ globals: Args;
23
+ };
24
+ /** Renders native React content from the current story args and composition context. */
25
+ type StoryRender<TArgs extends object = Args> = (args: TArgs, context: StoryContext<TArgs>) => ReactNode;
26
+ /** Values a decorator can override when invoking the decorated story. */
27
+ type StoryContextUpdate<TArgs extends object = Args> = {
28
+ /** Partial args merged into the current invocation without changing composed defaults. */
29
+ args?: Partial<TArgs>;
30
+ /** Global values overridden for the decorated invocation. */
31
+ globals?: Args;
32
+ };
33
+ /** Wraps story content and can override args or globals when invoking the supplied Story function. */
34
+ type Decorator<TArgs extends object = Args> = (Story: (update?: StoryContextUpdate<TArgs>) => ReactNode, context: StoryContext<TArgs>) => ReactNode;
35
+ /** Shared CSF annotations accepted at preview, component, and story scope. */
36
+ type Annotations<TArgs extends object> = {
37
+ /** Default args, with more specific annotation scopes taking precedence. */
38
+ args?: Partial<TArgs>;
39
+ /** Explicit argument descriptions, controls, and action registrations. */
40
+ argTypes?: ArgTypes<TArgs>;
41
+ /** Parameters merged across scopes, including native preview and layout settings. */
42
+ parameters?: Args;
43
+ /** Wrappers composed around the story's render function. */
44
+ decorators?: Decorator<TArgs>[];
45
+ /** Custom renderer used in place of the component or a less specific render annotation. */
46
+ render?: StoryRender<TArgs>;
47
+ /** Storybook tags composed with tags from other annotation scopes. */
48
+ tags?: string[];
49
+ };
50
+ /** Default export metadata describing a component's stories and their shared annotations. */
51
+ type ComponentMeta<TArgs extends object> = Annotations<TArgs> & Pick<ComponentAnnotations<NativeRenderer<TArgs>, TArgs>, "title" | "id" | "includeStories" | "excludeStories"> & {
52
+ /** Component rendered with story args when no custom render annotation applies. */
53
+ component?: ComponentType<TArgs>;
54
+ /** Global overrides shared by this component's stories. */
55
+ globals?: Args;
56
+ };
57
+ /** Resolves a component's props or accepts an explicitly supplied args object type. */
58
+ type ComponentOrProps<T> = T extends ComponentType<infer Props extends object> ? Props : T extends object ? T : Args;
59
+ /** Presents combined inferred args as a single object type. */
60
+ type Simplify<T> = {
61
+ [Key in keyof T]: T[Key];
62
+ };
63
+ /** Combines the argument requirements of every member of a union. */
64
+ type UnionToIntersection<T> = (T extends unknown ? (value: T) => void : never) extends (value: infer Intersection) => void ? Intersection : never;
65
+ /** Retains explicitly named args while removing broad index signatures. */
66
+ type DeclaredArgs<T> = {
67
+ [Key in keyof T as string extends Key ? never : number extends Key ? never : symbol extends Key ? never : Key]: T[Key];
68
+ };
69
+ /** Extracts the args accepted by a custom render function. */
70
+ type RenderArgs<T> = T extends (args: infer Props extends object, context: never) => ReactNode ? Props : object;
71
+ /** Combines the args required by a metadata object's decorators. */
72
+ type DecoratorArgs<T> = T extends readonly (infer Item)[] ? UnionToIntersection<Item extends Decorator<infer Props extends object> ? Props : object> : object;
73
+ /** Infers story args from component props, render functions, decorators, or declared defaults. */
74
+ type ArgsFrom<T> = T extends {
75
+ component?: infer Component;
76
+ render?: infer Render;
77
+ decorators?: infer Decorators;
78
+ } ? Simplify<(NonNullable<Component> extends ComponentType<infer Props extends object> ? Props : object) & DeclaredArgs<RenderArgs<NonNullable<Render>> & DecoratorArgs<NonNullable<Decorators>>>> : T extends {
79
+ args: infer Defaults extends object;
80
+ } ? Defaults : Args;
81
+ /** CSF3 default export metadata typed from a React component or an explicit args type. */
82
+ type Meta<T = Args> = ComponentMeta<ComponentOrProps<T>>;
83
+ /** Annotations for one CSF3 story object, including its display name and global overrides. */
84
+ type StoryAnnotations<TArgs extends object> = Annotations<TArgs> & {
85
+ /** Display name overriding the name derived from the story's named export. */
86
+ name?: string;
87
+ /** Global overrides applied specifically to this story. */
88
+ globals?: Args;
89
+ };
90
+ /** Extracts args already defaulted by component metadata. */
91
+ type DefaultArgs<T> = T extends {
92
+ args: infer Defaults;
93
+ } ? Defaults : Record<never, never>;
94
+ /** Makes args with metadata defaults optional while preserving other required args. */
95
+ type RequiredStoryArgs<T, TArgs extends object> = Omit<TArgs, keyof DefaultArgs<T>> & Partial<Pick<TArgs, keyof TArgs & keyof DefaultArgs<T>>>;
96
+ /** CSF3 story object inferred from metadata, a React component, or an explicit args type. */
97
+ type StoryObj<T = Args> = T extends {
98
+ component?: unknown;
99
+ render?: unknown;
100
+ decorators?: unknown;
101
+ args?: unknown;
102
+ } ? StoryAnnotations<ArgsFrom<T>> & (Record<never, never> extends RequiredStoryArgs<T, ArgsFrom<T>> ? {
103
+ args?: RequiredStoryArgs<T, ArgsFrom<T>>;
104
+ } : {
105
+ args: RequiredStoryArgs<T, ArgsFrom<T>>;
106
+ }) : StoryAnnotations<ComponentOrProps<T>>;
107
+ /** Project annotations shared by all composed stories. */
108
+ type Preview = Annotations<Args> & {
109
+ /** Initial global values available in story rendering and decorators. */
110
+ initialGlobals?: Args;
111
+ /** Storybook metadata describing global values; native toolbar controls are not provided. */
112
+ globalTypes?: GlobalTypes;
113
+ };
114
+ /** Renderable story with merged annotations; props override its composed default args. */
115
+ type ComposedStory<TArgs extends object = Args> = ((args: Partial<TArgs>) => ReactNode) & {
116
+ /** Default args merged across preview, component metadata, and story annotations. */
117
+ args: Partial<TArgs>;
118
+ /** Composed argument metadata available to native controls and actions. */
119
+ argTypes: ArgTypes<TArgs>;
120
+ /** Composed parameters available to the renderer and explorer. */
121
+ parameters: Args;
122
+ /** Composed global values available in story context. */
123
+ globals: Args;
124
+ /** Storybook identifier derived from component identity and the story export. */
125
+ id: string;
126
+ /** Story name attached by Storybook's portable composition API. */
127
+ storyName: string;
128
+ /** Tags resulting from composition across annotation scopes. */
129
+ tags: string[];
130
+ };
131
+ /** Imported CSF module containing default metadata and named story exports. */
132
+ type StoryModule = {
133
+ /** Component metadata validated when the module is composed. */
134
+ default: unknown;
135
+ };
136
+ /** Named module exports whose types match CSF3 stories, excluding module bookkeeping. */
137
+ type StoryKeys<TModule extends StoryModule> = {
138
+ [Key in keyof TModule]: Key extends "default" | "__esModule" | "__namedExportsOrder" ? never : TModule[Key] extends StoryAnnotations<ArgsFrom<TModule["default"]>> ? Key : never;
139
+ }[keyof TModule] & string;
140
+ /** Composed stories keyed by their original named exports. */
141
+ type ComposedStories<TModule extends StoryModule> = Record<StoryKeys<TModule>, ComposedStory<ArgsFrom<TModule["default"]>>>;
142
+ export type { Args, ArgTypes, ComponentMeta, ComposedStories, ComposedStory, Decorator, Meta, Preview, StoryAnnotations, StoryContext, StoryContextUpdate, StoryModule, StoryObj, StoryRender, };
143
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,GAAG,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACtD,OAAO,KAAK,EACR,oBAAoB,EACpB,WAAW,EACX,QAAQ,EACR,QAAQ,IAAI,iBAAiB,EAC7B,YAAY,IAAI,gBAAgB,EACnC,MAAM,0BAA0B,CAAC;AAElC,0FAA0F;AAC1F,KAAK,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEpC,iFAAiF;AACjF,KAAK,cAAc,CAAC,KAAK,SAAS,MAAM,GAAG,IAAI,IAAI,QAAQ,GAAG;IAC1D,kDAAkD;IAClD,SAAS,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC;IAChC,iDAAiD;IACjD,WAAW,EAAE,SAAS,CAAC;IACvB,0DAA0D;IAC1D,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC;CAC7B,CAAC;AAEF,sFAAsF;AACtF,KAAK,QAAQ,CAAC,KAAK,SAAS,MAAM,GAAG,IAAI,IAAI,OAAO,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAAC;AAE/E,mGAAmG;AACnG,KAAK,YAAY,CAAC,KAAK,SAAS,MAAM,GAAG,IAAI,IAAI,IAAI,CACjD,gBAAgB,CAAC,cAAc,CAAC,K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EACP,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,EAClB,WAAW,EACX,QAAQ,EACR,WAAW,GACd,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type * as Gtk from \"@gtkx/gi/gtk\";\nimport type { ComponentType, ReactNode } from \"react\";\nimport type {\n ComponentAnnotations,\n GlobalTypes,\n Renderer,\n ArgTypes as StorybookArgTypes,\n StoryContext as StorybookContext,\n} from \"storybook/internal/types\";\n\n/** Named values supplied to a story, including component props and custom render args. */\ntype Args = Record<string, unknown>;\n\n/** Storybook renderer types for React content rendered as native GTK widgets. */\ntype NativeRenderer<TArgs extends object = Args> = Renderer & {\n /** React component receiving the story's args. */\n component: ComponentType<TArgs>;\n /** React content returned by story rendering. */\n storyResult: ReactNode;\n /** Native widget type representing the preview canvas. */\n canvasElement: Gtk.Widget;\n};\n\n/** Explicit argument metadata used to configure native controls and named actions. */\ntype ArgTypes<TArgs extends object = Args> = Partial<StorybookArgTypes<TArgs>>;\n\n/** Composed story identity, args, and annotations available to render functions and decorators. */\ntype StoryContext<TArgs extends object = Args> = Pick<\n StorybookContext<NativeRenderer<TArgs>, TArgs>,\n \"id\" | \"name\" | \"title\" | \"args\" | \"initialArgs\" | \"argTypes\" | \"viewMode\"\n> & {\n /** Parameters merged from preview, component metadata, and the story. */\n parameters: Args;\n /** Global values after applying preview defaults and metadata or story overrides. */\n globals: Args;\n};\n\n/** Renders native React content from the current story args and composition context. */\ntype StoryRender<TArgs extends object = Args> = (args: TArgs, context: StoryContext<TArgs>) => ReactNode;\n\n/** Values a decorator can override when invoking the decorated story. */\ntype StoryContextUpdate<TArgs extends object = Args> = {\n /** Partial args merged into the current invocation without changing composed defaults. */\n args?: Partial<TArgs>;\n /** Global values overridden for the decorated invocation. */\n globals?: Args;\n};\n\n/** Wraps story content and can override args or globals when invoking the supplied Story function. */\ntype Decorator<TArgs extends object = Args> = (\n Story: (update?: StoryContextUpdate<TArgs>) => ReactNode,\n context: StoryContext<TArgs>,\n) => ReactNode;\n\n/** Shared CSF annotations accepted at preview, component, and story scope. */\ntype Annotations<TArgs extends object> = {\n /** Default args, with more specific annotation scopes taking precedence. */\n args?: Partial<TArgs>;\n /** Explicit argument descriptions, controls, and action registrations. */\n argTypes?: ArgTypes<TArgs>;\n /** Parameters merged across scopes, including native preview and layout settings. */\n parameters?: Args;\n /** Wrappers composed around the story's render function. */\n decorators?: Decorator<TArgs>[];\n /** Custom renderer used in place of the component or a less specific render annotation. */\n render?: StoryRender<TArgs>;\n /** Storybook tags composed with tags from other annotation scopes. */\n tags?: string[];\n};\n\n/** Default export metadata describing a component's stories and their shared annotations. */\ntype ComponentMeta<TArgs extends object> = Annotations<TArgs> & Pick<\n ComponentAnnotations<NativeRenderer<TArgs>, TArgs>,\n \"title\" | \"id\" | \"includeStories\" | \"excludeStories\"\n> & {\n /** Component rendered with story args when no custom render annotation applies. */\n component?: ComponentType<TArgs>;\n /** Global overrides shared by this component's stories. */\n globals?: Args;\n};\n\n/** Resolves a component's props or accepts an explicitly supplied args object type. */\ntype ComponentOrProps<T> = T extends ComponentType<infer Props extends object>\n ? Props\n : T extends object ? T : Args;\n\n/** Presents combined inferred args as a single object type. */\ntype Simplify<T> = { [Key in keyof T]: T[Key] };\n\n/** Combines the argument requirements of every member of a union. */\ntype UnionToIntersection<T> = (\n T extends unknown ? (value: T) => void : never\n) extends (value: infer Intersection) => void ? Intersection : never;\n\n/** Retains explicitly named args while removing broad index signatures. */\ntype DeclaredArgs<T> = {\n [Key in keyof T as string extends Key\n ? never\n : number extends Key ? never : symbol extends Key ? never : Key]: T[Key];\n};\n\n/** Extracts the args accepted by a custom render function. */\ntype RenderArgs<T> = T extends (args: infer Props extends object, context: never) => ReactNode ? Props : object;\n\n/** Combines the args required by a metadata object's decorators. */\ntype DecoratorArgs<T> = T extends readonly (infer Item)[]\n ? UnionToIntersection<Item extends Decorator<infer Props extends object> ? Props : object>\n : object;\n\n/** Infers story args from component props, render functions, decorators, or declared defaults. */\ntype ArgsFrom<T> = T extends { component?: infer Component; render?: infer Render; decorators?: infer Decorators }\n ? Simplify<\n (NonNullable<Component> extends ComponentType<infer Props extends object> ? Props : object) &\n DeclaredArgs<RenderArgs<NonNullable<Render>> & DecoratorArgs<NonNullable<Decorators>>>\n >\n : T extends { args: infer Defaults extends object } ? Defaults : Args;\n\n/** CSF3 default export metadata typed from a React component or an explicit args type. */\ntype Meta<T = Args> = ComponentMeta<ComponentOrProps<T>>;\n\n/** Annotations for one CSF3 story object, including its display name and global overrides. */\ntype StoryAnnotations<TArgs extends object> = Annotations<TArgs> & {\n /** Display name overriding the name derived from the story's named export. */\n name?: string;\n /** Global overrides applied specifically to this story. */\n globals?: Args;\n};\n\n/** Extracts args already defaulted by component metadata. */\ntype DefaultArgs<T> = T extends { args: infer Defaults } ? Defaults : Record<never, never>;\n\n/** Makes args with metadata defaults optional while preserving other required args. */\ntype RequiredStoryArgs<T, TArgs extends object> =\n Omit<TArgs, keyof DefaultArgs<T>> & Partial<Pick<TArgs, keyof TArgs & keyof DefaultArgs<T>>>;\n\n/** CSF3 story object inferred from metadata, a React component, or an explicit args type. */\ntype StoryObj<T = Args> = T extends { component?: unknown; render?: unknown; decorators?: unknown; args?: unknown }\n ? StoryAnnotations<ArgsFrom<T>> & (Record<never, never> extends RequiredStoryArgs<T, ArgsFrom<T>>\n ? { args?: RequiredStoryArgs<T, ArgsFrom<T>> }\n : { args: RequiredStoryArgs<T, ArgsFrom<T>> })\n : StoryAnnotations<ComponentOrProps<T>>;\n\n/** Project annotations shared by all composed stories. */\ntype Preview = Annotations<Args> & {\n /** Initial global values available in story rendering and decorators. */\n initialGlobals?: Args;\n /** Storybook metadata describing global values; native toolbar controls are not provided. */\n globalTypes?: GlobalTypes;\n};\n\n/** Renderable story with merged annotations; props override its composed default args. */\ntype ComposedStory<TArgs extends object = Args> = ((args: Partial<TArgs>) => ReactNode) & {\n /** Default args merged across preview, component metadata, and story annotations. */\n args: Partial<TArgs>;\n /** Composed argument metadata available to native controls and actions. */\n argTypes: ArgTypes<TArgs>;\n /** Composed parameters available to the renderer and explorer. */\n parameters: Args;\n /** Composed global values available in story context. */\n globals: Args;\n /** Storybook identifier derived from component identity and the story export. */\n id: string;\n /** Story name attached by Storybook's portable composition API. */\n storyName: string;\n /** Tags resulting from composition across annotation scopes. */\n tags: string[];\n};\n\n/** Imported CSF module containing default metadata and named story exports. */\ntype StoryModule = {\n /** Component metadata validated when the module is composed. */\n default: unknown;\n};\n\n/** Named module exports whose types match CSF3 stories, excluding module bookkeeping. */\ntype StoryKeys<TModule extends StoryModule> = {\n [Key in keyof TModule]: Key extends \"default\" | \"__esModule\" | \"__namedExportsOrder\"\n ? never\n : TModule[Key] extends StoryAnnotations<ArgsFrom<TModule[\"default\"]>>\n ? Key\n : never;\n}[keyof TModule] & string;\n\n/** Composed stories keyed by their original named exports. */\ntype ComposedStories<TModule extends StoryModule> = Record<\n StoryKeys<TModule>,\n ComposedStory<ArgsFrom<TModule[\"default\"]>>\n>;\n\nexport type {\n Args,\n ArgTypes,\n ComponentMeta,\n ComposedStories,\n ComposedStory,\n Decorator,\n Meta,\n Preview,\n StoryAnnotations,\n StoryContext,\n StoryContextUpdate,\n StoryModule,\n StoryObj,\n StoryRender,\n};\n"]}
@@ -0,0 +1,14 @@
1
+ import type { ComponentMeta, ComposedStory, Preview, StoryAnnotations, StoryRender } from "./types.js";
2
+ type ComposeStory = <TArgs extends object>(...annotations: [
3
+ story: StoryAnnotations<TArgs>,
4
+ meta: ComponentMeta<TArgs>,
5
+ preview: Preview,
6
+ renderer: {
7
+ render: StoryRender<TArgs>;
8
+ },
9
+ exportName?: string
10
+ ]) => ComposedStory<TArgs>;
11
+ declare const composeStoryAnnotations: ComposeStory;
12
+ export { composeStoryAnnotations };
13
+ export { isExportStory, storyNameFromExport } from "storybook/internal/csf";
14
+ //# sourceMappingURL=upstream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upstream.d.ts","sourceRoot":"","sources":["../src/upstream.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACR,aAAa,EACb,aAAa,EACb,OAAO,EACP,gBAAgB,EAChB,WAAW,EACd,MAAM,YAAY,CAAC;AAEpB,KAAK,YAAY,GAAG,CAAC,KAAK,SAAS,MAAM,EACrC,GAAG,WAAW,EAAE;IACZ,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC;IAC9B,IAAI,EAAE,aAAa,CAAC,KAAK,CAAC;IAC1B,OAAO,EAAE,OAAO;IAChB,QAAQ,EAAE;QAAE,MAAM,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;KAAE;IACxC,UAAU,CAAC,EAAE,MAAM;CACtB,KACA,aAAa,CAAC,KAAK,CAAC,CAAC;AAE1B,QAAA,MAAM,uBAAuB,EAAmB,YAAY,CAAC;AAE7D,OAAO,EAAE,uBAAuB,EAAE,CAAC;AAEnC,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { composeStory } from "storybook/preview-api";
2
+ const composeStoryAnnotations = composeStory;
3
+ export { composeStoryAnnotations };
4
+ export { isExportStory, storyNameFromExport } from "storybook/internal/csf";
5
+ //# sourceMappingURL=upstream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upstream.js","sourceRoot":"","sources":["../src/upstream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAmBrD,MAAM,uBAAuB,GAAG,YAA4B,CAAC;AAE7D,OAAO,EAAE,uBAAuB,EAAE,CAAC;AAEnC,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC","sourcesContent":["import { composeStory } from \"storybook/preview-api\";\nimport type {\n ComponentMeta,\n ComposedStory,\n Preview,\n StoryAnnotations,\n StoryRender,\n} from \"./types.js\";\n\ntype ComposeStory = <TArgs extends object>(\n ...annotations: [\n story: StoryAnnotations<TArgs>,\n meta: ComponentMeta<TArgs>,\n preview: Preview,\n renderer: { render: StoryRender<TArgs> },\n exportName?: string,\n ]\n) => ComposedStory<TArgs>;\n\nconst composeStoryAnnotations = composeStory as ComposeStory;\n\nexport { composeStoryAnnotations };\n\nexport { isExportStory, storyNameFromExport } from \"storybook/internal/csf\";\n"]}
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "@gtkx/storybook",
3
+ "version": "0.0.1",
4
+ "description": "Native story explorer and portable Component Story Format fixtures for GTKX applications.",
5
+ "keywords": [
6
+ "gtkx",
7
+ "gtk",
8
+ "gtk4",
9
+ "adwaita",
10
+ "react",
11
+ "storybook",
12
+ "csf",
13
+ "linux"
14
+ ],
15
+ "homepage": "https://gtkx.dev",
16
+ "bugs": {
17
+ "url": "https://github.com/gtkx-org/gtkx/issues"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/gtkx-org/gtkx.git",
22
+ "directory": "packages/storybook"
23
+ },
24
+ "license": "MPL-2.0",
25
+ "author": "Eugenio Depalo <eugeniodepalo@gmail.com>",
26
+ "type": "module",
27
+ "exports": {
28
+ "./package.json": "./package.json",
29
+ ".": {
30
+ "source": "./src/index.ts",
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "require": null,
34
+ "default": "./dist/index.js"
35
+ },
36
+ "./config": {
37
+ "source": "./src/config.ts",
38
+ "types": "./dist/config.d.ts",
39
+ "import": "./dist/config.js",
40
+ "require": null,
41
+ "default": "./dist/config.js"
42
+ },
43
+ "./explorer": {
44
+ "source": "./src/explorer.ts",
45
+ "types": "./dist/explorer.d.ts",
46
+ "import": "./dist/explorer.js",
47
+ "require": null,
48
+ "default": "./dist/explorer.js"
49
+ }
50
+ },
51
+ "sideEffects": false,
52
+ "files": [
53
+ "dist",
54
+ "src"
55
+ ],
56
+ "scripts": {
57
+ "release": "tsx ../../scripts/release-package.ts"
58
+ },
59
+ "nx": {
60
+ "targets": {
61
+ "lint": {
62
+ "options": {
63
+ "cwd": ".",
64
+ "command": "eslint {projectRoot}"
65
+ }
66
+ }
67
+ }
68
+ },
69
+ "engines": {
70
+ "node": ">=26.7.0"
71
+ },
72
+ "dependencies": {
73
+ "@gtkx/components": "workspace:*",
74
+ "@gtkx/react": "workspace:*",
75
+ "storybook": "10.6.0"
76
+ },
77
+ "peerDependencies": {
78
+ "react": "^19.2"
79
+ },
80
+ "devDependencies": {
81
+ "@gtkx/config": "workspace:*",
82
+ "@gtkx/testing": "workspace:*",
83
+ "@gtkx/vitest": "workspace:*",
84
+ "vitest": "catalog:"
85
+ }
86
+ }
@@ -0,0 +1,32 @@
1
+ import type { ReactNode } from "react";
2
+ import { markupEscapeText } from "@gtkx/gi/glib";
3
+ import { AdwActionRow, AdwPreferencesGroup } from "@gtkx/jsx/adw";
4
+ import { GtkButton } from "@gtkx/jsx/gtk";
5
+ import { useSyncExternalStore } from "react";
6
+ import type { ActionStore } from "./actions.js";
7
+
8
+ const ActionsPanel = ({ store }: { store: ActionStore }): ReactNode => {
9
+ const entries = useSyncExternalStore(store.subscribe, store.getSnapshot);
10
+
11
+ return (
12
+ <AdwPreferencesGroup
13
+ title="Actions"
14
+ description={entries.length === 0 ? "Interact with the story to record events." : ""}
15
+ headerSuffix={<GtkButton label="Clear actions" sensitive={entries.length > 0} onClicked={store.clear} />}
16
+ >
17
+ {entries.toReversed().map((entry) => (
18
+ <AdwActionRow
19
+ key={entry.id}
20
+ name={`storybook-action-${String(entry.id)}`}
21
+ title={markupEscapeText(entry.name, -1)}
22
+ subtitle={markupEscapeText(entry.error ?? entry.args.join(", "), -1)}
23
+ subtitleSelectable
24
+ subtitleLines={3}
25
+ cssClasses={entry.error === undefined ? [] : ["error"]}
26
+ />
27
+ ))}
28
+ </AdwPreferencesGroup>
29
+ );
30
+ };
31
+
32
+ export { ActionsPanel };
package/src/actions.ts ADDED
@@ -0,0 +1,234 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { inspect } from "node:util";
3
+ import type { Args, ArgTypes } from "./types.js";
4
+
5
+ type ActionEntry = {
6
+ id: number;
7
+ name: string;
8
+ args: string[];
9
+ timestamp: number;
10
+ error?: string;
11
+ };
12
+
13
+ /** A named story callback whose arguments can be recorded by the native explorer. */
14
+ type ActionCallback = (...args: unknown[]) => void;
15
+ type EventCallback = (...args: unknown[]) => unknown;
16
+
17
+ const actionNames: WeakMap<object, string> = new WeakMap();
18
+ const actionScope: AsyncLocalStorage<ActionStore> = new AsyncLocalStorage();
19
+ const DEFAULT_HISTORY_LIMIT = 100;
20
+ const MAX_ARGUMENT_LENGTH = 400;
21
+
22
+ const objectLabel = (value: unknown): string | undefined => {
23
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
24
+ return undefined;
25
+ }
26
+
27
+ const prototype: unknown = Object.getPrototypeOf(value);
28
+
29
+ if (prototype === null || typeof prototype !== "object" || prototype === Object.prototype) {
30
+ return undefined;
31
+ }
32
+
33
+ const constructor: unknown = Object.getOwnPropertyDescriptor(prototype, "constructor")?.value;
34
+
35
+ return typeof constructor === "function" ? `[${constructor.name}]` : "[Object]";
36
+ };
37
+
38
+ const formatArgument = (value: unknown): string => {
39
+ try {
40
+ const label = objectLabel(value);
41
+
42
+ if (label !== undefined) {
43
+ return label;
44
+ }
45
+
46
+ return inspect(value, {
47
+ depth: 1,
48
+ maxArrayLength: 5,
49
+ maxStringLength: 120,
50
+ customInspect: false,
51
+ getters: false,
52
+ breakLength: Infinity,
53
+ }).slice(0, MAX_ARGUMENT_LENGTH);
54
+ } catch {
55
+ return "[Unreadable value]";
56
+ }
57
+ };
58
+
59
+ /**
60
+ * Creates a named action for a story's callback arguments.
61
+ * The explorer records bounded argument summaries; calls outside its action scope have no effect.
62
+ *
63
+ * @param name Nonempty label displayed in the Actions panel.
64
+ * @returns A callback to supply in story args.
65
+ * @throws If the name is empty or contains only whitespace.
66
+ */
67
+ const action = (name: string): ActionCallback => {
68
+ if (name.trim().length === 0) {
69
+ throw new TypeError("An action needs a name");
70
+ }
71
+
72
+ const callback: ActionCallback = (...args) => {
73
+ actionScope.getStore()?.record(name, args);
74
+ };
75
+ actionNames.set(callback, name);
76
+
77
+ return callback;
78
+ };
79
+
80
+ class ActionStore {
81
+ private entries: ActionEntry[] = [];
82
+ private readonly listeners: Set<() => void> = new Set();
83
+ private nextId = 0;
84
+ private readonly limit: number;
85
+
86
+ getSnapshot = (): ActionEntry[] => this.entries;
87
+
88
+ subscribe = (listener: () => void): (() => void) => {
89
+ this.listeners.add(listener);
90
+
91
+ return () => {
92
+ this.listeners.delete(listener);
93
+ };
94
+ };
95
+
96
+ clear = (): void => {
97
+ this.publish([]);
98
+ };
99
+
100
+ constructor(limit: number = DEFAULT_HISTORY_LIMIT) {
101
+ if (!Number.isSafeInteger(limit) || limit < 1) {
102
+ throw new RangeError("Action history needs a positive integer limit");
103
+ }
104
+
105
+ this.limit = limit;
106
+ }
107
+
108
+ private publish(entries: ActionEntry[]): void {
109
+ this.entries = entries;
110
+
111
+ for (const listener of this.listeners) {
112
+ listener();
113
+ }
114
+ }
115
+
116
+ record(name: string, args: unknown[], error?: Error): void {
117
+ const entry: ActionEntry = {
118
+ id: this.nextId++,
119
+ name,
120
+ args: args.slice(0, 8).map((value) => formatArgument(value)),
121
+ timestamp: Date.now(),
122
+ ...(error !== undefined && { error: error.message.slice(0, MAX_ARGUMENT_LENGTH) }),
123
+ };
124
+ this.publish([...this.entries, entry].slice(-this.limit));
125
+ }
126
+ }
127
+
128
+ type CallbackOptions = { callback?: EventCallback; name: string; onError?: (error: Error) => void };
129
+
130
+ const settleCallback = async (
131
+ result: Promise<unknown>,
132
+ succeed: () => void,
133
+ fail: (cause: unknown) => void,
134
+ ): Promise<unknown> => {
135
+ try {
136
+ const value = await result;
137
+ succeed();
138
+
139
+ return value;
140
+ } catch (error) {
141
+ fail(error);
142
+
143
+ return undefined;
144
+ }
145
+ };
146
+
147
+ const bindCallback = (
148
+ store: ActionStore,
149
+ options: CallbackOptions,
150
+ ): EventCallback => (...values) => {
151
+ const succeed = (): void => {
152
+ store.record(options.name, values);
153
+ };
154
+ const fail = (cause: unknown): void => {
155
+ const error = cause instanceof Error ? cause : new Error(String(cause));
156
+ store.record(options.name, values, error);
157
+
158
+ if (options.onError === undefined) {
159
+ throw error;
160
+ }
161
+
162
+ options.onError(error);
163
+ };
164
+
165
+ try {
166
+ const result = actionScope.run(store, () => options.callback?.(...values));
167
+
168
+ if (result instanceof Promise) {
169
+ return settleCallback(result, succeed, fail);
170
+ }
171
+
172
+ succeed();
173
+
174
+ return result;
175
+ } catch (error) {
176
+ fail(error);
177
+
178
+ return;
179
+ }
180
+ };
181
+
182
+ const actionName = (value: unknown, configuredName: unknown): string | undefined => {
183
+ if (typeof configuredName === "string") {
184
+ return configuredName;
185
+ }
186
+
187
+ return typeof value === "function" ? actionNames.get(value) : undefined;
188
+ };
189
+
190
+ const isOriginalCallback = (value: unknown): value is EventCallback =>
191
+ typeof value === "function" && !actionNames.has(value);
192
+
193
+ const callbackOptions = (
194
+ value: unknown,
195
+ name: string | undefined,
196
+ onError: ((error: Error) => void) | undefined,
197
+ ): CallbackOptions | undefined => {
198
+ if (name === undefined) {
199
+ return undefined;
200
+ }
201
+
202
+ return {
203
+ name,
204
+ ...(isOriginalCallback(value) && { callback: value }),
205
+ ...(onError !== undefined && { onError }),
206
+ };
207
+ };
208
+
209
+ const bindActions = (
210
+ args: Args,
211
+ argTypes: ArgTypes,
212
+ store: ActionStore,
213
+ onError?: (error: Error) => void,
214
+ ): Args => {
215
+ const bound = { ...args };
216
+ const names = new Set([...Object.keys(args), ...Object.keys(argTypes)]);
217
+
218
+ for (const argument of names) {
219
+ const value = args[argument];
220
+ const name = actionName(value, argTypes[argument]?.action);
221
+ const options = callbackOptions(value, name, onError);
222
+
223
+ if (options === undefined) {
224
+ continue;
225
+ }
226
+
227
+ bound[argument] = bindCallback(store, options);
228
+ }
229
+
230
+ return bound;
231
+ };
232
+
233
+ export { action, ActionStore, bindActions };
234
+ export type { ActionCallback };