-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3466692
commit f503004
Showing
9 changed files
with
235 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import Highlight, { defaultProps, PrismTheme } from 'prism-react-renderer'; | ||
import { default as themeDark } from 'prism-react-renderer/themes/vsDark'; | ||
import { default as themeLight } from 'prism-react-renderer/themes/vsLight'; | ||
import * as React from 'react'; | ||
|
||
import { makeStyles, shorthands } from '@griffel/react'; | ||
|
||
import { DARK_THEME_COLOR_TOKENS, LIGHT_THEME_COLOR_TOKENS } from './colorTokens'; | ||
import { useThemeContext } from './ThemeContext'; | ||
import type { BrowserTheme } from './types'; | ||
|
||
function getCustomTheme(theme?: BrowserTheme): PrismTheme { | ||
const colortokens = theme === 'dark' ? DARK_THEME_COLOR_TOKENS : LIGHT_THEME_COLOR_TOKENS; | ||
return { | ||
plain: { | ||
color: colortokens.foreground, | ||
}, | ||
styles: [ | ||
...(theme === 'dark' ? themeDark.styles : themeLight.styles), | ||
{ | ||
types: ['keyword', 'atrule'], | ||
style: { | ||
color: colortokens.foreground, | ||
}, | ||
}, | ||
{ | ||
types: ['rule'], | ||
style: { | ||
color: colortokens.cssAtRule, | ||
}, | ||
}, | ||
{ | ||
types: ['property'], | ||
style: { | ||
color: colortokens.cssProperty, | ||
}, | ||
}, | ||
{ | ||
types: ['punctuation'], | ||
style: { | ||
color: colortokens.cssPunctuation, | ||
}, | ||
}, | ||
{ | ||
types: ['selector'], | ||
style: { | ||
color: colortokens.cssSelector, | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
const useColorIndicatorStyles = makeStyles({ | ||
colorIndicator: { | ||
height: '10px', | ||
width: '10px', | ||
display: 'inline-block', | ||
marginLeft: '2px', | ||
marginRight: '2px', | ||
...shorthands.border('1px', 'dotted', 'grey'), | ||
}, | ||
}); | ||
|
||
export const HighlightedCSS: React.FC<{ code: string }> = ({ code }) => { | ||
const theme = useThemeContext(); | ||
const customTheme = React.useMemo(() => getCustomTheme(theme), [theme]); | ||
|
||
const { colorIndicator } = useColorIndicatorStyles(); | ||
|
||
return ( | ||
<Highlight {...defaultProps} code={code} language="css" theme={customTheme}> | ||
{({ className, style, tokens, getLineProps, getTokenProps }) => ( | ||
<pre className={className} style={{ ...style, display: 'inline-block', textDecorationLine: 'inherit' }}> | ||
{tokens.map((line, i) => ( | ||
<div {...getLineProps({ line, key: i })}> | ||
{line.map((token, key) => { | ||
const tokenProps = getTokenProps({ token, key }); | ||
return tokenProps.className.includes('color') && token.content !== 'transparent' ? ( | ||
<span | ||
{...tokenProps} | ||
children={ | ||
<> | ||
{/* show a square color indicator for colors in css */} | ||
<span | ||
key={`${i}-color`} | ||
className={colorIndicator} | ||
style={{ backgroundColor: token.content }} | ||
/> | ||
{tokenProps.children} | ||
</> | ||
} | ||
/> | ||
) : ( | ||
<span {...tokenProps} /> | ||
); | ||
})} | ||
</div> | ||
))} | ||
</pre> | ||
)} | ||
</Highlight> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as React from 'react'; | ||
import type { BrowserTheme } from './types'; | ||
|
||
export const ThemeContext = React.createContext<BrowserTheme>('light'); | ||
|
||
export function useThemeContext() { | ||
return React.useContext(ThemeContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { ColorTokens } from './types'; | ||
|
||
export const LIGHT_THEME_COLOR_TOKENS: ColorTokens = { | ||
foreground: 'rgb(34, 34, 34)', | ||
background: 'white', | ||
|
||
cssAtRule: 'rgb(128, 134, 139)', | ||
cssProperty: 'rgb(19, 44, 121)', | ||
cssPunctuation: 'rgb(102, 102, 102)', | ||
cssSelector: 'rgb(158, 51, 121)', | ||
|
||
slotNameBackground: 'rgb(247, 247, 247)', | ||
slotNameBorder: 'rgb(202, 205, 209)', | ||
}; | ||
|
||
export const DARK_THEME_COLOR_TOKENS: ColorTokens = { | ||
...LIGHT_THEME_COLOR_TOKENS, | ||
background: 'rgb(32, 33, 36)', | ||
foreground: 'rgb(213, 213, 213)', | ||
|
||
cssAtRule: 'rgb(128, 134, 139)', | ||
cssProperty: 'rgb(156, 220, 254)', | ||
cssPunctuation: 'rgb(167, 167, 167)', | ||
cssSelector: 'rgb(175, 165, 143)', | ||
|
||
slotNameBackground: 'rgb(51, 51, 51)', | ||
slotNameBorder: 'rgb(73, 76, 80)', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters