GithubHelp home page GithubHelp logo

estevanmaito / tailwindcss-multi-theme Goto Github PK

View Code? Open in Web Editor NEW
524.0 7.0 44.0 4.46 MB

๐ŸŽจ The easiest way to create themes with Tailwind CSS.

Home Page: https://tailwindcss-multi-theme.now.sh/

License: MIT License

JavaScript 63.42% HTML 36.58%

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

UPDATE: Tailwind CSS ^1.7.0 (Use @apply with variants and other complex classes) now supports this syntax:

.btn {
  @apply border-4 border-gray-300 dark:border-dark-gray-600;
}

Another way, (and the only way for Tailwind CSS prior to v1.7.0), is the following.

If you're more into writing some CSS using @apply, you could try the code below. Note that it needs nesting support, and you can find more about it in the official docs.

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;
  }
}

If you want to avoid nesting for some reason, this syntax is also perfectly valid:

.theme-dark input {
  @apply bg-gray-800;
}

.theme-dark 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

dependabot[bot] avatar estevanmaito avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

tailwindcss-multi-theme's Issues

Support twin.macro

Hello. thanks for making an amazing tool. it seems that it is not compatible with twin.macro.
it would be great if you could add this feature.
thanks.

Does not support Tailwindcss 3.x

When installing latest version of Laravel and trying to implementing this fantastic theme I get the following error:

While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/tailwindcss
npm ERR! dev tailwindcss@"^3.1.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer tailwindcss@"^1.4.6" from [email protected]
npm ERR! node_modules/tailwindcss-multi-theme
npm ERR! tailwindcss-multi-theme@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

  • tailwindcss-multi-theme version: 1.0.2
  • Are you using other plugins? Which? Tailwind

Relevant code or config:

<!-- your code here -->

What you did:

Trying to install the theme after implementing latest version of Laravel.

What happened:

Reproduction:

Problem description:

Theme variants are removed by purgecss

  • tailwindcss-multi-theme version: 1.0.3
  • tailwindcss version 1.8.4
  • Are you using other plugins? Which? @tailwindcss/custom-forms

Relevant code or config:

{
    theme: {
        themeVariants: [ 'foo' ],
    },
    variants: {
        backgroundColor: [ 'responsive', 'hover', 'foo', 'foo:hover' ],
    }
}

What you did:

I added some theme variants as described in the docs and like shown above.

What happened:

The variants work as expected in dev builds without purgecss (really cool!).

Problem description:

The variants seem to get removed in prod builds when purgecss kicks in. They can't be found in the resulting optimized css at least.

Suggested solution:

Maybe this is caused by recent changes in tailwindcss when purgecss config was included? Is an adjusted regex required? Turning off the new purgeLayersByDefault setting doesn't help neither.

Thanks for this great plugin and for taking the time.

Postcss is not working in multiple theme

postcss.config.js

const tailwindcss = require('tailwindcss');
const purgecss = require('@fullhuman/postcss-purgecss');

if(process.env.RUN == 'production') {
  postCssPlugins = [
    tailwindcss('./tailwind.config.js'),
    require('autoprefixer'),
    purgecss({
      content: ['./pages/**/*.js']
    }),
  ]
}

Tailwind 2

Hi here,
Any chance to upgrade to Tailwind 2 by any chance?

How to use with @apply

Problem description:

Hi @estevanmaito first of all, thank you for this awesome plugin!

My question is how can I use the dark: especification with @aplly?

I`m not able to use with the following sintaxe

.toggle{
    @apply border-4 border-gray-300 dark:border-dark-gray-300;
}

I try scape using slashes, quotes etc, but not works...

Can you help me with this?

This project appears to be abandoned

I believe this package is abandoned and should be marked as such. No updates for two years, tailwind dependencies are very outdated. Projects that are using this package cannot update anymore. It would be great if the person that developed this package offered an alternative.

Support for Tailwind CSS 2.x

Hi there,

Thanks for building this.

Do you have any plans to upgrade this package to support Tailwindcss 2.x?

Attempted to npm update and got the follow error:

npm ERR! Could not resolve dependency:
npm ERR! peer tailwindcss@"^1.4.6" from [email protected]
npm ERR! node_modules/tailwindcss-multi-theme
npm ERR!   tailwindcss-multi-theme@"^1.0.4" from the root project

@estevanmaito facing issue like above but if when i install after tailwind installation, If install before tailwind installation its show just warning.

@estevanmaito facing issue like above but if when i install after tailwind installation, If install before tailwind installation its show just warning.

npm ERR! While resolving: undefined@undefined
npm ERR! Found: [email protected]
npm ERR! node_modules/tailwindcss
npm ERR!   dev tailwindcss@"^3.0.13" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer tailwindcss@"^1.4.6" from [email protected]
npm ERR! node_modules/tailwindcss-multi-theme
npm ERR!   tailwindcss-multi-theme@"*" from the root project

Originally posted by @AnasSharif in #17 (comment)

Variants not applying the correct selectors

Use https://github.com/tailwindcss/tailwindcss/blob/master/src/lib/substituteVariantsAtRules.js#L26 as reference for variants generation.

Earlier today I found that focus was not being correctly applied to placeholder pseudo elements. It was fixed in 0b5a4dd but it probably affects every other variants like hover, active etc.

It's not breaking anything, but may not produce the expected result (like any result at all).

Will take a look at it later today or at least tomorrow.

CSS functions?

First, this is a great plugin, and a great project, thanks.

Is your feature request related to a problem? Please describe.
It's great to have everything in the html: <div class="dark:bg-blue-100"></div>.

Describe the solution you'd like
But what would be even better would be to have something that can be used in CSS like the @screen directive:

input {
	@apply bg-gray-300;
}

input:focus {
	@apply bg-white;
}

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

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Focus and Hover dont work

version: "tailwindcss-multi-theme": "^1.0.3"
in my code this work:
focus:outline-none hover:text-gray-600 hover:bg-gray-300

But when i install Multi-Theme then stop work.
With Multi-theme:

2020-06-17_14-21-53

Without:

2020-06-17_14-24-15

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.