Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
32 changes: 32 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,32 @@
```js
// my-component/component.stories.js|jsx

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

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

export const Example = {
args: {
isChecked: false,
label: 'Switch Me!',
},
/**
* 👇 React believes the Storybook function "useArgs" is a hook, but it's really not.
* Using a named capital-letter function prevents an ESLint warning related to this.
* Don't lint? You can use an arrow function instead.
*/
render: function Render(args) {
const [{ isChecked }, updateArgs] = useArgs();

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

return <Switch {...args} onChange={onChange} isChecked={isChecked} />;
},
};
```
36 changes: 36 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,36 @@
```ts
// my-component/component.stories.ts|tsx

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

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

type Story = StoryObj<typeof Switch>;

export const Example = {
args: {
isChecked: false,
label: 'Switch Me!',
},
/**
* 👇 React believes the Storybook function "useArgs" is a hook, but it's really not.
* Using a named capital-letter function prevents an ESLint warning related to this.
* Don't lint? You can use an arrow function instead.
*/
render: function Render(args) {
const [{ isChecked }, updateArgs] = useArgs();

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

return <Switch {...args} onChange={onChange} isChecked={isChecked} />;
},
};
```
15 changes: 15 additions & 0 deletions docs/writing-stories/args.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ 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.

## Setting args from within a story

Components with interactivity often need their containing component, or page, to respond to events, modify their state, then pass a changed arg back to the rendered component to reflect the change. For example, you would want a Switch component to be checked by a user and the arg shown in Storybook to reflect the change. This can be accomplished by using the `useArgs` hook exported by `@storybook/preview-api`:

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

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

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

## 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