• This repository has been archived on 26/Sep/2020
  • Stars
    star
    209
  • Rank 187,266 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Examples to help you get started building your own Tailwind CSS plugins.

Tailwind CSS Example Plugins

To get started, clone the project and install the dependencies:

# Using npm
npm install

# Using Yarn
yarn

After that, you can process the CSS by running:

# Using npm
npm run build

# Using Yarn
yarn run build

Load up ./index.html in your browser and you're off to the races!

Object-Fit Utilities

View demo · View source

In plugins/object-fit/index.js you'll find an example of a plugin that adds a set of simple, non-configurable utility classes for the object-fit property.

The only option it exposes are the variants you'd like to generate (responsive, hover, focus, etc.), which you pass to the plugin as a simple array:

module.exports = {
  // ...

  plugins: [
    // ...
    require('./plugins/object-fit')(['responsive']),
  ],
}

This is just about the simplest type of plugin you could make.

Simple Buttons

View demo · View source

In plugins/simple-buttons/index.js you'll find an example of a plugin that adds new button component classes.

This plugin exposes quite a number of configuration options which can be passed to the plugin as an object:

module.exports = {
  // ...

  plugins: [
    // ...
    require('./plugins/simple-buttons')({


      // Set some default styles for all buttons.
      borderRadius: '.25rem',   // Default: .25rem
      fontWeight: '600',        // Default: 600
      lineHeight: '1.25',       // Default: 1.25
      fontSize: '1rem',         // Default: 1rem
      padding: '.5rem 1rem',    // Default: .5rem 1rem


      // Specify the button colors you'd like to generate.
      // 
      // By default, buttons are generated for all of Tailwind's
      // default base colors.
      colors: {
        // Class name: `.btn-primary`
        primary: {
          background: colors['blue'],
          text: colors['white'],
        },
        // Class name: `.btn-secondary`
        secondary: {
          background: colors['grey'],
          text: colors['black'],
        },
      },


      // Specify additional button sizes you'd like to generate.
      // 
      // You can override any of the default styles from above
      // at any given button size.
      sizes: {
        // Class name: `.btn-sm`
        sm: {
          fontSize: '.875rem',
          padding: '.5rem .75rem',
        },
        // Class name: `.btn-lg`
        lg: {
          fontSize: '1.25rem',
          padding: '.75rem 1.5rem',
          borderRadius: '.5rem',
        }
      }

    }),
  ],
}

Configuration is entirely optional; the plugin will use sane defaults based on Tailwind's default configuration if you don't provide any of your own overrides.

If you want to extend the plugin's default configuration instead of overriding it entirely, you can pass a function which accepts the default configuration, modifies it, and returns a new configuration object:

module.exports = {
  // ...

  plugins: [
    // ...
    require('./plugins/simple-buttons')(function (options) {
      options.sizes = Object.assign(options.sizes, {
        xl: {
          fontSize: '1.5rem',
          padding: '1rem 2rem',
          borderRadius: '.75rem',
        }
      })

      return options  
    }),
  ],
}

Again, the sky is the limit in terms of the API a plugin exposes for configuration. You can do anything you want!

CSS Grid Utilities

View demo · View source

In plugins/css-grid/index.js you'll find an example of a plugin that adds new utilities for using CSS Grid Layout.

It exposes three configuration options:

  • grids, for specifying all of the grid sizes you'd like to generate
  • gaps, for specifying the gap sizes you'd like to generate
  • variants, for specifying which variants to generate
module.exports = {
  // ...

  plugins: [
    // ...
    require('./plugins/css-grid')({
      grids: [2, 3, 5, 6, 8, 10, 12],
      gaps: {
        0: '0',
        4: '1rem',
        8: '2rem',
      },
      variants: ['responsive'],
    }),
  ],
}

With zero configuration, it will generate grids from 1 to 12 columns in size, no gap sizes, and responsive variants for each new utility.

The plugin generates the following sets of classes:

  • .grid, for setting display: grid on an element
  • .grid-columns-{size}, for specifying the number of columns in the grid
  • .grid-gap-{size}, for specifying the size of the gap between columns/rows
  • .col-span-{columns}, for specifying how wide a column should be
  • .col-start-{line} and .col-end-{line}, for specifying a column's start and end points explicitly (useful for reordering columns or leaving gaps)

It's not really practical to expose all of the power of CSS Grid through utilities, but this plugin is a good example of using CSS Grid to replace a column-only float or Flexbox grid.

More Repositories

1

tailwindcss

A utility-first CSS framework for rapid UI development.
TypeScript
81,562
star
2

headlessui

Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
TypeScript
25,564
star
3

heroicons

A set of free MIT-licensed high-quality SVG icons for UI development.
JavaScript
21,279
star
4

prettier-plugin-tailwindcss

A Prettier plugin for Tailwind CSS that automatically sorts classes based on our recommended class order.
TypeScript
5,382
star
5

tailwindcss-typography

Beautiful typographic defaults for HTML you don't control.
JavaScript
4,319
star
6

tailwindcss-forms

A plugin that provides a basic reset for form styles that makes form elements easy to override with utilities.
HTML
4,137
star
7

tailwindcss.com

The Tailwind CSS documentation website.
MDX
3,233
star
8

tailwindcss-intellisense

Intelligent Tailwind CSS tooling for Visual Studio Code
TypeScript
2,783
star
9

tailwindcss-jit

JavaScript
2,188
star
10

tailwindcss-custom-forms

A better base for styling form elements with Tailwind CSS.
JavaScript
1,553
star
11

tailwindcss-line-clamp

A plugin that provides utilities for visually truncating text after a fixed number of lines.
JavaScript
1,176
star
12

tailwindcss-container-queries

A plugin for Tailwind CSS v3.2+ that provides utilities for container queries.
TypeScript
1,131
star
13

tailwindcss-aspect-ratio

JavaScript
956
star
14

designing-with-tailwindcss

Source code for the "Designing with Tailwind CSS" course.
CSS
712
star
15

heroicons.com

JavaScript
697
star
16

tailwindcss-setup-examples

675
star
17

tailwindui-vue

Deprecated, please use the Headless UI repo instead.
JavaScript
647
star
18

play.tailwindcss.com

JavaScript
638
star
19

tailwindcss-playground

HTML
516
star
20

tailwindui-react

Deprecated, please use the Headless UI repo instead.
TypeScript
433
star
21

tailwindcss-from-zero-to-production

Source code for the "Tailwind CSS Up and Running" screencast series.
JavaScript
379
star
22

blog.tailwindcss.com

JavaScript
342
star
23

tailwindui-issues

Bug fixes and feature request tracking for Tailwind UI.
233
star
24

webpack-starter

A quick and simple example of using Tailwind CSS with Webpack.
JavaScript
212
star
25

discuss

A place to ask questions, get help, or share what you've built with Tailwind CSS.
171
star
26

tailwind-play-api

PHP
44
star
27

minimal-tailwind-postcss-plugin

A simple PostCSS plugin designed to cover all of the tricky integration points a build-tool needs to support for Tailwind CSS.
JavaScript
11
star
28

reproduction-headlessui-vue

JavaScript
9
star
29

reproduction-headlessui-react

JavaScript
1
star