Gathering detailed insights and metrics for tailwindcss-themer
Gathering detailed insights and metrics for tailwindcss-themer
Gathering detailed insights and metrics for tailwindcss-themer
Gathering detailed insights and metrics for tailwindcss-themer
An unopinionated, scalable, tailwindcss theming solution
npm install tailwindcss-themer
72.7
Supply Chain
96.8
Quality
81.9
Maintenance
100
Vulnerability
98.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
302 Stars
259 Commits
11 Forks
5 Watching
7 Branches
3 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
TypeScript (95.72%)
JavaScript (3.54%)
CSS (0.44%)
HTML (0.3%)
Cumulative downloads
Total Downloads
Last day
-19.7%
5,829
Compared to previous day
Last week
-2.4%
32,580
Compared to previous week
Last month
8.2%
149,126
Compared to previous month
Last year
146.6%
1,782,161
Compared to previous year
4
1
29
An unopinionated, scalable, tailwindcss theming solution
🎨 Theme anything: Anything that extends tailwind's config and can be expressed in css variables is something you can theme with this plugin, even other plugins
🍨 Unlimited themes: You can have as many themes as you want! This plugin doesn't care!
💫 Automatic variants: Automatically generate variants for all of your themes (i.e. use classes like my-theme:font-black
) to enable classes only when certain themes active.
🌑 Trivial dark theme: Because dark theme is just another theme implementing dark theme is as easy, no special config
🤖 Automatically handles colors and opacity: Using tailwind with css variables can get tricky with colors, but this plugin handles all of that for you!
😅 Easy theme management: A simple, declarative api that lets you easily create and modify themes
👋 Familiar api: The way you declare themes is the exact same way you extend tailwind with the exact same features
💻 Modern: Powered by css variables under the hood
🚀 Designed to reduce bundle size: Instead of duplicating all of your style definitions, the use of css variables lets you declare styles once
Install tailwindcss-themer
using npm
:
1npm install --save-dev tailwindcss-themer
or yarn
:
1yarn add --dev tailwindcss-themer
In your tailwind.config.js
file, add tailwindcss-themer
to the plugins
array
1// tailwind.config.js 2module.exports = { 3 theme: { 4 // ... 5 }, 6 plugins: [ 7 require('tailwindcss-themer') 8 // ... 9 ] 10}
Pass the plugin a config object representing your theme configuration (see Config for details)
1// tailwind.config.js 2module.exports = { 3 theme: { 4 // ... 5 }, 6 plugins: [ 7 require('tailwindcss-themer')({ 8 defaultTheme: { 9 // put the default values of any config you want themed 10 // just as if you were to extend tailwind's theme like normal https://tailwindcss.com/docs/theme#extending-the-default-theme 11 extend: { 12 // colors is used here for demonstration purposes 13 colors: { 14 primary: 'red' 15 } 16 } 17 }, 18 themes: [ 19 { 20 // name your theme anything that could be a valid css class name 21 // remember what you named your theme because you will use it as a class to enable the theme 22 name: 'my-theme', 23 // put any overrides your theme has here 24 // just as if you were to extend tailwind's theme like normal https://tailwindcss.com/docs/theme#extending-the-default-theme 25 extend: { 26 colors: { 27 primary: 'blue' 28 } 29 } 30 } 31 ] 32 }) 33 // ... 34 ] 35}
1<!-- this example uses pure html for demonstration purposes --> 2<!doctype html> 3<html lang="en"> 4 <head> 5 <!-- ... --> 6 </head> 7 <body> 8 <!-- this has "color: 'red'" assigned to it because that's what was specified as the default--> 9 <h1 class="text-primary">Hello world!</h1> 10 </body> 11</html>
You do this by adding a class of the theme's name to whatever you want themed. See Enabling your theme for more details.
1<!-- this example uses pure html for demonstration purposes --> 2<!doctype html> 3<html lang="en"> 4 <head> 5 <!-- ... --> 6 </head> 7 <!-- everything within this tag now has the "my-theme" config applied to it --> 8 <body class="my-theme"> 9 <!-- this has "color: 'blue'" assigned to it because that's what was specified in the "my-theme" config --> 10 <h1 class="text-primary">Hello world!</h1> 11 </body> 12</html>
This plugin doesn't care how you apply the class. That's up to you. All this plugin cares about is that the class is applied.
There are lots of other ways to enable the theme. To see all options see Enabling Your Theme.
If for some reason you need to apply classes only when certain themes are active and you can't express what you want via the normal theming process shown, you can use the automatically generated variants for each of your themes!
1<!-- this class will only activate when the "my-theme" class is active --> 2<h1 class="my-theme:font-bold">Hello world!</h1>
See Variants for more details.
See migrating.md for instructions on how to migrate between major versions.
By default, the config in the defaultTheme
section of the config will apply (i.e. if no class is applied).
There are three ways to enable your theme.
You can provide a selectors
array on your theme. The theme will be enabled within any element that matches any of those selectors.
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 colors: { 5 primary: 'red' 6 } 7 } 8 }, 9 themes: [ 10 { 11 name: 'darkTheme', 12 selectors: ['.dark-mode', '[data-theme="dark"]'] 13 extend: { 14 colors: { 15 primary: 'blue' 16 } 17 } 18 } 19 ] 20})
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body> 7 <!-- The default theme config would apply here --> 8 <h1 class="text-primary">Hello world!</h1> 9 </body> 10</html>
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body class="dark-mode"> 7 <!-- The "darkTheme" config would apply here --> 8 <h1 class="text-primary">Hello world!</h1> 9 </body> 10</html>
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body data-theme="dark"> 7 <!-- The "darkTheme" config would apply here --> 8 <h1 class="text-primary">Hello world!</h1> 9 </body> 10</html>
You can specify a media query within the mediaQuery
field on the theme. The theme will be enabled when the given media query evaluates to true.
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 colors: { 5 primary: 'red' 6 } 7 } 8 }, 9 themes: [ 10 { 11 name: 'darkTheme', 12 mediaQuery: '@media (prefers-color-scheme: dark)', 13 extend: { 14 colors: { 15 primary: 'blue' 16 } 17 } 18 } 19 ] 20})
If both selectors
and mediaQuery
is specified at the same time, the selectors
will take precedence.
If neither a selectors
array nor a mediaQuery
given, a default selectors
array will be added with one value as a class selector of the theme's name.
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 colors: { 5 primary: 'red' 6 } 7 } 8 }, 9 themes: [ 10 { 11 name: 'darkTheme', 12 // selectors: ['.darkTheme'] // this is implicit given no selectors or mediaQuery given 13 extend: { 14 colors: { 15 primary: 'blue' 16 } 17 } 18 } 19 ] 20})
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body> 7 <!-- The default theme config would apply here --> 8 <h1 class="text-primary">Hello world!</h1> 9 </body> 10</html>
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body class="darkTheme"> 7 <!-- The "darkTheme" config would apply here --> 8 <h1 class="text-primary">Hello world!</h1> 9 </body> 10</html>
If you don't want this fallback to be generated, set selectors
to an empty array []
.
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 colors: { 5 primary: 'red' 6 } 7 } 8 }, 9 themes: [ 10 { 11 name: 'darkTheme', 12 selectors: [] // turns off fallback selector 13 extend: { 14 colors: { 15 primary: 'blue' 16 } 17 } 18 } 19 ] 20})
In order to prevent a flash of unstyled content, you need to make sure that the class is applied before the first paint. Josh Comeau writes a great article about this.
Because this plugin enables themes in part based on existance of classes, it is possible to have multiple themes enabled at the same time. They can be overlapping or not. Its all up to how you apply the classes!
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body> 7 <div class="theme1"> 8 <!-- everything in this div will have theme1 styles --> 9 </div> 10 <div class="theme2"> 11 <!-- everything in this div will have theme2 styles --> 12 </div> 13 <div class="theme1 theme2"> 14 <!-- everything in this div will have both styles applied --> 15 <!-- which one has higher specificity is determined by the order of the themes in the "themes" section of the config --> 16 </div> 17 </body> 18</html>
If you apply two themes at the same time, the specificity is determined by the order of the themes in the themes
array of the config. Later defined themes override earlier configs i.e. theme in index 1 takes precedence over theme in index 0.
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 colors: { 5 primary: 'red' 6 }, 7 fontFamily: { 8 title: 'Helvetica' 9 } 10 } 11 }, 12 themes: [ 13 { 14 name: 'theme1', 15 extend: { 16 // ... 17 } 18 }, 19 { 20 // this theme will win out over theme1 if an element has both theme1 and theme2 clases on it because it is defined later in the "themes" array 21 name: 'theme2', 22 extend: { 23 // ... 24 } 25 } 26 ] 27})
This plugin works by first creating a custom tailwind extension configured to replace any values you specify with css variables. Then it generates css variables with proper scoping for all of your themes. It also creates variants for each one of your themes as a bonus.
In short it automates everything you would need to do to do this yourself plus adds theme variants for you.
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 colors: { 5 primary: 'red' 6 }, 7 fontFamily: { 8 title: 'Helvetica' 9 } 10 } 11 }, 12 themes: [ 13 { 14 name: 'my-theme', 15 extend: { 16 colors: { 17 primary: 'blue' 18 }, 19 fontFamily: { 20 title: 'ui-monospace' 21 } 22 } 23 } 24 ] 25})
For example, the above configuration creates a theme extension equivalent to the following hand written version.
1// tailwind.config.js 2module.exports = { 3 theme: { 4 extend: { 5 colors: { 6 primary: 'rgb(255 0 0 / <alpha-value>)' 7 }, 8 fontFamily: { 9 title: 'var(--font-family-title)' 10 } 11 } 12 } 13}
It also injects css variables with proper scoping into tailwind's base layer.
1/* this is configured by "defaultTheme" */ 2:root { 3 --colors-primary: 255 0 0; 4 --font-family-title: Helvetica; 5} 6 7/* this is configured by the "my-theme" configuration */ 8.my-theme { 9 --colors-primary: 0 0 255; 10 --font-family-title: ui-monospace; 11}
Notice how the css variable for the color we specified is broken up into rgb values. We need to do this to support opacity modifiers. See Opacity for more details.
Now, classes like text-primary
and font-title
are generated like the following:
1.font-title { 2 font-family: var(--font-family-title); 3} 4 5.text-primary { 6 --tw-text-opacity: 1; 7 color: rgba(var(--colors-primary), var(--tw-text-opacity)); 8}
For comparison, this is what those classes looked like before without theming:
1.font-title { 2 font-family: Helvetica; 3} 4 5.text-primary { 6 --tw-text-opacity: 1; 7 color: rgb(255 0 0 / var(--tw-text-opacity)); 8}
This plugin adds variants for each one of your themes, should you need them when applying classes.
Taking, again, the above configuration as an example, the my-theme
variant is generated for my-theme
. So now you can use classes like my-theme:font-title
which will enable the classes only when the theme is enabled. The generated css for this example is the following:
1.my-theme .my-theme\:font-title, 2.my-theme.my-theme\:font-title { 3 font-family: var(--font-family-title); 4}
All css variables are injected in tailwind's base layer.
All of the configuration in the defaultTheme
config generates css variables scoped to :root
.
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 colors: { 5 primary: 'red' 6 }, 7 fontFamily: { 8 title: 'Helvetica' 9 } 10 } 11 } 12 // ... 13})
For example the above defaultTheme
config generates the following css variables in the :root
scope.
1/* this is configured by "defaultTheme" */ 2:root { 3 --colors-primary: 255 0 0; 4 --font-family-title: Helvetica; 5}
For each theme specified in the themes
section of the config, its config generates css variables scoped to a class of the name
field.
1require('tailwindcss-themer')({ 2 // ... 3 themes: [ 4 { 5 name: 'my-theme-1', 6 extend: { 7 colors: { 8 primary: 'red' 9 }, 10 fontFamily: { 11 title: 'Helvetica' 12 } 13 } 14 }, 15 { 16 name: 'my-theme-2', 17 extend: { 18 colors: { 19 primary: 'blue' 20 }, 21 fontFamily: { 22 title: 'ui-monospace' 23 } 24 } 25 } 26 ] 27})
For example, the above config in the themes
section of the config generates the following css:
1.my-theme-1 { 2 --colors-primary: 255 0 0; 3 --font-family-title: Helvetica; 4} 5 6.my-theme-2 { 7 --colors-primary: 0 0 255; 8 --font-family-title: ui-monospace; 9}
As specified above, variants are generated for every named theme you make, even for the default theme. This is so you can use them as class modifiers to enable certain styles only when that theme is enabled. It works like hover and focus variants, but activated with the theme. This lets you write classes like my-theme:rounded-sm
if you need fine grained control to apply some styles when a theme is activated and you can't cleanly express what you want with css variables alone.
Do note that because tailwind automatically adds the
dark
variant, if you name one of your themesdark
, the variant this plugin creates for it will conflict with what tailwind automatically creates for you. It is recommended that you name your dark theme something else likedarkTheme
to avoid the conflict. Not all config options work for themes nameddark
.
See Themes named dark for additional details.
The theme variant generated for the default theme is defaultTheme
(e.g. defaultTheme:rounded-sm
), but this now requires that instead of omitting any theme class to enable the default theme, you explicitly declare you are using the default theme by adding the class of defaultTheme
to the place you want themed (no other feature is affected by this, using the default theme variant is the only feature that requires you to add the defaultTheme
class to use). This is because I haven't been able to create a css selector that excludes all parents with any of the other theme classes. If you can make one, feel free to open up an issue.
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body> 7 <!-- this won't work because it doesn't have a parent with the "defaultTheme" class --> 8 <!-- I would love to make this work, but I haven't come up with a css selector that would work --> 9 <h1 class="defaultTheme:font-bold">Hello world!</h1> 10 </body> 11</html>
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body class="defaultTheme"> 7 <!-- this now works --> 8 <h1 class="defaultTheme:font-bold">Hello world!</h1> 9 </body> 10</html>
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body class="neon"> 7 <!-- this is turned off because it doesnt have a parent with a class of "defaultTheme" --> 8 <h1 class="defaultTheme:font-bold">Hello world!</h1> 9 </body> 10</html>
If you've opted to use a prefix in your Tailwind CSS setup, it's essential to include <prefix><theme name>
alongside the theme class. This ensures proper application of variants. For instance, if your prefix is tw-
and your theme name is dark-theme
, you'd use dark-theme tw-dark-theme
together.
1<!doctype html> 2<html lang="en"> 3 <head> 4 <!-- ... --> 5 </head> 6 <body class="dark-theme tw-dark-theme"> 7 <!-- Properly utilize prefixes and themes --> 8 <h1 class="dark-theme:font-bold">Hello world!</h1> 9 </body> 10</html>
As you probably could tell from above, the names of the generated css variables are the kebab-cased version of the variable's path on the config object.
Naming works the same for all theme configs (default theme and named themes).
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 colors: { 5 myBrand: { 6 primary: { 7 500: 'red' 8 } 9 } 10 } 11 } 12 } 13 // ... 14})
The above config would generate a css variable of the name --colors-myBrand-primary-500
. If for some reason, camelCasing is converted to kebab-casing, make sure you have tailwind v3.0.12
or later installed as that version fixed that bug.
If you use DEFAULT
as a leaf value, it is dropped off of the generated css variable name.
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 colors: { 5 brand1: { 6 DEFAULT: { 7 primary: { 8 DEFAULT: 'red' 9 } 10 } 11 } 12 } 13 } 14 } 15 // ... 16})
The above config would generate a css variable of the name --colors-brand1-DEFAULT-primary
. See Default key for more details.
If anywhere in the path, an array is encountered, the index is used in the generated css variable name.
1require('tailwindcss-themer')({ 2 defaultTheme: { 3 extend: { 4 fontFamily: { 5 title: ['ui-sans-serif', 'system-ui'] 6 } 7 } 8 } 9 // ... 10})
The example above creates the following css variables:
1:root { 2 --font-family-title-0: ui-sans-serif; 3 --font-family-title-1: system-ui; 4}
This plugin comes with types. In order to take advantage of them, make sure the files that use this plugin are type checked. For most use cases, this means making sure your tailwind.config.js
file is type checked. The easiest way to do this is by adding //@ts-check
at the top of the file. See the typescript example for a reference implementation. You can also write your config as a tailwind.config.ts
file to achieve type checking.
Those styles are probably getting purged which happens by default in tailwindcss. Read the tailwind docs on how to control what gets purged and what doesn't for details on how this works.
If you're expecting the defaultTheme
to automatically contain tailwind defaults implicitly, read the section on Overwriting tailwind defaults for how to do this properly.
This plugin can only theme config values that can be expressed as CSS variables. It can't theme config that tunes how tailwind or other plugins generate their css. Check the kind of value you're trying to theme in order to resolve your issue.
For example, this plugin can't theme tailwind's theme.container.center config value. This is because it configures how tailwind configures its .container
class, not a particular style within that class.
1/** @type {import('tailwindcss').Config} */ 2module.exports = { 3 theme: { 4 container: { 5 center: true // 👈🏼 this is the value I'm talking about 6 } 7 } 8}
1/* when false or undefined */ 2.container { 3 width: 100%; 4} 5 6/* when true */ 7.container { 8 width: 100%; 9 margin-right: auto; 10 margin-left: auto; 11}
Instead, the kinds of values this plugin can configure are values like theme.contaner.padding because their values that can be expressed as CSS variables and aren't used to configure how tailwind or another plugin generates CSS.
Feel free to start a discussion.
Was this documentation insufficient for you?
Was it confusing?
Was it ... dare I say ... inaccurate?
If any of the above describes your feelings of this documentation. Feel free to open up an issue.
No vulnerabilities found.
No security vulnerabilities found.