All Projects → mariusmarais → tailwind-cascade

mariusmarais / tailwind-cascade

Licence: BSD-3-Clause License
Override TailwindCSS classes for component composition

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to tailwind-cascade

postcss-typed-css-classes
PostCSS plugin that generates typed entities from CSS classes for chosen programming language.
Stars: ✭ 12 (-57.14%)
Mutual labels:  classes, tailwindcss
Tailwindcss Classnames
TypeScript support for TailwindCSS
Stars: ✭ 305 (+989.29%)
Mutual labels:  composition, tailwindcss
permissionary
Tiny and framework-agnostic role-based permission management using composition over inheritance
Stars: ✭ 19 (-32.14%)
Mutual labels:  composition
jigsaw-blog-template
Starter template for a blog, using Jigsaw by Tighten
Stars: ✭ 75 (+167.86%)
Mutual labels:  tailwindcss
react-tailwind
This is a complementary React code for the tailwindcss project.
Stars: ✭ 29 (+3.57%)
Mutual labels:  tailwindcss
MERN Stack Project Ecommerce Hayroo
E-commerce Website | Reactjs | Nodejs | Mongodb | Expressjs | JWT | Tailwind Css
Stars: ✭ 255 (+810.71%)
Mutual labels:  tailwindcss
hn
💻 A personal Hacker News reader using Next.js
Stars: ✭ 43 (+53.57%)
Mutual labels:  tailwindcss
theodorusclarence.com
💠 Personal website and blog made using Next.js, TypeScript, Tailwind CSS, MDX Bundler, FaunaDB, and Preact.
Stars: ✭ 205 (+632.14%)
Mutual labels:  tailwindcss
nuxt-tailwind-purgecss
A Nuxt.js example with Tailwind CSS v1.2 and Purgecss
Stars: ✭ 37 (+32.14%)
Mutual labels:  tailwindcss
Batteries-Included-Next.js
A starting boilerplate for a TS Next.js project with batteries included. Tailwind CSS for styling, Jest and React Testing Library working with path aliases and node-mock-http for API route testing.
Stars: ✭ 35 (+25%)
Mutual labels:  tailwindcss
faeshare
MERN based social media web app made with the help of Next.js, Socket.io and TailwindCSS.
Stars: ✭ 37 (+32.14%)
Mutual labels:  tailwindcss
EddieHubCommunity.github.io
Information about our community
Stars: ✭ 88 (+214.29%)
Mutual labels:  tailwindcss
notes
Notas sobre JavaScript Full Stack
Stars: ✭ 70 (+150%)
Mutual labels:  tailwindcss
laravel-simple-select
Laravel Simple Select inputs component for Blade and Livewire.
Stars: ✭ 59 (+110.71%)
Mutual labels:  tailwindcss
react-tailwind-spotify-clone
Spotify clone with full functionality using React + Tailwind (Not finished)
Stars: ✭ 209 (+646.43%)
Mutual labels:  tailwindcss
tailwindcss-variables
Easily create css variables without the need for a css file!
Stars: ✭ 47 (+67.86%)
Mutual labels:  tailwindcss
react-tailwind-food-app
A responsive landing page for food delivery app made with React & Tailwind CSS.
Stars: ✭ 32 (+14.29%)
Mutual labels:  tailwindcss
react-heroicons
Heroicons as React components
Stars: ✭ 39 (+39.29%)
Mutual labels:  tailwindcss
addtobasic.github.io
CUI Portfolio like ubuntu terminal.
Stars: ✭ 18 (-35.71%)
Mutual labels:  tailwindcss
tailwindcss-custom-native
Tailwind CSS plugin that lets you write your configuration file with your own custom utilities as though they were native to the framework
Stars: ✭ 65 (+132.14%)
Mutual labels:  tailwindcss

DEPRECATED

For Tailwind 2.0+ check out https://github.com/dcastil/tailwind-merge

This project will still work for Tailwind 1, but no further changes will be made.

tailwind-cascade

NPM

An attempt at making composable components possible using TailwindCSS.

Install

npm install @mariusmarais/tailwind-cascade --save

Why?

TailwindCSS is amazing, but using it to create component libraries is difficult due to the CSS cascade. (See some discussions.)

Normally you would only use the specific Tailwind classes you need (that's the point), but when you're building a reusable component, you run into trouble with overriding some styles.

You could:

  • use completely unstyled base components like Tailwind React UI and style them one-by-one,
  • use CSS-in-JS to handle overriding has part of the build (I hear good things about Twin),
  • or throw caution to the wind and use this lib.

Usage

The twCascade function wraps classnames and supports all its standard usage patterns.

Create a base component with some Tailwind classes, wrapping the className with twCascade while letting the caller's className pass through:

// Example in React, but twCascade should work everywhere

import React, { FC, DetailedHTMLProps, ButtonHTMLAttributes } from 'react';

import { twCascade } from '@mariusmarais/tailwind-cascade';

interface MyButtonProps extends DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement> {
  setActive?: boolean;
  canHover?: boolean;
}

const MyButton: FC<MyButtonProps> = props => {
  const { disabled, className, children, ...other } = props;

  const activeClasses = ['border-gray-500', 'bg-gray-500', 'text-white'];
  const hoverClasses = ['border-gray-700', 'bg-gray-700 ', 'text-white'];

  return (
    <button
      className={twCascade(
        'text-sm',
        'w-32',
        'border',
        'border-gray-300',
        'rounded-md',
        disabled && 'cursor-default',
        !disabled && !setActive && activeClasses.map(n => `active:${n}`),
        !disabled && canHover && hoverClasses.map(n => `hover:${n}`),
        setActive && activeClasses,
        !setActive && 'text-gray-900',
        className
      )}
      disabled={disabled}
      {...other}
    >
      {children}
    </button>
  );
};

In your app, create three buttons, two in a classic button group, the other on its own:

render() {
  return (
    <>
      <MyButton
        setActive={direction === 'left'}
        disabled={direction === 'left'}
        className="w-24 border-r-0 rounded-r-none"}
      >
        Left
      </MyButton>

      <MyButton
        setActive={direction === 'right'}
        disabled={direction === 'right'}
        className="w-24 border-l-0 rounded-l-none"}
      >
        Inbound
      </MyButton>

      <MyButton className="ml-10">Go</MyButton>
    </>
  );
}

Usually the duplicate width and border options can cause trouble, but not anymore.

Advanced

Use const twCascade = createTailwindCascader({prefix: 'some-tw-prefix-'}) if your Tailwind classes have a prefix.

How does it work?

Pretty well, considering none of it was supposed to work this way...

All Tailwind classes are broken down into groups, like .object-bottom, .object-center, .object-left, and so on. Only the last class defined per group is kept.

For 90% of Tailwind this works very well, for the other 9.9% additional overrides are configured to allow cross-group overriding, such as .rounded-r overriding .rounded-tr & .rounded-br, and .rounded overriding all the other .rounded-* classes. (The other way around just works, since Tailwind generates more-specific CSS classes after more general ones, meaning the normal cascade works in your favor.)

It shouldn't be necessary to re-specify additional classes to override previous overrides, but hey, at least it's possible.

YMMV

Contributing

PLEASE HELP. Open an issue, and we figure it out.

Ideally, much smarter people than I can do away with the manual group definitions, simplify the build steps, and get rid of the unprofessional tone in the README.

Maybe there's a way for the TailwindCSS authors to make this lib simpler, or make it absolete. One can dream.

But this is a terrible idea!

Perhaps.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].