nidkil / vuetify-with-storybook

Setting up Storybook with Vuetify the right way
MIT License
117 stars 20 forks source link
component-development storybook testing visual-testing vue vue-cli-3 vuetify

Setting up Storybook with Vuetify logo

Setting up Storybook with Vuetify and Vue CLI 3

Learn how to setup and use it the correct way

 

I struggled to get Storybook to work with Vuetify and Vue CLI 3. In this repository I have documented how I got it up and running the correct way. Hopefully it will give others a kick start.

 

Table of Contents

Inspiration

The solution was inspired by vue-vuetify-storybook, which shows a working example of Vue CLI 3, Vuetify and Storybook and a lot of other goodies. I tried to setup Storybook with my own repository taking this repo as an example, but I failed. The problem with this repository is, that it does not explain how it works. It also uses different configurations for the application itself and Storybook. This means that a component can work for the one but not for the other. I wanted a shared configuration, so that when a component works in either you no it works for the other. That shared configuration is the ./src/plugins/vuetify.js plugin file that initializes Vuetify.

So you have the choice to follow along and configure your project yourself and really understand how it works or just clone and use the repository. The choice is yours. Are you going to choose the red pill or the blue pill?

Go to Table of Contents

Built with

Go to Table of Contents

Important

The following two points are key to getting Storybook to work correctly with Vuetify:

1) The Vuetify components must be imported individually from 'vuetify/lib' and added as components to Vue.use when initializing Vuetify, see the Vuetify documentation for more information. This is done in the ./src/plugins/vuetify.js plugin file. If a story isn't displaying correctly in Storybook and it does work correctly when running normally, always check that all Vuetify components it uses are imported!

> **IMPORTANT** Storybook does not work with `vuetify-loader`, at least I did not get it to work.

2) The font must be imported using the preview-head.html file.

Go to Table of Contents

Useful links

Go to Table of Contents

Create project manually

The following section describes the steps that need to be execute to manually create a project with Vue CLI 3, Vuetify and vue-cli-plugin-vuetify.

Go to Table of Contents

Addons

This section only describes those addon's that require special instructions to work correctly with Vue or Vuetify.

Backgrounds Addon

The Backgrounds Addon is used to change background colors inside the preview in Storybook. To get the background addon to work with Vuetify requires a hack to VApp, as it sets and controls the background color. To let the background addon control the background color we need to set the background of VApp to transparent. In ./config/storybook/config.js change the following:

addDecorator(() => ({
  template: '<v-app><story/></v-app>',
  style: '.theme--light.application { background-color: transparent; }'
}))

To:

addDecorator(() => ({
  template: '<v-app style="background-color: transparent;"><story/></v-app>',
  style: '.theme--light.application { background-color: transparent; }'
}))

Viewport Addon

The Viewport Addon allows stories to be displayed in different sizes and layouts in Storybook. This helps build responsive components inside of Storybook. Vuetify has a 12 point grid system. Built using flex-box, the grid is used to layout an application's content. It contains 5 types of media breakpoints that are used for targeting specific screen sizes and orientations. These media types can be added to the viewports of the Viewport addon to simplify testing how Vuetify components respond to different media breakpoints.

Add the following to the ./config/storybook/config.js file:

 import { configureViewport, INITIAL_VIEWPORTS } from '@storybook/addon-viewport'

 const vuetifyViewports = {
   VuetifyLg: {
     name: 'Vuetify LG',
     styles: {
       width: '1904px'
     },
     type: 'desktop'
   },
   VuetifyXs: {
     name: 'Vuetify XS',
     styles: {
       width: '600px'
     },
     type: 'mobile'
   },
   VuetifySm: {
     name: 'Vuetify SM',
     styles: {
       width: '960px'
     },
     type: 'mobile'
   },
   VuetifyMd: {
     name: 'Vuetify MD',
     styles: {
       width: '1264px'
     },
     type: 'tablet'
   },
   VuetifyXl: {
     name: 'Vuetify XL',
     styles: {
       width: '4096px'
     },
     type: 'desktop'
   }
 }

 configureViewport({
   defaultViewport: 'VuetifyMd',
   viewports: {
     ...vuetifyViewports,
     ...INITIAL_VIEWPORTS
   }
 })

Note Vuetify MD viewport is set as default, so that it is selected when a story is opened.

Storysource Addon

This Storysource Addon is used to show stories source in the Storyboard addon panel. Getting it to work with Vue CLI 3 is tricky as it abstracts away the webpack config. The internal webpack config is maintained using webpack-chain . The library provides an abstraction over the raw webpack config, with the ability to define named loader rules and named plugins, and later "tap" into those rules and modify their options. To tweak the webpack config you need to add or modify the vue.config.js file that is located in the project root. Add the following to this file:

module.exports = {
  chainWebpack: config => {
    if (process.env.STORYBOOK && process.env.STORYBOOK.trim() === 'true') {
      console.info('info => Updating webpack using chain-webpack')
      config.module
        .rule('addon-storysource')
        .enforce()
        .pre()
        .test(/\.stories\.jsx?$/)
        .use('@storybook/addon-storysource/loader')
        .loader('@storybook/addon-storysource/loader')
        .options({
          semi: false,
          printWidth: 120,
          singleQuote: true
        })
        .end()
    }
  }
}

NOTE We only want the Storysource Addon to kick in when running Storybook so we have to make the rule conditional. To do this we will check if the environment variable STORYBOOK is set. If it is the rule is added, otherwise it is ignored. We will set the environment variable in the storybook scripts in the package.json file:

{
  "scripts": {
    "storybook:build": "SET STORYBOOK=true & vue-cli-service storybook:build -c config/storybook",
    "storybook:serve": "SET STORYBOOK=true & vue-cli-service storybook:serve -p 6006 -c config/storybook"
  }
}

PRO TIP vue-cli-service exposes the inspect command for inspecting the resolved webpack config. The command will print the resolved webpack config to stdout, which also contains hints on how to access rules and plugins via chaining. Add the following entry to the scripts section in the package.json file to easily call the inspect command and redirect the output to the webpack.config.inspect.js file for easier inspection:

{
  "scripts": {
    "webpack:inspect": "SET STORYBOOK=true & vue inspect > webpack.config.inspect.js"
  }
}

Go to Table of Contents

Project setup

Install dependencies

npm install

Compiles and hot-reloads for development

npm run serve

Run Storybook

npm run storybook:serve

Compiles and minifies for production

npm run build

Run your tests

npm run test

Lints and fixes files

npm run lint

Run your unit tests

npm run test:unit

Go to Table of Contents

Misc

Go to Table of Contents

Roadmap

Currently the following is on the roadmap.

Any other suggestions? Please submit an issue.

Go to Table of Contents

Contributing

We welcome pull requests! What follows is the simplified version of the contribution process, please read here to fully understand our contribution policy and here to understand our code of conduct.

  1. Fork the repository here!
  2. Create your feature branch: git checkout -b my_new_feature
  3. If relevant, don't forget to add your tests
  4. Commit your changes: npm run commit
  5. Push to the branch: git push origin my-new-feature
  6. Submit a pull request :-)

TIP Learn all about forking a repo here. More information on cloning a repo here.

Go to Table of Contents

Support & brag about us

If you like this project, please support us by starring ⭐ this repository. Thx!

Please let the world know about us! Brag about us using Twitter, email, blog, Discord, Slack, forums, etc. etc. Thx!

Go to Table of Contents

Author

nidkil © nidkil, released under the MIT license. Authored and maintained by nidkil with help from contributors.

Website · GitHub @nidkil · Twitter @nidkil