-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
addons.ts
572 lines (520 loc) · 16.7 KB
/
addons.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* eslint-disable @typescript-eslint/naming-convention */
import type {
FC,
PropsWithChildren,
ReactElement,
ReactNode,
ValidationMap,
WeakValidationMap,
} from 'react';
import type { RenderData as RouterData } from '../../../router/src/types';
import type { ThemeVars } from '../../../theming/src/types';
import type { API_SidebarOptions } from './api';
import type {
Args,
ArgsStoryFn as ArgsStoryFnForFramework,
DecoratorFunction as DecoratorFunctionForFramework,
InputType,
Renderer,
LegacyStoryFn as LegacyStoryFnForFramework,
LoaderFunction as LoaderFunctionForFramework,
Parameters,
PartialStoryFn as PartialStoryFnForFramework,
StoryContext as StoryContextForFramework,
StoryFn as StoryFnForFramework,
StoryId,
StoryKind,
StoryName,
} from './csf';
import type { IndexEntry } from './indexer';
export type Addon_Types = Exclude<
Addon_TypesEnum,
| Addon_TypesEnum.experimental_PAGE
| Addon_TypesEnum.experimental_SIDEBAR_BOTTOM
| Addon_TypesEnum.experimental_SIDEBAR_TOP
>;
export interface Addon_ArgType<TArg = unknown> extends InputType {
defaultValue?: TArg;
}
export type Addons_ArgTypes<TArgs = Args> = {
[key in keyof Partial<TArgs>]: Addon_ArgType<TArgs[key]>;
} & {
// for custom defined args
[key in string]: Addon_ArgType<unknown>;
};
export type Addon_Comparator<T> = ((a: T, b: T) => boolean) | ((a: T, b: T) => number);
export type Addon_StorySortMethod = 'configure' | 'alphabetical';
export interface Addon_StorySortObjectParameter {
method?: Addon_StorySortMethod;
order?: any[];
locales?: string;
includeNames?: boolean;
}
// The `any` here is the story store's `StoreItem` record. Ideally we should probably only
// pass a defined subset of that full data, but we pass it all so far :shrug:
export type IndexEntryLegacy = [StoryId, any, Parameters, Parameters];
export type Addon_StorySortComparator = Addon_Comparator<IndexEntryLegacy>;
export type Addon_StorySortParameter = Addon_StorySortComparator | Addon_StorySortObjectParameter;
export type Addon_StorySortComparatorV7 = Addon_Comparator<IndexEntry>;
export type Addon_StorySortParameterV7 =
| Addon_StorySortComparatorV7
| Addon_StorySortObjectParameter;
// TODO: remove all these types, they belong in the renderer and csf-package
export interface Addon_OptionsParameter extends Object {
storySort?: Addon_StorySortParameter;
theme?: {
base: string;
brandTitle?: string;
};
[key: string]: any;
}
export interface Addon_OptionsParameterV7 extends Object {
storySort?: Addon_StorySortParameterV7;
theme?: {
base: string;
brandTitle?: string;
};
[key: string]: any;
}
export type Addon_StoryContext<TRenderer extends Renderer = Renderer> =
StoryContextForFramework<TRenderer>;
export type Addon_StoryContextUpdate = Partial<Addon_StoryContext>;
type Addon_ReturnTypeFramework<ReturnType> = {
component: any;
storyResult: ReturnType;
canvasElement: any;
};
export type Addon_PartialStoryFn<ReturnType = unknown> = PartialStoryFnForFramework<
Addon_ReturnTypeFramework<ReturnType>
>;
export type Addon_LegacyStoryFn<ReturnType = unknown> = LegacyStoryFnForFramework<
Addon_ReturnTypeFramework<ReturnType>
>;
export type Addon_ArgsStoryFn<ReturnType = unknown> = ArgsStoryFnForFramework<
Addon_ReturnTypeFramework<ReturnType>
>;
export type Addon_StoryFn<ReturnType = unknown> = StoryFnForFramework<
Addon_ReturnTypeFramework<ReturnType>
>;
export type Addon_DecoratorFunction<StoryFnReturnType = unknown> = DecoratorFunctionForFramework<
Addon_ReturnTypeFramework<StoryFnReturnType>
>;
export type Addon_LoaderFunction = LoaderFunctionForFramework<Addon_ReturnTypeFramework<unknown>>;
export interface Addon_WrapperSettings {
options: object;
parameters: {
[key: string]: any;
};
}
export type Addon_StoryWrapper = (
storyFn: Addon_LegacyStoryFn,
context: Addon_StoryContext,
settings: Addon_WrapperSettings
) => any;
export type Addon_MakeDecoratorResult = (...args: any) => any;
export interface Addon_AddStoryArgs<StoryFnReturnType = unknown> {
id: StoryId;
kind: StoryKind;
name: StoryName;
storyFn: Addon_StoryFn<StoryFnReturnType>;
parameters: Parameters;
}
export type Addon_ClientApiAddon<StoryFnReturnType = unknown> = Addon_Type & {
apply: (a: Addon_StoryApi<StoryFnReturnType>, b: any[]) => any;
};
export interface Addon_ClientApiAddons<StoryFnReturnType> {
[key: string]: Addon_ClientApiAddon<StoryFnReturnType>;
}
export type Addon_ClientApiReturnFn<StoryFnReturnType = unknown> = (
...args: any[]
) => Addon_StoryApi<StoryFnReturnType>;
export interface Addon_StoryApi<StoryFnReturnType = unknown> {
kind: StoryKind;
add: (
storyName: StoryName,
storyFn: Addon_StoryFn<StoryFnReturnType>,
parameters?: Parameters
) => Addon_StoryApi<StoryFnReturnType>;
addDecorator: (
decorator: Addon_DecoratorFunction<StoryFnReturnType>
) => Addon_StoryApi<StoryFnReturnType>;
addLoader: (decorator: Addon_LoaderFunction) => Addon_StoryApi<StoryFnReturnType>;
addParameters: (parameters: Parameters) => Addon_StoryApi<StoryFnReturnType>;
[k: string]: string | Addon_ClientApiReturnFn<StoryFnReturnType>;
}
export interface Addon_ClientStoryApi<StoryFnReturnType = unknown> {
storiesOf(kind: StoryKind, module: any): Addon_StoryApi<StoryFnReturnType>;
}
export type Addon_LoadFn = () => any;
export type Addon_RequireContext = any; // FIXME
export type Addon_Loadable = Addon_RequireContext | [Addon_RequireContext] | Addon_LoadFn;
// CSF types, to be re-org'ed in 6.1
export type Addon_BaseDecorators<StoryFnReturnType> = Array<
(story: () => StoryFnReturnType, context: Addon_StoryContext) => StoryFnReturnType
>;
export interface Addon_BaseAnnotations<
TArgs,
StoryFnReturnType,
TRenderer extends Renderer = Renderer
> {
/**
* 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 [Arg types](https://storybook.js.org/docs/react/api/arg-types)
*/
argTypes?: Addons_ArgTypes<TArgs>;
/**
* Custom metadata for a story.
* @see [Parameters](https://storybook.js.org/docs/react/writing-stories/parameters)
*/
parameters?: Parameters;
/**
* 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/#1-decorators)
*/
decorators?: Addon_BaseDecorators<StoryFnReturnType>;
/**
* Define a custom render function for the story(ies). If not passed, a default render function by the framework will be used.
*/
render?: (args: TArgs, context: Addon_StoryContext<TRenderer>) => StoryFnReturnType;
/**
* Function that is executed after the story is rendered.
*/
play?: (context: Addon_StoryContext<TRenderer>) => Promise<void> | void;
}
export interface Addon_Annotations<TArgs, StoryFnReturnType>
extends Addon_BaseAnnotations<TArgs, StoryFnReturnType> {
/**
* 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?: string[] | RegExp;
/**
* 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?: string[] | RegExp;
}
export interface Addon_BaseMeta<ComponentType> {
/**
* Title of the story which will be presented in the navigation. **Should be unique.**
*
* Stories can be organized in a nested structure using "/" as a separator.
*
* Since CSF 3.0 this property is optional.
*
* @example
* export default {
* ...
* title: 'Design System/Atoms/Button'
* }
*
* @see [Story Hierarchy](https://storybook.js.org/docs/basics/writing-stories/#story-hierarchy)
*/
title?: string;
/**
* Manually set the id of a story, which in particular is useful if you want to rename stories without breaking permalinks.
*
* Storybook will prioritize the id over the title for ID generation, if provided, and will prioritize the story.storyName over the export key for display.
*
* @see [Sidebar and URLs](https://storybook.js.org/docs/react/configure/sidebar-and-urls#permalinking-to-stories)
*/
id?: string;
/**
* The primary component for your story.
*
* Used by addons for automatic prop table generation and display of other component metadata.
*/
component?: ComponentType;
/**
* 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.
*
* @deprecated
*/
subcomponents?: Record<string, ComponentType>;
}
export type Addon_BaseStoryObject<TArgs, StoryFnReturnType> = {
/**
* Override the display name in the UI
*/
storyName?: string;
};
export type Addon_BaseStoryFn<TArgs, StoryFnReturnType> = {
(args: TArgs, context: Addon_StoryContext): StoryFnReturnType;
} & Addon_BaseStoryObject<TArgs, StoryFnReturnType>;
export type BaseStory<TArgs, StoryFnReturnType> =
| Addon_BaseStoryFn<TArgs, StoryFnReturnType>
| Addon_BaseStoryObject<TArgs, StoryFnReturnType>;
export interface Addon_RenderOptions {
active: boolean;
/**
* @deprecated You should not use key anymore as of Storybook 7.2 this render method is invoked as a React component.
* This property will be removed in 8.0.
* */
key?: unknown;
}
/**
* @deprecated This type is deprecated and will be removed in 8.0.
*/
export type ReactJSXElement = {
type: any;
props: any;
key: any;
};
export type Addon_Type =
| Addon_BaseType
| Addon_PageType
| Addon_WrapperType
| Addon_SidebarBottomType
| Addon_SidebarTopType;
export interface Addon_BaseType {
/**
* The title of the addon.
* This can be a simple string, but it can also be a React.FunctionComponent or a React.ReactElement.
*/
title: FCWithoutChildren | ReactNode;
/**
* The type of the addon.
* @example Addon_TypesEnum.PANEL
*/
type: Exclude<
Addon_Types,
| Addon_TypesEnum.PREVIEW
| Addon_TypesEnum.experimental_PAGE
| Addon_TypesEnum.experimental_SIDEBAR_BOTTOM
| Addon_TypesEnum.experimental_SIDEBAR_TOP
>;
/**
* The unique id of the addon.
* @warn This will become non-optional in 8.0
*
* This needs to be globally unique, so we recommend prefixing it with your org name or npm package name.
*
* Do not prefix with `storybook`, this is reserved for core storybook feature and core addons.
*
* @example 'my-org-name/my-addon-name'
*/
id?: string;
/**
* This component will wrap your `render` function.
*
* With it you can determine if you want your addon to be rendered or not.
*
* This is to facilitate addons keeping state, and keep listening for events even when they are not currently on screen/rendered.
*/
route?: (routeOptions: RouterData) => string;
/**
* This will determine the value of `active` prop of your render function.
*/
match?: (matchOptions: RouterData) => boolean;
/**
* The actual contents of your addon.
*
* This is called as a function, so if you want to use hooks,
* your function needs to return a JSX.Element within which components are rendered
*/
render: (renderOptions: Partial<Addon_RenderOptions>) => ReactElement<any, any> | null;
/**
* @unstable
*/
paramKey?: string;
/**
* @unstable
*/
disabled?: boolean;
/**
* @unstable
*/
hidden?: boolean;
}
/**
* This is a copy of FC from react/index.d.ts, but has the PropsWithChildren type removed
* this is correct and more type strict, and future compatible with React.FC in React 18+
*
* @deprecated This type is deprecated and will be removed in 8.0. (assuming the manager uses React 18 is out by then)
*/
interface FCWithoutChildren<P = {}> {
(props: P, context?: any): ReactElement<any, any> | null;
propTypes?: WeakValidationMap<P> | undefined;
contextTypes?: ValidationMap<any> | undefined;
defaultProps?: Partial<P> | undefined;
displayName?: string | undefined;
}
export interface Addon_PageType {
type: Addon_TypesEnum.experimental_PAGE;
/**
* The unique id of the page.
*/
id: string;
/**
* The URL to navigate to when Storybook needs to navigate to this page.
*/
url: string;
/**
* The title is used in mobile mode to represent the page in the navigation.
*/
title: FCWithoutChildren | string | ReactElement | ReactNode;
/**
* The main content of the addon, a function component without any props.
* Storybook will render your component always.
*
* If you want to render your component only when the URL matches, use the `Route` component.
* @example
* import { Route } from '@storybook/router';
*
* render: () => {
* return (
* <Route path="/my-addon">
* <MyAddonContent />
* </Route>
* );
* };
*/
render: FCWithoutChildren;
}
export interface Addon_WrapperType {
type: Addon_TypesEnum.PREVIEW;
/**
* The unique id of the page.
*/
id: string;
/**
* A React.FunctionComponent that wraps the story.
*
* This component must accept a children prop, and render it.
*/
render: FC<
PropsWithChildren<{
index: number;
children: ReactNode;
id: string;
storyId: StoryId;
active: boolean;
}>
>;
}
export interface Addon_SidebarBottomType {
type: Addon_TypesEnum.experimental_SIDEBAR_BOTTOM;
/**
* The unique id of the tool.
*/
id: string;
/**
* A React.FunctionComponent.
*/
render: FCWithoutChildren;
}
export interface Addon_SidebarTopType {
type: Addon_TypesEnum.experimental_SIDEBAR_TOP;
/**
* The unique id of the tool.
*/
id: string;
/**
* A React.FunctionComponent.
*/
render: FCWithoutChildren;
}
type Addon_TypeBaseNames = Exclude<
Addon_TypesEnum,
| Addon_TypesEnum.PREVIEW
| Addon_TypesEnum.experimental_PAGE
| Addon_TypesEnum.experimental_SIDEBAR_BOTTOM
| Addon_TypesEnum.experimental_SIDEBAR_TOP
>;
export interface Addon_TypesMapping extends Record<Addon_TypeBaseNames, Addon_BaseType> {
[Addon_TypesEnum.PREVIEW]: Addon_WrapperType;
[Addon_TypesEnum.experimental_PAGE]: Addon_PageType;
[Addon_TypesEnum.experimental_SIDEBAR_BOTTOM]: Addon_SidebarBottomType;
[Addon_TypesEnum.experimental_SIDEBAR_TOP]: Addon_SidebarTopType;
}
export type Addon_Loader<API> = (api: API) => void;
export interface Addon_Loaders<API> {
[key: string]: Addon_Loader<API>;
}
export interface Addon_Collection<T = Addon_Type> {
[key: string]: T;
}
export interface Addon_Elements {
[key: string]: Addon_Collection;
}
export interface Addon_ToolbarConfig {
hidden?: boolean;
}
export interface Addon_Config {
theme?: ThemeVars;
toolbar?: {
[id: string]: Addon_ToolbarConfig;
};
sidebar?: API_SidebarOptions;
[key: string]: any;
}
export enum Addon_TypesEnum {
/**
* This API is used to create a tab the toolbar above the canvas, This API might be removed in the future.
* @unstable
*/
TAB = 'tab',
/**
* This adds panels to the addons side panel.
*/
PANEL = 'panel',
/**
* This adds items in the toolbar above the canvas - on the left side.
*/
TOOL = 'tool',
/**
* This adds items in the toolbar above the canvas - on the right side.
*/
TOOLEXTRA = 'toolextra',
/**
* This adds wrapper components around the canvas/iframe component storybook renders.
* @unstable this API is not stable yet, and is likely to change in 8.0.
*/
PREVIEW = 'preview',
/**
* This adds pages that render instead of the canvas.
* @unstable
*/
experimental_PAGE = 'page',
/**
* This adds items in the bottom of the sidebar.
* @unstable
*/
experimental_SIDEBAR_BOTTOM = 'sidebar-bottom',
/**
* This adds items in the top of the sidebar.
* @unstable This will get replaced with a new API in 8.0, use at your own risk.
*/
experimental_SIDEBAR_TOP = 'sidebar-top',
/**
* @deprecated This property does nothing, and will be removed in Storybook 8.0.
*/
NOTES_ELEMENT = 'notes-element',
}