Skip to content

Commit 4d7b7e6

Browse files
authored
Add experimental support for color modes (#790)
1 parent c3ab71b commit 4d7b7e6

20 files changed

+167
-65
lines changed

.changeset/quick-rabbits-visit.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'scoobie': minor
3+
---
4+
5+
Blockquote, CodeBlock, CodeContainer, InlineCode, styles/codeBackgroundColor: Add experimental support for color modes
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

.storybook/preview.tsx

+26-13
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { BraidProvider, Card, PageBlock } from 'braid-design-system';
55
import docs from 'braid-design-system/themes/docs';
66
import seekJobs from 'braid-design-system/themes/seekJobs';
77
import wireframe from 'braid-design-system/themes/wireframe';
8+
import { themes } from 'prism-react-renderer';
89
import React from 'react';
910
import { Helmet, HelmetProvider } from 'react-helmet-async';
1011
import { BrowserRouter } from 'react-router-dom';
1112

13+
import { CodeThemeProvider } from '../src/components/CodeThemeProvider';
1214
import { ScoobieLink } from '../src/components/ScoobieLink';
1315
import { robotoHref, robotoMonoHref } from '../typography';
1416

@@ -18,6 +20,15 @@ export type BraidThemeName = 'docs' | 'seekJobs' | 'wireframe';
1820

1921
export default {
2022
globalTypes: {
23+
codeTheme: {
24+
description: 'Code theme to use',
25+
defaultValue: 'github',
26+
toolbar: {
27+
title: 'Code theme',
28+
icon: 'contrast',
29+
items: Object.keys(themes),
30+
},
31+
},
2132
theme: {
2233
description: 'Braid theme to use',
2334
defaultValue: 'seekJobs',
@@ -34,19 +45,21 @@ export default {
3445
theme={THEMES[globals.theme as BraidThemeName]}
3546
linkComponent={ScoobieLink}
3647
>
37-
<BrowserRouter>
38-
<HelmetProvider>
39-
<Helmet>
40-
<link href={robotoHref} rel="stylesheet" />
41-
<link href={robotoMonoHref} rel="stylesheet" />
42-
</Helmet>
43-
<PageBlock>
44-
<Card>
45-
<Story />
46-
</Card>
47-
</PageBlock>
48-
</HelmetProvider>
49-
</BrowserRouter>
48+
<CodeThemeProvider theme={globals.codeTheme}>
49+
<BrowserRouter>
50+
<HelmetProvider>
51+
<Helmet>
52+
<link href={robotoHref} rel="stylesheet" />
53+
<link href={robotoMonoHref} rel="stylesheet" />
54+
</Helmet>
55+
<PageBlock>
56+
<Card>
57+
<Story />
58+
</Card>
59+
</PageBlock>
60+
</HelmetProvider>
61+
</BrowserRouter>
62+
</CodeThemeProvider>
5063
</BraidProvider>
5164
),
5265
],

src/components/Blockquote.css.ts

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import { style } from '@vanilla-extract/css';
2-
import { vars } from 'braid-design-system/css';
2+
import { colorModeStyle, vars } from 'braid-design-system/css';
33

4-
export const quoteBlock = style({
5-
borderLeftColor: vars.backgroundColor.neutralLight,
6-
borderLeftStyle: 'solid',
7-
borderLeftWidth: vars.space.xxsmall,
8-
position: 'relative',
4+
export const quoteBlock = style([
5+
{
6+
borderLeftStyle: 'solid',
7+
borderLeftWidth: vars.space.xxsmall,
8+
borderRadius: vars.borderRadius.standard,
9+
position: 'relative',
10+
},
11+
colorModeStyle({
12+
darkMode: {
13+
backgroundColor: vars.backgroundColor.neutral,
14+
borderLeftColor: vars.borderColor.field,
15+
},
16+
lightMode: {
17+
backgroundColor: vars.backgroundColor.neutralLight,
18+
borderLeftColor: vars.borderColor.neutralLight,
19+
},
20+
}),
21+
]);
22+
23+
export const quoteMark = style({
24+
right: vars.space.xxsmall,
25+
top: vars.space.xxsmall,
926
});

src/components/Blockquote.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ export const Blockquote = ({ children }: Props) => (
1010
<Box className={styles.quoteBlock} padding="medium">
1111
<Stack space="medium">{children}</Stack>
1212

13-
<Box position="absolute" right={0} top={0} userSelect="none" zIndex={0}>
13+
<Box
14+
position="absolute"
15+
className={styles.quoteMark}
16+
userSelect="none"
17+
zIndex={0}
18+
>
1419
<Heading component="div" level="2">
1520
<Secondary></Secondary>
1621
</Heading>

src/components/CodeContainer.css.ts

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
import { style } from '@vanilla-extract/css';
22
import { calc } from '@vanilla-extract/css-utils';
3-
import { responsiveStyle, vars } from 'braid-design-system/css';
4-
import { darken } from 'polished';
3+
import { colorModeStyle, responsiveStyle, vars } from 'braid-design-system/css';
4+
import { darken, lighten } from 'polished';
55

66
import { codeBackgroundColor } from '../../styles';
77

8-
export const lineNumberContainer = style({
9-
backgroundColor: darken(0.05, codeBackgroundColor),
10-
borderTopLeftRadius: vars.borderRadius.large,
11-
borderBottomLeftRadius: vars.borderRadius.large,
12-
color: vars.foregroundColor.secondary,
13-
userSelect: 'none',
14-
});
8+
export const lineNumberContainer = style([
9+
{
10+
borderTopLeftRadius: vars.borderRadius.large,
11+
borderBottomLeftRadius: vars.borderRadius.large,
12+
color: vars.foregroundColor.secondary,
13+
userSelect: 'none',
14+
},
15+
colorModeStyle({
16+
darkMode: { backgroundColor: lighten(0.05, codeBackgroundColor.darkMode) },
17+
lightMode: { backgroundColor: darken(0.05, codeBackgroundColor.lightMode) },
18+
}),
19+
]);
1520

1621
export const codeContainer = style([
1722
{
18-
backgroundColor: codeBackgroundColor,
19-
borderColor: darken(0.05, codeBackgroundColor),
2023
borderStyle: 'solid',
2124
borderWidth: vars.borderWidth.standard,
2225

2326
overflow: 'auto',
2427
},
2528

29+
colorModeStyle({
30+
darkMode: {
31+
backgroundColor: codeBackgroundColor.darkMode,
32+
borderColor: lighten(0.05, codeBackgroundColor.darkMode),
33+
},
34+
lightMode: {
35+
backgroundColor: codeBackgroundColor.lightMode,
36+
borderColor: darken(0.05, codeBackgroundColor.lightMode),
37+
},
38+
}),
39+
2640
responsiveStyle({
2741
mobile: {
2842
// Roughly 15 lines of code at standard size.

src/components/CodeContainer.tsx

+23-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Highlight, type Token } from 'prism-react-renderer';
33

44
import { Prism, themes } from '../private/Prism';
55

6+
import { useCodeTheme } from './CodeThemeProvider';
7+
68
import * as styles from './CodeContainer.css';
79
import * as codeStyles from '../../styles/code.css';
810

@@ -14,24 +16,28 @@ export const CodeContainer = ({
1416
code: string;
1517
language: string;
1618
lineNumbers?: boolean;
17-
}) => (
18-
<Box borderRadius="large" className={styles.codeContainer}>
19-
<Highlight
20-
prism={Prism}
21-
code={code}
22-
language={language}
23-
theme={themes.github}
24-
>
25-
{({ getTokenProps, tokens }) => (
26-
<Box display="flex">
27-
{lineNumbers ? <LineNumbers count={tokens.length} /> : null}
19+
}) => {
20+
const theme = useCodeTheme();
2821

29-
<Lines getTokenProps={getTokenProps} lines={tokens} />
30-
</Box>
31-
)}
32-
</Highlight>
33-
</Box>
34-
);
22+
return (
23+
<Box borderRadius="large" className={styles.codeContainer}>
24+
<Highlight
25+
prism={Prism}
26+
code={code}
27+
language={language}
28+
theme={themes[theme]}
29+
>
30+
{({ getTokenProps, tokens }) => (
31+
<Box display="flex">
32+
{lineNumbers ? <LineNumbers count={tokens.length} /> : null}
33+
34+
<Lines getTokenProps={getTokenProps} lines={tokens} />
35+
</Box>
36+
)}
37+
</Highlight>
38+
</Box>
39+
);
40+
};
3541

3642
const LineNumbers = ({ count }: { count: number }) => {
3743
const numbers = [...new Array(count)].map((_, index) => index + 1);

src/components/CodeThemeProvider.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { themes } from 'prism-react-renderer';
2+
import { type ReactNode, createContext, useContext } from 'react';
3+
4+
type CodeTheme = keyof typeof themes;
5+
6+
const ctx = createContext<CodeTheme>('github');
7+
8+
interface CodeThemeProviderProps {
9+
children: ReactNode;
10+
11+
/**
12+
* The current color mode.
13+
*
14+
* This defaults to `lightMode` in the absence of a `ColorModeProvider`.
15+
*/
16+
theme: CodeTheme;
17+
}
18+
19+
export const CodeThemeProvider = ({
20+
children,
21+
theme: theme,
22+
}: CodeThemeProviderProps) => (
23+
<ctx.Provider value={theme}>{children}</ctx.Provider>
24+
);
25+
26+
export const useCodeTheme = (): CodeTheme => useContext(ctx);

src/components/InlineCode.css.ts

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { style, styleVariants } from '@vanilla-extract/css';
22
import { calc } from '@vanilla-extract/css-utils';
3-
import { vars } from 'braid-design-system/css';
4-
import { darken } from 'polished';
3+
import { colorModeStyle, vars } from 'braid-design-system/css';
4+
import { darken, lighten } from 'polished';
55

66
import { codeBackgroundColor, monospaceFontFamily } from '../../styles';
77

@@ -22,21 +22,31 @@ export const colourBlockWrapper = style({
2222
});
2323

2424
export const weightBorder = styleVariants({
25-
regular: {
26-
borderColor: darken(0.05, codeBackgroundColor),
27-
borderStyle: 'solid',
28-
borderWidth: vars.borderWidth.standard,
29-
},
25+
regular: [
26+
{
27+
borderStyle: 'solid',
28+
borderWidth: vars.borderWidth.standard,
29+
},
30+
colorModeStyle({
31+
darkMode: { borderColor: lighten(0.05, codeBackgroundColor.darkMode) },
32+
lightMode: { borderColor: darken(0.05, codeBackgroundColor.lightMode) },
33+
}),
34+
],
3035

3136
weak: {},
3237
});
3338

3439
export const weight = styleVariants({
35-
regular: {
36-
backgroundColor: codeBackgroundColor,
37-
paddingLeft: vars.space.xxsmall,
38-
paddingRight: vars.space.xxsmall,
39-
},
40+
regular: [
41+
{
42+
paddingLeft: vars.space.xxsmall,
43+
paddingRight: vars.space.xxsmall,
44+
},
45+
colorModeStyle({
46+
darkMode: { backgroundColor: codeBackgroundColor.darkMode },
47+
lightMode: { backgroundColor: codeBackgroundColor.lightMode },
48+
}),
49+
],
4050

4151
weak: {},
4252
});

src/components/InlineCode.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Box } from 'braid-design-system';
2-
import React, { Fragment, type ReactNode } from 'react';
2+
import { Fragment, type ReactNode } from 'react';
33

44
import * as styles from './InlineCode.css';
55

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { Blockquote } from './components/Blockquote';
22
export { CodeBlock } from './components/CodeBlock';
33
export { CodeContainer } from './components/CodeContainer';
4+
export { CodeThemeProvider } from './components/CodeThemeProvider';
45
export { CopyableText } from './components/CopyableText';
56
export { InlineCode } from './components/InlineCode';
67
export { InternalLink } from './components/InternalLink';

styles/index.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
// Braid grey.50
2-
// https://github.com/seek-oss/braid-design-system/blob/braid-design-system%4032.14.4/packages/braid-design-system/src/lib/color/palette.ts#L13
3-
export const codeBackgroundColor = '#F7F8FB';
1+
// https://github.com/seek-oss/braid-design-system/blob/braid-design-system%4033.5.0/packages/braid-design-system/src/lib/color/palette.ts#L2-L14
2+
export const codeBackgroundColor = {
3+
// Braid grey.50
4+
lightMode: '#F7F8FB',
45

5-
// SEEK's corporate font + GitHub defaults
6+
// Braid grey.900
7+
darkMode: '#0E131B',
8+
};
9+
10+
// SEEK's former font + GitHub defaults
611
export const monospaceFontFamily =
712
"'Roboto Mono',SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace";

0 commit comments

Comments
 (0)