-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
c52d45f
commit 7bf03b2
Showing
17 changed files
with
261 additions
and
124 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './src/index.css' |
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 |
---|---|---|
@@ -1,34 +1,94 @@ | ||
/** @jsx jsx */ | ||
import { useConfig } from 'docz' | ||
import { jsx, Styled } from 'theme-ui' | ||
import Highlight, { defaultProps } from 'prism-react-renderer' | ||
import { get } from 'lodash/fp' | ||
import { jsx, useColorMode } from 'theme-ui' | ||
import AceEditor from 'react-ace' | ||
|
||
export const Code = ({ codeString, language = 'jsx' }) => { | ||
const config = useConfig() | ||
const theme = get('themeConfig.prismjs', config) | ||
import 'brace/theme/dracula' | ||
import 'brace/theme/textmate' | ||
import 'brace/mode/jsx' | ||
import 'brace/ext/language_tools' | ||
import 'brace/ext/searchbox' | ||
|
||
import * as styles from './styles' | ||
|
||
const languages = [ | ||
'javascript', | ||
'java', | ||
'python', | ||
'xml', | ||
'ruby', | ||
'sass', | ||
'markdown', | ||
'mysql', | ||
'json', | ||
'html', | ||
'handlebars', | ||
'golang', | ||
'csharp', | ||
'elixir', | ||
'typescript', | ||
'css', | ||
] | ||
|
||
languages.forEach(lang => { | ||
require(`brace/mode/${lang}`) | ||
require(`brace/snippets/${lang}`) | ||
}) | ||
|
||
const themes = { | ||
light: 'textmate', | ||
dark: 'dracula', | ||
} | ||
|
||
const getLanguage = lang => { | ||
const defaultLanguage = 'jsx' | ||
if (typeof lang === 'string') return defaultLanguage | ||
|
||
const language = getter(lang, 'props.props.className') || defaultLanguage | ||
const result = language.replace('language-', '') | ||
|
||
if (result === 'js' || result === 'javascript') return 'jsx' | ||
if (result === 'ts' || result === 'tsx' || result === 'typescript') { | ||
return 'text/typescript' | ||
} | ||
return result | ||
} | ||
|
||
export const Code = ({ | ||
className, | ||
codeString, | ||
language = 'jsx', | ||
readOnly, | ||
onChange, | ||
}) => { | ||
const [colorMode] = useColorMode() | ||
const lang = getLanguage(language) | ||
return ( | ||
<Highlight | ||
{...defaultProps} | ||
theme={theme} | ||
code={codeString} | ||
language={language} | ||
> | ||
{({ className, style, tokens, getLineProps, getTokenProps }) => ( | ||
<Styled.pre className={className} style={style}> | ||
{tokens.map((line, i) => { | ||
if (line.length === 1 && line[0].empty) return null | ||
return ( | ||
<div key={line} {...getLineProps({ line, key: i })}> | ||
{line.map((token, key) => ( | ||
<span key={key} {...getTokenProps({ token, key })} /> | ||
))} | ||
</div> | ||
) | ||
})} | ||
</Styled.pre> | ||
)} | ||
</Highlight> | ||
<AceEditor | ||
className={className} | ||
sx={styles.editor} | ||
value={codeString} | ||
mode={lang} | ||
readOnly={readOnly} | ||
onChange={onChange} | ||
highlightActiveLine={false} | ||
theme={themes[colorMode]} | ||
name="code-editor" | ||
fontSize={16} | ||
style={{ | ||
width: 'calc(100% - 2px)', | ||
height: 200, | ||
}} | ||
setOptions={{ | ||
tabSize: 2, | ||
minLines: 5, | ||
maxLines: 20, | ||
wrap: true, | ||
autoScrollEditorIntoView: true, | ||
printMargin: false, | ||
}} | ||
editorProps={{ | ||
$blockScrolling: Infinity, | ||
}} | ||
/> | ||
) | ||
} |
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 |
---|---|---|
@@ -1 +1,7 @@ | ||
export const line = {} | ||
export const editor = { | ||
borderRadius: 'radius', | ||
border: '1px solid #000', | ||
borderColor: 'border', | ||
lineHeight: '1.4em', | ||
fontFamily: 'monospace', | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
export { default as ChevronDown } from 'react-feather/dist/icons/chevron-down' | ||
export { default as ChevronUp } from 'react-feather/dist/icons/chevron-up' | ||
export { default as Clipboard } from 'react-feather/dist/icons/clipboard' | ||
export { default as Code } from 'react-feather/dist/icons/code' | ||
export { default as Edit } from 'react-feather/dist/icons/edit-2' | ||
export { default as Sun } from 'react-feather/dist/icons/sun' | ||
export { default as Menu } from 'react-feather/dist/icons/menu' | ||
export { default as Github } from 'react-feather/dist/icons/github' | ||
export { default as Menu } from 'react-feather/dist/icons/menu' | ||
export { default as Search } from 'react-feather/dist/icons/search' | ||
export { default as ChevronUp } from 'react-feather/dist/icons/chevron-up' | ||
export { default as ChevronDown } from 'react-feather/dist/icons/chevron-down' | ||
export { default as Sun } from 'react-feather/dist/icons/sun' |
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,46 @@ | ||
/** @jsx jsx */ | ||
import { jsx } from 'theme-ui' | ||
import { useState } from 'react' | ||
import { LiveProvider, LiveError, LivePreview } from 'react-live' | ||
import copy from 'copy-text-to-clipboard' | ||
|
||
import * as styles from './styles' | ||
import * as Icons from '../Icons' | ||
import { Code } from '../Code' | ||
|
||
export const Playground = ({ code: initialCode, scope }) => { | ||
const [code, setCode] = useState(() => initialCode) | ||
const [showingCode, setShowingCode] = useState(false) | ||
|
||
const transformCode = code => { | ||
if (code.startsWith('()') || code.startsWith('class')) return code | ||
return `<React.Fragment>${code}</React.Fragment>` | ||
} | ||
|
||
const toggleCode = () => { | ||
setShowingCode(s => !s) | ||
} | ||
|
||
return ( | ||
<LiveProvider code={code} scope={scope} transformCode={transformCode}> | ||
<div sx={styles.previewWrapper}> | ||
<LivePreview sx={styles.preview(showingCode)} /> | ||
<div sx={styles.buttons}> | ||
<button sx={styles.button} onClick={() => copy(code)}> | ||
<Icons.Clipboard size={12} /> | ||
</button> | ||
<button sx={styles.button} onClick={toggleCode}> | ||
<Icons.Code size={12} /> | ||
</button> | ||
</div> | ||
</div> | ||
<LiveError sx={styles.error} /> | ||
<Code | ||
sx={styles.editor(showingCode)} | ||
codeString={code} | ||
onChange={setCode} | ||
language="jsx" | ||
/> | ||
</LiveProvider> | ||
) | ||
} |
25 changes: 25 additions & 0 deletions
25
core/gatsby-theme-docz/src/components/Playground/machine.js
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,25 @@ | ||
import { Machine, assign } from 'xstate' | ||
|
||
const actions = { | ||
changeCode: assign((ctx, ev) => ({ | ||
code: ev.data, | ||
})), | ||
} | ||
|
||
const machine = Machine({ | ||
id: 'playground', | ||
initial: 'idle', | ||
states: { | ||
idle: { | ||
on: { | ||
CHANGE: { | ||
actions: ['changeCode'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
export default machine.withConfig({ | ||
actions, | ||
}) |
54 changes: 54 additions & 0 deletions
54
core/gatsby-theme-docz/src/components/Playground/styles.js
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,54 @@ | ||
import * as mixins from '~utils/mixins' | ||
|
||
const border = { | ||
border: '1px solid black', | ||
borderColor: 'playground.border', | ||
} | ||
|
||
export const editor = showingCode => ({ | ||
...border, | ||
display: showingCode ? 'block' : 'none', | ||
m: 0, | ||
mt: '-1px', | ||
borderRadius: '0 0 5px 5px', | ||
}) | ||
|
||
export const error = { | ||
m: 0, | ||
py: 2, | ||
px: 3, | ||
bg: '#FF4757', | ||
fontSize: 1, | ||
color: 'white', | ||
} | ||
|
||
export const previewWrapper = { | ||
position: 'relative', | ||
} | ||
|
||
export const preview = showingCode => ({ | ||
...border, | ||
m: 0, | ||
p: 4, | ||
bg: 'playground.bg', | ||
borderRadius: showingCode ? '5px 5px 0 0' : '5px', | ||
}) | ||
|
||
export const buttons = { | ||
zIndex: 9, | ||
position: 'absolute', | ||
bottom: -24, | ||
right: 6, | ||
} | ||
|
||
export const button = { | ||
...mixins.ghostButton, | ||
py: 1, | ||
p: 2, | ||
bg: 'border', | ||
color: 'muted', | ||
borderRadius: '0 0 3px 3px', | ||
'& ~ &': { | ||
ml: 1, | ||
}, | ||
} |
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 @@ | ||
@import url('https://fonts.googleapis.com/css?family=Inconsolata&display=swap'); |
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
Oops, something went wrong.