@storybook/react 10.2.0-alpha.15 → 10.2.0-alpha.16

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/dist/index.d.ts CHANGED
@@ -498,39 +498,157 @@ declare function composeStory<TArgs extends Args = Args>(story: StoryAnnotations
498
498
  */
499
499
  declare function composeStories<TModule extends Store_CSFExports<ReactRenderer, any>>(csfExports: TModule, projectAnnotations?: ProjectAnnotations<ReactRenderer>): Omit<StoriesWithPartialProps<ReactRenderer, TModule>, keyof Store_CSFExports>;
500
500
 
501
+ /** Extracts and unions all args types from an array of decorators. */
502
+ type DecoratorsArgs<TRenderer extends Renderer, Decorators> = UnionToIntersection<Decorators extends DecoratorFunction<TRenderer, infer TArgs> ? TArgs : unknown>;
503
+ type InferArgs<TArgs, T, Decorators> = Simplify<TArgs & Simplify<RemoveIndexSignature<DecoratorsArgs<ReactTypes & T, Decorators>>>>;
504
+ type InferReactTypes<T, TArgs, Decorators> = ReactTypes & T & {
505
+ args: Simplify<InferArgs<TArgs, T, Decorators>>;
506
+ };
507
+ /**
508
+ * Creates a React-specific preview configuration with CSF factories support.
509
+ *
510
+ * This function wraps the base `definePreview` and adds React-specific annotations for rendering
511
+ * and documentation. It returns a `ReactPreview` that provides type-safe `meta()` and `story()`
512
+ * factory methods.
513
+ *
514
+ * @example
515
+ *
516
+ * ```ts
517
+ * // .storybook/preview.ts
518
+ * import { definePreview } from '@storybook/react';
519
+ *
520
+ * export const preview = definePreview({
521
+ * addons: [],
522
+ * parameters: { layout: 'centered' },
523
+ * });
524
+ * ```
525
+ */
501
526
  declare function __definePreview<Addons extends PreviewAddon<never>[]>(input: {
502
527
  addons: Addons;
503
528
  } & ProjectAnnotations<ReactTypes & InferTypes<Addons>>): ReactPreview<ReactTypes & InferTypes<Addons>>;
504
- type InferArgs<TArgs extends Args, T extends AddonTypes, Decorators extends DecoratorFunction<ReactTypes & T, any>> = Simplify<TArgs & Simplify<RemoveIndexSignature<DecoratorsArgs<ReactTypes & T, Decorators>>>>;
529
+ /**
530
+ * React-specific Preview interface that provides type-safe CSF factory methods.
531
+ *
532
+ * Use `preview.meta()` to create a meta configuration for a component, and then `meta.story()` to
533
+ * create individual stories. The type system will infer args from the component props, decorators,
534
+ * and any addon types.
535
+ *
536
+ * @example
537
+ *
538
+ * ```ts
539
+ * const meta = preview.meta({ component: Button });
540
+ * export const Primary = meta.story({ args: { label: 'Click me' } });
541
+ * ```
542
+ */
505
543
  /** @ts-expect-error We cannot implement the meta faithfully here, but that is okay. */
506
544
  interface ReactPreview<T extends AddonTypes> extends Preview$1<ReactTypes & T> {
507
- meta<TArgs extends Args, Decorators extends DecoratorFunction<ReactTypes & T, any>, TMetaArgs extends Partial<TArgs>>(meta: {
508
- render?: ArgsStoryFn<ReactTypes & T, TArgs>;
545
+ /**
546
+ * Narrows the type of the preview to include additional type information. This is useful when you
547
+ * need to add args that aren't inferred from the component.
548
+ *
549
+ * @example
550
+ *
551
+ * ```ts
552
+ * const meta = preview.type<{ args: { theme: 'light' | 'dark' } }>().meta({
553
+ * component: Button,
554
+ * });
555
+ * ```
556
+ */
557
+ type<R>(): ReactPreview<T & R>;
558
+ meta<TArgs extends Args, Decorators extends DecoratorFunction<ReactTypes & T, any>, TMetaArgs extends Partial<TArgs & T['args']>>(meta: {
559
+ render?: ArgsStoryFn<ReactTypes & T, TArgs & T['args']>;
509
560
  component?: ComponentType<TArgs>;
510
561
  decorators?: Decorators | Decorators[];
511
562
  args?: TMetaArgs;
512
- } & Omit<ComponentAnnotations<ReactTypes & T, TArgs>, 'decorators' | 'component' | 'args' | 'render'>): ReactMeta<ReactTypes & T & {
513
- args: InferArgs<TArgs, T, Decorators>;
514
- }, Omit<ComponentAnnotations<ReactTypes & T & {
515
- args: InferArgs<TArgs, T, Decorators>;
516
- }>, 'args'> & {
563
+ } & Omit<ComponentAnnotations<ReactTypes & T, TArgs>, 'decorators' | 'component' | 'args' | 'render'>): ReactMeta<InferReactTypes<T, TArgs, Decorators>, Omit<ComponentAnnotations<InferReactTypes<T, TArgs, Decorators>>, 'args'> & {
517
564
  args: Partial<TArgs> extends TMetaArgs ? {} : TMetaArgs;
518
565
  }>;
519
566
  }
520
- type DecoratorsArgs<TRenderer extends Renderer, Decorators> = UnionToIntersection<Decorators extends DecoratorFunction<TRenderer, infer TArgs> ? TArgs : unknown>;
567
+ /**
568
+ * React-specific Meta interface returned by `preview.meta()`.
569
+ *
570
+ * Provides the `story()` method to create individual stories with proper type inference. Args
571
+ * provided in meta become optional in stories, while missing required args must be provided at the
572
+ * story level.
573
+ */
521
574
  interface ReactMeta<T extends ReactTypes, MetaInput extends ComponentAnnotations<T>>
522
- /** @ts-expect-error hard */
575
+ /** @ts-expect-error ReactMeta requires two type parameters, but Meta's constraints differ */
523
576
  extends Meta$1<T, MetaInput> {
577
+ /**
578
+ * Creates a story with a custom render function that takes no args.
579
+ *
580
+ * This overload allows you to define a story using just a render function or an object with a
581
+ * render function that doesn't depend on args. Since the render function doesn't use args, no
582
+ * args need to be provided regardless of what's required by the component.
583
+ *
584
+ * @example
585
+ *
586
+ * ```ts
587
+ * // Using just a render function
588
+ * export const CustomRender = meta.story(() => <div>Custom content</div>);
589
+ *
590
+ * // Using an object with render
591
+ * export const WithRender = meta.story({
592
+ * render: () => <MyComponent prop="static" />,
593
+ * });
594
+ * ```
595
+ */
524
596
  story<TInput extends (() => ReactTypes['storyResult']) | (StoryAnnotations<T, T['args']> & {
525
597
  render: () => ReactTypes['storyResult'];
526
598
  })>(story: TInput): ReactStory<T, TInput extends () => ReactTypes['storyResult'] ? {
527
599
  render: TInput;
528
600
  } : TInput>;
601
+ /**
602
+ * Creates a story with custom configuration including args, decorators, or other annotations.
603
+ *
604
+ * This is the primary overload for defining stories. Args that were already provided in meta
605
+ * become optional, while any remaining required args must be specified here.
606
+ *
607
+ * @example
608
+ *
609
+ * ```ts
610
+ * // Provide required args not in meta
611
+ * export const Primary = meta.story({
612
+ * args: { label: 'Click me', disabled: false },
613
+ * });
614
+ *
615
+ * // Override meta args and add story-specific configuration
616
+ * export const Disabled = meta.story({
617
+ * args: { disabled: true },
618
+ * decorators: [withCustomWrapper],
619
+ * });
620
+ * ```
621
+ */
529
622
  story<TInput extends Simplify<StoryAnnotations<T, AddMocks<T['args'], MetaInput['args']>, SetOptional<T['args'], keyof T['args'] & keyof MetaInput['args']>>>>(story: TInput
530
623
  /** @ts-expect-error hard */
531
624
  ): ReactStory<T, TInput>;
625
+ /**
626
+ * Creates a story with no additional configuration.
627
+ *
628
+ * This overload is only available when all required args have been provided in meta. The
629
+ * conditional type `Partial<T['args']> extends SetOptional<...>` checks if the remaining required
630
+ * args (after accounting for args provided in meta) are all optional. If so, the function accepts
631
+ * zero arguments `[]`. Otherwise, it requires `[never]` which makes this overload unmatchable,
632
+ * forcing the user to provide args.
633
+ *
634
+ * @example
635
+ *
636
+ * ```ts
637
+ * // When meta provides all required args, story() can be called with no arguments
638
+ * const meta = preview.meta({ component: Button, args: { label: 'Hi', disabled: false } });
639
+ * export const Default = meta.story(); // Valid - all args provided in meta
640
+ * ```
641
+ */
532
642
  story(..._args: Partial<T['args']> extends SetOptional<T['args'], keyof T['args'] & keyof MetaInput['args']> ? [] : [never]): ReactStory<T, {}>;
533
643
  }
644
+ /**
645
+ * React-specific Story interface returned by `meta.story()`.
646
+ *
647
+ * Represents a single story with its configuration and provides access to the composed story for
648
+ * testing via `story.run()`.
649
+ *
650
+ * Also includes a `Component` property for portable story compatibility.
651
+ */
534
652
  interface ReactStory<T extends ReactTypes, TInput extends StoryAnnotations<T, T['args']>> extends Story<T, TInput> {
535
653
  Component: ComponentType<Partial<T['args']>>;
536
654
  }
package/dist/preset.js CHANGED
@@ -1,10 +1,10 @@
1
- import CJS_COMPAT_NODE_URL_4nd2qi8yguw from 'node:url';
2
- import CJS_COMPAT_NODE_PATH_4nd2qi8yguw from 'node:path';
3
- import CJS_COMPAT_NODE_MODULE_4nd2qi8yguw from "node:module";
1
+ import CJS_COMPAT_NODE_URL_8h0e3j6j29r from 'node:url';
2
+ import CJS_COMPAT_NODE_PATH_8h0e3j6j29r from 'node:path';
3
+ import CJS_COMPAT_NODE_MODULE_8h0e3j6j29r from "node:module";
4
4
 
5
- var __filename = CJS_COMPAT_NODE_URL_4nd2qi8yguw.fileURLToPath(import.meta.url);
6
- var __dirname = CJS_COMPAT_NODE_PATH_4nd2qi8yguw.dirname(__filename);
7
- var require = CJS_COMPAT_NODE_MODULE_4nd2qi8yguw.createRequire(import.meta.url);
5
+ var __filename = CJS_COMPAT_NODE_URL_8h0e3j6j29r.fileURLToPath(import.meta.url);
6
+ var __dirname = CJS_COMPAT_NODE_PATH_8h0e3j6j29r.dirname(__filename);
7
+ var require = CJS_COMPAT_NODE_MODULE_8h0e3j6j29r.createRequire(import.meta.url);
8
8
 
9
9
  // ------------------------------------------------------------
10
10
  // end of CJS compatibility banner, injected by Storybook's esbuild configuration
package/dist/preview.d.ts CHANGED
@@ -389,39 +389,157 @@ type AddMocks<TArgs, DefaultArgs> = Simplify<{
389
389
  } ? DefaultArgs[T] : TArgs[T] : TArgs[T];
390
390
  }>;
391
391
 
392
+ /** Extracts and unions all args types from an array of decorators. */
393
+ type DecoratorsArgs<TRenderer extends Renderer, Decorators> = UnionToIntersection<Decorators extends DecoratorFunction<TRenderer, infer TArgs> ? TArgs : unknown>;
394
+ type InferArgs<TArgs, T, Decorators> = Simplify<TArgs & Simplify<RemoveIndexSignature<DecoratorsArgs<ReactTypes & T, Decorators>>>>;
395
+ type InferReactTypes<T, TArgs, Decorators> = ReactTypes & T & {
396
+ args: Simplify<InferArgs<TArgs, T, Decorators>>;
397
+ };
398
+ /**
399
+ * Creates a React-specific preview configuration with CSF factories support.
400
+ *
401
+ * This function wraps the base `definePreview` and adds React-specific annotations for rendering
402
+ * and documentation. It returns a `ReactPreview` that provides type-safe `meta()` and `story()`
403
+ * factory methods.
404
+ *
405
+ * @example
406
+ *
407
+ * ```ts
408
+ * // .storybook/preview.ts
409
+ * import { definePreview } from '@storybook/react';
410
+ *
411
+ * export const preview = definePreview({
412
+ * addons: [],
413
+ * parameters: { layout: 'centered' },
414
+ * });
415
+ * ```
416
+ */
392
417
  declare function __definePreview<Addons extends PreviewAddon<never>[]>(input: {
393
418
  addons: Addons;
394
419
  } & ProjectAnnotations<ReactTypes & InferTypes<Addons>>): ReactPreview<ReactTypes & InferTypes<Addons>>;
395
- type InferArgs<TArgs extends Args, T extends AddonTypes, Decorators extends DecoratorFunction<ReactTypes & T, any>> = Simplify<TArgs & Simplify<RemoveIndexSignature<DecoratorsArgs<ReactTypes & T, Decorators>>>>;
420
+ /**
421
+ * React-specific Preview interface that provides type-safe CSF factory methods.
422
+ *
423
+ * Use `preview.meta()` to create a meta configuration for a component, and then `meta.story()` to
424
+ * create individual stories. The type system will infer args from the component props, decorators,
425
+ * and any addon types.
426
+ *
427
+ * @example
428
+ *
429
+ * ```ts
430
+ * const meta = preview.meta({ component: Button });
431
+ * export const Primary = meta.story({ args: { label: 'Click me' } });
432
+ * ```
433
+ */
396
434
  /** @ts-expect-error We cannot implement the meta faithfully here, but that is okay. */
397
435
  interface ReactPreview<T extends AddonTypes> extends Preview<ReactTypes & T> {
398
- meta<TArgs extends Args, Decorators extends DecoratorFunction<ReactTypes & T, any>, TMetaArgs extends Partial<TArgs>>(meta: {
399
- render?: ArgsStoryFn<ReactTypes & T, TArgs>;
436
+ /**
437
+ * Narrows the type of the preview to include additional type information. This is useful when you
438
+ * need to add args that aren't inferred from the component.
439
+ *
440
+ * @example
441
+ *
442
+ * ```ts
443
+ * const meta = preview.type<{ args: { theme: 'light' | 'dark' } }>().meta({
444
+ * component: Button,
445
+ * });
446
+ * ```
447
+ */
448
+ type<R>(): ReactPreview<T & R>;
449
+ meta<TArgs extends Args, Decorators extends DecoratorFunction<ReactTypes & T, any>, TMetaArgs extends Partial<TArgs & T['args']>>(meta: {
450
+ render?: ArgsStoryFn<ReactTypes & T, TArgs & T['args']>;
400
451
  component?: ComponentType<TArgs>;
401
452
  decorators?: Decorators | Decorators[];
402
453
  args?: TMetaArgs;
403
- } & Omit<ComponentAnnotations<ReactTypes & T, TArgs>, 'decorators' | 'component' | 'args' | 'render'>): ReactMeta<ReactTypes & T & {
404
- args: InferArgs<TArgs, T, Decorators>;
405
- }, Omit<ComponentAnnotations<ReactTypes & T & {
406
- args: InferArgs<TArgs, T, Decorators>;
407
- }>, 'args'> & {
454
+ } & Omit<ComponentAnnotations<ReactTypes & T, TArgs>, 'decorators' | 'component' | 'args' | 'render'>): ReactMeta<InferReactTypes<T, TArgs, Decorators>, Omit<ComponentAnnotations<InferReactTypes<T, TArgs, Decorators>>, 'args'> & {
408
455
  args: Partial<TArgs> extends TMetaArgs ? {} : TMetaArgs;
409
456
  }>;
410
457
  }
411
- type DecoratorsArgs<TRenderer extends Renderer, Decorators> = UnionToIntersection<Decorators extends DecoratorFunction<TRenderer, infer TArgs> ? TArgs : unknown>;
458
+ /**
459
+ * React-specific Meta interface returned by `preview.meta()`.
460
+ *
461
+ * Provides the `story()` method to create individual stories with proper type inference. Args
462
+ * provided in meta become optional in stories, while missing required args must be provided at the
463
+ * story level.
464
+ */
412
465
  interface ReactMeta<T extends ReactTypes, MetaInput extends ComponentAnnotations<T>>
413
- /** @ts-expect-error hard */
466
+ /** @ts-expect-error ReactMeta requires two type parameters, but Meta's constraints differ */
414
467
  extends Meta<T, MetaInput> {
468
+ /**
469
+ * Creates a story with a custom render function that takes no args.
470
+ *
471
+ * This overload allows you to define a story using just a render function or an object with a
472
+ * render function that doesn't depend on args. Since the render function doesn't use args, no
473
+ * args need to be provided regardless of what's required by the component.
474
+ *
475
+ * @example
476
+ *
477
+ * ```ts
478
+ * // Using just a render function
479
+ * export const CustomRender = meta.story(() => <div>Custom content</div>);
480
+ *
481
+ * // Using an object with render
482
+ * export const WithRender = meta.story({
483
+ * render: () => <MyComponent prop="static" />,
484
+ * });
485
+ * ```
486
+ */
415
487
  story<TInput extends (() => ReactTypes['storyResult']) | (StoryAnnotations<T, T['args']> & {
416
488
  render: () => ReactTypes['storyResult'];
417
489
  })>(story: TInput): ReactStory<T, TInput extends () => ReactTypes['storyResult'] ? {
418
490
  render: TInput;
419
491
  } : TInput>;
492
+ /**
493
+ * Creates a story with custom configuration including args, decorators, or other annotations.
494
+ *
495
+ * This is the primary overload for defining stories. Args that were already provided in meta
496
+ * become optional, while any remaining required args must be specified here.
497
+ *
498
+ * @example
499
+ *
500
+ * ```ts
501
+ * // Provide required args not in meta
502
+ * export const Primary = meta.story({
503
+ * args: { label: 'Click me', disabled: false },
504
+ * });
505
+ *
506
+ * // Override meta args and add story-specific configuration
507
+ * export const Disabled = meta.story({
508
+ * args: { disabled: true },
509
+ * decorators: [withCustomWrapper],
510
+ * });
511
+ * ```
512
+ */
420
513
  story<TInput extends Simplify<StoryAnnotations<T, AddMocks<T['args'], MetaInput['args']>, SetOptional<T['args'], keyof T['args'] & keyof MetaInput['args']>>>>(story: TInput
421
514
  /** @ts-expect-error hard */
422
515
  ): ReactStory<T, TInput>;
516
+ /**
517
+ * Creates a story with no additional configuration.
518
+ *
519
+ * This overload is only available when all required args have been provided in meta. The
520
+ * conditional type `Partial<T['args']> extends SetOptional<...>` checks if the remaining required
521
+ * args (after accounting for args provided in meta) are all optional. If so, the function accepts
522
+ * zero arguments `[]`. Otherwise, it requires `[never]` which makes this overload unmatchable,
523
+ * forcing the user to provide args.
524
+ *
525
+ * @example
526
+ *
527
+ * ```ts
528
+ * // When meta provides all required args, story() can be called with no arguments
529
+ * const meta = preview.meta({ component: Button, args: { label: 'Hi', disabled: false } });
530
+ * export const Default = meta.story(); // Valid - all args provided in meta
531
+ * ```
532
+ */
423
533
  story(..._args: Partial<T['args']> extends SetOptional<T['args'], keyof T['args'] & keyof MetaInput['args']> ? [] : [never]): ReactStory<T, {}>;
424
534
  }
535
+ /**
536
+ * React-specific Story interface returned by `meta.story()`.
537
+ *
538
+ * Represents a single story with its configuration and provides access to the composed story for
539
+ * testing via `story.run()`.
540
+ *
541
+ * Also includes a `Component` property for portable story compatibility.
542
+ */
425
543
  interface ReactStory<T extends ReactTypes, TInput extends StoryAnnotations<T, T['args']>> extends Story<T, TInput> {
426
544
  Component: ComponentType<Partial<T['args']>>;
427
545
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/react",
3
- "version": "10.2.0-alpha.15",
3
+ "version": "10.2.0-alpha.16",
4
4
  "description": "Storybook React renderer",
5
5
  "keywords": [
6
6
  "storybook"
@@ -50,7 +50,7 @@
50
50
  ],
51
51
  "dependencies": {
52
52
  "@storybook/global": "^5.0.0",
53
- "@storybook/react-dom-shim": "10.2.0-alpha.15",
53
+ "@storybook/react-dom-shim": "10.2.0-alpha.16",
54
54
  "react-docgen": "^8.0.2"
55
55
  },
56
56
  "devDependencies": {
@@ -77,7 +77,7 @@
77
77
  "peerDependencies": {
78
78
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
79
79
  "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
80
- "storybook": "^10.2.0-alpha.15",
80
+ "storybook": "^10.2.0-alpha.16",
81
81
  "typescript": ">= 4.9.x"
82
82
  },
83
83
  "peerDependenciesMeta": {