storybook-addon-jsx
Display the JSX of the story
Last updated 2 months ago by alisowski .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install storybook-addon-jsx 
SYNC missed versions from official npm registry.


Storybook-addon-jsx

Build Status Total Download Current Version

This Storybook addon shows you the JSX of the story. This preview works for Vue components as well. The outputted JSX will reflect any changes made to the storybok by knobs or controls.

Storybook Addon JSX Demo

Getting started

Installation

First install the addon from npm:

npm i --save-dev storybook-addon-jsx
# or
yarn add --dev storybook-addon-jsx

Configuration

For the latest storybook all you need to do is add the addon to your .storybook/main.js:

module.exports = {
  addons: ["storybook-addon-jsx"],
};

If you are using storybook@5.x or lower you will need to add the following to .storybook/addons.js:

import "storybook-addon-jsx/register";

Usage

Import it into your stories file and then use it when you write stories:

import React from "react";
import { storiesOf } from "@storybook/react";
import { jsxDecorator } from "storybook-addon-jsx";

import { TestComponent } from './TestComponent':

export default {
  title: "Components/TestComponent",
  decorators: [jsxDecorator],
};

export const Paris = () => (
  <TestComponent fontSize={45} fontFamily="Roboto" align="center" color="#CAF200">
    Hello
  </TestComponent>
);

export const Orleans = () => <Test color="#236544">Hello</Test>;

Or to configure it globally add the jsxDecorator to your .storybook/preview.js:

const { addDecorator } = require("@storybook/react");
const { jsxDecorator } = require("storybook-addon-jsx");

addDecorator(jsxDecorator);

Vue

You can also use this addon with @storybook/vue.

.storybook/preview.js

import { configure, addDecorator } from "@storybook/vue";
import { jsxDecorator } from "storybook-addon-jsx";

addDecorator(jsxDecorator);

If a Vue story defines its view with a template string then it will be displayed.

import { storiesOf } from "@storybook/vue";

storiesOf("Vue", module).add("template property", () => ({
  template: `<div></div>`,
}));

Options

JSX

This addon support all options from react-element-to-jsx-string as well as the following options.

  • skip (default: 0) : Skip element in your component to display
export default {
  title: "Components/TestComponent",
  parameters: {
    jsx: { skip: 1 },
  },
};
  • onBeforeRender(domString: string) => string (default: undefined) : function that receives the dom as a string before render.
export default {
  title: "Components/TestComponent",
  parameters: {
    jsx: {
      onBeforeRender: domString => {
        if (domString.search('dangerouslySetInnerHTML') < 0) {
          return ''
        }

        try {
          domString = /(dangerouslySetInnerHTML={{)([^}}]*)/.exec(domString)[2]
          domString = /(')([^']*)/.exec(domString)[2]
        } catch (err) {}

        return domString
      },
    },
  },
};

Disable JSX Addon

If enabled globally, the JSX addon can be disabled on individual stories:

export const Simple = () => <div>Hello</div>;

Simple.story = {
  parameters: {
    jsx: {
      disable: true,
    },
  },
};

Vue Options

  • enableBeautify (default: true) : Beautify the template string
  • All HTML options from js-beautify

Global Options

To configure global options for this plugin, add the following to your config.js.

import { addParameters } from "@storybook/react";

addParameters({
  jsx: {
    // your options
  },
});

Function Props

If you provide a funtion to one of your props storybook-addon-jsx will display that functions toString result. This is usaully very ugly. To override this include the following util function that will print an easiy to read string.

/**
 * Overrides the toString on a function so that it addon-jsx prints
 * the callbacks in a copy-paste-able way.
 */
export const callback = <T extends Function>(fn: T): T => {
  /** A toString to render the function in storybook */
  // eslint-disable-next-line no-param-reassign
  fn.toString = () => "() => {}";
  return fn;
};

This works well with the @storybook/addon-actions too.

export ExampleStory = () => (
  <TestComponent onClick={callback(action('onClick'))} />
)

Including DocGen Information

This addon will display prop type information while hovering over a component or prop. This is accomplished through a babel plugin in the default storybook configuration. To use the docgen information for TypeScript components you must include be using a typescript docgen loader

import { addParameters } from "@storybook/react";

addParameters({
  jsx: {
    // your options
  },
});

TypeScript Monorepo DocGen

In a TypeScript monorepo you will probably be importing components through package names. In this situation storybook will load your compiled typescript and lose information about the props.

One solution to get around this is to add a unique property to your component's package.json that points directly at the TypeScript source. We can then set storybook's webpack configuration to look for this property first, which will allow the TypeScript loader to insert docgen information.

In your component's package.json:

{
  // Can be any string you want, here we choose "source"
  "source": "src/index.tsx"
}

Then in your webpack config for storybook:

config.resolve.mainFields = ["source", "module", "main"];

Testing with storyshots

If you are using the addWithJSX method you will need to include storybook-addon-jsx in your test file.

import initStoryshots from "@storybook/addon-storyshots";
import { setAddon } from "@storybook/react";
import JSXAddon from "storybook-addon-jsx";

setAddon(JSXAddon);

initStoryshots({
  /* configuration options */
});

Usage with IE11

Some of the dependencies that this package has use APIs not available in IE11. To get around this you can add the following to your webpack.config.js file (your paths might be slightly different):

config.module.rules.push({
  test: /\.js/,
  include: path.resolve(__dirname, "../node_modules/stringify-object"),
  use: [
    {
      loader: "babel-loader",
      options: {
        presets: ["env"],
      },
    },
  ],
});

Contributors ✨

Thanks goes to these wonderful people (emoji key):


William

???? ???? ???? ????

Andrew Lisowski

