From 6da83255f4b3405c872119e4621aba7768d3e423 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 21 Jul 2023 15:11:33 +0800 Subject: [PATCH] feat: support customize accent colors --- example/color.json | 28 ++++++++++++++++++ src/app/config.d.ts | 30 +++++++++++++------- src/providers/root/accent-color-provider.tsx | 17 ++++++++--- 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 example/color.json diff --git a/example/color.json b/example/color.json new file mode 100644 index 0000000000..636b50c052 --- /dev/null +++ b/example/color.json @@ -0,0 +1,28 @@ +{ + "color": { + "light": [ + "#33A6B8", + "#FF6666", + "#26A69A", + "#fb7287", + "#69a6cc", + "#F11A7B", + "#78C1F3", + "#FF6666", + "#FF9EAA", + "#7ACDF6" + ], + "dark": [ + "#F596AA", + "#A0A7D4", + "#ff7b7b", + "#99D8CF", + "#838BC6", + "#FFE5AD", + "#9BE8D8", + "#A1CCD1", + "#FFD0D0", + "#EAAEBA" + ] + } +} diff --git a/src/app/config.d.ts b/src/app/config.d.ts index ee27c8cde1..6b1cfe3818 100644 --- a/src/app/config.d.ts +++ b/src/app/config.d.ts @@ -1,5 +1,24 @@ import type { ScriptProps } from 'next/script' +export interface AppThemeConfig { + config: AppConfig + footer: FooterConfig +} + +export interface AccentColor { + light: string[] + dark: string[] +} + +export interface AppConfig { + site: Site + hero: Hero + module: Module + color?: AccentColor + + custom?: Custom +} + export interface LinkSection { name: string links: { @@ -16,18 +35,7 @@ export interface OtherInfo { link: string } } -export interface AppThemeConfig { - config: AppConfig - footer: FooterConfig -} -export interface AppConfig { - site: Site - hero: Hero - module: Module - - custom?: Custom -} export interface Custom { css: string[] styles: any[] diff --git a/src/providers/root/accent-color-provider.tsx b/src/providers/root/accent-color-provider.tsx index 212af8f4bb..0228b00fea 100644 --- a/src/providers/root/accent-color-provider.tsx +++ b/src/providers/root/accent-color-provider.tsx @@ -1,8 +1,12 @@ import { useServerInsertedHTML } from 'next/navigation' +import type { AccentColor } from '~/app/config' import type { PropsWithChildren } from 'react' import { sample } from '~/lib/_' import { hexToHsl } from '~/lib/color' +import { noopObj } from '~/lib/noop' + +import { useAppConfigSelector } from './aggregation-data-provider' const accentColorLight = [ // 浅葱 @@ -22,10 +26,13 @@ const accentColorDark = [ '#99D8CF', '#838BC6', ] + export const AccentColorProvider = ({ children }: PropsWithChildren) => { + const { light, dark } = + useAppConfigSelector((config) => config.color) || (noopObj as AccentColor) useServerInsertedHTML(() => { - const accentColorL = sample(accentColorLight) - const accentColorD = sample(accentColorDark) + const accentColorL = sample(light ?? accentColorLight) + const accentColorD = sample(dark ?? accentColorDark) const lightHsl = hexToHsl(accentColorL) const darkHsl = hexToHsl(accentColorD) @@ -35,12 +42,14 @@ export const AccentColorProvider = ({ children }: PropsWithChildren) => { return (