0.3.0 • Published 3 years ago

tailwindcss-fl v0.3.0

Weekly downloads
87
License
MIT
Repository
github
Last release
3 years ago

tailwindcss-fl

TailwindCSS plugin to generate fluid utility classes by leveraging existing config.

Motivation

  • Increase productivity by writing less markup
  • Increase readability with more concise class lists
  • Increase consistency by using ratios to scale down
  • Enable smooth scaling between sizes rather than sizes jumping between breakpoints

Media query classes

<div class="m-4 lg:m-6 xl:m-8 max-w-sm lg:max-w-lg xl:max-w-xl">
    <h2 class="text-base lg:text-2xl xl:text-4xl">
        <!-- -->
    </h2>
</div>

Fluid classes

<div class="fl:m-8 fl:max-w-xl">
    <h2 class="fl:text-4xl">
        <!-- -->
    </h2>
</div>

Installation

$ npm install tailwindcss-fl

Add to tailwind.config.js

module.exports = {
    plugins: [
        require('tailwindcss-fl')({
            screenMin: 'screens.sm',
            screenMax: 'screens.xl',
            defaultRatio: 1.618,
        }),
    ],
}

Utilities

Out the box, the following classes are generated.

KeyClasses
fontSizefl:text-{ keys }
marginfl:{ m, mt, mr, mb, ml, mx, my, -m, -mt, -mr, -mb, -ml, -mx, -my }-{ keys }
paddingfl:{ p, pt, pr, pb, pl, px, py }-{ keys }
spacefl:{ space-x, space-y }-{ keys }
gapfl:gap-{ keys }
widthfl:w-{ keys }
maxWidthfl:max-w-{ keys }
heightfl:h-{ keys }
top/right/bottom/left/insetfl:{top, right, bottom, left, inset, -top, -right, -bottom, -left, -inset}-{ keys }
borderWidthfl:border-{t, r, b, l}-{ keys }
borderRadiusfl:rounded-{ keys }

Custom classes defined in tailwind.config.js under theme: {} will be used to generate the fluid utility classes.

List of generated classes using the default provided by tailwind

Config

Below is the full default config.

{
    prefix: 'fl',
    separator: ':',
    defaultRatio: 1.618,
    screenMin: 'screens.sm',
    screenMax: 'screens.xl',
    rootRem: 16,
    clamp: true,
    extend: true,
    variants: [],
    theme: { },
}
OptionTypeDescription
prefixStringClass name prefix for fluid classes.
separatorStringClass name sepator for fluid classes.
defaultRatioNumberScale down using golden ratio 1.618.
screenMinArrayScreen size to scale from. screens.{key} or custom rem/px value.
screenMaxArrayScreen size to scale to. screens.{key} or custom rem/px value.
rootRemNumber1rem is equal to 16px. Default should work for most cases.
clampBooleanEnable the use of clamp() to avoid using media queries.
extendBoolean or ArrayExtend existing class values, or provide an array of keys to extend, ['margin', 'padding'].
variantsArrayTailwind variants, not recommended.
themeObjectDetailed in depth below.

Theme

theme: { } allows for more fine-tuned control of fluid classes.

Using defaultRatio only

Define a defaultRatio to be applied to all classes under a specific key.

theme: {
    maxWidth: 2,
    padding: 1.5,
},

The default ratios are applied to classes fl:max-w-{ keys } and fl:{ p, pt, pr, pb, pl, px, py }-{ keys }

List of generated classes with properties and ratios

Using defaultRatio and/or a custom config

For more control, a config object can be used to target specific utility classes, the same way tailwind does.

This is especially useful for fontSize because smaller font sizes should not scale down much.

theme: {
    fontSize: {
        defaultRatio: 1.125,
        config: {
            'base': 1.125,
            'lg': 1.125,
            'xl': 1.25,
            '2xl': 1.25,
            '3xl': 1.35,
            '4xl': 1.35,
            '5xl': 1.5,
            '6xl': 1.5,
        },
    },
},

Class names can be grouped for the same ratio.

theme: {
    fontSize: {
        defaultRatio: 1.125,
        config: {
            'base lg': 1.125,
            'xl 2xl': 1.25,
            '3xl 4xl': 1.35,
            '5xl 6xl': 1.618,
        },
    },
},

The custom ratios are applied to classes fl:text-{ keys }

List of generated classes with properties and ratios

Passing an array to custom config

The power of the plugin is extending existing utilities using ratios.

However, an array of [min, max, screenMin, screenMax] can also be passed, either to overwrite an existing class, or to create a new custom class.

Parameters are based on postcss-range-value.

ParamTypeDescription
min(required)String or Numberrem/px value or a scale down ratio
max(required)String or Numberrem/px value or a scale up ratio
screenMinStringscreens.{key} or rem/px value
screenMaxStringscreens.{key} or rem/px value

Some examples below

theme: {
    maxWidth: {
        defaultRatio: 2,
        config: {
            /* scale down by 2x resulting in 12rem to 24rem between default screen sizes */
            '12/24': [2, '24rem'],

            /* scale up by 2x resulting in 24rem to 48rem between default screen sizes */
            '24/48': ['24rem', 2],

            /* scale from 32rem to 64rem between screens.md and screens.lg */
            '32/64': ['32rem', '64rem', 'screens.md', 'screens.lg'],
        },
    },
},

The custom values are added to classes fl:max-w-{ keys }

List of generated classes with properties and ratios

Fin

Feature requests, questions, hatemail? @ the person who has already spent too much time building this.