An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
September 1, 2019 11:19 am GMT
Original Link: https://dev.to/jerriclynsjohn/svelte-tailwind-storybook-starter-template-2nih
Svelte Tailwind Storybook Starter Template
First of all, here's the link to the Github repo.
Integrating these three together took sometime as there was no official documentation for how to do this. You can make use of the repo and get started with your project immediately.
Steps to build
- Clone this repo
git clone https://github.com/jerriclynsjohn/svelte-storybook-tailwind.git
- Go to the directory
cd svelte-storybook-tailwind
- Install dependencies
yarn
- To develop your Svelte App:
yarn dev
- To develop UI components independent of your app:
yarn stories
Lengthy post ahead
If you want to understand the full length of the integration process then please read on:
Steps to build it all by yourself and some tips
Instantiate Svelte App
- Start the template file using
npx degit sveltejs/template svelte-storybook-tailwind
- Go to the directory
cd svelte-storybook-tailwind
- Install dependencies
yarn
- Try run the svelte app
yarn dev
Add Tailwind into the project
- Install dependencies:
yarn add -D tailwindcss @fullhuman/postcss-purgecss autoprefixer postcss postcss-import svelte-preprocess
- Change the rollup config as shown:
import svelte from 'rollup-plugin-svelte';import resolve from 'rollup-plugin-node-resolve';import commonjs from 'rollup-plugin-commonjs';import livereload from 'rollup-plugin-livereload';import { terser } from 'rollup-plugin-terser';import postcss from 'rollup-plugin-postcss';import autoPreprocess from 'svelte-preprocess';const production = !process.env.ROLLUP_WATCH;export default { input: 'src/main.js', output: { sourcemap: true, format: 'iife', name: 'app', file: 'public/bundle.js', }, plugins: [ svelte({ preprocess: autoPreprocess({ postcss: true, }), // enable run-time checks when not in production dev: !production, // we'll extract any component CSS out into // a separate file better for performance css: css => { css.write('public/bundle.css'); }, }), postcss({ extract: 'public/utils.css', }), // If you have external dependencies installed from // npm, you'll most likely need these plugins. In // some cases you'll need additional configuration // consult the documentation for details: // https://github.com/rollup/rollup-plugin-commonjs resolve({ browser: true, dedupe: importee => importee === 'svelte' || importee.startsWith('svelte/'), }), commonjs(), // Watch the `public` directory and refresh the // browser on changes when not in production !production && livereload('public'), // If we're building for production (npm run build // instead of npm run dev), minify production && terser(), ], watch: { clearScreen: false, },};
Add tailwind config using the command
npx tailwind init
Add PostCSS config
./postcss.config.js
as follows:
const production = !process.env.ROLLUP_WATCH;const purgecss = require('@fullhuman/postcss-purgecss');module.exports = { plugins: [ require('postcss-import')(), require('tailwindcss'), require('autoprefixer'), production && purgecss({ content: ['./**/*.html', './**/*.svelte'], defaultExtractor: content => { const regExp = new RegExp(/[A-Za-z0-9-_:/]+/g); const matchedTokens = []; let match = regExp.exec(content); // To make sure that you do not lose any tailwind classes used in class directive. // https://github.com/tailwindcss/discuss/issues/254#issuecomment-517918397 while (match) { if (match[0].startsWith('class:')) { matchedTokens.push(match[0].substring(6)); } else { matchedTokens.push(match[0]); } match = regExp.exec(content); } return matchedTokens; }, }), ],};
- Build the project with some TailwindCSS utilities
yarn dev
Add Storybook into the Svelte Project
- Add Storybook dependencies
yarn add -D @storybook/svelte
- Add tailwind configs in the
webpack.config.js
under.storybook
:
const path = require('path');module.exports = ({ config, mode }) => { config.module.rules.push({ test: /\.css$/, loaders: [ { loader: 'postcss-loader', options: { sourceMap: true, config: { path: './.storybook/', }, }, }, ], include: path.resolve(__dirname, '../storybook/'), }); return config;};
- Create the
postcss.config.js
under.storybook
:
var tailwindcss = require('tailwindcss');module.exports = { plugins: [ require('postcss-import')(), tailwindcss('./tailwind.config.js'), require('autoprefixer'), ],};
- Add a utils.css file under
storybook/css/
and make sure youimport 'utils.css'
in yourstories.js
files:
/* Import Tailwind as Global Utils */@import 'tailwindcss/base';@import 'tailwindcss/components';@import 'tailwindcss/utilities';
- Add 5 commonly used Storybook Addons:
- Source:
yarn add -D @storybook/addon-storysource
- Actions:
yarn add -D @storybook/addon-actions
- Notes:
yarn add -D @storybook/addon-notes
- Viewport:
yarn add -D @storybook/addon-viewport
- Accessibility:
yarn add @storybook/addon-a11y --dev
- Source:
- Make sure you have babel and svelte-loader dependencies
yarn add -D babel-loader @babel/core svelte-loader
- Add npm script in your
package.json
{ "scripts": { // Rest of the scripts "stories": "start-storybook", "build-stories": "build-storybook" }}
- Create a config file at the root
.storybook/config.js
with the following content:
import { configure, addParameters, addDecorator } from '@storybook/svelte';import { withA11y } from '@storybook/addon-a11y';// automatically import all files ending in *.stories.jsconst req = require.context('../storybook/stories', true, /\.stories\.js$/);function loadStories() { req.keys().forEach(filename => req(filename));}configure(loadStories, module);addDecorator(withA11y);addParameters({ viewport: { viewports: newViewports } });
- Create an addon file at the root
.storybook/addons.js
with the following content and keepadding additional addons in this file.
import '@storybook/addon-storysource/register';import '@storybook/addon-actions/register';import '@storybook/addon-notes/register';import '@storybook/addon-viewport/register';import '@storybook/addon-a11y/register';
- Add changes to the
webpack.config.js
to accomodate for Source addon:
const path = require('path');module.exports = ({ config, mode }) => { config.module.rules.push( { test: /\.css$/, loaders: [ { loader: 'postcss-loader', options: { sourceMap: true, config: { path: './.storybook/', }, }, }, ], include: path.resolve(__dirname, '../storybook/'), }, //This is the new block for the addon { test: /\.stories\.js?$/, loaders: [require.resolve('@storybook/addon-storysource/loader')], include: [path.resolve(__dirname, '../storybook')], enforce: 'pre', }, ); return config;};
- Write your Svelte component in
storybook\components
and yes you can use your regular.svelte
file. The only thing is that you cant use templates in a story yet, not supported, but yes youcan compose other components together. For the starter pack lets just create a clickable button.
<script> export let text = '';</script><button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"> {text}</button>
- Write your stories in
storybook/stories
and you can name any number of story file with<anything>.stories.js
, for the starter package we can create stories ofButton
with thereadme notes at<anything>.stories.md
. Note: reference the css here to make sure that tailwindis called by postcss:
import '../../css/utils.css';import { storiesOf } from '@storybook/svelte';import ButtonSimple from '../../components/buttons/button-simple.svelte';import markdownNotes from './buttons.stories.md';storiesOf('Buttons | Buttons', module) //Traditional Alert .add( 'Simple', () => ({ Component: ButtonSimple, props: { text: 'Button' }, }), { notes: { markdown: markdownNotes } }, );
- Run your storyboard
yarn stories
And that's a wrap!
Original Link: https://dev.to/jerriclynsjohn/svelte-tailwind-storybook-starter-template-2nih
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To