Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameters: Remove passArgsFirst flag #25585

Merged
merged 6 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
607 changes: 331 additions & 276 deletions MIGRATION.md

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions code/lib/preview-api/README-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,12 @@ Changing the args cause the story to be re-rendered with the new set of args.

### Using args in a story

By default (starting in 6.0) the args will be passed to the story as first argument and the context as second:
By default the args will be passed to the story as first argument and the context as second:

```js
const YourStory = ({ x, y } /*, context*/) => /* render your story using `x` and `y` */
```

If you set the `parameters.options.passArgsFirst` option on a story to false, args are passed to a story in the context, preserving the pre-6.0 story API; like parameters, they are available as `context.args`.

```js
const YourStory = ({ args: { x, y }}) => /* render your story using `x` and `y` */
```

### Arg types and values

Arg types are used by the docs addon to populate the props table and are documented there. They are controlled by `argTypes` and can (sometimes) be automatically inferred from type information about the story or the component rendered by the story.
Expand Down
34 changes: 0 additions & 34 deletions code/lib/preview-api/src/modules/store/csf/prepareStory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ describe('prepareStory', () => {
expect(parameters).toEqual({ __isArgsStory: true });
});

it('does not set `__isArgsStory` if `passArgsFirst` is disabled', () => {
const { parameters } = prepareStory(
{ id, name, parameters: { passArgsFirst: false }, moduleExport },
{ id, title },
{ render }
);

expect(parameters).toEqual({ passArgsFirst: false, __isArgsStory: false });
});

it('does not set `__isArgsStory` if `render` does not take args', () => {
const { parameters } = prepareStory(
{ id, name, moduleExport },
Expand Down Expand Up @@ -417,30 +407,6 @@ describe('prepareStory', () => {
expect.objectContaining({ args: { one: 'mapped', two: 2, three: 3 } })
);
});

it('passes args as the first argument to the story if `parameters.passArgsFirst` is true', () => {
const renderMock = vi.fn();
const firstStory = prepareStory(
{ id, name, args: { a: 1 }, parameters: { passArgsFirst: true }, moduleExport },
{ id, title },
{ render: renderMock }
);

firstStory.undecoratedStoryFn({ args: firstStory.initialArgs, ...firstStory } as any);
expect(renderMock).toHaveBeenCalledWith(
{ a: 1 },
expect.objectContaining({ args: { a: 1 } })
);

const secondStory = prepareStory(
{ id, name, args: { a: 1 }, parameters: { passArgsFirst: false }, moduleExport },
{ id, title },
{ render: renderMock }
);

secondStory.undecoratedStoryFn({ args: secondStory.initialArgs, ...secondStory } as any);
expect(renderMock).toHaveBeenCalledWith(expect.objectContaining({ args: { a: 1 } }));
});
});

