Skip to content

Commit 6da8325

Browse files
committed
feat: support customize accent colors
1 parent 31e943a commit 6da8325

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

example/color.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"color": {
3+
"light": [
4+
"#33A6B8",
5+
"#FF6666",
6+
"#26A69A",
7+
"#fb7287",
8+
"#69a6cc",
9+
"#F11A7B",
10+
"#78C1F3",
11+
"#FF6666",
12+
"#FF9EAA",
13+
"#7ACDF6"
14+
],
15+
"dark": [
16+
"#F596AA",
17+
"#A0A7D4",
18+
"#ff7b7b",
19+
"#99D8CF",
20+
"#838BC6",
21+
"#FFE5AD",
22+
"#9BE8D8",
23+
"#A1CCD1",
24+
"#FFD0D0",
25+
"#EAAEBA"
26+
]
27+
}
28+
}

src/app/config.d.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
import type { ScriptProps } from 'next/script'
22

3+
export interface AppThemeConfig {
4+
config: AppConfig
5+
footer: FooterConfig
6+
}
7+
8+
export interface AccentColor {
9+
light: string[]
10+
dark: string[]
11+
}
12+
13+
export interface AppConfig {
14+
site: Site
15+
hero: Hero
16+
module: Module
17+
color?: AccentColor
18+
19+
custom?: Custom
20+
}
21+
322
export interface LinkSection {
423
name: string
524
links: {
@@ -16,18 +35,7 @@ export interface OtherInfo {
1635
link: string
1736
}
1837
}
19-
export interface AppThemeConfig {
20-
config: AppConfig
21-
footer: FooterConfig
22-
}
2338

24-
export interface AppConfig {
25-
site: Site
26-
hero: Hero
27-
module: Module
28-
29-
custom?: Custom
30-
}
3139
export interface Custom {
3240
css: string[]
3341
styles: any[]

src/providers/root/accent-color-provider.tsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { useServerInsertedHTML } from 'next/navigation'
2+
import type { AccentColor } from '~/app/config'
23
import type { PropsWithChildren } from 'react'
34

45
import { sample } from '~/lib/_'
56
import { hexToHsl } from '~/lib/color'
7+
import { noopObj } from '~/lib/noop'
8+
9+
import { useAppConfigSelector } from './aggregation-data-provider'
610

711
const accentColorLight = [
812
// 浅葱
@@ -22,10 +26,13 @@ const accentColorDark = [
2226
'#99D8CF',
2327
'#838BC6',
2428
]
29+
2530
export const AccentColorProvider = ({ children }: PropsWithChildren) => {
31+
const { light, dark } =
32+
useAppConfigSelector((config) => config.color) || (noopObj as AccentColor)
2633
useServerInsertedHTML(() => {
27-
const accentColorL = sample(accentColorLight)
28-
const accentColorD = sample(accentColorDark)
34+
const accentColorL = sample(light ?? accentColorLight)
35+
const accentColorD = sample(dark ?? accentColorDark)
2936

3037
const lightHsl = hexToHsl(accentColorL)
3138
const darkHsl = hexToHsl(accentColorD)
@@ -35,12 +42,14 @@ export const AccentColorProvider = ({ children }: PropsWithChildren) => {
3542

3643
return (
3744
<style
45+
data-light={accentColorL}
46+
data-dark={accentColorD}
3847
dangerouslySetInnerHTML={{
39-
__html: `html[data-theme='dark'] {
48+
__html: `html[data-theme='light'] {
4049
--a: ${`${hl} ${sl}% ${ll}%`};
4150
--af: ${`${hl} ${sl}% ${ll + 6}%`};
4251
}
43-
html[data-theme='light'] {
52+
html[data-theme='dark'] {
4453
--a: ${`${hd} ${sd}% ${ld}%`};
4554
--af: ${`${hd} ${sd}% ${ld - 6}%`};
4655
}

0 commit comments

Comments
 (0)