-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a Tutorial component that will flip between files and their…
… code
- Loading branch information
1 parent
211d8c9
commit 5d0fdc6
Showing
6 changed files
with
281 additions
and
52 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,53 @@ | ||
import React, { Children, cloneElement } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import parseCodeBlockProps from '../utils/parseCodeBlockProps'; | ||
import { isCodeBlock, isShellCommand } from '../utils/codeBlock'; | ||
|
||
const Tutorial = ({ children }) => { | ||
const initialState = Children.toArray(children) | ||
.flatMap((child) => Children.toArray(child.props.children)) | ||
.filter((child) => isCodeBlock(child) && !isShellCommand(child)) | ||
.reduce((map, child) => { | ||
const { fileName, language } = parseCodeBlockProps(child); | ||
|
||
return map.has(fileName) | ||
? map | ||
: map.set(fileName, { code: '', language }); | ||
}, new Map()); | ||
|
||
return Children.toArray(children).reduce((steps, stepElement, idx, arr) => { | ||
const codeBlock = Children.toArray(stepElement.props.children).find( | ||
(child) => isCodeBlock(child) && !isShellCommand(child) | ||
); | ||
|
||
if (!codeBlock) { | ||
return [...steps, stepElement]; | ||
} | ||
|
||
const previousStep = | ||
idx === 0 | ||
? new Map(initialState) | ||
: new Map(steps[idx - 1].props.step || initialState); | ||
|
||
const { fileName, code, language } = parseCodeBlockProps(codeBlock); | ||
|
||
return [ | ||
...steps, | ||
cloneElement(stepElement, { | ||
initialSelectedFile: fileName, | ||
step: previousStep.set(fileName, { code, language }), | ||
index: idx, | ||
totalSteps: arr.length, | ||
children: Children.toArray(stepElement.props.children).filter( | ||
(child) => isShellCommand(child) || !isCodeBlock(child) | ||
), | ||
}), | ||
]; | ||
}, []); | ||
}; | ||
|
||
Tutorial.propTypes = { | ||
children: PropTypes.node, | ||
}; | ||
|
||
export default Tutorial; |
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,71 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { CodeBlock } from '@newrelic/gatsby-theme-newrelic'; | ||
import path from 'path'; | ||
import { css } from '@emotion/core'; | ||
import { darken } from 'polished'; | ||
|
||
const TutorialEditor = ({ initialSelectedFile, files }) => { | ||
const [selectedFile, setSelectedFile] = useState(initialSelectedFile); | ||
const { language, code } = files.get(selectedFile); | ||
|
||
return ( | ||
<div> | ||
<div | ||
css={css` | ||
display: flex; | ||
background: ${darken(0.05, '#2e3440')}; | ||
border-top-left-radius: 0.25rem; | ||
border-top-right-radius: 0.25rem; | ||
`} | ||
> | ||
{Array.from(files.keys()).map((fileName) => ( | ||
<button | ||
key={fileName} | ||
type="button" | ||
onClick={() => setSelectedFile(fileName)} | ||
css={css` | ||
padding: 0.5rem 1rem; | ||
cursor: pointer; | ||
font-size: 0.75rem; | ||
border: 0; | ||
outline: 0; | ||
color: currentColor; | ||
background: ${fileName === selectedFile | ||
? 'var(--color-nord-0)' | ||
: 'inherit'}; | ||
`} | ||
> | ||
{path.basename(fileName)} | ||
</button> | ||
))} | ||
</div> | ||
|
||
{Array.from(files.entries()).map(([fileName, { code, language }]) => ( | ||
<CodeBlock | ||
key={fileName} | ||
lineNumbers | ||
language={language} | ||
fileName={fileName} | ||
css={css` | ||
display: ${fileName === selectedFile ? 'block' : 'none'}; | ||
> div:first-of-type { | ||
border-top-left-radius: 0; | ||
border-top-right-radius: 0; | ||
} | ||
`} | ||
> | ||
{code} | ||
</CodeBlock> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
TutorialEditor.propTypes = { | ||
initialSelectedFile: PropTypes.string.isRequired, | ||
files: PropTypes.instanceOf(Map).isRequired, | ||
}; | ||
|
||
export default TutorialEditor; |
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,71 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { css } from '@emotion/core'; | ||
import TutorialEditor from './TutorialEditor'; | ||
import Markdown from './Markdown'; | ||
|
||
const TutorialStep = ({ | ||
children, | ||
initialSelectedFile, | ||
step, | ||
index, | ||
title, | ||
totalSteps, | ||
}) => { | ||
return ( | ||
<div | ||
css={css` | ||
&:not(:last-child) { | ||
margin-bottom: 2rem; | ||
} | ||
`} | ||
> | ||
<p | ||
css={css` | ||
font-size: 0.75rem; | ||
color: var(--accent-text-color); | ||
margin-bottom: 0; | ||
`} | ||
> | ||
Step {index + 1} of {totalSteps} | ||
</p> | ||
<h3 | ||
css={css` | ||
font-size: 1rem; | ||
font-weight: bold; | ||
margin-top: 0 !important; | ||
`} | ||
> | ||
<Markdown source={title} /> | ||
</h3> | ||
{step ? ( | ||
<div | ||
css={css` | ||
display: grid; | ||
grid-template-columns: repeat(2, calc(50% - 0.5rem)); | ||
grid-gap: 1rem; | ||
`} | ||
> | ||
<div>{children}</div> | ||
<TutorialEditor | ||
files={step} | ||
initialSelectedFile={initialSelectedFile} | ||
/> | ||
</div> | ||
) : ( | ||
children | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
TutorialStep.propTypes = { | ||
children: PropTypes.node, | ||
index: PropTypes.number.isRequired, | ||
initialSelectedFile: PropTypes.string.isRequired, | ||
step: PropTypes.instanceOf(Map).isRequired, | ||
title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), | ||
totalSteps: PropTypes.number.isRequired, | ||
}; | ||
|
||
export default TutorialStep; |
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.