• Stars
    star
    237
  • Rank 163,984 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This Storybook addon show you the JSX / template of the story


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 [email protected] 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;
      }
    }
  }
};
  • displayName (default: 0) : You can manually name the components that use useMemo or useRef.
export default {
  title: 'Components/TestComponent',
  parameters: {
    jsx: {
      displayName: () => 'CustomName'
    }
  }
};

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

πŸ’»

Semih Raif GΓΌrel

πŸ“–

Lee Powell

πŸš‡ πŸ’»

Jimmy Andrade

πŸš‡

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

More Repositories

1

storybook

Storybook is a frontend workshop for building UI components and pages in isolation. Made for UI development, testing, and documentation.
TypeScript
82,383
star
2

design-system

πŸ—ƒ Storybook Design System
TypeScript
1,862
star
3

react-native

πŸ““ Storybook for React Native!
TypeScript
942
star
4

builder-vite

A builder plugin to run and build Storybooks with Vite
TypeScript
884
star
5

addon-designs

A Storybook addon that embeds Figma, websites, or images in the addon panel.
TypeScript
851
star
6

react-inspector

πŸ” Power of Browser DevTools inspectors right inside your React app
TypeScript
740
star
7

testing-react

Testing utilities that allow you to reuse your Storybook stories in your React unit tests!
TypeScript
589
star
8

presets

🧩 Presets for Storybook
TypeScript
424
star
9

marksy

πŸ“‘ A markdown to custom VDOM components library
JavaScript
354
star
10

vue-cli-plugin-storybook

Vue CLI plugin for Storybook
JavaScript
280
star
11

eslint-plugin-storybook

πŸŽ—Official ESLint plugin for Storybook
TypeScript
228
star
12

frontpage

🌐 The website for storybook.js.org
TypeScript
209
star
13

test-runner

πŸš• Turn stories into executable tests
TypeScript
202
star
14

storybook-addon-console

storybook-addon. Redirects console output into action logger panel
TypeScript
198
star
15

native

πŸ“± Storybook for Native: iOS, Android, Flutter
TypeScript
176
star
16

babel-plugin-react-docgen

πŸ“ Babel plugin to add react-docgen info into your code.
JavaScript
162
star
17

telejson

πŸ›° JSON parse & stringify with support for cyclic objects, functions, dates, regex, infinity, undefined, null, NaN, Classes, Instances
TypeScript
158
star
18

vs-code-plugin

Aesop: a VSCode Extension to stage Storybook stories inside your IDE.
TypeScript
130
star
19

addon-kit

Everything you need to build a Storybook addon
TypeScript
122
star
20

brand

🎨 Materials for your articles and talks about storybook
89
star
21

addon-svelte-csf

[Incubation] CSF using Svelte components.
TypeScript
86
star
22

desktop

πŸ’» Desktop app for all your Storybooks
JavaScript
71
star
23

addon-react-native-web

Build react-native-web projects in Storybook for React
TypeScript
71
star
24

testing-library

Instrumented version of Testing Library for Storybook Interactions
TypeScript
56
star
25

require-context.macro

πŸ–‡ A Babel macro needed for some advanced Storybook setups. Used to mock webpack's context function in test environments.
JavaScript
48
star
26

addon-styling

A base addon for configuring popular styling tools
TypeScript
44
star
27

ember-cli-storybook

πŸ“’ Ember storybook adapter
JavaScript
36
star
28

solidjs

SolidJS integration for Storybook, maintained by the community
TypeScript
35
star
29

aem

Adobe Experience Manager Storybook app with events, knobs, docs, addons, and more
JavaScript
33
star
30

paths.macro

πŸ‘£ Babel plugin that returns an object containing paths like __dirname and __filename as static values
JavaScript
32
star
31

shout-out-kit

An image generation API template
JavaScript
30
star
32

linter-config

πŸ“ ESlint config, Prettier config, Remark config for storybook
JavaScript
27
star
33

SBNext

A future SB
JavaScript
26
star
34

sandboxes

MDX
25
star
35

addon-measure

JavaScript
20
star
36

addon-postcss

This Storybook addon can be used to run the PostCSS preprocessor against your stories.
JavaScript
20
star
37

addon-styling-webpack

Successor to @storybook/addon-styling. Configure the styles of your webpack storybook with ease!
TypeScript
18
star
38

