Skip to content

Commit f512ea2

Browse files
committed
Add keyboard shortcut to preview live code examples in dark mode
1 parent 562459e commit f512ea2

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

docs/src/@primer/gatsby-theme-doctocat/components/live-preview-wrapper.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
import {BaseStyles, Box} from '@primer/components'
2+
import {theme as darkTheme} from '@primer/components/theme-dark-preval'
3+
import {theme as lightTheme} from '@primer/components/theme-preval'
24
import React from 'react'
5+
import {ThemeProvider} from 'styled-components'
6+
7+
// This is a temporary way to allow us to preview components in dark mode.
8+
// We'll replace this component when @primer/components has a first-class
9+
// API for theme switching.
10+
function ThemeSwitcher({children}) {
11+
const [theme, setTheme] = React.useState('light')
12+
13+
React.useEffect(() => {
14+
function handleKeyDown(event) {
15+
// Use `ctrl + cmd + t` to toggle between light and dark mode
16+
if (event.key === 't' && event.ctrlKey && event.metaKey) {
17+
setTheme(theme === 'light' ? 'dark' : 'light')
18+
}
19+
}
20+
document.addEventListener('keydown', handleKeyDown)
21+
22+
return function cleanup() {
23+
document.removeEventListener('keydown', handleKeyDown)
24+
}
25+
}, [theme])
26+
27+
return <ThemeProvider theme={theme === 'light' ? lightTheme : darkTheme}>{children}</ThemeProvider>
28+
}
329

430
// Users can shadow this file to wrap live previews.
531
// This is useful for applying global styles.
632
function LivePreviewWrapper({children}) {
733
return (
8-
<Box width="100%" p={3}>
9-
<BaseStyles>{children}</BaseStyles>
10-
</Box>
34+
<ThemeSwitcher>
35+
<Box width="100%" p={3} bg="bg.canvas" sx={{borderTopLeftRadius: 2, borderTopRightRadius: 2}}>
36+
<BaseStyles>{children}</BaseStyles>
37+
</Box>
38+
</ThemeSwitcher>
1139
)
1240
}
1341

src/theme-dark-preval.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @preval
2+
// This file needs to be a JavaScript file using CommonJS to be compatiable with preval.
3+
4+
// This is a temporary theme file used to allow us to preview components in dark mode
5+
// on the docs site. We'll remove this file when we have a more robust way to create themes.
6+
7+
const {theme: lightTheme} = require('./theme-preval')
8+
const {default: primitives} = require('@primer/primitives')
9+
const deepmerge = require('deepmerge')
10+
const {filterObject, isShadowValue, isColorValue} = require('./utils/theme')
11+
12+
const {scale: _excludeScaleColors, ...functionalColors} = filterObject(primitives.colors.dark, value =>
13+
isColorValue(value)
14+
)
15+
const {scale: _excludeScaleShadows, ...functionalShadows} = filterObject(primitives.colors.dark, value =>
16+
isShadowValue(value)
17+
)
18+
19+
const mergedColors = deepmerge(lightTheme.colors, functionalColors)
20+
const mergedShadows = deepmerge(lightTheme.shadows, functionalShadows)
21+
22+
const theme = {
23+
...lightTheme,
24+
colors: mergedColors,
25+
shadows: mergedShadows
26+
}
27+
28+
module.exports = {theme}

0 commit comments

Comments
 (0)