How to configure VueJS + PostCss + Tailwind with Storybook

后端 未结 3 1095
自闭症患者
自闭症患者 2021-02-08 00:38

Anyone have success setting up a project using VueJS, PostCss, and Tailwind with component development in Storybook?

I\'ve gotten this far:

  1. New vue
3条回答
  •  甜味超标
    2021-02-08 01:11

    I've never used tailwindCSS or postCSS (explicitly) before, so I decided to take this as an opportunity to learn to setup/configure both of these.

    You can find a complete example with Tailwind-styled components in storybook here: https://github.com/gurupras/vuejs-postcss-tailwind-with-storybook

    Steps

    • Set up VueJS project: vue create vue-postcss-tailwind-storybook
    • Install and initialize tailwindCSS
      • npm install tailwindcss
      • ./node_modules/.bin/tailwind init (generates tailwind.config.js)
    • Update postcss.config.js to contain the following:
    module.exports = {
      plugins: [
        require('tailwindcss')('tailwind.config.js'),
        require('autoprefixer')()
      ]
    }
    
    • Add Vue storybook plugin
      • vue add storybook
    • Add a CSS file containing the tailwind directives (e.g. src/style/tailwind.css)
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
    • optional Add import '@/style/tailwind.css' to your main.js to ensure that they're available to all parts of your app
    • Create your component
      • I'm going to assume the following component exists: src/components/alert.vue
    • Set up your story

    src/stories/alert.stories.js

    /* eslint-disable import/no-extraneous-dependencies */
    import { storiesOf } from '@storybook/vue'
    
    // You need to import this once
    import '@/style/tailwind.css'
    
    import Alert from '@/components/alert.vue'
    
    storiesOf('Alert', module)
      .add('with text', () => ({
        components: { Alert },
        template: ''
      }))
    
    • Run storybook npm run storybook:serve
    • Develop your component on storybook while having tailwindCSS available!

    Hope this helps with your setup. Meanwhile, I'm going to read up and see how TailwindCSS can help me create better components!

提交回复
热议问题