Coder Social home page Coder Social logo

storybook-solid-js's Introduction

Storybook for Solid-js example

ATTENTION

Solid is now supported by storybook, so firstly check this link: https://storybook.js.org/docs/solid/get-started/install/

This repo is the example of adoption storybook for solid-js.

Thanks to guys from this thread: solidjs/solid-docs-next#35

Instructions

Storybook v7

You need to change the following files.

1. .storybook/main.js

module.exports = {
  stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/addon-interactions",
  ],
  framework: {
    name: "@storybook/html-vite",
    options: {},
  },
  docs: {
    autodocs: "tag",
  },
};

2. .storybook/preview.js

If you want HMR works for solid you need to add /* @refresh reload */ to the beginning of this file however it's not the only change. See the details below.

/* @refresh reload */
/**
 * Don't forget the line above for HMR!
 * 
 * Note: for some reason HMR breaks if you change .stories file,
 * however reloading the page fixes this issue
 */ 

import { render } from "solid-js/web";

export const decorators = [
  (Story) => {
    const solidRoot = document.createElement("div");

    render(Story, solidRoot);

    return solidRoot;
  },
];

/** Autogenerated by Storybook */
export const parameters = {
  actions: { argTypesRegex: "^on[A-Z].*" },
  controls: {
    matchers: {
      color: /(background|color)$/i,
      date: /Date$/,
    },
  },
};

That's it!

HMR

To make HMR work for your component you need to render it as JSX:

// Correct! HMR works!
// Let's assume that this is storybook meta object
export default {
  // ...
  render: (props) => <Counter {...props} />,
  // ...
} as Meta<ComponentProps<typeof Counter>>;

If you write the code like this, it won't work:

// Wrong! HMR doesn't work!
// Let's assume that this is storybook meta object
export default {
  // ...
  render: Counter,
  // ...
} as Meta<ComponentProps<typeof Counter>>;

Here's an example story for Counter component.

import { Counter, CounterProps } from "../Counter";

import type { Meta, StoryObj } from "@storybook/html";
import type { ComponentProps } from "solid-js";

type Story = StoryObj<CounterProps>;

export const Default: Story = {
  args: {
    initialValue: 12,
    theme: "default",
  },
};

export default {
  title: "Example/Counter",
  tags: ["autodocs"],
  /**
   * Here you need to render JSX for HMR work!
   *
   * render: Counter won't trigger HMR updates
   */
  render: (props) => <Counter {...props} />,
  argTypes: {
    initialValue: { control: "number" },
    theme: {
      options: ["default", "red"],
      control: { type: "radio" },
    },
  },
} as Meta<ComponentProps<typeof Counter>>;

Storybook v6

To see the files for the storybook v6 see THIS.

1. Initialize storybook in your repo as html project:

npx storybook init --type html

2. Update storybook config files in .storybook folder as follows:

main.js

Add vite-plugin-solid to storybook config.

const Solid = require("vite-plugin-solid");

module.exports = {
  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/addon-interactions",
  ],
  framework: "@storybook/html",
  core: {
    builder: "@storybook/builder-vite",
  },
  features: {
    storyStoreV7: true,
  },
  
  // Add solid plugin here
  async viteFinal(config, { configType }) {
    config.plugins.unshift(Solid({ hot: false }));

    return config;
  },
};

preview.js

Customize your preview.js file as follows.

import { render } from "solid-js/web";

export const parameters = {
  actions: { argTypesRegex: "^on[A-Z].*" },
  controls: {
    matchers: {
      color: /(background|color)$/i,
      date: /Date$/,
    },
  },
};

export const decorators = [
  (Story) => {

    const root = document.getElementById("root");
    const solidRoot = document.createElement("div");

    solidRoot.setAttribute("id", "solid-root");
    root.appendChild(solidRoot);

    render(Story, solidRoot);

    return solidRoot;
    // return createRoot(() => Story()); // do not work correctly https://github.com/solidjs/solid/issues/553
  },
];

