|
1 | 1 | 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' |
2 | 4 | 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 | +} |
3 | 29 |
|
4 | 30 | // Users can shadow this file to wrap live previews.
|
5 | 31 | // This is useful for applying global styles.
|
6 | 32 | function LivePreviewWrapper({children}) {
|
7 | 33 | 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> |
11 | 39 | )
|
12 | 40 | }
|
13 | 41 |
|
|
0 commit comments