Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support configure css variables generation location #3466

Merged
merged 9 commits into from
Jun 20, 2023
2 changes: 1 addition & 1 deletion packages/nuxt/nuxt.shim.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare module 'vuestci-ui' {
}

declare module '#imports' {
export * from 'nuxt/dist/head/runtime/composables.ts'
export * from '@unhead/vue'
}

declare module '#vuestic-config' {
Expand Down
22 changes: 13 additions & 9 deletions packages/nuxt/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { markRaw, computed, watch, type Ref } from 'vue'

import { defineNuxtPlugin, useCookie } from '#app'
import { useHead } from '#imports'
import { useHead, ReactiveHead } from '#imports'
import NuxtLink from '#app/components/nuxt-link'
import configFromFile from '#vuestic-config'

Expand Down Expand Up @@ -56,21 +56,25 @@ export default defineNuxtPlugin(async (nuxtApp) => {
if (colorConfig) {
useHead(computed(() => {
return {
htmlAttrs: {
style: colorConfig.renderCSSVariables()
}
}
style: [
{
innerHTML: colorConfig.renderCSSVariablesStyleContent()
}
]
} satisfies ReactiveHead
}))
}

const colorsClasses = getGlobalProperty(app, '$vaColorsClasses')
if (colorsClasses) {
useHead(computed(() => {
return {
htmlAttrs: {
style: colorsClasses.renderColorHelpers()
}
}
style: [
{
innerHTML: colorsClasses.renderColorHelpers()
}
]
} satisfies ReactiveHead
}))
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-alert/useAlertStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const useAlertStyles = (props: AlertStyleProps) => {
const contentStyle = computed(() => {
return {
alignItems: props.center ? 'center' : '',
color: (props.border || props.outline) ? getColor(getTextColor(background.value), undefined, true) : textColorComputed.value,
color: (props.border || props.outline) ? 'currentColor' : textColorComputed.value,
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { PartialGlobalConfig } from './../../global-config/types'
import { ColorVariables } from './../types'
import { App, watch } from 'vue'
import { App, watch, computed } from 'vue'
import { isServer } from '../../../utils/ssr'
import { cssVariableName } from '../utils'
import { useColors } from '../../../composables'
import { generateUniqueId } from '../../../utils/uuid'
import { addOrUpdateStyleElement } from '../../../utils/dom'

export const setCSSVariable = (name: string, value: string, root: HTMLElement) => {
root.style.setProperty(cssVariableName(name), value)
}

export const generateCSSVariable = (key: string, value: string) => {
return `${cssVariableName(key)}: ${value};\n`
}

export const createColorConfigPlugin = (app: App, config?: PartialGlobalConfig) => {
const { colors: configColors, getTextColor, getColor, currentPresetName, applyPreset } = useColors()

Expand All @@ -23,27 +29,41 @@ export const createColorConfigPlugin = (app: App, config?: PartialGlobalConfig)
return `${renderedColors};${renderedOnColors}`
}

const updateColors = (newValue: ColorVariables | undefined) => {
if (!newValue) { return }
if (isServer()) { return }

const root = document.documentElement
const renderCSSVariablesStyleContent = (colors: ColorVariables = configColors) => {
const colorNames = Object.keys(colors)

const colorNames = Object.keys(newValue)
let result = ':root {\n'
colorNames.forEach((key) => {
setCSSVariable(key, newValue[key], root)
result += generateCSSVariable(key, colors[key])
})
colorNames.forEach((key) => {
setCSSVariable(`on-${key}`, getColor(getTextColor(newValue[key])), root)
result += generateCSSVariable(`on-${key}`, getColor(getTextColor(colors[key])))
})
result += '}\n'

return result
}

const uniqueId = computed(generateUniqueId)

const updateColors = (newValue: ColorVariables | undefined) => {
if (!newValue) { return }
if (isServer()) { return }

const styleContent = renderCSSVariablesStyleContent(newValue)

addOrUpdateStyleElement(`va-color-variables-${uniqueId.value}`, () => styleContent)
}

watch(configColors, (newValue) => {
updateColors(newValue)
}, { immediate: true, deep: true })

return {
colors: configColors,
currentPresetName,
renderCSSVariables,
updateColors,
renderCSSVariablesStyleContent,
}
}