mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-31 20:28:09 +01:00
improve code reusability
This commit is contained in:
@@ -1,43 +1,13 @@
|
|||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
import colors from 'tailwindcss/colors'
|
|
||||||
import type { UseHeadInput } from '@unhead/vue/types'
|
import type { UseHeadInput } from '@unhead/vue/types'
|
||||||
import { defineNuxtPlugin, useAppConfig, useNuxtApp, useHead } from '#imports'
|
import { defineNuxtPlugin, useAppConfig, useNuxtApp, useHead } from '#imports'
|
||||||
|
import { generateColorStyles } from '../utils/colors'
|
||||||
const shades = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950] as const
|
|
||||||
|
|
||||||
function getColor(color: keyof typeof colors, shade: typeof shades[number]): string {
|
|
||||||
if (color in colors && typeof colors[color] === 'object' && shade in colors[color]) {
|
|
||||||
return colors[color][shade] as string
|
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateShades(key: string, value: string) {
|
|
||||||
return `${shades.map(shade => `--ui-color-${key}-${shade}: var(--color-${value === 'neutral' ? 'old-neutral' : value}-${shade}, ${getColor(value as keyof typeof colors, shade)});`).join('\n ')}`
|
|
||||||
}
|
|
||||||
function generateColor(key: string, shade: number) {
|
|
||||||
return `--ui-${key}: var(--ui-color-${key}-${shade});`
|
|
||||||
}
|
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin(() => {
|
||||||
const appConfig = useAppConfig()
|
const appConfig = useAppConfig()
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
|
|
||||||
const root = computed(() => {
|
const root = computed(() => generateColorStyles(appConfig.ui.colors))
|
||||||
const { neutral, ...colors } = appConfig.ui.colors
|
|
||||||
|
|
||||||
return `@layer base {
|
|
||||||
:root {
|
|
||||||
${Object.entries(appConfig.ui.colors).map(([key, value]: [string, string]) => generateShades(key, value)).join('\n ')}
|
|
||||||
}
|
|
||||||
:root, .light {
|
|
||||||
${Object.keys(colors).map(key => generateColor(key, 500)).join('\n ')}
|
|
||||||
}
|
|
||||||
.dark {
|
|
||||||
${Object.keys(colors).map(key => generateColor(key, 400)).join('\n ')}
|
|
||||||
}
|
|
||||||
}`
|
|
||||||
})
|
|
||||||
|
|
||||||
// Head
|
// Head
|
||||||
const headData: UseHeadInput = {
|
const headData: UseHeadInput = {
|
||||||
|
|||||||
34
src/runtime/utils/colors.ts
Normal file
34
src/runtime/utils/colors.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import colors from 'tailwindcss/colors'
|
||||||
|
|
||||||
|
export const shades = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950] as const
|
||||||
|
|
||||||
|
export function getColor(color: keyof typeof colors, shade: typeof shades[number]): string {
|
||||||
|
if (color in colors && typeof colors[color] === 'object' && shade in colors[color]) {
|
||||||
|
return colors[color][shade] as string
|
||||||
|
}
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateShades(key: string, value: string) {
|
||||||
|
return `${shades.map(shade => `--ui-color-${key}-${shade}: var(--color-${value === 'neutral' ? 'old-neutral' : value}-${shade}, ${getColor(value as keyof typeof colors, shade)});`).join('\n ')}`
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateColor(key: string, shade: number) {
|
||||||
|
return `--ui-${key}: var(--ui-color-${key}-${shade});`
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateColorStyles(colors: Record<string, string>) {
|
||||||
|
const { neutral, ...rest } = colors
|
||||||
|
|
||||||
|
return `@layer base {
|
||||||
|
:root {
|
||||||
|
${Object.entries(colors).map(([key, value]: [string, string]) => generateShades(key, value)).join('\n ')}
|
||||||
|
}
|
||||||
|
:root, .light {
|
||||||
|
${Object.keys(rest).map(key => generateColor(key, 500)).join('\n ')}
|
||||||
|
}
|
||||||
|
.dark {
|
||||||
|
${Object.keys(rest).map(key => generateColor(key, 400)).join('\n ')}
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
}
|
||||||
@@ -1,46 +1,15 @@
|
|||||||
import { computed, watchEffect } from 'vue'
|
import { computed, watchEffect } from 'vue'
|
||||||
import colors from 'tailwindcss/colors'
|
|
||||||
import { useHead } from '@unhead/vue'
|
import { useHead } from '@unhead/vue'
|
||||||
import type { Plugin } from 'vue'
|
import type { Plugin } from 'vue'
|
||||||
import { useAppConfig } from '../composables/useAppConfig'
|
import { useAppConfig } from '../composables/useAppConfig'
|
||||||
|
import { generateColorStyles } from '../../utils/colors'
|
||||||
const shades = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950] as const
|
|
||||||
|
|
||||||
function getColor(color: keyof typeof colors, shade: typeof shades[number]): string {
|
|
||||||
if (color in colors && typeof colors[color] === 'object' && shade in colors[color]) {
|
|
||||||
return colors[color][shade] as string
|
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateShades(key: string, value: string) {
|
|
||||||
return `${shades.map(shade => `--ui-color-${key}-${shade}: var(--color-${value === 'neutral' ? 'old-neutral' : value}-${shade}, ${getColor(value as keyof typeof colors, shade)});`).join('\n ')}`
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateColor(key: string, shade: number) {
|
|
||||||
return `--ui-${key}: var(--ui-color-${key}-${shade});`
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
install(app) {
|
install(app) {
|
||||||
app.runWithContext(() => {
|
app.runWithContext(() => {
|
||||||
const appConfig = useAppConfig()
|
const appConfig = useAppConfig()
|
||||||
|
|
||||||
const root = computed(() => {
|
const root = computed(() => generateColorStyles(appConfig.ui.colors))
|
||||||
const { neutral, ...colors } = appConfig.ui.colors
|
|
||||||
|
|
||||||
return `@layer base {
|
|
||||||
:root {
|
|
||||||
${Object.entries(appConfig.ui.colors).map(([key, value]: [string, string]) => generateShades(key, value)).join('\n ')}
|
|
||||||
}
|
|
||||||
:root, .light {
|
|
||||||
${Object.keys(colors).map(key => generateColor(key, 500)).join('\n ')}
|
|
||||||
}
|
|
||||||
.dark {
|
|
||||||
${Object.keys(colors).map(key => generateColor(key, 400)).join('\n ')}
|
|
||||||
}
|
|
||||||
}`
|
|
||||||
})
|
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
style: [{
|
style: [{
|
||||||
@@ -58,7 +27,6 @@ export default {
|
|||||||
styleEl.id = 'nuxt-ui-colors-vue'
|
styleEl.id = 'nuxt-ui-colors-vue'
|
||||||
document.head.appendChild(styleEl)
|
document.head.appendChild(styleEl)
|
||||||
}
|
}
|
||||||
|
|
||||||
styleEl.innerHTML = root.value
|
styleEl.innerHTML = root.value
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user