npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nlabs/storybook-addon-links

v3.4.3

Published

Story Links addon for storybook

Downloads

3

Readme

Story Links Addon

Build Status on CircleCI CodeFactor Known Vulnerabilities BCH compliance codecov


The Storybook Links addon can be used to create links that navigate between stories in Storybook.

This addon works with Storybook for:

Getting Started

Install this addon by adding the @nlabs/storybook-addon-links dependency:

yarn add @nlabs/storybook-addon-links

First configure it as an addon by adding it to your addons.js file (located in the Storybook config directory).

import '@nlabs/storybook-addon-links/register';

Then you can import linkTo in your stories and use like this:

import { storiesOf } from '@nlabs/storybook-react'
import { linkTo } from '@nlabs/storybook-addon-links'

storiesOf('Button', module)
  .add('First', () => (
    <button onClick={linkTo('Button', 'Second')}>Go to "Second"</button>
  ))
  .add('Second', () => (
    <button onClick={linkTo('Button', 'First')}>Go to "First"</button>
  ));

Have a look at the linkTo function:

import { linkTo } from '@nlabs/storybook-addon-links'

linkTo('Toggle', 'off')
linkTo(() => 'Toggle', () => 'off')
linkTo('Toggle') // Links to the first story in the 'Toggle' kind

With that, you can link an event in a component to any story in the Storybook.

  • First parameter is the the story kind name (what you named with storiesOf).
  • Second (optional) parameter is the story name (what you named with .add). If the second parameter is omitted, the link will point to the first story in the given kind.

You 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:

import { storiesOf } from '@nlabs/storybook-react';
import { LinkTo, linkTo } from '@nlabs/storybook-addon-links';

storiesOf('Select', module)
  .add('Index', () => (
    <select value="Index" onChange={linkTo('Select', e => e.currentTarget.value)}>
      <option>Index</option>
      <option>First</option>
      <option>Second</option>
      <option>Third</option>
    </select>
  ))
  .add('First', () =>  <LinkTo story="Index">Go back</LinkTo>)
  .add('Second', () => <LinkTo story="Index">Go back</LinkTo>)
  .add('Third', () => <LinkTo story="Index">Go back</LinkTo>);

hrefTo function

If 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:

import { storiesOf } from '@nlabs/storybook-react';
import { hrefTo } from '@nlabs/storybook-addon-links';
import { action } from '@nlabs/storybook-addon-actions';

storiesOf('Href', module)
  .add('log', () => {
    hrefTo('Href', 'log').then(action('URL of this story'));

    return <span>See action logger</span>;
  });

LinkTo component (React only)

One 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. A React implementation of such a component can be imported from @nlabs/storybook-addon-links package:

import { storiesOf } from '@nlabs/storybook-react';
import { LinkTo } from '@nlabs/storybook-addon-links';

storiesOf('Link', module)
  .add('First', () => (
    <LinkTo story="Second">Go to Second</LinkTo>
  ))
  .add('Second', () => (
    <LinkTo story="First">Go to First</LinkTo>
  ));

It accepts all the props the a element does, plus story and kind. It the kind prop is omitted, the current kind will be preserved.

<LinkTo
  kind="Toggle"
  story="off"
  target="_blank"
  title="link to second story"
  style={{color: '#1474f3'}}
>Go to Second</LinkTo>

To implement such a component for another framework, you need to add special handling for click event on native a element. See RoutedLink sources for reference.