Building A Tailwind CSS Component Library for Vue.js

Learn how to build a Tailwind CSS component library for Vue.js in this step-by-step guide. We'll cover everything you need to know, from creating reusable components to styling them with Tailwind CSS. By the end of this guide, you'll have a custom component library that you can use to build beautiful and responsive Vue.js apps.

Install

npm

npm install @advanced-data-machines/tailwindcss-vue

yarn

yarn add @advanced-data-machines/tailwindcss-vue

Usage

import Vue from 'vue';
// styles for transitions and other base options
import '@advanced-data-machines/tailwindcss-vue/dist/tailwindcss-vue.css';
import TailwindcssVue from '@advanced-data-machines/tailwindcss-vue';

Vue.use(TailwindcssVue);

Base Theme Expectations

Use of this project assumes you are using PostCSS and Tailwind CSS in your Vue project.

postcss.config.js

// replace './tailwind.config.js' to your own config path
module.exports = {
	plugins: [
		require('tailwindcss')('./tailwind.config.js'),
		require('autoprefixer')
	]
};

tailwind.config.js

This file is likely to change to base color scheme without the need for custom color names

const { colors } = require('tailwindcss/defaultTheme');
// variant defaults
// https://tailwindcss.com/docs/configuring-variants/#default-variants-reference
module.exports = {
	theme: {
		extend: {
			spacing: {
				'9': '2.25rem',
				'11': '2.75rem',
				'14': '3.5rem',
				'18': '4.5rem'
			},
			fontSize: {
				'xxs': '0.625rem'
			},
			stroke: {
				...colors
			}
		},
		container: {
			center: true
		}
	},
	variants: {
		borderColor: ['responsive', 'hover', 'focus', 'first', 'last'],
		borderRadius: ['responsive', 'first', 'last'],
		borderWidth: ['responsive', 'first', 'last'],
		margin: ['responsive', 'before', 'first', 'last'],
		textColor: ['responsive', 'hover', 'focus', 'before']
	},
	plugins: [
		function({ addVariant, e }) {
			addVariant('before', ({ modifySelectors, separator }) => {
				modifySelectors(({ className }) => {
					return `.${e(`before${separator}${className}`)}:before`;
				});
			});
		}
	]
};

Developing/Running Locally

Clone the repo to your computer then change to the project directory. Once in the root of the project, use the install command with your dependency manager of choice (yarn or npm).

cd tailwindcss-vue
npm install

or

cd tailwindcss-vue
yarn install

The project is set up with a Vue SPA playground to test components. In the root of the project directory you can run the development server with the serve command.

npm run serve

or

yarn serve

You can also build the project and serve it to another local project using npm link or the equivalent yarn link. While still in the root of the project directory, run the build:bundle command.

npm run build:bundle

or

yarn build:bundle

This will add the build to the ‘dist’ folder as a consumable library. Once completed, run the npm link command.

npm link

Next, in the project you wish to link, run the npm link tailwindcss-vue in the same directory as the package.json file. This will add the project to the global scope to be referenced as a traditional installed npm package (import TailwindcssVue from ‘@advanced-data-machines/tailwindcss-vue’).

npm link @advanced-data-machines/tailwindcss-vue

Local Docs

This project is bundled with VuePress to generate its documentation. You can run it locally by running the docs:dev run command. The docs are currently a work-in-progress.

npm run docs:dev

or

yarn docs:dev

#vuejs #javascript #tailwindcss #tailwind 

Building A Tailwind CSS Component Library for Vue.js
4 Likes143.25 GEEK