Skip to content

Commit 18953b2

Browse files
Make tint color subtler when equal to primary color (#2808)
Co-authored-by: Valentino Hudhra <[email protected]>
1 parent 21cbd9e commit 18953b2

File tree

3 files changed

+127
-83
lines changed

3 files changed

+127
-83
lines changed

.changeset/six-hotels-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': patch
3+
---
4+
5+
Subtler tint color when based on the primary color, by mixing in some gray

packages/gitbook/src/components/RootLayout/CustomizationRootLayout.tsx

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,36 @@ import {
22
CustomizationCorners,
33
CustomizationHeaderPreset,
44
CustomizationIconsStyle,
5-
CustomizationSettings,
5+
type CustomizationSettings,
66
CustomizationSidebarBackgroundStyle,
77
CustomizationSidebarListStyle,
8-
CustomizationTint,
9-
SiteCustomizationSettings,
8+
type CustomizationThemedColor,
9+
type CustomizationTint,
10+
type SiteCustomizationSettings,
1011
} from '@gitbook/api';
1112
import { IconsProvider, IconStyle } from '@gitbook/icons';
1213
import assertNever from 'assert-never';
13-
import colors from 'tailwindcss/colors';
1414

1515
import { fontNotoColorEmoji, fonts, ibmPlexMono } from '@/fonts';
1616
import { getSpaceLanguage } from '@/intl/server';
1717
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';
1928
import { tcls } from '@/lib/tailwind';
2029

2130
import { ClientContexts } from './ClientContexts';
2231

2332
import '@gitbook/icons/style.css';
2433
import './globals.css';
2534

26-
const DEFAULT_TINT_COLOR = '#787878';
27-
2835
/**
2936
* Layout shared between the content and the PDF renderer.
3037
* It takes care of setting the theme and the language.
@@ -38,6 +45,7 @@ export async function CustomizationRootLayout(props: {
3845
const headerTheme = generateHeaderTheme(customization);
3946
const language = getSpaceLanguage(customization);
4047
const tintColor = getTintColor(customization);
48+
const mixColor = getTintMixColor(customization.styling.primaryColor, tintColor);
4149
const sidebarStyles = getSidebarStyles(customization);
4250

4351
return (
@@ -52,8 +60,8 @@ export async function CustomizationRootLayout(props: {
5260
? ' straight-corners'
5361
: '',
5462
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}`,
5765
)}
5866
>
5967
<head>
@@ -68,7 +76,7 @@ export async function CustomizationRootLayout(props: {
6876
>{`
6977
:root {
7078
${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 } })}
7280
${generateColorVariable('neutral', DEFAULT_TINT_COLOR)}
7381
7482
--header-background: ${hexToRgb(headerTheme.backgroundColor.light)};
@@ -77,7 +85,7 @@ export async function CustomizationRootLayout(props: {
7785
7886
.dark {
7987
${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 } })}
8189
${generateColorVariable('neutral', DEFAULT_TINT_COLOR, { darkMode: true })}
8290
8391
--header-background: ${hexToRgb(headerTheme.backgroundColor.dark)};
@@ -130,6 +138,38 @@ function getTintColor(
130138
}
131139
}
132140

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+
133173
/**
134174
* Get the sidebar styles from the customization settings.
135175
* If it is a space customization, it will return the default styles.
@@ -157,9 +197,8 @@ function generateColorVariable(
157197
{
158198
withContrast = true,
159199
...options // Pass any options along to the colorScale() function
160-
}: {
200+
}: ColorScaleOptions & {
161201
withContrast?: boolean;
162-
[key: string]: any;
163202
} = {},
164203
) {
165204
const shades: Record<string, string> =
@@ -191,8 +230,8 @@ function generateHeaderTheme(customization: CustomizationSettings | SiteCustomiz
191230
case CustomizationHeaderPreset.Default: {
192231
return {
193232
backgroundColor: {
194-
light: colors.white,
195-
dark: colors.black,
233+
light: LIGHT_BASE,
234+
dark: DARK_BASE,
196235
},
197236
linkColor: {
198237
light: customization.styling.primaryColor.light,
@@ -209,24 +248,24 @@ function generateHeaderTheme(customization: CustomizationSettings | SiteCustomiz
209248
linkColor: {
210249
light: colorContrast(
211250
tintColor?.light ?? customization.styling.primaryColor.light,
212-
[colors.white, colors.black],
251+
[LIGHT_BASE, DARK_BASE],
213252
),
214253
dark: colorContrast(
215254
tintColor?.dark ?? customization.styling.primaryColor.dark,
216-
[colors.white, colors.black],
255+
[LIGHT_BASE, DARK_BASE],
217256
),
218257
},
219258
};
220259
}
221260
case CustomizationHeaderPreset.Contrast: {
222261
return {
223262
backgroundColor: {
224-
light: colors.black,
225-
dark: colors.white,
263+
light: DARK_BASE,
264+
dark: LIGHT_BASE,
226265
},
227266
linkColor: {
228-
light: colors.white,
229-
dark: colors.black,
267+
light: LIGHT_BASE,
268+
dark: DARK_BASE,
230269
},
231270
};
232271
}
@@ -236,22 +275,20 @@ function generateHeaderTheme(customization: CustomizationSettings | SiteCustomiz
236275
light:
237276
customization.header.backgroundColor?.light ??
238277
tintColor?.light ??
239-
colors.white,
278+
LIGHT_BASE,
240279
dark:
241-
customization.header.backgroundColor?.dark ??
242-
tintColor?.dark ??
243-
colors.black,
280+
customization.header.backgroundColor?.dark ?? tintColor?.dark ?? DARK_BASE,
244281
},
245282
linkColor: {
246283
light:
247284
customization.header.linkColor?.light ??
248285
(tintColor?.light &&
249-
colorContrast(tintColor.light, [colors.white, colors.black])) ??
286+
colorContrast(tintColor.light, [LIGHT_BASE, DARK_BASE])) ??
250287
customization.styling.primaryColor.light,
251288
dark:
252289
customization.header.linkColor?.dark ??
253290
(tintColor?.dark &&
254-
colorContrast(tintColor.dark, [colors.white, colors.black])) ??
291+
colorContrast(tintColor.dark, [LIGHT_BASE, DARK_BASE])) ??
255292
customization.styling.primaryColor.dark,
256293
},
257294
};

0 commit comments

Comments
 (0)