jest

Instrumented version of Jest for Storybook Interactions
TypeScript
18
star
39

addon-coverage

TypeScript
18
star
40

nextjs-example

Example app showing Storybook integrated with Next.js (v11) pages
JavaScript
17
star
41

testing-vue3

Testing utilities that allow you to reuse your stories in your unit tests
TypeScript
16
star
42

bench

Storybook benchmark
TypeScript
15
star
43

testing-angular

TypeScript
13
star
44

mdx2-csf

MDX to CSF compiler using MDXv2
TypeScript
13
star
45

addon-knobs

Storybook addon prop editor component
TypeScript
10
star
46

testing-vue

TypeScript
10
star
47

jest-storybook

Test storybook stories automagically using Jest. πŸ§™πŸ»β€β™€οΈ
JavaScript
9
star
48

addon-google-analytics

Storybook addon for google analytics
JavaScript
9
star
49

expect

Browser-compatible version of Jest's `expect`
JavaScript
7
star
50

storybook-nuxt

Storybook integration for Nuxt Framework - Unleashing the power of Storybook in Nuxt/Vue Land!
TypeScript
6
star
51

raycast-extension-sandboxes

A Raycast Extension to quickly create sandboxes online or locally
TypeScript
6
star
52

icons

Library of icons used in apps and marketing sites
TypeScript
6
star
53

vue3-code-examples

it is a repo to show Vuetify implementation with new @storybook/vue3 reactive mode
TypeScript
6
star
54

marko

Storybook framework support for marko
TypeScript
6
star
55

addon-graphql

Storybook addon to display the GraphiQL IDE
TypeScript
6
star
56

addon-onboarding

TypeScript
6
star
57

action

🚒 WIP, storybook github action - build your storybook from github
JavaScript
6
star
58

addon-queryparams

parameter addon for storybook
TypeScript
6
star
59

storybook-day

Storybook day website
TypeScript
5
star
60

addon-ie11

JavaScript
5
star
61

addon-design-assets

Design asset preview for storybook
JavaScript
5
star
62

addon-cssresources

A storybook addon to switch between css resources at runtime for your story
TypeScript
5
star
63

create-svelte-with-args

A small CLI wrapper around the create-svelte package that enables you to replace the interactive prompts with CLI arguments.
JavaScript
5
star
64

addon-events

Add events to your Storybook stories.
TypeScript
4
star
65

governance

βš–οΈ Storybook governance and community policies
4
star
66

community

πŸ™Œ Storybook community resources: monthly meetings, meetups, conferences
4
star
67

mdx1-csf

MDX to CSF compiler using MDXv1
TypeScript
4
star
68

addon-react-native-server

A replacement for @storybook/react-native-server which will enable multiple devices to sync over websockets
TypeScript
4
star
69

docs-mdx

TypeScript
3
star
70

addon-angular-ivy

TypeScript
3
star
71

babel-plugin-named-exports-order

Babel plugin for preserving exports order across transforms
JavaScript
3
star
72

nextjs-server

Embedded Storybook in Next.js
TypeScript
3
star
73

addon-bench

Storybook benchmarking helper
JavaScript
3
star
74

repro-react-cra

Reproduction template for Create React App
JavaScript
3
star
75

react-native-demo

Storybook for React Native monorepo
2
star
76

ember

Storybook for Ember
TypeScript
2
star
77

docs2-prototype

TypeScript
2
star
78

components-marketing

TypeScript
2
star
79

repro-templates

WIP
JavaScript
2
star
80

eslint-config-storybook

πŸ”— wrapper around our @storybook/linter-config package
JavaScript
2
star
81

.github

Repo community health files
1
star
82

regression-test-other

1
star
83

e2e-testing-starter

JavaScript
1
star
84

vue3-autogen-controls

TypeScript
1
star
85

stencil-builder-test

TypeScript
1
star
86

auto-config

TypeScript
1
star
87

external-sandboxes

MDX
1
star
88

create-webpack5-react

JavaScript
1
star
89

playwright-ct

Playwright component testing against a Storybook instance
TypeScript
1
star
90

regression-test-preact

1
star
91

addon-webpack5-compiler-babel

Adds babel as a Webpack5 compiler to Storybook
TypeScript
1
star
92

web

TypeScript
1
star