???? ???? ???? ????

Norbert de Langen

???? ????

Samuel Vaillant

???? ????

Alexandre BODIN

????

Christophe Coevoet

????

Leonel Galán

????

Lincoln Anderson

????

Simon Mollweide

????

lflpowell

????

lionelbenychou

????

Brad Adams

????

Andrew Hansen

????

Peter Mikitsh

???? ????

lisamartin00

????

This project follows the all-contributors specification. Contributions of any kind welcome!

Current Tags

  • 4.0.0                                ...           beta (3 years ago)
  • 7.3.5-canary.127.504.0                                ...           canary (2 months ago)
  • 7.3.4                                ...           latest (2 months ago)

97 Versions

  • 7.3.5-canary.127.504.0                                ...           2 months ago
  • 7.3.5-canary.126.499.0                                ...           2 months ago
  • 7.3.5-canary.125.494.0                                ...           2 months ago
  • 7.3.5-canary.120.489.0                                ...           2 months ago
  • 7.3.5-canary.111.488.0                                ...           2 months ago
  • 7.3.5-canary.123.487.0                                ...           2 months ago
  • 7.3.4                                ...           2 months ago
  • 7.3.4-canary.122.468.0                                ...           2 months ago
  • 7.3.4-canary.120.463.0                                ...           3 months ago
  • 7.3.4-canary.118.457.0                                ...           3 months ago
  • 7.3.4-canary.111.458.0                                ...           3 months ago
  • 7.3.3                                ...           3 months ago
  • 7.3.3-canary.119.442.0                                ...           3 months ago
  • 7.3.3-canary.118.437.0                                ...           3 months ago
  • 7.3.3-canary.111.432.0                                ...           3 months ago
  • 7.3.2                                ...           3 months ago
  • 7.3.2-canary.111.415.0                                ...           4 months ago
  • 7.3.1                                ...           4 months ago
  • 7.3.1-canary.115.404.0                                ...           4 months ago
  • 7.3.1-canary.115.399.0                                ...           4 months ago
  • 7.3.1-canary.111.393.0                                ...           5 months ago
  • 7.3.0                                ...           5 months ago
  • 7.2.4-canary.108.363.0                                ...           5 months ago
  • 7.2.3                                ...           6 months ago
  • 7.2.3-canary.104.351.0                                ...           6 months ago
  • 7.2.3-canary.104.352.0                                ...           6 months ago
  • 7.2.2                                ...           6 months ago
  • 7.2.2-canary.103.336.0                                ...           6 months ago
  • 7.2.2-canary.103.332.0                                ...           6 months ago
  • 7.2.1                                ...           7 months ago
  • 7.2.1-canary.102.320.0                                ...           7 months ago
  • 7.2.1-canary.102.313.0                                ...           7 months ago
  • 7.2.0                                ...           7 months ago
  • 7.1.16-canary.101.304.0                                ...           7 months ago
  • 7.1.15                                ...           7 months ago
  • 7.1.15-canary.99.289                                ...           7 months ago
  • 7.1.14                                ...           9 months ago
  • 7.1.13                                ...           a year ago
  • 7.1.12                                ...           a year ago
  • 7.1.12-canary.92.253                                ...           a year ago
  • 7.1.12-canary.92.249                                ...           a year ago
  • 7.1.12-canary.92.246                                ...           a year ago
  • 7.1.11                                ...           a year ago
  • 7.1.11-canary.91.230                                ...           a year ago
  • 7.1.11-canary.91.226                                ...           a year ago
  • 7.1.10                                ...           a year ago
  • 7.1.10-canary.90.217                                ...           a year ago
  • 7.1.10-canary.90.213                                ...           a year ago
  • 7.1.9                                ...           a year ago
  • 7.1.8                                ...           a year ago
  • 7.1.7                                ...           a year ago
  • 7.1.7-canary.90.195                                ...           a year ago
  • 7.1.6                                ...           a year ago
  • 7.1.6-canary.185                                ...           a year ago
  • 7.1.5                                ...           a year ago
  • 7.1.4                                ...           a year ago
  • 7.1.4-canary.171                                ...           a year ago
  • 7.1.3                                ...           a year ago
  • 7.1.2                                ...           2 years ago
  • 7.1.1                                ...           2 years ago
  • 7.1.0                                ...           2 years ago
  • 7.0.2                                ...           2 years ago
  • 7.0.1                                ...           2 years ago
  • 7.0.0                                ...           2 years ago
  • 6.0.1                                ...           2 years ago
  • 6.0.0                                ...           2 years ago
  • 5.4.0                                ...           2 years ago
  • 5.3.0                                ...           3 years ago
  • 5.2.0                                ...           3 years ago
  • 5.1.0                                ...           3 years ago
  • 5.0.0                                ...           3 years ago
  • 4.2.0                                ...           3 years ago
  • 4.1.1                                ...           3 years ago
  • 4.1.0                                ...           3 years ago
  • 4.0.0                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 2.1.2                                ...           3 years ago
  • 2.1.1                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.10                                ...           3 years ago
  • 2.0.9                                ...           4 years ago
  • 2.0.8                                ...           4 years ago
  • 2.0.7                                ...           4 years ago
  • 2.0.6                                ...           4 years ago
  • 2.0.5                                ...           4 years ago
  • 2.0.4                                ...           4 years ago
  • 2.0.3                                ...           4 years ago
  • 2.0.2                                ...           4 years ago
  • 2.0.1                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 0.0.3                                ...           4 years ago
  • 0.0.2                                ...           4 years ago
Downloads
Today 6
This Week 23
This Month 265
Last Day 5
Last Week 135
Last Month 428

Copyright 2014 - 2017 © taobao.org |