describe('storyFn', () => {
Expand Down
13 changes: 3 additions & 10 deletions code/lib/preview-api/src/modules/store/csf/prepareStory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { global } from '@storybook/global';
import type {
Args,
ArgsStoryFn,
LegacyStoryFn,
ModuleExport,
NormalizedComponentAnnotations,
NormalizedProjectAnnotations,
Expand Down Expand Up @@ -69,12 +68,8 @@ export function prepareStory<TRenderer extends Renderer>(
return updatedContext;
};

const undecoratedStoryFn: LegacyStoryFn<TRenderer> = (context: StoryContext<TRenderer>) => {
const { passArgsFirst: renderTimePassArgsFirst = true } = context.parameters;
return renderTimePassArgsFirst
? (render as ArgsStoryFn<TRenderer>)(context.args, context)
: (render as LegacyStoryFn<TRenderer>)(context);
};
const undecoratedStoryFn = (context: StoryContext<TRenderer>) =>
(render as ArgsStoryFn<TRenderer>)(context.args, context);

// Currently it is only possible to set these globally
const { applyDecorators = defaultDecorateStory, runStep } = projectAnnotations;
Expand Down Expand Up @@ -171,9 +166,7 @@ function preparePartialAnnotations<TRenderer extends Renderer>(
componentAnnotations.render ||
projectAnnotations.render;

const { passArgsFirst = true } = parameters;

parameters.__isArgsStory = passArgsFirst && render && render.length > 0;
parameters.__isArgsStory = render && render.length > 0;
}

// Pull out args[X] into initialArgs for argTypes enhancers
Expand Down
87 changes: 83 additions & 4 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5320,6 +5320,20 @@ __metadata:
languageName: unknown
linkType: soft

"@storybook/channels@npm:8.0.0-alpha.9":
version: 8.0.0-alpha.9
resolution: "@storybook/channels@npm:8.0.0-alpha.9"
dependencies:
"@storybook/client-logger": "npm:8.0.0-alpha.9"
"@storybook/core-events": "npm:8.0.0-alpha.9"
"@storybook/global": "npm:^5.0.0"
qs: "npm:^6.10.0"
telejson: "npm:^7.2.0"
tiny-invariant: "npm:^1.3.1"
checksum: c7465f1d48e0be63f855849c71828906f6f3420411e17a02bebfab441b195c59c12ddbcf8da8a2d630941758b54ca4db1d2ab7850c922ceec394c8264afaf134
languageName: node
linkType: hard

"@storybook/channels@workspace:*, @storybook/channels@workspace:lib/channels":
version: 0.0.0-use.local
resolution: "@storybook/channels@workspace:lib/channels"
Expand Down Expand Up @@ -5397,7 +5411,16 @@ __metadata:
languageName: node
linkType: hard

"@storybook/client-logger@npm:8.0.0-alpha.9, @storybook/client-logger@workspace:*, @storybook/client-logger@workspace:lib/client-logger":
"@storybook/client-logger@npm:8.0.0-alpha.9":
version: 8.0.0-alpha.9
resolution: "@storybook/client-logger@npm:8.0.0-alpha.9"
dependencies:
"@storybook/global": "npm:^5.0.0"
checksum: bc0527a186d901434ea5e5b1807b56288f74c66ff43ea355762bb643f8e257e9b80be94d60f196178b1019abae2baf5c50d4ffd010ec44ec9c8349abc352bf78
languageName: node
linkType: hard

"@storybook/client-logger@workspace:*, @storybook/client-logger@workspace:lib/client-logger":
version: 0.0.0-use.local
resolution: "@storybook/client-logger@workspace:lib/client-logger"
dependencies:
Expand Down Expand Up @@ -5511,7 +5534,16 @@ __metadata:
languageName: unknown
linkType: soft

"@storybook/core-events@npm:8.0.0-alpha.9, @storybook/core-events@workspace:*, @storybook/core-events@workspace:lib/core-events":
"@storybook/core-events@npm:8.0.0-alpha.9":
version: 8.0.0-alpha.9
resolution: "@storybook/core-events@npm:8.0.0-alpha.9"
dependencies:
ts-dedent: "npm:^2.0.0"
checksum: 8868baf90e7f5124966d762a69e06678988105b292b9093d74d2ecd5489cbdad6bc421be19b28b75e9e446074f1ef4fc4ee699ad896ed78bbe99ae0e94cfa416
languageName: node
linkType: hard

"@storybook/core-events@workspace:*, @storybook/core-events@workspace:lib/core-events":
version: 0.0.0-use.local
resolution: "@storybook/core-events@workspace:lib/core-events"
dependencies:
Expand Down Expand Up @@ -5785,7 +5817,22 @@ __metadata:
languageName: node
linkType: hard

"@storybook/instrumenter@npm:8.0.0-alpha.9, @storybook/instrumenter@workspace:*, @storybook/instrumenter@workspace:lib/instrumenter":
"@storybook/instrumenter@npm:8.0.0-alpha.9":
version: 8.0.0-alpha.9
resolution: "@storybook/instrumenter@npm:8.0.0-alpha.9"
dependencies:
"@storybook/channels": "npm:8.0.0-alpha.9"
"@storybook/client-logger": "npm:8.0.0-alpha.9"
"@storybook/core-events": "npm:8.0.0-alpha.9"
"@storybook/global": "npm:^5.0.0"
"@storybook/preview-api": "npm:8.0.0-alpha.9"
"@vitest/utils": "npm:^0.34.6"
util: "npm:^0.12.4"
checksum: 2c1e8953a5e178bb9631878a233623f7df637164bd72d76987431926e81bb3ca6eda71ed1aeda9f21675103c6c5b7e9fba1d3d9a3dd283a821bb11aeea17ac1d
languageName: node
linkType: hard

"@storybook/instrumenter@workspace:*, @storybook/instrumenter@workspace:lib/instrumenter":
version: 0.0.0-use.local
resolution: "@storybook/instrumenter@workspace:lib/instrumenter"
dependencies:
Expand Down Expand Up @@ -6156,7 +6203,28 @@ __metadata:
languageName: unknown
linkType: soft

"@storybook/preview-api@npm:8.0.0-alpha.9, @storybook/preview-api@workspace:*, @storybook/preview-api@workspace:lib/preview-api":
"@storybook/preview-api@npm:8.0.0-alpha.9":
version: 8.0.0-alpha.9
resolution: "@storybook/preview-api@npm:8.0.0-alpha.9"
dependencies:
"@storybook/channels": "npm:8.0.0-alpha.9"
"@storybook/client-logger": "npm:8.0.0-alpha.9"
"@storybook/core-events": "npm:8.0.0-alpha.9"
"@storybook/csf": "npm:^0.1.2"
"@storybook/global": "npm:^5.0.0"
"@storybook/types": "npm:8.0.0-alpha.9"
"@types/qs": "npm:^6.9.5"
dequal: "npm:^2.0.2"
lodash: "npm:^4.17.21"
memoizerific: "npm:^1.11.3"
qs: "npm:^6.10.0"
ts-dedent: "npm:^2.0.0"
util-deprecate: "npm:^1.0.2"
checksum: 8a911b9d937e5f458fa4f51cda4abf6c3575c11c03bb17f4ca726aa940badcf41ea801bfc947120e440f07a8d3b54a4f14cb1afca94961d28aaec52f5129b072
languageName: node
linkType: hard

"@storybook/preview-api@workspace:*, @storybook/preview-api@workspace:lib/preview-api":
version: 0.0.0-use.local
resolution: "@storybook/preview-api@workspace:lib/preview-api"
dependencies:
Expand Down Expand Up @@ -6718,6 +6786,17 @@ __metadata:
languageName: unknown
linkType: soft

"@storybook/types@npm:8.0.0-alpha.9":
version: 8.0.0-alpha.9
resolution: "@storybook/types@npm:8.0.0-alpha.9"
dependencies:
"@storybook/channels": "npm:8.0.0-alpha.9"
"@types/express": "npm:^4.7.0"
file-system-cache: "npm:2.3.0"
checksum: c40518716905b046b176f9f7ff34748e4561cc669039f02fd15841f1d4320089d3734a41532ea4551ed3b0b18c7900a10b9330e182e07e23325781d5400a8f78
languageName: node
linkType: hard

"@storybook/types@workspace:*, @storybook/types@workspace:lib/types":
version: 0.0.0-use.local
resolution: "@storybook/types@workspace:lib/types"
Expand Down
23 changes: 0 additions & 23 deletions docs/essentials/toolbars-and-globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,29 +182,6 @@ Using the example above, you can modify any story to retrieve the **Locale** `gl

<!-- prettier-ignore-end -->

<Callout variant="info" icon="💡">

In Storybook 6.0, if you set the global option `passArgsFirst: false` for backward compatibility, the story context is passed as the first argument:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'react/my-component-story-use-globaltype-backwards-compat.js.mdx',
'vue/my-component-story-use-globaltype-backwards-compat.js.mdx',
'angular/my-component-story-use-globaltype-backwards-compat.ts.mdx',
'svelte/my-component-story-use-globaltype-backwards-compat.js.mdx',
'svelte/my-component-story-use-globaltype-backwards-compat.ts.mdx',
'web-components/my-component-story-use-globaltype-backwards-compat.js.mdx',
'web-components/my-component-story-use-globaltype-backwards-compat.ts.mdx',
'solid/my-component-story-use-globaltype-backwards-compat.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

</Callout>

## Consuming globals from within an addon

If you're working on a Storybook addon and need to retrieve globals, you can do so. The `@storybook/manager-api` package provides a hook for this scenario. You can use the [`useGlobals()`](../addons/addons-api.md#useglobals) hook to retrieve any globals you want.
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading