Code Monkey home page Code Monkey logo

storybook-addon-next-router's Introduction

Storybook Addon Next Router

Use Next.js Router in your Storybook stories.

Versions

  • Use 1.x if you're using storybook 5.x
  • Use 3.x if you're using storybook 6.x

Note: these docs refer to 3.0

Add the addon to your configuration in .storybook/main.js

module.exports = {
  ...config,
  addons: [
    ...your addons
    "storybook-addon-next-router",
  ],
};

Add the RouterContext.Provider to .storybook/preview.js

import { RouterContext } from "next/dist/shared/lib/router-context"; // next 11.1
import { RouterContext } from "next/dist/next-server/lib/router-context"; // next < 11.1


export const parameters = {
  nextRouter: {
    Provider: RouterContext.Provider,
  },
}

Usage in story

import MyComponentThatHasANextLink from "../component-that-has-a-next-link";

export default {
  title: "My Story",
};

// if you have the actions addon
// you can click the links and see the route change events there
export const Example = () => <MyComponentThatHasANextLink />;

Example.story = {
  parameters: {
    nextRouter: {
      path: "/profile/[id]",
      asPath: "/profile/lifeiscontent",
      query: {
        id: "lifeiscontent",
      },
    },
  },
};

Custom defaults

in preview.js

export const parameters = {
    nextRouter: {
        path: '/', // defaults to `/`
        asPath: '/', // defaults to `/`
        query: {}, // defaults to `{}`
        push() {
        } // defaults to using addon actions integration,
        //   can override any method in the router
    }
};

Read more about the options available for next/router at https://nextjs.org/docs/api-reference/next/router

Example App

To see real world usage of how to use this addon, check out the example app:

https://github.com/lifeiscontent/realworld

storybook-addon-next-router's People

Contributors

lifeiscontent avatar dependabot[bot] avatar stevensacks avatar armandabric avatar bcomnes avatar dora1998 avatar tricoder42 avatar willheslam avatar coderkevin avatar niiccolas avatar

Forkers

jhchill666

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.