Git Product home page Git Product logo

tailwindcss-multi-theme's Introduction

Tailwind CSS Multi Theme

codecov Travis (.org) npm MIT License

Most theme plugins ask too much from the start. If you know how to create a simple page with default Tailwind, you already know how to use this theme plugin.

๐Ÿงช See it live

๐Ÿงฑ See examples

๐Ÿ’ฟ Install

npm install tailwindcss-multi-theme

In tailwind.config.js add themeVariants to the theme property, with the value(s) of your theme(s), and require the plugin. That's it.

module.exports = {
  theme: {
    themeVariants: ['dark']
  },
  variants: {
    // just add dark to any variant that you want to style
  },
  plugins: [require('tailwindcss-multi-theme')],
}

It will create a set of classes based on your variants and expect a class .theme-<the name of your themeVariants> at the top of your HTML document.

themeVariants: ['dark'] would activate its classes under .theme-dark.

๐Ÿš€ Usage

๐Ÿ‘‰ themeVariants is the only configuration option.

It expects an array of strings, so there is no limit to how many themes you can create. Want a dark and a neon theme (you don't need to specify your default)? Do this:

module.exports = {
  theme: {
    themeVariants: ['dark', 'neon']
  },
  variants: {
    // just add dark and neon to any variant that you want to style
  },
  plugins: [require('tailwindcss-multi-theme')],
}

You can now place the class .theme-dark or .theme-neon at the top of your HTML (eg. on body or an enclosing div) and just write classes like:

dark:bg-gray-900 dark:text-gray-300

But just this won't work. You need to specify what variants of your theme you want, in your variants:

...
variants: {
  backgroundColor: ['responsive', 'hover', 'focus', 'dark'],
  textColor: ['responsive', 'hover', 'focus', 'dark'],
},
...

What if you need to style the hover, focus or any other variant on some specific theme?

...
variants: {
  backgroundColor: ['responsive', 'hover', 'focus', 'dark', 'dark:hover', 'dark:focus'],
  textColor: ['responsive', 'hover', 'focus', 'dark', 'dark:hover', 'dark:focus'],
},
...

The same way you would write it in HTML (dark:hover:bg-red-100) you write in your variants settings, just by adding a : before the variant.

So, if you're already using focus-within, it would be called dark:focus-within, considering your theme is called dark.

Using inside CSS with @apply

If you're more into writing some CSS using @apply, you could try this:

input {
	@apply bg-gray-300;
}

input:focus {
	@apply bg-white;
}

/**
 * Place your theme styles inside .theme-<your-theme>
 * In this case, we have themeVariants: ['dark']
 */
.theme-dark {
	input {
		@apply bg-gray-800;
	}

	input:focus {
		@apply bg-gray-500;
	}
}

How to automatically apply the theme based on user's preferences?

a.k.a prefers-color-scheme

You should use prefers-dark.js to detect if it is supported. If so, the theme will be applied automatically. Place it in the top of the head of your HTML (execute early to reduce the flash of light theme).

By the way, you can check one of the examples

If you're looking for a CSS only approach, you could give tailwindcss-theming a try.

โ“ Why another theme plugin?

I'll tell you the truth. I'm lazy. I created this plugin for people that, like me, just want to keep writing Tailwind CSS as always, with the same familiar syntax, no theme files, no extensive obligatory docs read to know how to color my backgrounds.

It just prepends your theme variable to the good old Tailwind classes.

tailwindcss-multi-theme's People

Contributors

estevanmaito avatar

Watchers

 avatar

Forkers

mikenkansah

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.