@@ -13,6 +13,7 @@ import {
1313import { SearchProvider } from '@/components/docs/search-provider' ;
1414import { GoogleAnalytics } from '@/components/docs/google-analytics' ;
1515import { sanitizeSvg } from '@/lib/sanitize-svg' ;
16+ import { primaryThemeCss } from '@/lib/primary-theme' ;
1617
1718interface LoadedSiteConfig extends SiteConfig {
1819 customHeadHtml ?: string ;
@@ -27,33 +28,6 @@ function emptySiteConfig(): LoadedSiteConfig {
2728 } ;
2829}
2930
30- function adj ( r : number , g : number , b : number , mul : number ) {
31- return `rgb(${ Math . min ( 255 , Math . round ( r * mul ) ) } ,${ Math . min ( 255 , Math . round ( g * mul ) ) } ,${ Math . min ( 255 , Math . round ( b * mul ) ) } )` ;
32- }
33-
34- function parsePrimary ( hex : string ) {
35- const r = parseInt ( hex . slice ( 1 , 3 ) , 16 ) ;
36- const g = parseInt ( hex . slice ( 3 , 5 ) , 16 ) ;
37- const b = parseInt ( hex . slice ( 5 , 7 ) , 16 ) ;
38- const lum = ( 0.299 * r + 0.587 * g + 0.114 * b ) / 255 ;
39- const lightBgMul = lum < 0.15 ? 1.8 : lum < 0.3 ? 1.3 : 1 ;
40- const darkBgMul = lum < 0.15 ? 4 : lum < 0.3 ? 2.2 : lum < 0.4 ? 1.4 : 1 ;
41- const textMul =
42- lum > 0.9 ? 0.45 : lum > 0.7 ? 0.88 : lum > 0.5 ? 0.92 : lum < 0.15 ? 3.5 : lum < 0.3 ? 2 : 1 ;
43- const lightColor = adj ( r , g , b , lightBgMul ) ;
44- const darkColor = adj ( r , g , b , darkBgMul ) ;
45- const lightText = adj ( r , g , b , textMul ) ;
46- const darkText = adj ( r , g , b , darkBgMul ) ;
47- const lightLum = lum * lightBgMul ;
48- const darkLum = Math . min ( 1 , lum * darkBgMul ) ;
49- const lightFg = lightLum > 0.5 ? '#0a0a0a' : '#fafafa' ;
50- const darkFg = darkLum > 0.5 ? '#0a0a0a' : '#fafafa' ;
51- const targetTintLum = 0.45 ;
52- const tintMul = lum > 0 ? Math . max ( 1 , targetTintLum / lum ) : 6 ;
53- const cardTint = adj ( r , g , b , Math . min ( tintMul , 10 ) ) ;
54- return { lightColor, darkColor, lightFg, darkFg, lightText, darkText, cardTint } ;
55- }
56-
5731function createThemeInitializationScript ( defaultTheme : 'light' | 'dark' | 'system' ) : string {
5832 return `(()=>{try{const e=document.documentElement,p=new URLSearchParams(location.search).get("appearance");let t=p==="light"||p==="dark"?p:"";if(!t){try{const s=localStorage.getItem("theme");if(s==="light"||s==="dark"||s==="system")t=s}catch{}}if(!t)t="${ defaultTheme } ";const r=t==="system"?(matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"):t;e.classList.remove("light","dark");e.classList.add(r);e.style.colorScheme=r}catch{}})();` ;
5933}
@@ -196,13 +170,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
196170 const { customHeadHtml, ...siteConfig } = readSiteConfig ( ) ;
197171 const defaultTheme = siteConfig . theme ?? 'system' ;
198172
199- const pc = siteConfig . primaryColor ;
200- const primaryCss = pc
201- ? ( ( ) => {
202- const p = parsePrimary ( pc ) ;
203- return `html{--color-primary:${ p . lightColor } !important;--color-primary-foreground:${ p . lightFg } !important;--primary-text:${ p . lightText } ;--primary-card-tint:${ p . cardTint } }html.dark{--color-primary:${ p . darkColor } !important;--color-primary-foreground:${ p . darkFg } !important;--primary-text:${ p . darkText } ;--primary-card-tint:${ p . cardTint } }@media(prefers-color-scheme:dark){html:not(.light){--color-primary:${ p . darkColor } !important;--color-primary-foreground:${ p . darkFg } !important;--primary-text:${ p . darkText } ;--primary-card-tint:${ p . cardTint } }}` ;
204- } ) ( )
205- : '' ;
173+ const primaryCss = primaryThemeCss ( siteConfig . primaryColor ?? '' ) ;
206174
207175 return (
208176 < html
0 commit comments