@bsthun/tailwindcss-instant-dark-mode 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc +11 -0
- package/LICENSE +19 -0
- package/README.md +52 -0
- package/doc/feature-language-server-autocompletion.png +0 -0
- package/index.ts +83 -0
- package/package.json +27 -0
package/.prettierrc
ADDED
package/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Thun
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
12
|
+
copies or substantial portions of the Software.
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# tailwindcss-instant-dark-mode
|
2
|
+
|
3
|
+
A tailwindcss plugin that make a class to instant apply both light and dark variants of a color automatically.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
Adding new color class to tailwindcss, topup from original class with `-dark` prefix that will automatically apply both light and dark variants of a color.
|
8
|
+
|
9
|
+
| Original tailwindcss variant | Instant dark mode |
|
10
|
+
|----------|------------------------|
|
11
|
+
| `bg-blue-100 dark:bg-blue-900` | `bg-blue-100-dark` |
|
12
|
+
| `text-kmutt-800 dark:text-kmutt-200` | `text-kmutt-800-dark` |
|
13
|
+
| `hover:border-blue-300 dark:hover:border-blue-700` | `hover:border-blue-300-dark` |
|
14
|
+
|
15
|
+
Supported colors properties:
|
16
|
+
- backgroundColor / `bg-`
|
17
|
+
- borderColor / `border-`
|
18
|
+
- color / `text-`
|
19
|
+
- ringColor / `border-`
|
20
|
+
- divideColor / `ring-`
|
21
|
+
- divideColor / `divide-`
|
22
|
+
- placeholderColor / `placeholder-`
|
23
|
+
|
24
|
+
#### Additional features
|
25
|
+
- Support for custom color classes
|
26
|
+
- Works with `hover:` and other variants
|
27
|
+
- Support tailwindcss language server for auto-completion
|
28
|
+

