Skip to content

Commit

Permalink
fix(module): allow CSS variables in tailwind colors (nuxt#2014)
Browse files Browse the repository at this point in the history
  • Loading branch information
davestewart authored and patrick-hofmann committed Oct 3, 2024
1 parent bd3fc65 commit 82009ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/runtime/plugins/colors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computed } from 'vue'
import { get, hexToRgb } from '../utils'
import { get, parseConfigValue } from '../utils'
import { defineNuxtPlugin, useAppConfig, useNuxtApp, useHead } from '#imports'
import colors from '#tailwind-config/theme/colors'

Expand All @@ -19,10 +19,10 @@ export default defineNuxtPlugin(() => {
}

return `:root {
${Object.entries(primary || colors.green).map(([key, value]) => `--color-primary-${key}: ${hexToRgb(value)};`).join('\n')}
${Object.entries(primary || colors.green).map(([key, value]) => `--color-primary-${key}: ${parseConfigValue(value)};`).join('\n')}
--color-primary-DEFAULT: var(--color-primary-500);
${Object.entries(gray || colors.cool).map(([key, value]) => `--color-gray-${key}: ${hexToRgb(value)};`).join('\n')}
${Object.entries(gray || colors.cool).map(([key, value]) => `--color-gray-${key}: ${parseConfigValue(value)};`).join('\n')}
}
.dark {
Expand Down
10 changes: 9 additions & 1 deletion src/runtime/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,22 @@ export function mergeConfig<T> (strategy: Strategy, ...configs): T {
return defuTwMerge({}, ...configs) as T
}

const rxHex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i

export function parseConfigValue (value: string) {
return rxHex.test(value)
? hexToRgb(value)
: value
}

export function hexToRgb (hex: string) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i
hex = hex.replace(shorthandRegex, function (_, r, g, b) {
return r + r + g + g + b + b
})

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

0 comments on commit 82009ab

Please sign in to comment.