UNPKG

4.16 kBMarkdownView Raw
1# Story Links Addon
2
3The Storybook Links addon can be used to create links that navigate between stories in [Storybook](https://storybook.js.org).
4
5[Framework Support](https://github.com/storybookjs/storybook/blob/main/ADDONS_SUPPORT.md)
6
7## Getting Started
8
9Install this addon by adding the `@storybook/addon-links` dependency:
10
11```sh
12yarn add -D @storybook/addon-links
13```
14
15within `.storybook/main.js`:
16
17```js
18module.exports = {
19 addons: ['@storybook/addon-links'],
20};
21```
22
23Then you can import `linkTo` in your stories and use like this:
24
25```js
26import { linkTo } from '@storybook/addon-links';
27
28export default {
29 title: 'Button',
30};
31
32export const first = () => <button onClick={linkTo('Button', 'second')}>Go to "Second"</button>;
33export const second = () => <button onClick={linkTo('Button', 'first')}>Go to "First"</button>;
34```
35
36Have a look at the linkTo function:
37
38```js
39import { linkTo } from '@storybook/addon-links';
40
41linkTo('Toggle', 'off');
42linkTo(
43 () => 'Toggle',
44 () => 'off'
45);
46linkTo('Toggle'); // Links to the first story in the 'Toggle' kind
47```
48
49With that, you can link an event in a component to any story in the Storybook.
50
51- First parameter is the story kind name (what you named with `title`).
52- Second (optional) parameter is the story name (what you named with `exported name`).
53 If the second parameter is omitted, the link will point to the first story in the given kind.
54
55You can also pass a function instead for any of above parameter. That function accepts arguments emitted by the event and it should return a string:
56
57```js
58import { LinkTo, linkTo } from '@storybook/addon-links';
59
60export default {
61 title: 'Select',
62};
63
64export const index = () => (
65 <select value="Index" onChange={linkTo('Select', (e) => e.currentTarget.value)}>
66 <option>index</option>
67 <option>first</option>
68 <option>second</option>
69 <option>third</option>
70 </select>
71);
72export const first = () => <LinkTo story="index">Go back</LinkTo>;
73export const second = () => <LinkTo story="index">Go back</LinkTo>;
74export const third = () => <LinkTo story="index">Go back</LinkTo>;
75```
76
77## hrefTo function
78
79If you want to get an URL for a particular story, you may use `hrefTo` function. It returns a promise, which resolves to string containing a relative URL:
80
81```js
82import { hrefTo } from '@storybook/addon-links';
83import { action } from '@storybook/addon-actions';
84
85export default {
86 title: 'Href',
87};
88
89export const log = () => {
90 hrefTo('Href', 'log').then(action('URL of this story'));
91
92 return <span>See action logger</span>;
93};
94```
95
96## withLinks decorator
97
98`withLinks` decorator enables a declarative way of defining story links, using data attributes.
99Here is an example in React, but it works with any framework:
100
101```js
102import { withLinks } from '@storybook/addon-links';
103
104export default {
105 title: 'Button',
106 decorators: [withLinks],
107};
108
109export const first = () => (
110 <button data-sb-kind="OtherKind" data-sb-story="otherStory">
111 Go to "OtherStory"
112 </button>
113);
114```
115
116## LinkTo component (React only)
117
118One possible way of using `hrefTo` is to create a component that uses native `a` element, but prevents page reloads on plain left click, so that one can still use default browser methods to open link in new tab.
119A React implementation of such a component can be imported from `@storybook/addon-links` package:
120
121```js
122import LinkTo from '@storybook/addon-links/react';
123
124export default {
125 title: 'Link',
126};
127
128export const first = () => <LinkTo story="second">Go to Second</LinkTo>;
129export const second = () => <LinkTo story="first">Go to First</LinkTo>;
130```
131
132It accepts all the props the `a` element does, plus `story` and `kind`. It the `kind` prop is omitted, the current kind will be preserved.
133
134```js
135<LinkTo
136 kind="Toggle"
137 story="off"
138 target="_blank"
139 title="link to second story"
140 style={{ color: '#1474f3' }}
141>
142 Go to Second
143</LinkTo>
144```
145
146To implement such a component for another framework, you need to add special handling for `click` event on native `a` element. See [`RoutedLink` sources](https://github.com/storybookjs/storybook/blob/main/addons/links/src/react/components/RoutedLink.tsx) for reference.