Comments

  • .npmrc file is necessary because I use npm v8, however storybook doesn't support it, and that's why it requires this file.

storybook-solid-js's People

Contributors

elite174 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

storybook-solid-js's Issues

Defining multiple stories breaks UI rendering

image

If we expand Your example by:

export const Second: Story = {
  args: {
    initialValue: 35,
  },
};

Then we see, that only LAST story renders.
It happened in our private repository, no matter how eazy or advanced component is.

If we click something on Storybook controls - it will render in missing place of default story view, but not at first render.

Can we use this setup with solid-start/vite?

Hey ๐Ÿ‘‹๐Ÿป

Your guide helped get me up and running, thank you.

In the guide, we use vite-plugin-solid in vite.config.ts as a plugin, which works.

Now I'm moving over to using solid-start so I can join everything up into a working app. To do this, I have to use a different plugin (solid-start/vite) - but doing this causes an error. Do you know if it's possible to use solid-start & storybook 7 successfully together?

The error:

ERR! TypeError: Cannot read properties of null (reading 'once')
ERR!     at configureServer (file:///Users/mike/Documents/shots-fired/node_modules/solid-start/vite/plugin.js:146:23)
ERR!     at Module.createServer (file:///Users/mike/Documents/shots-fired/node_modules/vite/dist/node/chunks/dep-79892de8.js:62948:30)
ERR!     at Module.start (/Users/mike/Documents/shots-fired/node_modules/@storybook/builder-vite/dist/index.js:160:12552)
ERR!     at async storybookDevServer (/Users/mike/Documents/shots-fired/node_modules/@storybook/core-server/dist/index.js:35:7330)
ERR!     at async buildDevStandalone (/Users/mike/Documents/shots-fired/node_modules/@storybook/core-server/dist/index.js:48:2786)
ERR!     at async withTelemetry (/Users/mike/Documents/shots-fired/node_modules/@storybook/core-server/dist/index.js:35:3620)
ERR!     at async dev (/Users/mike/Documents/shots-fired/node_modules/@storybook/cli/dist/generate.js:430:400)
ERR!     at async Command.<anonymous> (/Users/mike/Documents/shots-fired/node_modules/@storybook/cli/dist/generate.js:432:225)

I'm using SB 7.0.18.

Expected renderToDOM

Thanks for making this example!

I'm getting this error when I run it with a trivial component and a very similar vite setup.

Error: Expected your framework's preset to export a `renderToDOM` field.

Perhaps it needs to be upgraded for Storybook 6.4?

I have the same version of storybook from your package, 7.0.0-beta so not sure what the 6.4 message is about.

note about newer version not working

Thanks for creating this example. Works great.

Just want to share that if I upgrade the versions to 7.0.0-beta.55,
I got:

ReferenceError: process is not defined
    at node_modules/util/util.js (http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-MEV2Q23E.js?v=2948c77e:1213:5)
    at __require2 (http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-HYZYPRER.js?v=2948c77e:15:50)
    at node_modules/assert/build/internal/assert/assertion_error.js (http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-MEV2Q23E.js?v=2948c77e:2042:20)
    at __require2 (http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-HYZYPRER.js?v=2948c77e:15:50)
    at node_modules/assert/build/assert.js (http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-MEV2Q23E.js?v=2948c77e:3359:26)
    at __require2 (http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-HYZYPRER.js?v=2948c77e:15:50)
    at http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-MEV2Q23E.js?v=2948c77e:3843:24
    at node_modules/doctrine/lib/utility.js (http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-MEV2Q23E.js?v=2948c77e:3844:7)
    at __require2 (http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-HYZYPRER.js?v=2948c77e:15:50)
    at http://localhost:6007/node_modules/.cache/.vite-storybook/deps/chunk-MEV2Q23E.js?v=2948c77e:3855:17

It's probably the newer version added the assertion module causing this.

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.