Code Monkey home page Code Monkey logo

tailwindcss-classnames's Introduction

tailwindcss-classnames

Functional typed classnames for TailwindCSS

TailwindCSS is a CSS library that has gained a lot of traction. The developer experience is pretty epic and you ensure a low footprint on your css by composing existing classes for most of your css.

So why mess with it?

TailwindCSS is based on strings and with some nice tooling on top like TailwindCSS VSCode extension you get a pretty descent experience. That said, there are limitations to a purely declarative approach of strings. When using tailwindcss-classnames you will get additional power in the form of:

  • Validation of classnames: You can not write the wrong classname, cause the API only allows you to insert valid classnames
  • Functional approach: Since we are working in Typescript we get more freedom in using functional powers like composition and dynamic composition
  • Defining by variables: Even though it is nice to write TailwindCSS inline with your elements, it does not scale. You want to move definitions outside of the component for reusability and composition

You can not get this experience using pure TailwindCSS and the VSCode extension, but you do get it with tailwindcss-classnames.

Edit tailwindcss-classnames

Install

Please follow the guide to set up TailwindCSS. Now do:

npm install tailwindcss-classnames

The project is literally the classnames project with custom typing and functions for applying pseudo elements. That means it arrives at your browser at approximately 422b minified and gzipped (bundlephobia).

Create classes

import { classnames } from 'tailwindcss-classnames';

classnames('border-none', 'rounded-sm');

The arguments passed to classnames is typed, which means you get discoverability. You can even search for the supported classes:

DISCOVER

Dynamic classes

Since we are using classnames you can also add your classes dynamically:

import { classnames } from 'tailwindcss-classnames';

classnames('border-none', 'rounded-sm', {
  ['bg-gray-200']: true,
});

Pseudo selectors

Since we are in a functional world we use functions to define our pseudo selectors:

import { classnames, hover } from 'tailwindcss-classnames';

classnames('bg-gray-500', hover('bg-blue-500'));

Composing classes

Even though classnames just returns a string, it is a special typed string that you can compose into other definitions.

import { classnames } from 'tailwindcss-classnames';

export const button = classnames('border-none', 'rounded-sm');

export const redButton = classnames(button, 'bg-red-100');

Using with React

Since React has excellent typing support I want to give an example of how you could use it.

// styles.ts
import { classnames } from 'tailwindcss-classnames';

export const form = classnames('container', 'w-full');

export const button = classnames('border-none', 'rounded-sm');

export const alertButton = classnames(button, 'bg-red-100');

export const disabled = classnames('opacity-25', 'bg-gray-100');

export const submitButton = (disabled: boolean) =>
  classnames(styles.button, {
    [styles.disabled]: disabled,
  });

// App.tsx
import * as React from 'react';
import * as styles from './styles';

export const App: React.FC<{ disabled }> = ({ disabled }) => {
  return (
    <form className={styles.form}>
      <button type="submit" className={styled.submitButton(disabled)}>
        Submit
      </button>
      <button className={styles.alertButton}>Cancel</button>
    </form>
  );
};

Custom typing

By default you have all the classes available as types, though you might not use all of them. You can customize your own by:

import { createCustom, TBackgroundColor, TBackgroundSize } from 'tailwindcss-classnames';

type Classes = TBackgroundColor | TBackgroundSize;

const {
  classnames,
  hover,
  active,
  disabled,
  visited,
  firstChild,
  lastChild,
  oddChild,
  evenChild,
  groupHover,
  focusWithin,
} = createCustom<Classes>();

export {
  classnames,
  hover,
  active,
  disabled,
  visited,
  firstChild,
  lastChild,
  oddChild,
  evenChild,
  groupHover,
  focusWithin,
};

You can also base it on the groups of functionality:

import { TBackgrounds, TBorders } from 'tailwindcss-classnames';

type Classes = TBackgrounds | TBorders;

tailwindcss-classnames's People

Contributors

christianalfoni avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.