Skip to content

Commit

Permalink
feat: support customize accent colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Jul 21, 2023
1 parent 31e943a commit 6da8325
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
28 changes: 28 additions & 0 deletions example/color.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
30 changes: 19 additions & 11 deletions src/app/config.d.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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[]
Expand Down
17 changes: 13 additions & 4 deletions src/providers/root/accent-color-provider.tsx
Original file line number Diff line number Diff line change
@@ -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 = [
// 浅葱
Expand All @@ -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)
Expand All @@ -35,12 +42,14 @@ export const AccentColorProvider = ({ children }: PropsWithChildren) => {

return (
<style
data-light={accentColorL}
data-dark={accentColorD}
dangerouslySetInnerHTML={{
__html: `html[data-theme='dark'] {
__html: `html[data-theme='light'] {
--a: ${`${hl} ${sl}% ${ll}%`};
--af: ${`${hl} ${sl}% ${ll + 6}%`};
}
html[data-theme='light'] {
html[data-theme='dark'] {
--a: ${`${hd} ${sd}% ${ld}%`};
--af: ${`${hd} ${sd}% ${ld - 6}%`};
}
Expand Down

1 comment on commit 6da8325

@vercel
Copy link

@vercel vercel bot commented on 6da8325 Jul 21, 2023

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:

shiro – ./

springtide.vercel.app
shiro-innei.vercel.app
shiro-git-main-innei.vercel.app
innei.in

Please sign in to comment.