mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +01:00
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { computed } from 'vue'
|
|
import { hexToRgb } from '../utils'
|
|
import { defineNuxtPlugin, useAppConfig } from '#imports'
|
|
import colors from '#tailwind-config/theme/colors'
|
|
|
|
export default defineNuxtPlugin(() => {
|
|
const appConfig = useAppConfig()
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
const root = computed(() => {
|
|
const primary: Record<string, string> | undefined = colors[appConfig.ui.primary]
|
|
const gray: Record<string, string> | undefined = colors[appConfig.ui.gray]
|
|
|
|
if (!primary) {
|
|
console.warn(`[@nuxt/ui] Primary color '${appConfig.ui.primary}' not found in Tailwind config`)
|
|
}
|
|
if (!gray) {
|
|
console.warn(`[@nuxt/ui] Gray color '${appConfig.ui.gray}' not found in Tailwind config`)
|
|
}
|
|
|
|
return `:root {
|
|
${Object.entries(primary || colors.green).map(([key, value]) => `--color-primary-${key}: ${hexToRgb(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')}
|
|
}
|
|
|
|
.dark {
|
|
--color-primary-DEFAULT: var(--color-primary-400);
|
|
}
|
|
`
|
|
})
|
|
|
|
// Head
|
|
const headData: any = {
|
|
style: [{
|
|
innerHTML: () => root.value,
|
|
tagPriority: -2,
|
|
id: 'nuxt-ui-colors'
|
|
}]
|
|
}
|
|
|
|
// SPA mode
|
|
if (process.client && nuxtApp.isHydrating && !nuxtApp.payload.serverRendered) {
|
|
const style = document.createElement('style')
|
|
|
|
style.innerHTML = root.value
|
|
style.setAttribute('data-nuxt-ui-colors', '')
|
|
document.head.appendChild(style)
|
|
|
|
headData.script = [{
|
|
innerHTML: 'document.head.removeChild(document.querySelector(\'[data-nuxt-ui-colors]\'))'
|
|
}]
|
|
}
|
|
|
|
useHead(headData)
|
|
})
|