|
29
|
+
|
30
|
+
#### Limitations
|
31
|
+
- Only works with colors that defined in number format (e.g. `bg-blue-500`), not with color name (e.g. `bg-blue`)
|
32
|
+
- Only map directly between 2 color pair, with more customizations, may need to fallback to original tailwindcss syntax.
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
|
36
|
+
```bash
|
37
|
+
npm install @bsthun/tailwindcss-instant-dark-mode
|
38
|
+
```
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
```js
|
43
|
+
// tailwind.config.js
|
44
|
+
import { tailwindcssInstantDarkMode } from '@bsthun/tailwindcss-instant-dark-mode';
|
45
|
+
|
46
|
+
const config: Config = {
|
47
|
+
darkMode: ['class'],
|
48
|
+
plugins: [tailwindcssInstantDarkMode()],
|
49
|
+
};
|
50
|
+
|
51
|
+
export default config;
|
52
|
+
```
|
Binary file
|
package/index.ts
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
import plugin from 'tailwindcss/plugin'
|
2
|
+
|
3
|
+
export type tailwindInstantDarkModeConfig = {
|
4
|
+
colorMap: Record<string, string> | null
|
5
|
+
}
|
6
|
+
|
7
|
+
export const tailwindcssInstantDarkMode = (config?: tailwindInstantDarkModeConfig) => {
|
8
|
+
// Helper to get the dark mode equivalent of a color
|
9
|
+
const getDarkEquivalent = (color: string) => {
|
10
|
+
// Map light colors to their dark equivalents
|
11
|
+
if (!config)
|
12
|
+
config = {
|
13
|
+
colorMap: null,
|
14
|
+
}
|
15
|
+
|
16
|
+
if (!config.colorMap) {
|
17
|
+
config.colorMap = {
|
18
|
+
'50': '900',
|
19
|
+
'100': '800',
|
20
|
+
'200': '700',
|
21
|
+
'300': '600',
|
22
|
+
'400': '500',
|
23
|
+
'500': '400',
|
24
|
+
'600': '300',
|
25
|
+
'700': '200',
|
26
|
+
'800': '100',
|
27
|
+
'900': '50',
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
// Extract color base and shade
|
32
|
+
const match = color.match(/^(.+)-(\d+)$/)
|
33
|
+
if (!match) return color
|
34
|
+
|
35
|
+
const [_, base, shade] = match
|
36
|
+
const darkShade = config.colorMap[shade]
|
37
|
+
|
38
|
+
return darkShade ? `${base}-${darkShade}` : color
|
39
|
+
}
|
40
|
+
|
41
|
+
return plugin(function ({ addComponents, theme }) {
|
42
|
+
// Get all color utilities from theme
|
43
|
+
const utilities = Object.entries(theme('colors') as Record<string, any>).reduce(
|
44
|
+
(acc: Record<string, any>, [colorName, colorValues]) => {
|
45
|
+
if (typeof colorValues === 'object') {
|
46
|
+
Object.entries(colorValues).forEach(([shade, colorValue]) => {
|
47
|
+
const properties = [
|
48
|
+
['backgroundColor', 'bg'],
|
49
|
+
['borderColor', 'border'],
|
50
|
+
['color', 'text'],
|
51
|
+
['ringColor', 'ring'],
|
52
|
+
['divideColor', 'divide'],
|
53
|
+
['placeholderColor', 'placeholder'],
|
54
|
+
] as const
|
55
|
+
|
56
|
+
properties.forEach(([prop, prefix]) => {
|
57
|
+
const className = `${prefix}-${colorName}-${shade}-dark`
|
58
|
+
|
59
|
+
const darkShadeKey = getDarkEquivalent(`${colorName}-${shade}`)
|
60
|
+
const [darkColorName, darkShade] = darkShadeKey.split('-')
|
61
|
+
|
62
|
+
const darkValue = theme('colors')?.[darkColorName]?.[darkShade]
|
63
|
+
|
64
|
+
if (colorValue && darkValue) {
|
65
|
+
acc[`.${className}`] = {
|
66
|
+
[prop]: colorValue,
|
67
|
+
'.dark &': {
|
68
|
+
[prop]: darkValue,
|
69
|
+
},
|
70
|
+
transition: 'color 0.2s, background-color 0.2s, border-color 0.2s',
|
71
|
+
}
|
72
|
+
}
|
73
|
+
})
|
74
|
+
})
|
75
|
+
}
|
76
|
+
return acc
|
77
|
+
},
|
78
|
+
{}
|
79
|
+
)
|
80
|
+
|
81
|
+
addComponents(utilities)
|
82
|
+
})
|
83
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"name": "@bsthun/tailwindcss-instant-dark-mode",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "A tailwind plugin that make a class to instant apply both light and dark variants of a color automatically.",
|
5
|
+
"repository": {
|
6
|
+
"type": "git",
|
7
|
+
"url": "git+https://github.com/BSthun/tailwindcss-instant-dark-mode.git"
|
8
|
+
},
|
9
|
+
"scripts": {
|
10
|
+
"format": "prettier --write ."
|
11
|
+
},
|
12
|
+
"keywords": [
|
13
|
+
"tailwind",
|
14
|
+
"dark-mode"
|
15
|
+
],
|
16
|
+
"author": "BSthun <bsthun@gmail.com>",
|
17
|
+
"license": "MIT",
|
18
|
+
"bugs": {
|
19
|
+
"url": "https://github.com/BSthun/tailwindcss-instant-dark-mode/issues"
|
20
|
+
},
|
21
|
+
"homepage": "https://github.com/BSthun/tailwindcss-instant-dark-mode#readme",
|
22
|
+
"devDependencies": {
|
23
|
+
"@types/tailwindcss": "^3.0.11",
|
24
|
+
"prettier": "^3.4.2",
|
25
|
+
"tailwindcss": "^2.0.0"
|
26
|
+
}
|
27
|
+
}
|