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

docs: add information about @storybook/preview-api useArgs hook #22590

Merged
merged 23 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2a9b09e
docs: add information about @storybook/preview-api useArgs hook
jonthenerd May 17, 2023
9e5f981
docs: fix issue with code sample
jonthenerd May 17, 2023
b701f0f
docs: fix code block - remove unnecessary export
jonthenerd May 17, 2023
aade9cc
docs: now passes eslint
jonthenerd May 17, 2023
1d54130
docs: change code block to be minimum example that does not cause esl…
jonthenerd May 17, 2023
d12518c
docs: fix typo in header
jonthenerd May 17, 2023
665c1a8
docs: align code pattern to ensure no eslint or storybook issues
jonthenerd May 18, 2023
449b052
make changes per recommendations
jonthenerd Jun 16, 2023
cec4576
fix typos; align style to other js code snippets
jonthenerd Jun 16, 2023
8add32d
fix mispelling in comments
jonthenerd Jun 16, 2023
c3ea9cd
Merge branch 'next' into patch-1
jonniebigodes Jul 6, 2023
ed5bd70
Merge branch 'next' into patch-1
jonthenerd Aug 11, 2023
f9e2e0d
add typescript 4.9 snippet
jonthenerd Aug 11, 2023
44fa3c3
removed added file. Why wasn't this excluded via .gitignore???
jonthenerd Aug 11, 2023
504fefa
modify text to align to pull request comment
jonthenerd Aug 11, 2023
522bb81
Merge branch 'next' into patch-1
jonthenerd Aug 11, 2023
41d8edf
remove link to incorrect api
jonthenerd Aug 11, 2023
91cce63
Merge branch 'next' into patch-1
jonniebigodes Sep 6, 2023
78ae771
Merge branch 'next' into patch-1
jonthenerd Sep 10, 2023
eea29bc
add react IFRenderer to args docs addition
jonthenerd Sep 10, 2023
d9a0caa
Merge branch 'next' into patch-1
jonthenerd Oct 1, 2023
0cacd64
Merge branch 'next' into patch-1
jonniebigodes Oct 2, 2023
106bf9a
Merge branch 'next' into patch-1
jonniebigodes Oct 2, 2023
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
31 changes: 31 additions & 0 deletions docs/snippets/react/page-story-args-within-story.js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
```js
// my-component/component.stories.js|jsx

import { useArgs } from '@storybook/preview-api';
import { Checkbox } from './checkbox';

export default {
title: 'Inputs/Checkbox',
component: Checkbox,
};

export const Example = {
args: {
isChecked: false,
label: 'Try Me!',
},
/**
* 👇 To avoid linting issues, it is recommended to use a function with a capitalized name.
* If you are not concerned with linting, you may use an arrow function.
*/
render: function Render(args) {
const [{ isChecked }, updateArgs] = useArgs();

function onChange() {
updateArgs({ isChecked: !isChecked });
}

return <Checkbox {...args} onChange={onChange} isChecked={isChecked} />;
},
};
```
35 changes: 35 additions & 0 deletions docs/snippets/react/page-story-args-within-story.ts-4-9.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
```ts
// my-component/component.stories.ts|tsx

import { StoryObj, Meta } from '@storybook/react';
import { useArgs } from '@storybook/preview-api';
import { Checkbox } from './checkbox';

const meta = {
title: 'Inputs/Checkbox',
component: Checkbox,
} satisfies Meta<typeof Checkbox>;
export default meta;

type Story = StoryObj<typeof Checkbox>;

export const Example = {
args: {
isChecked: false,
label: 'Try Me!',
},
/**
* 👇 To avoid linting issues, it is recommended to use a function with a capitalized name.
* If you are not concerned with linting, you may use an arrow function.
*/
render: function Render(args) {
const [{ isChecked }, updateArgs] = useArgs();

function onChange() {
updateArgs({ isChecked: !isChecked });
}

return <Checkbox {...args} onChange={onChange} isChecked={isChecked} />;
},
} satisfies Story;
```
35 changes: 35 additions & 0 deletions docs/snippets/react/page-story-args-within-story.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
```ts
jonniebigodes marked this conversation as resolved.
Show resolved Hide resolved
// my-component/component.stories.ts|tsx

import { StoryObj, Meta } from '@storybook/react';
import { useArgs } from '@storybook/preview-api';
import { Checkbox } from './checkbox';

const meta: Meta<typeof Checkbox> = {
title: 'Inputs/Checkbox',
component: Checkbox,
};
export default meta;

type Story = StoryObj<typeof Checkbox>;

export const Example: Story = {
args: {
isChecked: false,
label: 'Try Me!',
},
/**
* 👇 To avoid linting issues, it is recommended to use a function with a capitalized name.
* If you are not concerned with linting, you may use an arrow function.
*/
render: function Render(args) {
const [{ isChecked }, updateArgs] = useArgs();

function onChange() {
updateArgs({ isChecked: !isChecked });
}

return <Checkbox {...args} onChange={onChange} isChecked={isChecked} />;
},
};
```
19 changes: 19 additions & 0 deletions docs/writing-stories/args.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ Similarly, special formats are available for dates and colors. Date objects will

Args specified through the URL will extend and override any default values of args set on the story.

<IfRenderer renderer='react'>

## Setting args from within a story
jonniebigodes marked this conversation as resolved.
Show resolved Hide resolved

Interactive components often need to be controlled by their containing component or page to respond to events, modify their state and reflect those changes in the UI. For example, when a user toggles a switch component, the switch should be checked, and the arg shown in Storybook should reflect the change. To enable this, you can use the `useArgs` API exported by `@storybook/preview-api`:

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

<CodeSnippets
paths={[
'react/page-story-args-within-story.js.mdx',
'react/page-story-args-within-story.ts.mdx'
]}
/>

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

</IfRenderer>

## Mapping to complex arg values

Complex values such as JSX elements cannot be serialized to the manager (e.g., the Controls addon) or synced with the URL. Arg values can be "mapped" from a simple string to a complex type using the `mapping` property in `argTypes` to work around this limitation. It works in any arg but makes the most sense when used with the `select` control type.
Expand Down