DEV Community

Cover image for Setting up Stencil 4 with Storybook 8
Joaquin Senosiain, Jr
Joaquin Senosiain, Jr

Posted on

2

Setting up Stencil 4 with Storybook 8

I had a lot of trouble getting this setup and working properly, but I finally managed to get it working. I hope this saves someone the same anguish I went through.

Step 1: Install Stencil

npm init stencil
Enter fullscreen mode Exit fullscreen mode

At the prompt select components then name your project and confirm.

Step 2: Navigate to Project and Install Dependencies

cd <your-project-name>
npm install
Enter fullscreen mode Exit fullscreen mode

Step 3: Integrate Storybook

npx sb init --type web_components
Enter fullscreen mode Exit fullscreen mode

Select Vite at the prompt.

Step 4. Update compilerOptions in tsconfig.json

"compilerOptions": {
  ...,
  "skipLibCheck": true
}
Enter fullscreen mode Exit fullscreen mode

Step 5. Configure Storybook

Open .storybook/preview.js and add the following code to the top of the file.

import {defineCustomElements} from '../loader';
defineCustomElements();
Enter fullscreen mode Exit fullscreen mode

Now, open .storybook/main.ts (you may have to update the js file to ts) and copy the following code.

import type { StorybookConfig } from '@storybook/web-components-vite';

const config: StorybookConfig = {
  stories: ['../src/components', '../src/styleguide', '../src/stories'],
  addons: ['@storybook/addon-essentials'],
  //staticDirs: ['../dist/lib-components'],
  docs: {
    autodocs: true
  },
  async viteFinal(config, { configType }) {
    const { mergeConfig } = await import('vite')

    if (configType !== 'DEVELOPMENT') {
      return config
    }

    return mergeConfig(config, {
      build: {
        // this is set to 'dist' by default which causes hot-reloading for stencil components to break
        // see: https://vitejs.dev/config/server-options.html#server-watch
        // setting it to anything other than dist fixes the issue
        outDir: 'dist-vite'
      }
    })
  },
  core: {
    disableTelemetry: true
  },
  framework: '@storybook/web-components-vite'
}

export default config
Enter fullscreen mode Exit fullscreen mode

You may have to install vite.

npm install -D vite --legacy-peer-deps
Enter fullscreen mode Exit fullscreen mode

The --legacy-peer-deps flag was need as of this writing.

Step 6. Setup first Story

Delete src/stories.

In the src/components/my-component folder, create my-component.stories.tsx and add the following code.

export default {
  // this creates a 'Components' folder and a 'MyComponent' subfolder
  title: 'Components/MyComponent',
};

const Template = (args) => `<my-component first="${args.first}" middle="${args.middle}" last="${args.last}"></my-component>`;

export const Example = Template.bind({});
Example.args = {
  first: 'Alan',
  middle: 'Dean',
  last: 'Foster'
};
Enter fullscreen mode Exit fullscreen mode

Final Step: Run Project

Open terminal and run:

npm run build -- --watch
Enter fullscreen mode Exit fullscreen mode

Open a separate terminal and run.

npm run storybook
Enter fullscreen mode Exit fullscreen mode

Your project should be up and running and any changes you make in you my-component.tsx file will immediately be reflected in Storybook.

I hope this helps someone out there.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay