Skip to content

Commit 7f50c70

Browse files
authoredSep 16, 2024··
fix(module): allow CSS variables in tailwind colors (#2014)

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed
 

‎src/runtime/plugins/colors.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { computed } from 'vue'
2-
import { get, hexToRgb } from '../utils'
2+
import { get, parseConfigValue } from '../utils'
33
import { defineNuxtPlugin, useAppConfig, useNuxtApp, useHead } from '#imports'
44
import colors from '#tailwind-config/theme/colors'
55

@@ -19,10 +19,10 @@ export default defineNuxtPlugin(() => {
1919
}
2020

2121
return `:root {
22-
${Object.entries(primary || colors.green).map(([key, value]) => `--color-primary-${key}: ${hexToRgb(value)};`).join('\n')}
22+
${Object.entries(primary || colors.green).map(([key, value]) => `--color-primary-${key}: ${parseConfigValue(value)};`).join('\n')}
2323
--color-primary-DEFAULT: var(--color-primary-500);
2424
25-
${Object.entries(gray || colors.cool).map(([key, value]) => `--color-gray-${key}: ${hexToRgb(value)};`).join('\n')}
25+
${Object.entries(gray || colors.cool).map(([key, value]) => `--color-gray-${key}: ${parseConfigValue(value)};`).join('\n')}
2626
}
2727
2828
.dark {

‎src/runtime/utils/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@ export function mergeConfig<T> (strategy: Strategy, ...configs): T {
4141
return defuTwMerge({}, ...configs) as T
4242
}
4343

44+
const rxHex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i
45+
46+
export function parseConfigValue (value: string) {
47+
return rxHex.test(value)
48+
? hexToRgb(value)
49+
: value
50+
}
51+
4452
export function hexToRgb (hex: string) {
4553
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
4654
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i
4755
hex = hex.replace(shorthandRegex, function (_, r, g, b) {
4856
return r + r + g + g + b + b
4957
})
5058

51-
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
59+
const result = rxHex.exec(hex)
5260
return result
5361
? `${parseInt(result[1], 16)} ${parseInt(result[2], 16)} ${parseInt(result[3], 16)}`
5462
: null

0 commit comments

Comments
 (0)
Please sign in to comment.