mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.0 KiB
64 lines
2.0 KiB
2 years ago
|
import { ConfigProvider } from 'ant-design-vue'
|
||
|
import type { Theme as AntTheme } from 'ant-design-vue/es/config-provider'
|
||
2 years ago
|
import tinycolor from 'tinycolor2'
|
||
2 years ago
|
import { hexToRGB, themeV2Colors, useCssVar, useInjectionState } from '#imports'
|
||
2 years ago
|
|
||
2 years ago
|
export interface ThemeConfig extends AntTheme {
|
||
2 years ago
|
primaryColor: string
|
||
2 years ago
|
accentColor: string
|
||
|
}
|
||
|
|
||
|
const [setup, use] = useInjectionState((config?: Partial<ThemeConfig>) => {
|
||
|
const primaryColor = useCssVar('--color-primary', typeof document !== 'undefined' ? document.documentElement : null)
|
||
|
const accentColor = useCssVar('--color-accent', typeof document !== 'undefined' ? document.documentElement : null)
|
||
|
|
||
2 years ago
|
/** current theme config */
|
||
|
const currentTheme = ref({
|
||
|
primaryColor: themeV2Colors['royal-blue'].DEFAULT,
|
||
|
accentColor: themeV2Colors.pink['500'],
|
||
|
})
|
||
|
|
||
2 years ago
|
/** set initial config */
|
||
|
setTheme(config ?? currentTheme.value)
|
||
|
|
||
|
/** set theme (persists in localstorage) */
|
||
2 years ago
|
function setTheme(theme?: Partial<ThemeConfig>) {
|
||
2 years ago
|
const themePrimary = theme?.primaryColor ? tinycolor(theme.primaryColor) : tinycolor(themeV2Colors['royal-blue'].DEFAULT)
|
||
|
const themeAccent = theme?.accentColor ? tinycolor(theme.accentColor) : tinycolor(themeV2Colors.pink['500'])
|
||
2 years ago
|
|
||
2 years ago
|
// convert hex colors to rgb values
|
||
2 years ago
|
primaryColor.value = themePrimary.isValid()
|
||
|
? hexToRGB(themePrimary.toHex8String())
|
||
|
: hexToRGB(themeV2Colors['royal-blue'].DEFAULT)
|
||
|
accentColor.value = themeAccent.isValid() ? hexToRGB(themeAccent.toHex8String()) : hexToRGB(themeV2Colors.pink['500'])
|
||
2 years ago
|
|
||
2 years ago
|
currentTheme.value = {
|
||
|
primaryColor: themePrimary.toHex8String().toUpperCase(),
|
||
|
accentColor: themeAccent.toHex8String().toUpperCase(),
|
||
|
}
|
||
2 years ago
|
|
||
|
ConfigProvider.config({
|
||
2 years ago
|
theme: currentTheme.value,
|
||
2 years ago
|
})
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
theme: currentTheme,
|
||
|
setTheme,
|
||
|
}
|
||
2 years ago
|
}, 'theme')
|
||
2 years ago
|
|
||
|
export const provideTheme = setup
|
||
|
|
||
|
export function useTheme(config?: Partial<ThemeConfig>) {
|
||
|
const theme = use()
|
||
|
|
||
|
if (!theme) {
|
||
|
return setup(config)
|
||
|
} else {
|
||
|
if (config) theme.setTheme(config)
|
||
|
}
|
||
|
|
||
|
return theme
|
||
|
}
|