-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for CSS variables in UnoCSS references
- Loading branch information
Showing
8 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import subscribeThemeStore from "./theme"; | ||
|
||
/** | ||
* Subscribe to the store. | ||
* 订阅 store。 | ||
*/ | ||
export function subscribeStore() { | ||
subscribeThemeStore(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { colorToRgb, generateColorPalettes, setCssVariable } from "@celeris/utils"; | ||
import { effectScope, onScopeDispose, watch } from "vue"; | ||
import type { GlobalThemeOverrides } from "naive-ui"; | ||
import { kebabCase } from "lodash-es"; | ||
import { useDesignStore } from "~/store/modules/design"; | ||
|
||
/** | ||
* 订阅NaiveUI自定义主题的变化 | ||
* Subscribe to changes in the NaiveUI custom theme | ||
*/ | ||
export default function subscribeThemeStore() { | ||
const designStore = useDesignStore(); | ||
const scope = effectScope(); | ||
|
||
scope.run(() => { | ||
watch( | ||
() => designStore.getNaiveUICustomTheme, | ||
(newTheme) => { | ||
if (newTheme?.common) { | ||
addThemeCssVariablesToHtml(newTheme?.common); | ||
} | ||
}, | ||
{ immediate: true }, | ||
); | ||
}); | ||
/** | ||
* 组件销毁时清理effect scope | ||
* Clean up the effect scope when the component is destroyed | ||
*/ | ||
onScopeDispose(() => { | ||
scope.stop(); | ||
}); | ||
} | ||
|
||
type ThemeVars = Exclude<GlobalThemeOverrides["common"], undefined>; | ||
type ThemeVarsKeys = keyof ThemeVars; | ||
|
||
/** | ||
* 将主题颜色变量添加到HTML中 | ||
* Add theme color variables to HTML | ||
* @param {ThemeVars} themeVars - 主题变量对象 | ||
*/ | ||
function addThemeCssVariablesToHtml(themeVars: ThemeVars) { | ||
for (const [key, color] of Object.entries(themeVars) as [ThemeVarsKeys, string][]) { | ||
if (color) { | ||
const { r, g, b } = colorToRgb(color); | ||
setCssVariable(`--${kebabCase(key)}`, `${r},${g},${b}`); | ||
if (key === "primaryColor") { | ||
const colorPalettes = generateColorPalettes(color); | ||
|
||
for (let index = 0; index < colorPalettes.length; index++) { | ||
const palette = colorPalettes[index]; | ||
const { r: pR, g: pG, b: pB } = colorToRgb(palette); | ||
setCssVariable(`--${kebabCase(key)}${index + 1}`, `${pR},${pG},${pB}`); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import { colord } from "colord"; | ||
import type { RgbaColor } from "colord/types"; | ||
|
||
export function isWhiteColor(color: string) { | ||
return colord(color).isEqual("#ffffff"); | ||
} | ||
|
||
export function isBlackColor(color: string) { | ||
return colord(color).isEqual("#000000"); | ||
} | ||
|
||
export function colorToRgb(color: string): RgbaColor { | ||
return colord(color).toRgb(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cdebe37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
celeris-web-api – ./services/admin
celeris-web-api-git-master-kirklin.vercel.app
celeris-web-api.vercel.app
celeris-web-api-kirklin.vercel.app
cdebe37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
celeris-web – ./apps/admin
celeris-web-kirklin.vercel.app
celeris-web-git-master-kirklin.vercel.app
celeris-web.vercel.app