@@ -2,29 +2,36 @@ import {
2
2
CustomizationCorners ,
3
3
CustomizationHeaderPreset ,
4
4
CustomizationIconsStyle ,
5
- CustomizationSettings ,
5
+ type CustomizationSettings ,
6
6
CustomizationSidebarBackgroundStyle ,
7
7
CustomizationSidebarListStyle ,
8
- CustomizationTint ,
9
- SiteCustomizationSettings ,
8
+ type CustomizationThemedColor ,
9
+ type CustomizationTint ,
10
+ type SiteCustomizationSettings ,
10
11
} from '@gitbook/api' ;
11
12
import { IconsProvider , IconStyle } from '@gitbook/icons' ;
12
13
import assertNever from 'assert-never' ;
13
- import colors from 'tailwindcss/colors' ;
14
14
15
15
import { fontNotoColorEmoji , fonts , ibmPlexMono } from '@/fonts' ;
16
16
import { getSpaceLanguage } from '@/intl/server' ;
17
17
import { getStaticFileURL } from '@/lib/assets' ;
18
- import { colorContrast , colorScale , hexToRgb , shadesOfColor } from '@/lib/colors' ;
18
+ import {
19
+ colorContrast ,
20
+ colorScale ,
21
+ type ColorScaleOptions ,
22
+ DARK_BASE ,
23
+ DEFAULT_TINT_COLOR ,
24
+ hexToRgb ,
25
+ LIGHT_BASE ,
26
+ shadesOfColor ,
27
+ } from '@/lib/colors' ;
19
28
import { tcls } from '@/lib/tailwind' ;
20
29
21
30
import { ClientContexts } from './ClientContexts' ;
22
31
23
32
import '@gitbook/icons/style.css' ;
24
33
import './globals.css' ;
25
34
26
- const DEFAULT_TINT_COLOR = '#787878' ;
27
-
28
35
/**
29
36
* Layout shared between the content and the PDF renderer.
30
37
* It takes care of setting the theme and the language.
@@ -38,6 +45,7 @@ export async function CustomizationRootLayout(props: {
38
45
const headerTheme = generateHeaderTheme ( customization ) ;
39
46
const language = getSpaceLanguage ( customization ) ;
40
47
const tintColor = getTintColor ( customization ) ;
48
+ const mixColor = getTintMixColor ( customization . styling . primaryColor , tintColor ) ;
41
49
const sidebarStyles = getSidebarStyles ( customization ) ;
42
50
43
51
return (
@@ -52,8 +60,8 @@ export async function CustomizationRootLayout(props: {
52
60
? ' straight-corners'
53
61
: '' ,
54
62
tintColor ? ' tint' : 'no-tint' ,
55
- sidebarStyles . background && ' sidebar-' + sidebarStyles . background ,
56
- sidebarStyles . list && ' sidebar-list-' + sidebarStyles . list ,
63
+ sidebarStyles . background && ` sidebar-${ sidebarStyles . background } ` ,
64
+ sidebarStyles . list && ` sidebar-list-${ sidebarStyles . list } ` ,
57
65
) }
58
66
>
59
67
< head >
@@ -68,7 +76,7 @@ export async function CustomizationRootLayout(props: {
68
76
> { `
69
77
:root {
70
78
${ generateColorVariable ( 'primary' , customization . styling . primaryColor . light ) }
71
- ${ generateColorVariable ( 'tint' , tintColor ? ( tintColor ? .light ?? customization . styling . primaryColor . light ?? DEFAULT_TINT_COLOR ) : DEFAULT_TINT_COLOR , { mix : ! tintColor ? customization . styling . primaryColor . light : undefined } ) }
79
+ ${ generateColorVariable ( 'tint' , tintColor ? tintColor . light : DEFAULT_TINT_COLOR , { mix : mixColor && { color : mixColor . color . light , ratio : mixColor . ratio . light } } ) }
72
80
${ generateColorVariable ( 'neutral' , DEFAULT_TINT_COLOR ) }
73
81
74
82
--header-background: ${ hexToRgb ( headerTheme . backgroundColor . light ) } ;
@@ -77,7 +85,7 @@ export async function CustomizationRootLayout(props: {
77
85
78
86
.dark {
79
87
${ generateColorVariable ( 'primary' , customization . styling . primaryColor . dark , { darkMode : true } ) }
80
- ${ generateColorVariable ( 'tint' , tintColor ? ( tintColor ? .dark ?? customization . styling . primaryColor . dark ?? DEFAULT_TINT_COLOR ) : DEFAULT_TINT_COLOR , { darkMode : true , mix : ! tintColor ? customization . styling . primaryColor . dark : undefined } ) }
88
+ ${ generateColorVariable ( 'tint' , tintColor ? tintColor . dark : DEFAULT_TINT_COLOR , { darkMode : true , mix : mixColor && { color : mixColor ?. color . dark , ratio : mixColor . ratio . dark } } ) }
81
89
${ generateColorVariable ( 'neutral' , DEFAULT_TINT_COLOR , { darkMode : true } ) }
82
90
83
91
--header-background: ${ hexToRgb ( headerTheme . backgroundColor . dark ) } ;
@@ -130,6 +138,38 @@ function getTintColor(
130
138
}
131
139
}
132
140
141
+ function getTintMixColor (
142
+ primaryColor : CustomizationThemedColor ,
143
+ tintColor : CustomizationTint [ 'color' ] | undefined ,
144
+ ) : {
145
+ color : CustomizationThemedColor ;
146
+ ratio : { light : number ; dark : number } ;
147
+ } {
148
+ if ( ! tintColor ) {
149
+ // Mix in a bit of the primary colour into neutral, to match with primary nicely.
150
+ return {
151
+ color : primaryColor ,
152
+ ratio : {
153
+ light : 0.2 ,
154
+ dark : 0.1 ,
155
+ } ,
156
+ } ;
157
+ }
158
+
159
+ // Mix in neutral into the tint colour to offset it from the primary, and to make the effect less intense.
160
+ // If the tint colour differs from the primary colour, we use the tint colour fully without mixing.
161
+ return {
162
+ color : {
163
+ light : DEFAULT_TINT_COLOR ,
164
+ dark : DEFAULT_TINT_COLOR ,
165
+ } ,
166
+ ratio : {
167
+ light : tintColor . light . toUpperCase ( ) === primaryColor . light . toUpperCase ( ) ? 0.4 : 0 ,
168
+ dark : tintColor . dark . toUpperCase ( ) === primaryColor . dark . toUpperCase ( ) ? 0.4 : 0 ,
169
+ } ,
170
+ } ;
171
+ }
172
+
133
173
/**
134
174
* Get the sidebar styles from the customization settings.
135
175
* If it is a space customization, it will return the default styles.
@@ -157,9 +197,8 @@ function generateColorVariable(
157
197
{
158
198
withContrast = true ,
159
199
...options // Pass any options along to the colorScale() function
160
- } : {
200
+ } : ColorScaleOptions & {
161
201
withContrast ?: boolean ;
162
- [ key : string ] : any ;
163
202
} = { } ,
164
203
) {
165
204
const shades : Record < string , string > =
@@ -191,8 +230,8 @@ function generateHeaderTheme(customization: CustomizationSettings | SiteCustomiz
191
230
case CustomizationHeaderPreset . Default : {
192
231
return {
193
232
backgroundColor : {
194
- light : colors . white ,
195
- dark : colors . black ,
233
+ light : LIGHT_BASE ,
234
+ dark : DARK_BASE ,
196
235
} ,
197
236
linkColor : {
198
237
light : customization . styling . primaryColor . light ,
@@ -209,24 +248,24 @@ function generateHeaderTheme(customization: CustomizationSettings | SiteCustomiz
209
248
linkColor : {
210
249
light : colorContrast (
211
250
tintColor ?. light ?? customization . styling . primaryColor . light ,
212
- [ colors . white , colors . black ] ,
251
+ [ LIGHT_BASE , DARK_BASE ] ,
213
252
) ,
214
253
dark : colorContrast (
215
254
tintColor ?. dark ?? customization . styling . primaryColor . dark ,
216
- [ colors . white , colors . black ] ,
255
+ [ LIGHT_BASE , DARK_BASE ] ,
217
256
) ,
218
257
} ,
219
258
} ;
220
259
}
221
260
case CustomizationHeaderPreset . Contrast : {
222
261
return {
223
262
backgroundColor : {
224
- light : colors . black ,
225
- dark : colors . white ,
263
+ light : DARK_BASE ,
264
+ dark : LIGHT_BASE ,
226
265
} ,
227
266
linkColor : {
228
- light : colors . white ,
229
- dark : colors . black ,
267
+ light : LIGHT_BASE ,
268
+ dark : DARK_BASE ,
230
269
} ,
231
270
} ;
232
271
}
@@ -236,22 +275,20 @@ function generateHeaderTheme(customization: CustomizationSettings | SiteCustomiz
236
275
light :
237
276
customization . header . backgroundColor ?. light ??
238
277
tintColor ?. light ??
239
- colors . white ,
278
+ LIGHT_BASE ,
240
279
dark :
241
- customization . header . backgroundColor ?. dark ??
242
- tintColor ?. dark ??
243
- colors . black ,
280
+ customization . header . backgroundColor ?. dark ?? tintColor ?. dark ?? DARK_BASE ,
244
281
} ,
245
282
linkColor : {
246
283
light :
247
284
customization . header . linkColor ?. light ??
248
285
( tintColor ?. light &&
249
- colorContrast ( tintColor . light , [ colors . white , colors . black ] ) ) ??
286
+ colorContrast ( tintColor . light , [ LIGHT_BASE , DARK_BASE ] ) ) ??
250
287
customization . styling . primaryColor . light ,
251
288
dark :
252
289
customization . header . linkColor ?. dark ??
253
290
( tintColor ?. dark &&
254
- colorContrast ( tintColor . dark , [ colors . white , colors . black ] ) ) ??
291
+ colorContrast ( tintColor . dark , [ LIGHT_BASE , DARK_BASE ] ) ) ??
255
292
customization . styling . primaryColor . dark ,
256
293
} ,
257
294
} ;
0 commit comments