0.4.0 • Published 4 years ago

tailwind-svg-import v0.4.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Tailwind SVG Import

This project adds handling for file system SVGs into Tailwind content.

The goal of the project is to allow you to provide Tailwind a directory and add those directly to utilities or components based on mode. It also optionally allows for SVGs to be colorized with additional configuration.

Install

  1. Install the plugin:
# Using npm
npm install tailwind-svg-import --save-dev

# Using Yarn
yarn add tailwind-svg-import -D
  1. Add it to your tailwind.config.js file:
// tailwind.config.js
module.exports = {
  // ...
  plugins: [require('tailwind-svg-import')],
};

Documentation

User SVG Files

This plugin doesn't make any opinions about where your assets are stored in your system. For this to work, the files must be available locally. It is recommended you use a tool such as SVGO to preconfigure your SVG files. If you wish to use the colorize option, the SVG file must contain currentColor on the SVG attribute you wish to colorize.

Note that this plugin operates by replacing that value with each value in the color configuration. Sample SVGO config from @tailwindlabs/heroicons:

'plugins':
  - 'removeDimensions': true
  - 'removeXMLNS': false
  - 'sortAttrs': true
  - 'removeAttrs':
      attrs: 'fill'
  - 'addAttributesToSVGElement':
      attributes:
        - 'fill': 'currentColor'

Options

All options are optional and will merge and overwrite default options.

  • colors: Object. Define the colors you wish to use for colorize.
  • colorize: Boolean. Optionally replace currentColor in your SVG with provided colors.
  • dirs: Object. The file directories this plugin should scan.
  • mode: String (all|vars|utils). Add CSS vars or SVGs, utility classes, or both.

Mode

The mode setting is used to decide how the plugin operates:

  • vars: Create CSS Variables
  • utils: Create background-image utility classes
  • all: Create both the above
Output Naming

In 'vars' or 'all' mode, this plugin will create CSS Variables in the following format:

--icon-{name}-{directory}-{color}: url('{contents}')

In 'utils' or 'all' mode, this plugin will create utility classes in the following format:

.icon-{name}-{directory}-{color} {
  background-image: url('{contents}')
}

In each example, the {directory} names are only appended if there are more than one directory in the dirs configuration whereas the {color} name is appended only if the colorize option is set to true.

Purge

Note that utils mode is purged, whereas vars are added to :root and are not purged.

Default Options

All keys are optional and will resolve to the contents of src/defaultOptions.js if no user options are chosen. User and Default options are merged with preference for user options.

module.exports = {
  svgs: {
    colors,
    colorize: false,
    dirs: {
      outline,
      solid,
    },
    mode,
  },
};

By default this plugin provides 10 Tailwind Heroicons to keep the initial size of the package small and to demonstrate what the plugin can do. However, it's best to use your own icons for this plugin. Tailwind’s Heroicons are a great start if you don’t have specific assets generated by design. We do recommend only including the icons you need as the CSS vars that this plugin uses can’t be purged.

Local development

  1. Clone the repository:

    git clone https://github.com/jryanconklin/tailwind-svg-import tailwind-svg-import
    
    cd tailwind-svg-import
  2. Install the dependencies:

    # Using npm
    npm install
    
    # Using Yarn
    yarn
  3. Add as a plugin to your project.

0.4.0

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago