Skip to content

feat: use default colors from tailwind by default #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { createVariableColors, variableColorsPlugin } from 'tailwindcss-variable
const config: Config = {
content: ['./src/**/*.tsx'],
theme: {
// You can also not pass the colors parameter,
// it will use the colors from tailwindcss by default.
colors: createVariableColors(colors),
},
plugins: [variableColorsPlugin(colors)],
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,12 @@ describe('should', () => {
}
`)
})

const defaultVariableColors = createVariableColors()
it('should ignore deprecated colors by default', () => {
expect(defaultVariableColors['amber']['100']).toMatchInlineSnapshot(
'"rgb(var(--tw-color-amber-100) / <alpha-value>)"',
)
expect(defaultVariableColors['blueGray']).toBeUndefined()
})
})
25 changes: 23 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import defaultColors from 'tailwindcss/colors'

import { Colors } from './type'

export { variableColorsPlugin } from './plugin'
Expand All @@ -17,10 +19,29 @@ type VariableColors = {
}
}

export function createVariableColors<T extends Colors>(colors: T) {
const deprecatedColors = [
'lightBlue',
'warmGray',
'trueGray',
'coolGray',
'blueGray',
] as const

export function getDefaultColors() {
if (deprecatedColors.some((color) => color in defaultColors)) {
deprecatedColors.forEach((color) => {
if (color in defaultColors) {
delete defaultColors[color]
}
})
}
return defaultColors
}

export function createVariableColors<T extends Colors>(colors?: T) {
const variableColors = {} as any

for (const [key, value] of Object.entries(colors)) {
for (const [key, value] of Object.entries(colors ?? getDefaultColors())) {
if (typeof value === 'string') {
if (!value.startsWith('#')) {
// color keyword
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import plugin from 'tailwindcss/plugin'
import { Colors } from './type'
import { getDefaultColors } from '.'

interface Options {
mode?: 'invert'
}
export function variableColorsPlugin<T extends Colors>(
colors: T,
colors?: T,
options: Options = {},
) {
return plugin(({ addBase }) => {
addBase({
':root': generateColorVariables(colors),
':root': generateColorVariables(colors ?? getDefaultColors()),
})
})
}
Expand Down
5 changes: 2 additions & 3 deletions packages/core/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Config } from 'tailwindcss'
import colors from 'tailwindcss/colors'
import { createVariableColors, variableColorsPlugin } from './src'

const config: Config = {
content: ['./src/**/*.tsx'],
theme: {
colors: createVariableColors(colors),
colors: createVariableColors(),
},
plugins: [variableColorsPlugin(colors)],
plugins: [variableColorsPlugin()],
}

export default config
5 changes: 2 additions & 3 deletions playground/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Config } from 'tailwindcss'
import colors from 'tailwindcss/colors'
import {
createVariableColors,
variableColorsPlugin,
Expand All @@ -12,7 +11,7 @@ const config: Config = {
'./src/app/**/*.{js,ts,jsx,tsx,mdx}',
],
theme: {
colors: createVariableColors(colors),
colors: createVariableColors(),
extend: {
backgroundImage: {
'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
Expand All @@ -25,7 +24,7 @@ const config: Config = {
padding: '1rem',
},
},
plugins: [variableColorsPlugin(colors)],
plugins: [variableColorsPlugin()],
}

export default config