From 15087f503e10f267c96874989c6d0b8ec0480a21 Mon Sep 17 00:00:00 2001 From: XiaoDaiGua-Ray <51957438+XiaoDaiGua-Ray@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:03:57 +0800 Subject: [PATCH] feat(theme): add switchable interface styles --- __test__/app/global-provider.spec.ts | 22 +- __test__/hooks/useTheme.spec.ts | 10 +- __test__/layout/preferences.spec.ts | 57 ++++- .../provider/AppNaiveGlobalProvider/index.tsx | 40 ++-- .../provider/AppStyleProvider/index.tsx | 120 +++-------- apps/admin/src/app-config/app-config.ts | 2 - apps/admin/src/hooks/template/useTheme.ts | 34 --- .../src/layout/components/Menu/index.scss | 3 - .../SettingDrawer/components/ThemeSegment.tsx | 36 +--- .../components/SettingDrawer/index.scss | 8 +- .../segment-views/Appearance.tsx | 39 +++- .../src/locales/lang/en-US/preferences.json | 8 + .../src/locales/lang/zh-CN/preferences.json | 8 + apps/admin/src/store/hooks/useSettingStore.ts | 8 +- .../src/store/modules/setting/constant.ts | 2 + apps/admin/src/store/modules/setting/index.ts | 132 +++++++----- .../store/modules/setting/preference-file.ts | 28 +-- apps/admin/src/store/modules/setting/types.ts | 2 + apps/docs/examples/DemoFrame.vue | 42 ++-- apps/docs/guide/appearance.md | 20 +- apps/docs/reference/configuration.md | 2 +- apps/docs/reference/store/setting.md | 5 +- packages/admin-shell/src/Tabs.scss | 6 +- packages/admin-shell/src/shell.scss | 2 +- packages/ui/src/theme-presets.ts | 198 ++++++++++++++---- packages/ui/src/theme.css | 46 ++-- 26 files changed, 515 insertions(+), 365 deletions(-) diff --git a/__test__/app/global-provider.spec.ts b/__test__/app/global-provider.spec.ts index 77ae210c..c0f20032 100644 --- a/__test__/app/global-provider.spec.ts +++ b/__test__/app/global-provider.spec.ts @@ -1,7 +1,8 @@ import { mount } from '@vue/test-utils' import { createPinia, disposePinia, setActivePinia } from 'pinia' -import { nextTick } from 'vue' +import { h, nextTick } from 'vue' import GlobalProvider from '../../apps/admin/src/app-components/provider/AppNaiveGlobalProvider' +import AppStyleProvider from '../../apps/admin/src/app-components/provider/AppStyleProvider' import { piniaSettingStore } from '../../apps/admin/src/store/modules/setting' it('reuses the global message instance across themes and removes its host on unmount', async () => { @@ -9,16 +10,31 @@ it('reuses the global message instance across themes and removes its host on unm setActivePinia(pinia) const originalChildren = new Set(document.body.children) - const wrapper = mount(GlobalProvider, { global: { plugins: [pinia] } }) + const wrapper = mount(GlobalProvider, { + global: { plugins: [pinia] }, + slots: { default: () => h(AppStyleProvider) }, + }) const message = window.$message const hosts = () => Array.from(document.body.children).filter((el) => !originalChildren.has(el)) try { expect(hosts()).toHaveLength(1) - piniaSettingStore().updateSettingState('_appTheme', true) + const store = piniaSettingStore() + + store.updateSettingState('themeStyle', 'material') + store.updateSettingState('appTheme', 'dark') await nextTick() expect(window.$message).toBe(message) + expect( + document.documentElement.style.getPropertyValue('--r-token-primary'), + ).toBe('#d6bbff') + expect( + document.documentElement.style.getPropertyValue('--r-token-radius'), + ).toBe('20px') + expect( + document.documentElement.classList.contains('r-template--dark'), + ).toBe(true) expect(hosts()).toHaveLength(1) } finally { wrapper.unmount() diff --git a/__test__/hooks/useTheme.spec.ts b/__test__/hooks/useTheme.spec.ts index 148d5b67..a9b0f141 100644 --- a/__test__/hooks/useTheme.spec.ts +++ b/__test__/hooks/useTheme.spec.ts @@ -47,17 +47,17 @@ describe('useTheme', async () => { }) it('selects auto mode and applies the system color preference', () => { - const spy = vi - .spyOn(window, 'matchMedia') - .mockReturnValue({ matches: true } as MediaQueryList) + const dark = window.matchMedia('(prefers-color-scheme: dark)').matches try { syncSystemTheme() - expect(getAppTheme()).toMatchObject({ theme: true, themeLabel: 'Dark' }) + expect(getAppTheme()).toMatchObject({ + theme: dark, + themeLabel: dark ? 'Dark' : 'Light', + }) expect(useSettingGetters().getAppThemeStr.value).toBe('auto') } finally { lightTheme() - spy.mockRestore() } }) }) diff --git a/__test__/layout/preferences.spec.ts b/__test__/layout/preferences.spec.ts index 3201eb76..0fc69b6f 100644 --- a/__test__/layout/preferences.spec.ts +++ b/__test__/layout/preferences.spec.ts @@ -1,10 +1,7 @@ import { createPinia, disposePinia, setActivePinia } from 'pinia' import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' import { createApp, nextTick } from 'vue' -import { - APP_CATCH_KEY, - GLOBAL_CLASS_NAMES, -} from '../../apps/admin/src/app-config' +import { APP_CATCH_KEY } from '../../apps/admin/src/app-config' import { findMenuRoot, firstMenuLeaf, @@ -74,6 +71,10 @@ describe('layout preferences', () => { localeLanguage: 'en-US', sideBarLogo: { icon: 'ray', title: 'Saved title' }, menuTagSwitch: false, + primaryColorOverride: { + common: { primaryColor: '#3468db', bodyColor: '#000000' }, + Card: { borderRadius: '99px' }, + }, }), ) const { pinia, app } = mountPersistedSettings() @@ -88,6 +89,10 @@ describe('layout preferences', () => { }) expect(store.localeLanguage).toBe('en-US') expect(store.menuTagSwitch).toBe(false) + expect(store.themeStyle).toBe('naive') + expect(store.themeOverrides.common?.primaryColor).toBe('#3468db') + expect(store.themeOverrides.common?.bodyColor).toBeUndefined() + expect(store.themeOverrides.Card).toBeUndefined() store.updateSettingState('layoutConfig', { mode: 'top' }) expect(store.layoutConfig.fixedHeader).toBe(true) expect(store.layoutConfig.mode).toBe('top') @@ -101,13 +106,11 @@ describe('layout preferences', () => { it('restores the exported preferences through the existing store and persistence', async () => { const { pinia, app } = mountPersistedSettings() const store = piniaSettingStore() - const property = GLOBAL_CLASS_NAMES.rayTemplateThemePrimaryColor - const fadeProperty = GLOBAL_CLASS_NAMES.rayTemplateThemePrimaryFadeColor - const { style } = document.documentElement try { store.updateSettingState('layoutConfig', { mode: 'right' }) store.updateSettingState('appTheme', 'dark') + store.updateSettingState('themeStyle', 'material') store.updateSettingState('watermarkConfig', { content: 'Portable' }) store.updateSettingState('keepAliveConfig', { keepAliveExclude: ['Page'], @@ -119,12 +122,14 @@ describe('layout preferences', () => { store.updateSettingState('appTheme', 'light') store.updateSettingState('appRootRoute', { path: '/custom-home' }) store.changePrimaryColor('#000000') + store.updateSettingState('themeStyle', 'plain') store.importPreferences(JSON.stringify(file)) await nextTick() expect(store.exportPreferences()).toEqual(file) expect(store._appTheme).toBe(true) - expect(style.getPropertyValue(property)).toBe('#3468db') + expect(store.themeOverrides.common?.primaryColor).toBe('#3468db') + expect(store.themeOverrides.Card?.borderRadius).toBe('20px') expect(store.appRootRoute.path).toBe('/custom-home') expect(file.preferences).not.toHaveProperty('appRootRoute') expect(file.preferences).not.toHaveProperty('lockScreenSwitch') @@ -132,13 +137,43 @@ describe('layout preferences', () => { JSON.parse( localStorage.getItem(APP_CATCH_KEY.appPiniaSettingStore) ?? 'null', ), - ).toMatchObject({ layoutConfig: { mode: 'right' }, appTheme: 'dark' }) + ).toMatchObject({ + layoutConfig: { mode: 'right' }, + appTheme: 'dark', + themeStyle: 'material', + }) } finally { app.unmount() disposePinia(pinia) localStorage.removeItem(APP_CATCH_KEY.appPiniaSettingStore) - style.removeProperty(property) - style.removeProperty(fadeProperty) + } + }) + + it('imports a legacy file and resets preset tokens independently of the accent', () => { + const pinia = createPinia() + + setActivePinia(pinia) + const store = piniaSettingStore() + + try { + store.updateSettingState('themeStyle', 'material') + store.changePrimaryColor('#3468db') + const { preferences, ...file } = store.exportPreferences() + const { themeStyle: _style, ...legacy } = preferences + + store.importPreferences(JSON.stringify({ ...file, preferences: legacy })) + expect(store.themeStyle).toBe('naive') + expect(store.themeOverrides.Card).toBeUndefined() + expect(store.themeOverrides.common?.primaryColor).toBe('#3468db') + + store.changePrimaryColor() + store.updateSettingState('themeStyle', 'plain') + expect(store.themeOverrides.common?.boxShadow1).toBe('none') + store.updateSettingState('themeStyle', 'modern') + expect(store.themeOverrides.common?.boxShadow1).not.toBe('none') + expect(store.themeOverrides.common?.primaryColor).toBe('#3b63d9') + } finally { + disposePinia(pinia) } }) diff --git a/apps/admin/src/app-components/provider/AppNaiveGlobalProvider/index.tsx b/apps/admin/src/app-components/provider/AppNaiveGlobalProvider/index.tsx index 3529eed6..f0841632 100644 --- a/apps/admin/src/app-components/provider/AppNaiveGlobalProvider/index.tsx +++ b/apps/admin/src/app-components/provider/AppNaiveGlobalProvider/index.tsx @@ -19,21 +19,21 @@ export default defineComponent({ const { getPrimaryColorOverride, getAppTheme, getLocaleLanguage } = useSettingGetters() - const localePackage = computed(() => { - const key = getLocaleLanguage.value + const configProviderProps = computed(() => { + const { locale, dateLocal } = getNaiveLocales(getLocaleLanguage.value) - return getNaiveLocales(key) + return { + theme: getAppTheme.value ? darkTheme : null, + themeOverrides: getPrimaryColorOverride.value, + locale, + dateLocale: dateLocal, + } }) - // 离散 API 自行挂载 Vue 应用;配置响应式更新,切换主题时无需在 render 中重新挂载。 + // 离散 API 有独立的 Vue 应用,复用同一份响应式配置。 const { message, notification, dialog, loadingBar, unmount } = createDiscreteApi(['message', 'dialog', 'notification', 'loadingBar'], { - configProviderProps: computed(() => ({ - theme: getAppTheme.value ? darkTheme : null, - themeOverrides: getPrimaryColorOverride.value, - locale: localePackage.value.locale, - dateLocale: localePackage.value.dateLocal, - })), + configProviderProps, }) window.$dialog = dialog @@ -44,32 +44,18 @@ export default defineComponent({ expose() - return { - getPrimaryColorOverride, - localePackage, - getAppTheme, - } + return { configProviderProps } }, render() { - const { - $slots: { default: slotDefault }, - } = this - const { getPrimaryColorOverride, getAppTheme, localePackage } = this - return ( - + - {slotDefault?.()} + {this.$slots.default?.()} diff --git a/apps/admin/src/app-components/provider/AppStyleProvider/index.tsx b/apps/admin/src/app-components/provider/AppStyleProvider/index.tsx index 3fbbf45c..6c371091 100644 --- a/apps/admin/src/app-components/provider/AppStyleProvider/index.tsx +++ b/apps/admin/src/app-components/provider/AppStyleProvider/index.tsx @@ -1,100 +1,44 @@ -import { APP_CATCH_KEY, APP_THEME, GLOBAL_CLASS_NAMES } from '@/app-config' +import { GLOBAL_CLASS_NAMES } from '@/app-config' import { useSettingGetters } from '@/store' -import type { SettingState } from '@/store/modules/setting/types' -import { - colorToRgba, - getStorage, - removeClass, - setClass, - setStyle, -} from '@/utils' +import { createRayThemeStyle } from '@ray-template/ui/theme-presets' import { useWindowSize } from '@vueuse/core' -import { get } from 'lodash-es' +import { useThemeVars } from 'naive-ui' export default defineComponent({ name: 'AppStyleProvider', - setup(_, { expose }) { - const { getAppTheme } = useSettingGetters() + setup() { + const { getAppTheme, getPrimaryColorOverride } = useSettingGetters() const { height, width } = useWindowSize() - - // 同步主题色变量至 html,如果未获取到缓存值则已默认值填充 - const syncPrimaryColorToBody = () => { - // 默认主题色 - const { - appPrimaryColor: { primaryColor, primaryFadeColor }, - } = APP_THEME - // 主题色配置 class 名 - const { rayTemplateThemePrimaryColor, rayTemplateThemePrimaryFadeColor } = - GLOBAL_CLASS_NAMES - - const html = document.documentElement - - // 获取缓存 naive ui 配置项 - const primaryColorOverride = getStorage( - APP_CATCH_KEY.appPiniaSettingStore, - 'localStorage', - ) - - if (primaryColorOverride) { - // 获取主色调 - const p = get( - primaryColorOverride, - 'primaryColorOverride.common.primaryColor', - primaryColor, - ) - // 将主色调任意颜色转换为 rgba 格式 - const fp = colorToRgba(p, 0.85) - - // 设置全局主题色 css 变量 - html.style.setProperty(rayTemplateThemePrimaryColor, p) // 主色调 - // 降低透明度后的主色调 - html.style.setProperty( - rayTemplateThemePrimaryFadeColor, - fp || primaryFadeColor, - ) - } - } - - // 隐藏加载动画 - const hiddenLoadingAnimation = () => { - // pre-loading-animation 是默认 id - const el = document.getElementById(GLOBAL_CLASS_NAMES.preLoadingAnimation) - - if (el) { - setStyle(el, { - display: 'none', - }) - } - } - - // 切换主题时,同步更新 html class 以便于进行自定义 css 配置 - const updateGlobalThemeClass = (bool: boolean) => { - const html = document.documentElement - const { darkClassName, lightClassName } = GLOBAL_CLASS_NAMES - - bool - ? removeClass(html, lightClassName) - : removeClass(html, darkClassName) - - setClass(html, bool ? darkClassName : lightClassName) - } - - syncPrimaryColorToBody() - hiddenLoadingAnimation() + const vars = useThemeVars() + const html = document.documentElement + const { darkClassName, lightClassName, htmlHeight, htmlWidth } = + GLOBAL_CLASS_NAMES watchEffect(() => { - // 当切换主题时,更新 html 当前的注入 class - updateGlobalThemeClass(getAppTheme.value) - // 注入全局宽高尺寸 - setStyle(document.documentElement, { - [GLOBAL_CLASS_NAMES.htmlHeight]: `${height.value}px`, - [GLOBAL_CLASS_NAMES.htmlWidth]: `${width.value}px`, - }) + html.classList.toggle(darkClassName, getAppTheme.value) + html.classList.toggle(lightClassName, !getAppTheme.value) + + const styles = createRayThemeStyle( + vars.value, + getPrimaryColorOverride.value, + ) + + for (const [name, value] of Object.entries(styles)) { + html.style.setProperty(name, value) + } }) - expose() - }, - render() { - return
+ watchEffect(() => { + html.style.setProperty(htmlHeight, `${height.value}px`) + html.style.setProperty(htmlWidth, `${width.value}px`) + }) + + const splash = document.getElementById( + GLOBAL_CLASS_NAMES.preLoadingAnimation, + ) + + if (splash) splash.style.display = 'none' + + return () => null }, }) diff --git a/apps/admin/src/app-config/app-config.ts b/apps/admin/src/app-config/app-config.ts index 041762f4..65a850f1 100644 --- a/apps/admin/src/app-config/app-config.ts +++ b/apps/admin/src/app-config/app-config.ts @@ -16,8 +16,6 @@ export const RAY_TEMPLATE_DOCS_URL = export const GLOBAL_CLASS_NAMES = { darkClassName: 'r-template--dark', lightClassName: 'r-template--light', - rayTemplateThemePrimaryColor: '--r-theme-primary-color', - rayTemplateThemePrimaryFadeColor: '--r-theme-primary-fade-color', preLoadingAnimation: 'pre-loading-animation', htmlHeight: '--html-height', htmlWidth: '--html-width', diff --git a/apps/admin/src/hooks/template/useTheme.ts b/apps/admin/src/hooks/template/useTheme.ts index e1d03572..9c0bb459 100644 --- a/apps/admin/src/hooks/template/useTheme.ts +++ b/apps/admin/src/hooks/template/useTheme.ts @@ -1,6 +1,4 @@ -import { APP_THEME } from '@/app-config' import { useSettingActions, useSettingGetters } from '@/store' -import { merge } from 'lodash-es' import { useI18n } from './useI18n' export type ThemeLabel = 'Dark' | 'Light' @@ -14,26 +12,6 @@ export interface AppThemeInfo { themeI18nLabel: string } -const setThemeOverrides = (theme: boolean) => { - const { getPrimaryColorOverride } = useSettingGetters() - const { updateSettingState } = useSettingActions() - - updateSettingState( - 'primaryColorOverride', - theme - ? merge( - {}, - getPrimaryColorOverride.value, - APP_THEME.appNaiveUIThemeOverrides.dark, - ) - : merge( - {}, - getPrimaryColorOverride.value, - APP_THEME.appNaiveUIThemeOverrides.light, - ), - ) -} - export const useTheme = () => { /** * @@ -60,9 +38,7 @@ export const useTheme = () => { const darkTheme = () => { const { updateSettingState } = useSettingActions() - updateSettingState('_appTheme', true) updateSettingState('appTheme', 'dark') - setThemeOverrides(true) } /** @@ -73,9 +49,7 @@ export const useTheme = () => { const lightTheme = () => { const { updateSettingState } = useSettingActions() - updateSettingState('_appTheme', false) updateSettingState('appTheme', 'light') - setThemeOverrides(false) } /** @@ -84,15 +58,9 @@ export const useTheme = () => { * 同步系统主题。 */ const syncSystemTheme = () => { - const { getAppTheme } = useSettingGetters() const { updateSettingState } = useSettingActions() - updateSettingState( - '_appTheme', - window.matchMedia('(prefers-color-scheme: dark)').matches, - ) updateSettingState('appTheme', 'auto') - setThemeOverrides(getAppTheme.value) } /** @@ -105,9 +73,7 @@ export const useTheme = () => { const theme = getAppTheme.value const { updateSettingState } = useSettingActions() - updateSettingState('_appTheme', !theme) updateSettingState('appTheme', !theme ? 'dark' : 'light') - setThemeOverrides(!theme) } return { diff --git a/apps/admin/src/layout/components/Menu/index.scss b/apps/admin/src/layout/components/Menu/index.scss index 480854af..5fa313b0 100644 --- a/apps/admin/src/layout/components/Menu/index.scss +++ b/apps/admin/src/layout/components/Menu/index.scss @@ -3,9 +3,6 @@ } .app-menu__sider { - --r-primary-text: var(--n-text-color); - --r-selected-text: var(--n-text-color); - border-inline-end: 1px solid var(--r-line); &--end { diff --git a/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/components/ThemeSegment.tsx b/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/components/ThemeSegment.tsx index 256e579d..4454920d 100644 --- a/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/components/ThemeSegment.tsx +++ b/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/components/ThemeSegment.tsx @@ -2,14 +2,12 @@ import { RIcon, RSegment } from '@/components' import { useI18n } from '@/hooks' import { useSettingActions, useSettingGetters } from '@/store' import type { SettingState } from '@/store/modules/setting/types' -import { useColorMode } from '@vueuse/core' export default defineComponent({ name: 'ThemeSegment', setup() { - const { system } = useColorMode() const { t } = useI18n('preferences') - const { getAppThemeStr, getAppTheme } = useSettingGetters() + const { getAppThemeStr } = useSettingGetters() const { updateSettingState } = useSettingActions() const options = computed(() => [ { @@ -29,35 +27,9 @@ export default defineComponent({ }, ]) const modelAppTheme = computed({ - get: () => { - if (getAppThemeStr.value === 'auto') { - return 'auto' - } - - return getAppTheme.value ? 'dark' : 'light' - }, - set: (val) => { - updateThemeFn(val, system.value) - }, - }) - - const updateThemeFn = ( - themeModel: SettingState['appTheme'], - systemTheme: 'dark' | 'light', - ) => { - if (themeModel === 'auto') { - updateSettingState('_appTheme', systemTheme === 'dark') - updateSettingState('appTheme', 'auto') - - return - } - - updateSettingState('_appTheme', themeModel === 'dark') - updateSettingState('appTheme', themeModel) - } - - watchEffect(() => { - updateThemeFn(modelAppTheme.value, system.value) + get: () => getAppThemeStr.value, + set: (value: SettingState['appTheme']) => + updateSettingState('appTheme', value), }) return { diff --git a/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/index.scss b/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/index.scss index 67ba8732..484a3bb8 100644 --- a/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/index.scss +++ b/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/index.scss @@ -41,7 +41,7 @@ width: 40px; height: 40px; border: 1px solid var(--r-line); - border-radius: 10px; + border-radius: var(--r-control-radius); background: var(--r-surface-muted); color: var(--r-primary-text); } @@ -72,7 +72,7 @@ > .n-tabs-nav { padding-inline: 8px; border: 1px solid var(--r-line); - border-radius: 10px; + border-radius: var(--r-control-radius); background: var(--r-surface); } @@ -91,7 +91,7 @@ .r-settings-section { padding: 16px; border: 1px solid var(--r-line); - border-radius: 12px; + border-radius: var(--r-radius); background: var(--r-surface); h3 { @@ -124,7 +124,7 @@ .r-settings-switches { display: grid; border: 1px solid var(--r-line); - border-radius: 8px; + border-radius: var(--r-control-radius); background: var(--r-surface-muted); } diff --git a/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/segment-views/Appearance.tsx b/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/segment-views/Appearance.tsx index 0341a8f1..e098612e 100644 --- a/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/segment-views/Appearance.tsx +++ b/apps/admin/src/layout/components/SiderBar/components/SettingDrawer/segment-views/Appearance.tsx @@ -1,7 +1,17 @@ import { APP_THEME, CONTENT_TRANSITION_OPTIONS } from '@/app-config' import { useI18n } from '@/hooks' import { useSettingActions, useSettingGetters } from '@/store' -import { NColorPicker, NForm, NFormItem, NSelect, NSwitch } from 'naive-ui' +import { themeStyles } from '@ray-template/ui/theme-presets' +import type { ThemeStyle } from '@ray-template/ui/theme-presets' +import { + NButton, + NColorPicker, + NForm, + NFormItem, + NSelect, + NSwitch, + useThemeVars, +} from 'naive-ui' import SettingsSection from '../components/SettingsSection' import ThemeSegment from '../components/ThemeSegment' @@ -12,7 +22,8 @@ export default defineComponent({ const { changePrimaryColor, updateSettingState, toggleColorWeakness } = useSettingActions() const { - getPrimaryColorOverride, + getThemeStyle, + getCustomPrimaryColor, getDrawerPlacement, getContentTransition, getMenuTagSwitch, @@ -20,6 +31,13 @@ export default defineComponent({ getCopyrightSwitch, getColorWeakness, } = useSettingGetters() + const themeVars = useThemeVars() + const styleOptions = computed(() => + themeStyles.map((value) => ({ + value, + label: t(`themeStyle.${value}`), + })), + ) const switches = [ ['breadcrumbSwitch', getBreadcrumbSwitch], ['menuTagSwitch', getMenuTagSwitch], @@ -36,16 +54,31 @@ export default defineComponent({ showFeedback={false} class="r-settings-fields" > + + + updateSettingState('themeStyle', value) + } + /> + { if (value) changePrimaryColor(value) }} /> + {getCustomPrimaryColor.value && ( + changePrimaryColor()}> + {t('appearance.resetColor')} + + )} { * @description * 获取 Naive UI 覆盖配置。 */ - const getPrimaryColorOverride = computed(() => variable.primaryColorOverride) + const getPrimaryColorOverride = computed(() => variable.themeOverrides) + const getCustomPrimaryColor = computed( + () => variable.primaryColorOverride.common?.primaryColor, + ) + const getThemeStyle = computed(() => variable.themeStyle) /** * * @description @@ -124,6 +128,8 @@ export const useSettingGetters = () => { getLayoutConfig, getDrawerPlacement, getPrimaryColorOverride, + getCustomPrimaryColor, + getThemeStyle, getAppTheme, getMenuTagSwitch, getBreadcrumbSwitch, diff --git a/apps/admin/src/store/modules/setting/constant.ts b/apps/admin/src/store/modules/setting/constant.ts index d85659f6..2007ac12 100644 --- a/apps/admin/src/store/modules/setting/constant.ts +++ b/apps/admin/src/store/modules/setting/constant.ts @@ -3,6 +3,7 @@ import type { LayoutConfig, SettingState } from './types' type OmitKeys = | 'layoutConfig' + | 'themeStyle' | 'contentTransition' | 'watermarkSwitch' | 'keepAliveConfig' @@ -21,6 +22,7 @@ export { LAYOUT_MODES } // SettingDrawer 默认配置系统配置项 const defaultSettingConfig: Readonly = { + themeStyle: 'naive', layoutConfig: { mode: 'side', contentWidth: 'fluid', diff --git a/apps/admin/src/store/modules/setting/index.ts b/apps/admin/src/store/modules/setting/index.ts index 591b0ec4..7f0d45d8 100644 --- a/apps/admin/src/store/modules/setting/index.ts +++ b/apps/admin/src/store/modules/setting/index.ts @@ -1,4 +1,4 @@ -import { APP_CATCH_KEY, APP_THEME, GLOBAL_CLASS_NAMES } from '@/app-config' +import { APP_CATCH_KEY, APP_THEME } from '@/app-config' import { useDayjs, useI18n } from '@/hooks' import type { LocalKey } from '@/hooks' import { resolveIconName } from '@/icons' @@ -8,31 +8,24 @@ import type { AnyFn } from '@/types' import { setStorage } from '@/utils/cache' import { colorToRgba, setStyle } from '@/utils/element' import { updateObjectValue } from '@/utils/update-object-value' +import { + normalizeThemeStyle, + themeStyleOverrides, +} from '@ray-template/ui/theme-presets' import { usePreferredDark } from '@vueuse/core' -import { cloneDeep, merge } from 'lodash-es' +import { cloneDeep } from 'lodash-es' import { getDefaultSettingConfig, normalizeLayoutConfig } from './constant' import { createPreferenceFile, parsePreferenceFile } from './preference-file' export const piniaSettingStore = defineStore( 'setting', () => { - const { - appPrimaryColor: { primaryColor, primaryFadeColor }, - } = APP_THEME const { locale } = useI18n() const { locale: dayjsLocal } = useDayjs() const preferredDark = usePreferredDark() const settingState = reactive({ - // 默认主题色 - primaryColorOverride: { - common: { - primaryColor: primaryColor, - primaryColorHover: primaryFadeColor, - primaryColorPressed: primaryColor, - primaryColorSuppl: primaryFadeColor, - }, - }, + primaryColorOverride: {}, // 内部使用,用于判断是否为黑夜主题(为了兼容历史遗留版本);true 为黑夜主题,false 为明亮主题 _appTheme: false, // 当前主题样式 @@ -70,24 +63,47 @@ export const piniaSettingStore = defineStore( * @description * 切换主题色,传递对应颜色即可更新 naive-ui 的主题色。 */ - const changePrimaryColor = (value: string, alpha = 0.85) => { - const alphaColor1 = colorToRgba(value, alpha) - const themeOverrides = { - primaryColor: value, - primaryColorHover: alphaColor1, - primaryColorPressed: value, - primaryColorSuppl: alphaColor1, + const changePrimaryColor = (value?: string, alpha = 0.85) => { + if (!value) { + settingState.primaryColorOverride = {} + + return } - const { rayTemplateThemePrimaryColor, rayTemplateThemePrimaryFadeColor } = - GLOBAL_CLASS_NAMES - const html = document.documentElement - settingState.primaryColorOverride.common = themeOverrides + const hover = colorToRgba(value, alpha) || value - // 设置主题色变量 - html.style.setProperty(rayTemplateThemePrimaryColor, value) - // 设置主题色辅助色变量 - html.style.setProperty(rayTemplateThemePrimaryFadeColor, alphaColor1) + settingState.primaryColorOverride = { + common: { + primaryColor: value, + primaryColorHover: hover, + primaryColorPressed: value, + primaryColorSuppl: hover, + }, + } + } + + const themeOverrides = computed(() => { + const mode = settingState._appTheme ? 'dark' : 'light' + const style = normalizeThemeStyle(settingState.themeStyle) + const preset = + style === 'modern' + ? APP_THEME.appNaiveUIThemeOverrides[mode] + : themeStyleOverrides[style][mode] + + return { + ...preset, + common: { + ...preset.common, + ...settingState.primaryColorOverride.common, + }, + } + }) + + const syncAppTheme = () => { + settingState._appTheme = + settingState.appTheme === 'auto' + ? preferredDark.value + : settingState.appTheme === 'dark' } /** @@ -101,7 +117,7 @@ export const piniaSettingStore = defineStore( * * @example * updateSettingState('drawerPlacement', 'left') - * updateSettingState('appTheme', true) + * updateSettingState('appTheme', 'dark') */ const updateSettingState = < T extends keyof SettingState, @@ -122,6 +138,13 @@ export const piniaSettingStore = defineStore( return } + if (key === 'themeStyle') { + settingState.themeStyle = normalizeThemeStyle(value) + cb?.() + + return + } + updateObjectValue(settingState, key, value as V[T], cb) } @@ -134,35 +157,17 @@ export const piniaSettingStore = defineStore( const importPreferences = (text: string) => { const { primaryColor, ...preferences } = parsePreferenceFile(text) - Object.assign(settingState, preferences, { - _appTheme: - preferences.appTheme === 'auto' - ? preferredDark.value - : preferences.appTheme === 'dark', - }) + Object.assign(settingState, preferences) changePrimaryColor(primaryColor) } watch( - () => (settingState.appTheme === 'auto' ? preferredDark.value : void 0), - (systemDark) => { - if (systemDark !== void 0) settingState._appTheme = systemDark - }, - { immediate: true }, - ) - - watch( - () => settingState._appTheme, - (dark) => { - settingState.primaryColorOverride = merge( - {}, - settingState.primaryColorOverride, - dark - ? APP_THEME.appNaiveUIThemeOverrides.dark - : APP_THEME.appNaiveUIThemeOverrides.light, - ) - }, - { immediate: true }, + () => + settingState.appTheme === 'auto' + ? preferredDark.value + : settingState.appTheme, + syncAppTheme, + { flush: 'sync' }, ) watch( @@ -187,6 +192,8 @@ export const piniaSettingStore = defineStore( return { ...toRefs(settingState), + themeOverrides, + syncAppTheme, updateLocale, changePrimaryColor, updateSettingState, @@ -202,6 +209,21 @@ export const piniaSettingStore = defineStore( storage: window.localStorage, afterHydrate: ({ store }) => { store.layoutConfig = normalizeLayoutConfig(store.layoutConfig) + store.themeStyle = normalizeThemeStyle(store.themeStyle) + store.syncAppTheme() + + // 旧缓存混有整套主题 token;只保留用户设置的主色,避免污染其它风格。 + const common = store.primaryColorOverride?.common + const { primaryColor, primaryFadeColor } = APP_THEME.appPrimaryColor + const wasDefault = + common?.bodyColor && + common.primaryColor === primaryColor && + common.primaryColorHover === primaryFadeColor + const accent = common?.primaryColor + + store.changePrimaryColor( + !wasDefault && typeof accent === 'string' ? accent : void 0, + ) if (typeof store.sideBarLogo?.icon === 'string') { store.sideBarLogo.icon = resolveIconName(store.sideBarLogo.icon) diff --git a/apps/admin/src/store/modules/setting/preference-file.ts b/apps/admin/src/store/modules/setting/preference-file.ts index fb328bcb..cec9acdf 100644 --- a/apps/admin/src/store/modules/setting/preference-file.ts +++ b/apps/admin/src/store/modules/setting/preference-file.ts @@ -1,4 +1,5 @@ -import { APP_THEME, CONTENT_TRANSITION_OPTIONS } from '@/app-config' +import { CONTENT_TRANSITION_OPTIONS } from '@/app-config' +import { themeStyles } from '@ray-template/ui/theme-presets' import { pick } from 'lodash-es' import * as v from 'valibot' import { LAYOUT_MODES } from './constant' @@ -37,17 +38,20 @@ const preferences = v.strictObject({ fixedFooter: flag, }), appTheme: v.picklist(['light', 'dark', 'auto']), - primaryColor: v.pipe( - v.string(), - v.maxLength(64), - v.regex(/^(?:#[\da-f]+|(?:rgba?|hsla?)\([\d.,%+\s/-]+\))$/i), - v.check((value) => { - const { style } = document.createElement('span') + themeStyle: v.optional(v.picklist(themeStyles), 'naive'), + primaryColor: v.optional( + v.pipe( + v.string(), + v.maxLength(64), + v.regex(/^(?:#[\da-f]+|(?:rgba?|hsla?)\([\d.,%+\s/-]+\))$/i), + v.check((value) => { + const { style } = document.createElement('span') - style.color = value + style.color = value - return style.color !== '' - }), + return style.color !== '' + }), + ), ), contentTransition: v.picklist( CONTENT_TRANSITION_OPTIONS.map(({ value }) => value), @@ -91,9 +95,7 @@ export function createPreferenceFile(state: SettingState) { version: 1, preferences: { ...pick(state, Object.keys(preferences.entries)), - primaryColor: - state.primaryColorOverride.common?.primaryColor ?? - APP_THEME.appPrimaryColor.primaryColor, + primaryColor: state.primaryColorOverride.common?.primaryColor, }, }) } diff --git a/apps/admin/src/store/modules/setting/types.ts b/apps/admin/src/store/modules/setting/types.ts index ab184098..9d9148e4 100644 --- a/apps/admin/src/store/modules/setting/types.ts +++ b/apps/admin/src/store/modules/setting/types.ts @@ -5,6 +5,7 @@ import type { Placement, } from '@/types' import type { LayoutConfig } from '@ray-template/admin-shell' +import type { ThemeStyle } from '@ray-template/ui/theme-presets' import type { GlobalThemeOverrides } from 'naive-ui' export interface WatermarkConfig { @@ -32,6 +33,7 @@ export interface SettingState { layoutConfig: LayoutConfig drawerPlacement: Placement primaryColorOverride: GlobalThemeOverrides + themeStyle: ThemeStyle appTheme: 'dark' | 'light' | 'auto' _appTheme: boolean menuTagSwitch: boolean diff --git a/apps/docs/examples/DemoFrame.vue b/apps/docs/examples/DemoFrame.vue index bf09686b..c8a493ae 100644 --- a/apps/docs/examples/DemoFrame.vue +++ b/apps/docs/examples/DemoFrame.vue @@ -5,7 +5,12 @@ :locale="locale === 'zh-CN' ? zhCN : undefined" :dateLocale="locale === 'zh-CN' ? dateZhCN : undefined" > -
+
@@ -14,10 +19,16 @@ diff --git a/apps/docs/guide/appearance.md b/apps/docs/guide/appearance.md index 5632e0a9..611bd799 100644 --- a/apps/docs/guide/appearance.md +++ b/apps/docs/guide/appearance.md @@ -2,9 +2,25 @@ ## 主题与样式 -默认主题色由 `apps/admin/src/app-config/design-config.ts` 管理;主题和语言由应用 Provider 同步到 Naive UI。新增组件时优先使用现有设计变量、组件库配置和局部样式。必须覆盖 Naive UI 内部样式时,以自己的组件类限定作用范围。 +在「偏好设置 → 外观 → 界面风格」切换。风格与浅色、深色、跟随系统独立;切换即时生效,刷新和偏好导入导出会保留选择。 -全局 Sass 辅助文件通过 `vite.custom.config.ts` 注入;CSS Modules 的名称以 camelCase 暴露。图表 options 由页面维护,`RChart` / `useChart` 负责实例生命周期、尺寸与主题同步;使用新增的 ECharts 图表类型时,按现有注册入口加载依赖。 +| 风格 | 外观 | +| ------------- | -------------------------------------- | +| Naive UI 默认 | 使用组件库原生 token,初次使用选中此项 | +| 现代化 | Ray 蓝、低饱和底色、适度圆角和留白 | +| Material | 紫色调、较大的圆角、胶囊按钮和分层表面 | +| 朴素 | 中性灰、小圆角和无阴影表面 | + +手动选择的主题色优先于预设,并随风格保留;点击「使用风格默认色」可恢复当前风格的配色。历史设置会补齐默认风格并保留曾手动选择的主色。 + +### 样式放在哪里 + +- Naive UI 组件的颜色、圆角、密度和阴影走 [`theme-overrides`](https://www.naiveui.com/zh-CN/docs/customize-theme)。四套预设定义在 `packages/ui/src/theme-presets.ts`。每次用当前风格、明暗模式和用户主色计算配置,页面与离散消息、通知和弹窗共享同一份配置。 +- 自有导航、标签页和工作区使用 `packages/ui/src/theme.css` 的 `--r-*` 语义变量。后台把 `useThemeVars()` 解析出的值映射到根节点,文档示例把相同映射限制在示例容器。自有元素使用这套变量,以便和组件库一起换肤。 +- 全局 Sass 保留 reset、工具和历史表单宽度约定。组件 SCSS 负责布局、滚动和交互;仅在组件库没有合适 API 时,用自有类限定内部结构选择器的范围。不要在全局选择器中给 `.n-*` 写配色或直接依赖 `--n-*` 来切换主题。 +- 首屏启动样式内联于 `apps/admin/index.html`,用于应用挂载前显示品牌、启动状态和明暗模式。图表配色仍由 ECharts 主题和页面 options 管理。 + +全局 Sass 辅助文件通过 `vite.custom.config.ts` 注入;CSS Modules 的名称以 camelCase 暴露。 ## 中英文同时维护 diff --git a/apps/docs/reference/configuration.md b/apps/docs/reference/configuration.md index 570c1198..3fad804a 100644 --- a/apps/docs/reference/configuration.md +++ b/apps/docs/reference/configuration.md @@ -5,7 +5,7 @@ | 文件 | 配置 | | --------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `apps/admin/src/app-config/app-config.ts` | `GLOBAL_CLASS_NAMES`(明暗类与 CSS 变量)、`MESSAGE_PROVIDER`(上下文消息,默认最多 5 条、不可关闭)、`LAYOUT_CONTENT_SPIN_WHEN_ROUTE_CHANGE`(默认关)、`APP_GLOBAL_LOADING`、`PRE_LOADING_CONFIG`、`APP_CATCH_KEY` / `APP_CATCH_KEY_PREFIX`、`CONTENT_TRANSITION_OPTIONS`。 | -| `apps/admin/src/app-config/design-config.ts` | `APP_THEME` 的候选色、默认蓝 `#3b63d9`、Naive UI 明暗覆盖、ECharts 默认主题。 | +| `apps/admin/src/app-config/design-config.ts` | `APP_THEME` 的候选色、构建品牌蓝 `#3b63d9`、现代化预设的明暗覆盖、ECharts 默认主题。四套预设在 `packages/ui/src/theme-presets.ts`。 | | `apps/admin/src/app-config/local-config.ts` | `LOCAL_OPTIONS`、`SYSTEM_DEFAULT_LOCAL`、`SYSTEM_FALLBACK_LOCALE`、`DAYJS_LOCAL_MAP`、`DEFAULT_DAYJS_LOCAL`。默认与回退语言为 `zh-CN`。 | | `apps/admin/src/app-config/request-config.ts` | `AXIOS_CONFIG`。默认同源相对地址、5 秒超时、`withCredentials: false`、JSON Content-Type。 | | `apps/admin/src/app-config/router-config.ts` | `SETUP_ROUTER_ACTION`(加载条与守卫默认开)、`WHITE_ROUTES`(路由名白名单)、`SUPER_ADMIN`(默认空)、`LAYOUT_CONTENT_REF` / `LAYOUT_SIDER_REF`。 | diff --git a/apps/docs/reference/store/setting.md b/apps/docs/reference/store/setting.md index b570a4ef..8659d932 100644 --- a/apps/docs/reference/store/setting.md +++ b/apps/docs/reference/store/setting.md @@ -8,7 +8,8 @@ | ---------------------------------------------- | ------------------------------------------------------------------------------------------ | | `getLayoutConfig` | `layoutConfig`,布局模式和工作区尺寸,见下表。 | | `getDrawerPlacement` | `drawerPlacement`,设置抽屉方向,默认 `right`。 | -| `getPrimaryColorOverride` | `primaryColorOverride`,Naive UI 主题覆盖;默认主色 `#3b63d9`。 | +| `getPrimaryColorOverride` | Naive UI 生效的 token 覆盖,由当前风格、明暗和用户主色计算。 | +| `getThemeStyle` / `getCustomPrimaryColor` | 界面风格与手动主色。默认 `naive`,未指定手动主色时使用风格原色。 | | `getAppTheme` / `getAppThemeStr` | `_appTheme` 为实际深色布尔值,`appTheme` 为 `light/dark/auto` 模式,初始 `light`。 | | `getLocaleLanguage` | `localeLanguage`,应用语言,默认读取历史值或系统默认配置。 | | `getMenuTagSwitch` / `getBreadcrumbSwitch` | `menuTagSwitch` / `breadcrumbSwitch`,默认显示标签和面包屑。 | @@ -32,7 +33,7 @@ | ------------------------------------------- | --------------------------------------------------------------------------------------- | | `updateSettingState(key, value, callback?)` | 更新已有字段。普通对象浅合并;`layoutConfig` 额外做格式与范围校验。成功后调用可选回调。 | | `updateLocale(key)` | 更新 Vue I18n、Day.js、语言缓存和 Store,Naive UI 从 Store 同步语言。 | -| `changePrimaryColor(value, alpha = 0.85)` | 更新 Naive UI 主色、hover/pressed/supplement 和 HTML CSS 变量。 | +| `changePrimaryColor(value?, alpha = 0.85)` | 覆盖当前风格的主色;省略 value 恢复当前风格原色。 | | `toggleColorWeakness(bool)` | 更新设置并切换 HTML 反色滤镜。 | 切换主题使用 [useTheme](../hooks/appearance#usetheme),切换首页使用 [useAppRoot](../hooks/navigation#useapproot)。修改嵌套配置时传入完整的下一级对象,浅合并不会递归保存更深层字段。 diff --git a/packages/admin-shell/src/Tabs.scss b/packages/admin-shell/src/Tabs.scss index 492b06ac..947c3f37 100644 --- a/packages/admin-shell/src/Tabs.scss +++ b/packages/admin-shell/src/Tabs.scss @@ -30,7 +30,7 @@ min-height: 28px; overflow: hidden; border: 1px solid transparent; - border-radius: 7px; + border-radius: var(--r-control-radius); background: var(--r-surface-muted); color: var(--r-text-secondary); @@ -86,7 +86,7 @@ block-size: 26px; padding: 0; overflow: hidden; - border-radius: 5px; + border-radius: var(--r-control-radius); opacity: 0; pointer-events: none; transition: @@ -98,7 +98,7 @@ position: absolute; z-index: -1; inset: 3px; - border-radius: 5px; + border-radius: var(--r-control-radius); background: currentColor; opacity: 0; transition: diff --git a/packages/admin-shell/src/shell.scss b/packages/admin-shell/src/shell.scss index a8066e22..2e968eb8 100644 --- a/packages/admin-shell/src/shell.scss +++ b/packages/admin-shell/src/shell.scss @@ -91,7 +91,7 @@ inline-size: 36px; block-size: 36px; border: 1px solid var(--r-control-border); - border-radius: 8px; + border-radius: var(--r-control-radius); color: var(--r-text); background: var(--r-surface); cursor: pointer; diff --git a/packages/ui/src/theme-presets.ts b/packages/ui/src/theme-presets.ts index cba98ca0..3ee86bc8 100644 --- a/packages/ui/src/theme-presets.ts +++ b/packages/ui/src/theme-presets.ts @@ -1,36 +1,32 @@ -/** 明暗主题共用的菜单尺寸与前景色配置。 */ -const menuThemeOverrides = { - itemHeight: '40px', - borderRadius: '8px', - itemTextColorActive: 'var(--r-selected-text)', - itemTextColorActiveHover: 'var(--r-selected-text)', - itemTextColorChildActive: 'var(--r-selected-text)', - itemTextColorChildActiveHover: 'var(--r-selected-text)', - itemTextColorActiveHorizontal: 'var(--r-selected-text)', - itemTextColorChildActiveHorizontal: 'var(--r-selected-text)', - itemTextColorHoverHorizontal: 'var(--r-selected-text)', - itemIconColorActive: 'var(--r-selected-text)', - itemIconColorActiveHover: 'var(--r-selected-text)', - itemIconColorChildActive: 'var(--r-selected-text)', - itemIconColorChildActiveHover: 'var(--r-selected-text)', - itemIconColorActiveHorizontal: 'var(--r-selected-text)', - itemIconColorChildActiveHorizontal: 'var(--r-selected-text)', -} +import type { GlobalThemeOverrides, useThemeVars } from 'naive-ui' export const rayPrimaryColor = { primaryColor: '#3b63d9', primaryFadeColor: 'rgba(59, 99, 217, 0.85)', } +type ThemePalette = Record<'light' | 'dark', GlobalThemeOverrides> + +const modernComponents = { + Card: { + borderRadius: '12px', + titleFontSizeMedium: '16px', + paddingMedium: '20px 24px', + }, + Menu: { itemHeight: '40px', borderRadius: '8px' }, + Button: { fontWeight: '500' }, + Tag: { borderRadius: '5px' }, +} satisfies GlobalThemeOverrides + export const rayThemeOverrides = { dark: { + ...modernComponents, common: { borderRadius: '8px', fontWeightStrong: '600', lineHeight: '1.6', heightMedium: '36px', heightSmall: '30px', - baseColor: '#172131', bodyColor: '#101722', cardColor: '#172131', modalColor: '#192638', @@ -43,25 +39,22 @@ export const rayThemeOverrides = { borderColor: '#687b96', dividerColor: '#2c394d', hoverColor: 'rgba(179, 192, 211, 0.08)', + primaryColor: '#aabaff', + primaryColorHover: '#c4ceff', + primaryColorPressed: '#96a9fa', + primaryColorSuppl: '#aabaff', + boxShadow1: '0 1px 2px rgb(0 0 0 / 10%), 0 8px 24px rgb(0 0 0 / 10%)', }, - Card: { - borderRadius: '12px', - titleFontSizeMedium: '16px', - paddingMedium: '20px 24px', - }, - Menu: menuThemeOverrides, - Button: { fontWeight: '500' }, - Tag: { borderRadius: '5px', textColorPrimary: 'var(--r-primary-text)' }, Layout: { siderColor: '#172131', headerColor: '#172131' }, }, light: { + ...modernComponents, common: { borderRadius: '8px', fontWeightStrong: '600', lineHeight: '1.6', heightMedium: '36px', heightSmall: '30px', - baseColor: '#ffffff', bodyColor: '#f5f7fb', cardColor: '#ffffff', modalColor: '#ffffff', @@ -74,15 +67,148 @@ export const rayThemeOverrides = { borderColor: '#8492a8', dividerColor: '#e4e9f1', hoverColor: 'rgba(82, 100, 124, 0.06)', + primaryColor: rayPrimaryColor.primaryColor, + primaryColorHover: '#4b70df', + primaryColorPressed: '#2b50be', + primaryColorSuppl: rayPrimaryColor.primaryColor, + boxShadow1: '0 1px 2px rgb(25 43 74 / 3%), 0 6px 20px rgb(25 43 74 / 3%)', }, - Card: { - borderRadius: '12px', - titleFontSizeMedium: '16px', - paddingMedium: '20px 24px', - }, - Menu: menuThemeOverrides, - Button: { fontWeight: '500' }, - Tag: { borderRadius: '5px', textColorPrimary: 'var(--r-primary-text)' }, Layout: { siderColor: '#ffffff', headerColor: '#ffffff' }, }, +} satisfies ThemePalette + +const materialComponents = { + Card: { borderRadius: '20px' }, + Button: { + fontWeight: '500', + borderRadiusSmall: '999px', + borderRadiusMedium: '999px', + borderRadiusLarge: '999px', + }, + Menu: { itemHeight: '44px', borderRadius: '24px' }, + Tag: { borderRadius: '8px' }, + DataTable: { borderRadius: '16px' }, +} satisfies GlobalThemeOverrides + +const plainComponents = { + Card: { borderRadius: '2px' }, + Menu: { itemHeight: '36px', borderRadius: '2px' }, + Tag: { borderRadius: '2px' }, +} satisfies GlobalThemeOverrides + +export const themeStyles = ['naive', 'modern', 'material', 'plain'] as const +export type ThemeStyle = (typeof themeStyles)[number] + +export const normalizeThemeStyle = (value: unknown): ThemeStyle => + themeStyles.find((style) => style === value) ?? 'naive' + +export const themeStyleOverrides: Record = { + naive: { light: {}, dark: {} }, + modern: rayThemeOverrides, + material: { + light: { + ...materialComponents, + common: { + primaryColor: '#655292', + primaryColorHover: '#755fa4', + primaryColorPressed: '#514178', + primaryColorSuppl: '#655292', + borderRadius: '12px', + heightMedium: '40px', + bodyColor: '#f5f1fa', + cardColor: '#fffaff', + actionColor: '#efe8f7', + popoverColor: '#fffaff', + modalColor: '#fffaff', + textColor1: '#201a29', + textColor2: '#433a50', + textColor3: '#61566c', + borderColor: '#8c8199', + dividerColor: '#e1d8ea', + boxShadow1: '0 2px 6px rgb(46 31 65 / 14%)', + }, + Layout: { siderColor: '#fffaff', headerColor: '#fffaff' }, + }, + dark: { + ...materialComponents, + common: { + primaryColor: '#d6bbff', + primaryColorHover: '#e4d1ff', + primaryColorPressed: '#bf9ff1', + primaryColorSuppl: '#d6bbff', + borderRadius: '12px', + heightMedium: '40px', + bodyColor: '#16121c', + cardColor: '#241e2c', + actionColor: '#302838', + popoverColor: '#2b2433', + modalColor: '#2b2433', + textColor1: '#f1e7fa', + textColor2: '#ded0e8', + textColor3: '#b9a9c4', + borderColor: '#867791', + dividerColor: '#473c52', + }, + Layout: { siderColor: '#241e2c', headerColor: '#241e2c' }, + }, + }, + plain: { + light: { + ...plainComponents, + common: { + primaryColor: '#454545', + primaryColorHover: '#555555', + primaryColorPressed: '#292929', + primaryColorSuppl: '#454545', + borderRadius: '2px', + bodyColor: '#f5f5f5', + actionColor: '#eeeeee', + boxShadow1: 'none', + boxShadow2: 'none', + boxShadow3: 'none', + }, + }, + dark: { + ...plainComponents, + common: { + primaryColor: '#d5d5d5', + primaryColorHover: '#ededed', + primaryColorPressed: '#bdbdbd', + primaryColorSuppl: '#d5d5d5', + borderRadius: '2px', + bodyColor: '#161616', + cardColor: '#202020', + popoverColor: '#252525', + modalColor: '#252525', + actionColor: '#2b2b2b', + boxShadow1: 'none', + boxShadow2: 'none', + boxShadow3: 'none', + }, + Layout: { siderColor: '#202020', headerColor: '#202020' }, + }, + }, } + +export const createRayThemeStyle = ( + vars: ReturnType['value'], + overrides: GlobalThemeOverrides, +): Record => ({ + '--r-token-primary': vars.primaryColor, + '--r-token-primary-hover': vars.primaryColorHover, + '--r-token-canvas': vars.bodyColor, + '--r-token-surface': vars.cardColor, + '--r-token-surface-muted': vars.actionColor, + '--r-token-surface-hover': `color-mix(in srgb, ${vars.textColor1} 9%, ${vars.cardColor})`, + '--r-token-divider': vars.dividerColor, + '--r-token-border': vars.borderColor, + '--r-token-text': vars.textColor1, + '--r-token-text-secondary': vars.textColor2, + '--r-token-text-muted': vars.textColor3, + '--r-token-shadow': vars.boxShadow1, + '--r-token-radius': + typeof overrides.Card?.borderRadius === 'string' + ? overrides.Card.borderRadius + : vars.borderRadius, + '--r-token-control-radius': vars.borderRadius, +}) diff --git a/packages/ui/src/theme.css b/packages/ui/src/theme.css index 18d21d8b..8ea73848 100644 --- a/packages/ui/src/theme.css +++ b/packages/ui/src/theme.css @@ -1,42 +1,44 @@ :root { - --r-theme-primary-color: #3b63d9; + color-scheme: light dark; +} + +:root, +.r-theme-scope { + --r-theme-primary-color: var(--r-token-primary, Highlight); + --r-theme-primary-fade-color: var(--r-token-primary-hover, Highlight); --r-bezier: cubic-bezier(0.4, 0, 0.2, 1); --r-primary-text: var(--r-theme-primary-color); --r-selected-text: var(--r-theme-primary-color); - --r-canvas: #f5f7fb; - --r-surface: #ffffff; - --r-surface-muted: #f7f9fc; - --r-surface-hover: #eef2f8; - --r-line: #e4e9f1; - --r-control-border: #8492a8; - --r-text: #1d2a3e; - --r-text-secondary: #52647c; - --r-text-muted: #617187; - --r-shadow-soft: 0 1px 2px rgb(25 43 74 / 3%), 0 6px 20px rgb(25 43 74 / 3%); - --r-radius: 12px; + --r-canvas: var(--r-token-canvas, Canvas); + --r-surface: var(--r-token-surface, Canvas); + --r-surface-muted: var(--r-token-surface-muted, Canvas); + --r-surface-hover: var(--r-token-surface-hover, Canvas); + --r-line: var(--r-token-divider, ButtonBorder); + --r-control-border: var(--r-token-border, ButtonBorder); + --r-text: var(--r-token-text, CanvasText); + --r-text-secondary: var(--r-token-text-secondary, CanvasText); + --r-text-muted: var(--r-token-text-muted, CanvasText); + --r-shadow-soft: var(--r-token-shadow, none); + --r-control-radius: var(--r-token-control-radius, 4px); + --r-radius: var(--r-token-radius, 4px); --r-font-mono: 'SFMono-Regular', Consolas, 'Liberation Mono', monospace; +} + +.r-template--light { color-scheme: light; } .r-template--dark { --r-primary-text: var(--r-text-secondary); --r-selected-text: var(--r-text); - --r-canvas: #101722; - --r-surface: #172131; - --r-surface-muted: #1c293b; - --r-surface-hover: #243247; - --r-line: #2c394d; - --r-control-border: #687b96; - --r-text: #e9eef7; - --r-text-secondary: #b3c0d3; - --r-text-muted: #a0b0c6; - --r-shadow-soft: 0 1px 2px rgb(0 0 0 / 10%), 0 8px 24px rgb(0 0 0 / 10%); color-scheme: dark; } @media (forced-colors: active) { :root, + .r-theme-scope, .r-template--dark { + --r-theme-primary-color: Highlight; --r-primary-text: CanvasText; --r-selected-text: CanvasText; --r-canvas: Canvas;