Skip to content

Commit 893c6d9

Browse files
committed
feat: add generic
1 parent 5042bab commit 893c6d9

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/index.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import type { DefaultColors } from 'tailwindcss/types/generated/colors'
1+
import { Colors } from './type'
2+
23
export { defineColorVariables } from './plugin'
34

45
type VariableColors = {
5-
[key in keyof DefaultColors]: DefaultColors[key] extends `#${string}`
6+
[key in keyof Colors]: Colors[key] extends `#${string}`
67
? `rgb(var(--tw-color-${key}) / <alpha-value>)`
7-
: DefaultColors[key] extends string
8+
: Colors[key] extends string
89
? // has no uppercase letter
9-
DefaultColors[key] extends Lowercase<DefaultColors[key]>
10+
Colors[key] extends Lowercase<Colors[key]>
1011
? key
11-
: DefaultColors[key]
12+
: Colors[key]
1213
: {
13-
[level in keyof DefaultColors[key]]: level extends string | number
14+
[level in keyof Colors[key]]: level extends string | number
1415
? `rgb(var(--tw-color-${key}-${level}) / <alpha-value>)`
1516
: never
1617
}
1718
}
1819

19-
export function createVariableColors(colors: DefaultColors) {
20+
export function createVariableColors<T extends Colors>(colors: T) {
2021
const variableColors = {} as any
2122

2223
for (const [key, value] of Object.entries(colors)) {
@@ -26,9 +27,7 @@ export function createVariableColors(colors: DefaultColors) {
2627
variableColors[key] = value
2728
} else {
2829
// key color
29-
variableColors[
30-
key
31-
] = `rgb(var(--tw-color-${key}) / <alpha-value>)`
30+
variableColors[key] = `rgb(var(--tw-color-${key}) / <alpha-value>)`
3231
}
3332
} else {
3433
// tonal palette

src/plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import plugin from 'tailwindcss/plugin'
2-
import { DefaultColors } from 'tailwindcss/types/generated/colors'
32
import {
43
argbFromHex,
54
redFromArgb,
65
greenFromArgb,
76
blueFromArgb,
87
} from '@material/material-color-utilities'
8+
import { Colors } from './type'
99

1010
interface Options {
1111
mode?: 'invert'
1212
}
13-
export function defineColorVariables(
14-
colors: DefaultColors,
13+
export function defineColorVariables<T extends Colors>(
14+
colors: T,
1515
options: Options = {},
1616
) {
1717
return plugin(({ addBase }) => {
@@ -21,7 +21,7 @@ export function defineColorVariables(
2121
})
2222
}
2323

24-
export function createColorVariables(colors: DefaultColors) {
24+
export function createColorVariables<T extends Colors>(colors: T) {
2525
const colorVariables = {} as any
2626
const darkColorVariables = {} as any
2727

src/type.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { DefaultColors } from 'tailwindcss/types/generated/colors'
2+
3+
type T = {
4+
[key: string]: string
5+
}
6+
7+
export type Colors =
8+
| DefaultColors
9+
| {
10+
[key: string]: string | T
11+
}

0 commit comments

Comments
 (0)