-
Notifications
You must be signed in to change notification settings - Fork 47
/
story.ts
416 lines (352 loc) · 13.6 KB
/
story.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import type { RemoveIndexSignature, Simplify, UnionToIntersection } from 'type-fest';
import type { SBScalarType, SBType } from './SBType';
export * from './SBType.js';
export type StoryId = string;
export type ComponentId = string;
export type ComponentTitle = string;
export type StoryName = string;
/** @deprecated */
export type StoryKind = ComponentTitle;
export type Tag = string;
export interface StoryIdentifier {
componentId: ComponentId;
title: ComponentTitle;
/** @deprecated */
kind: ComponentTitle;
id: StoryId;
name: StoryName;
/** @deprecated */
story: StoryName;
tags: Tag[];
}
export interface Parameters {
[name: string]: any;
}
export interface StrictParameters {
[name: string]: unknown;
}
type ConditionalTest = { truthy?: boolean } | { exists: boolean } | { eq: any } | { neq: any };
type ConditionalValue = { arg: string } | { global: string };
export type Conditional = ConditionalValue & ConditionalTest;
export interface InputType {
name?: string;
description?: string;
defaultValue?: any;
type?: SBType | SBScalarType['name'];
if?: Conditional;
[key: string]: any;
}
export interface StrictInputType extends InputType {
name: string;
type?: SBType;
}
export interface Args {
[name: string]: any;
}
export interface StrictArgs {
[name: string]: unknown;
}
export type ArgTypes<TArgs = Args> = { [name in keyof TArgs]: InputType };
export type StrictArgTypes<TArgs = Args> = { [name in keyof TArgs]: StrictInputType };
export type Globals = { [name: string]: any };
export type GlobalTypes = { [name: string]: InputType };
export type StrictGlobalTypes = { [name: string]: StrictInputType };
export type Renderer = {
/** What is the type of the `component` annotation in this renderer? */
component: unknown;
/** What does the story function return in this renderer? */
storyResult: unknown;
/** What type of element does this renderer render to? */
canvasElement: unknown;
// A generic type T that can be used in the definition of the component like this:
// component: (args: this['T']) => string;
// This generic type will eventually be filled in with TArgs
// Credits to Michael Arnaldi.
T?: unknown;
};
/** @deprecated - use `Renderer` */
export type AnyFramework = Renderer;
export type StoryContextForEnhancers<
TRenderer extends Renderer = Renderer,
TArgs = Args
> = StoryIdentifier & {
component?: (TRenderer & { T: any })['component'];
subcomponents?: Record<string, (TRenderer & { T: any })['component']>;
parameters: Parameters;
initialArgs: TArgs;
argTypes: StrictArgTypes<TArgs>;
};
export type ArgsEnhancer<TRenderer extends Renderer = Renderer, TArgs = Args> = (
context: StoryContextForEnhancers<TRenderer, TArgs>
) => TArgs;
export type ArgTypesEnhancer<TRenderer extends Renderer = Renderer, TArgs = Args> = ((
context: StoryContextForEnhancers<TRenderer, TArgs>
) => StrictArgTypes<TArgs>) & {
secondPass?: boolean;
};
export type StoryContextUpdate<TArgs = Args> = {
args?: TArgs;
globals?: Globals;
// NOTE: it is currently possibly to add *any* key you like to the context
// (although you cannot override the basic keys). This will likely be removed in future.
[key: string]: any;
};
export type ViewMode = 'story' | 'docs';
export type StoryContextForLoaders<
TRenderer extends Renderer = Renderer,
TArgs = Args
> = StoryContextForEnhancers<TRenderer, TArgs> &
Required<StoryContextUpdate<TArgs>> & {
hooks: unknown;
viewMode: ViewMode;
originalStoryFn: StoryFn<TRenderer>;
};
export type LoaderFunction<TRenderer extends Renderer = Renderer, TArgs = Args> = (
context: StoryContextForLoaders<TRenderer, TArgs>
) => Promise<Record<string, any>>;
export type StoryContext<
TRenderer extends Renderer = Renderer,
TArgs = Args
> = StoryContextForLoaders<TRenderer, TArgs> & {
loaded: Record<string, any>;
abortSignal: AbortSignal;
canvasElement: TRenderer['canvasElement'];
};
export type StepLabel = string;
export type StepFunction<TRenderer extends Renderer = Renderer, TArgs = Args> = (
label: StepLabel,
play: PlayFunction<TRenderer, TArgs>
) => Promise<void> | void;
export type PlayFunctionContext<TRenderer extends Renderer = Renderer, TArgs = Args> = StoryContext<
TRenderer,
TArgs
> & {
step: StepFunction<TRenderer, TArgs>;
};
export type PlayFunction<TRenderer extends Renderer = Renderer, TArgs = Args> = (
context: PlayFunctionContext<TRenderer, TArgs>
) => Promise<void> | void;
// This is the type of story function passed to a decorator -- does not rely on being passed any context
export type PartialStoryFn<TRenderer extends Renderer = Renderer, TArgs = Args> = (
update?: StoryContextUpdate<Partial<TArgs>>
) => TRenderer['storyResult'];
// This is a passArgsFirst: false user story function
export type LegacyStoryFn<TRenderer extends Renderer = Renderer, TArgs = Args> = (
context: StoryContext<TRenderer, TArgs>
) => TRenderer['storyResult'];
// This is a passArgsFirst: true user story function
export type ArgsStoryFn<TRenderer extends Renderer = Renderer, TArgs = Args> = (
args: TArgs,
context: StoryContext<TRenderer, TArgs>
) => (TRenderer & { T: TArgs })['storyResult'];
// This is either type of user story function
export type StoryFn<TRenderer extends Renderer = Renderer, TArgs = Args> =
| LegacyStoryFn<TRenderer, TArgs>
| ArgsStoryFn<TRenderer, TArgs>;
export type DecoratorFunction<TRenderer extends Renderer = Renderer, TArgs = Args> = (
fn: PartialStoryFn<TRenderer, TArgs>,
c: StoryContext<TRenderer, TArgs>
) => TRenderer['storyResult'];
export type DecoratorApplicator<TRenderer extends Renderer = Renderer, TArgs = Args> = (
storyFn: LegacyStoryFn<TRenderer, TArgs>,
decorators: DecoratorFunction<TRenderer, TArgs>[]
) => LegacyStoryFn<TRenderer, TArgs>;
export type StepRunner<TRenderer extends Renderer = Renderer, TArgs = Args> = (
label: StepLabel,
play: PlayFunction<TRenderer, TArgs>,
context: PlayFunctionContext<TRenderer, TArgs>
) => Promise<void>;
export type BaseAnnotations<TRenderer extends Renderer = Renderer, TArgs = Args> = {
/**
* Wrapper components or Storybook decorators that wrap a story.
*
* Decorators defined in Meta will be applied to every story variation.
* @see [Decorators](https://storybook.js.org/docs/addons/introduction/#1-decorators)
*/
decorators?: DecoratorFunction<TRenderer, Simplify<TArgs>>[];
/**
* Custom metadata for a story.
* @see [Parameters](https://storybook.js.org/docs/basics/writing-stories/#parameters)
*/
parameters?: Parameters;
/**
* Dynamic data that are provided (and possibly updated by) Storybook and its addons.
* @see [Arg story inputs](https://storybook.js.org/docs/react/api/csf#args-story-inputs)
*/
args?: Partial<TArgs>;
/**
* ArgTypes encode basic metadata for args, such as `name`, `description`, `defaultValue` for an arg. These get automatically filled in by Storybook Docs.
* @see [Control annotations](https://github.com/storybookjs/storybook/blob/91e9dee33faa8eff0b342a366845de7100415367/addons/controls/README.md#control-annotations)
*/
argTypes?: Partial<ArgTypes<TArgs>>;
/**
* Asynchronous functions which provide data for a story.
* @see [Loaders](https://storybook.js.org/docs/react/writing-stories/loaders)
*/
loaders?: LoaderFunction<TRenderer, TArgs>[];
/**
* Define a custom render function for the story(ies). If not passed, a default render function by the renderer will be used.
*/
render?: ArgsStoryFn<TRenderer, TArgs>;
};
export type ProjectAnnotations<
TRenderer extends Renderer = Renderer,
TArgs = Args
> = BaseAnnotations<TRenderer, TArgs> & {
argsEnhancers?: ArgsEnhancer<TRenderer, Args>[];
argTypesEnhancers?: ArgTypesEnhancer<TRenderer, Args>[];
globals?: Globals;
globalTypes?: GlobalTypes;
applyDecorators?: DecoratorApplicator<TRenderer, Args>;
runStep?: StepRunner<TRenderer, TArgs>;
};
type StoryDescriptor = string[] | RegExp;
export interface ComponentAnnotations<TRenderer extends Renderer = Renderer, TArgs = Args>
extends BaseAnnotations<TRenderer, TArgs> {
/**
* Title of the component which will be presented in the navigation. **Should be unique.**
*
* Components can be organized in a nested structure using "/" as a separator.
*
* Since CSF 3.0 this property is optional -- it can be inferred from the filesystem path
*
* @example
* export default {
* ...
* title: 'Design System/Atoms/Button'
* }
*
* @see [Story Hierarchy](https://storybook.js.org/docs/basics/writing-stories/#story-hierarchy)
*/
title?: ComponentTitle;
/**
* Id of the component (prefix of the story id) which is used for URLs.
*
* By default is inferred from sanitizing the title
*
* @see [Story Hierarchy](https://storybook.js.org/docs/basics/writing-stories/#story-hierarchy)
*/
id?: ComponentId;
/**
* Used to only include certain named exports as stories. Useful when you want to have non-story exports such as mock data or ignore a few stories.
* @example
* includeStories: ['SimpleStory', 'ComplexStory']
* includeStories: /.*Story$/
*
* @see [Non-story exports](https://storybook.js.org/docs/formats/component-story-format/#non-story-exports)
*/
includeStories?: StoryDescriptor;
/**
* Used to exclude certain named exports. Useful when you want to have non-story exports such as mock data or ignore a few stories.
* @example
* excludeStories: ['simpleData', 'complexData']
* excludeStories: /.*Data$/
*
* @see [Non-story exports](https://storybook.js.org/docs/formats/component-story-format/#non-story-exports)
*/
excludeStories?: StoryDescriptor;
/**
* The primary component for your story.
*
* Used by addons for automatic prop table generation and display of other component metadata.
*/
component?: (TRenderer & {
// We fall back to `any` when TArgs is missing (and so TArgs will be Args).
// We also fallback to `any` for any other "top" type (Record<string, any>, Record<string, unknown>, any, unknown).
// This is because you can not assign Component with more specific props, to a Component that accepts anything
// For example this won't compile
// const Button: FC<Args> = (props: {prop: number} ) => {}
//
// Note that the subtyping relationship is inversed for T and (t: T) => any. As this is fine:
// const args: Args = { prop: 1 };
// The correct way would probably to fall back to `never`, being the inverse of unknown. Or maybe `Record<string, never>`
//
// Any is really weird as it pretends to be never and unknown at the same time (so being the absolute bottom and top type at the same time)
// However, I don't have the guts to fallback to Record<string, never>, forgive me.
//
// If this all doesn't make sense, you may want to look at the test: You can assign a component to Meta, even when you pass a top type.
T: Record<string, unknown> extends Required<TArgs> ? any : TArgs;
})['component'];
/**
* Auxiliary subcomponents that are part of the stories.
*
* Used by addons for automatic prop table generation and display of other component metadata.
*
* @example
* import { Button, ButtonGroup } from './components';
*
* export default {
* ...
* subcomponents: { Button, ButtonGroup }
* }
*
* By defining them each component will have its tab in the args table.
*/
subcomponents?: Record<string, TRenderer['component']>;
/**
* Function that is executed after the story is rendered.
*/
play?: PlayFunction<TRenderer, TArgs>;
/**
* Named tags for a story, used to filter stories in different contexts.
*/
tags?: Tag[];
}
export type StoryAnnotations<
TRenderer extends Renderer = Renderer,
TArgs = Args,
TRequiredArgs = Partial<TArgs>
> = BaseAnnotations<TRenderer, TArgs> & {
/**
* Override the display name in the UI (CSF v3)
*/
name?: StoryName;
/**
* Override the display name in the UI (CSF v2)
*/
storyName?: StoryName;
/**
* Function that is executed after the story is rendered.
*/
play?: PlayFunction<TRenderer, TArgs>;
/**
* Named tags for a story, used to filter stories in different contexts.
*/
tags?: Tag[];
/** @deprecated */
story?: Omit<StoryAnnotations<TRenderer, TArgs>, 'story'>;
// eslint-disable-next-line @typescript-eslint/ban-types
} & ({} extends TRequiredArgs ? { args?: TRequiredArgs } : { args: TRequiredArgs });
export type LegacyAnnotatedStoryFn<TRenderer extends Renderer = Renderer, TArgs = Args> = StoryFn<
TRenderer,
TArgs
> &
StoryAnnotations<TRenderer, TArgs>;
export type LegacyStoryAnnotationsOrFn<TRenderer extends Renderer = Renderer, TArgs = Args> =
| LegacyAnnotatedStoryFn<TRenderer, TArgs>
| StoryAnnotations<TRenderer, TArgs>;
export type AnnotatedStoryFn<TRenderer extends Renderer = Renderer, TArgs = Args> = ArgsStoryFn<
TRenderer,
TArgs
> &
StoryAnnotations<TRenderer, TArgs>;
export type StoryAnnotationsOrFn<TRenderer extends Renderer = Renderer, TArgs = Args> =
| AnnotatedStoryFn<TRenderer, TArgs>
| StoryAnnotations<TRenderer, TArgs>;
export type ArgsFromMeta<TRenderer extends Renderer, Meta> = Meta extends {
render?: ArgsStoryFn<TRenderer, infer RArgs>;
loaders?: (infer Loaders)[];
decorators?: (infer Decorators)[];
}
? Simplify<
RemoveIndexSignature<
RArgs & DecoratorsArgs<TRenderer, Decorators> & LoaderArgs<TRenderer, Loaders>
>
>
: unknown;
type DecoratorsArgs<TRenderer extends Renderer, Decorators> = UnionToIntersection<
Decorators extends DecoratorFunction<TRenderer, infer TArgs> ? TArgs : unknown
>;
type LoaderArgs<TRenderer extends Renderer, Loaders> = UnionToIntersection<
Loaders extends LoaderFunction<TRenderer, infer TArgs> ? TArgs : unknown
>;