Skip to content

Commit 2b81a44

Browse files
authored
feat: use default colors from tailwind by default (#4)
* feat: use default colors from tailwind by default * docs: update
1 parent a65f145 commit 2b81a44

File tree

6 files changed

+40
-10
lines changed

6 files changed

+40
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { createVariableColors, variableColorsPlugin } from 'tailwindcss-variable
2323
const config: Config = {
2424
content: ['./src/**/*.tsx'],
2525
theme: {
26+
// You can also not pass the colors parameter,
27+
// it will use the colors from tailwindcss by default.
2628
colors: createVariableColors(colors),
2729
},
2830
plugins: [variableColorsPlugin(colors)],

packages/core/src/index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,12 @@ describe('should', () => {
388388
}
389389
`)
390390
})
391+
392+
const defaultVariableColors = createVariableColors()
393+
it('should ignore deprecated colors by default', () => {
394+
expect(defaultVariableColors['amber']['100']).toMatchInlineSnapshot(
395+
'"rgb(var(--tw-color-amber-100) / <alpha-value>)"',
396+
)
397+
expect(defaultVariableColors['blueGray']).toBeUndefined()
398+
})
391399
})

packages/core/src/index.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import defaultColors from 'tailwindcss/colors'
2+
13
import { Colors } from './type'
24

35
export { variableColorsPlugin } from './plugin'
@@ -17,10 +19,29 @@ type VariableColors = {
1719
}
1820
}
1921

20-
export function createVariableColors<T extends Colors>(colors: T) {
22+
const deprecatedColors = [
23+
'lightBlue',
24+
'warmGray',
25+
'trueGray',
26+
'coolGray',
27+
'blueGray',
28+
] as const
29+
30+
export function getDefaultColors() {
31+
if (deprecatedColors.some((color) => color in defaultColors)) {
32+
deprecatedColors.forEach((color) => {
33+
if (color in defaultColors) {
34+
delete defaultColors[color]
35+
}
36+
})
37+
}
38+
return defaultColors
39+
}
40+
41+
export function createVariableColors<T extends Colors>(colors?: T) {
2142
const variableColors = {} as any
2243

23-
for (const [key, value] of Object.entries(colors)) {
44+
for (const [key, value] of Object.entries(colors ?? getDefaultColors())) {
2445
if (typeof value === 'string') {
2546
if (!value.startsWith('#')) {
2647
// color keyword

packages/core/src/plugin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import plugin from 'tailwindcss/plugin'
22
import { Colors } from './type'
3+
import { getDefaultColors } from '.'
34

45
interface Options {
56
mode?: 'invert'
67
}
78
export function variableColorsPlugin<T extends Colors>(
8-
colors: T,
9+
colors?: T,
910
options: Options = {},
1011
) {
1112
return plugin(({ addBase }) => {
1213
addBase({
13-
':root': generateColorVariables(colors),
14+
':root': generateColorVariables(colors ?? getDefaultColors()),
1415
})
1516
})
1617
}

packages/core/tailwind.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { Config } from 'tailwindcss'
2-
import colors from 'tailwindcss/colors'
32
import { createVariableColors, variableColorsPlugin } from './src'
43

54
const config: Config = {
65
content: ['./src/**/*.tsx'],
76
theme: {
8-
colors: createVariableColors(colors),
7+
colors: createVariableColors(),
98
},
10-
plugins: [variableColorsPlugin(colors)],
9+
plugins: [variableColorsPlugin()],
1110
}
1211

1312
export default config

playground/tailwind.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Config } from 'tailwindcss'
2-
import colors from 'tailwindcss/colors'
32
import {
43
createVariableColors,
54
variableColorsPlugin,
@@ -12,7 +11,7 @@ const config: Config = {
1211
'./src/app/**/*.{js,ts,jsx,tsx,mdx}',
1312
],
1413
theme: {
15-
colors: createVariableColors(colors),
14+
colors: createVariableColors(),
1615
extend: {
1716
backgroundImage: {
1817
'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
@@ -25,7 +24,7 @@ const config: Config = {
2524
padding: '1rem',
2625
},
2726
},
28-
plugins: [variableColorsPlugin(colors)],
27+
plugins: [variableColorsPlugin()],
2928
}
3029

3130
export default config

0 commit comments

Comments
 (0)