-
Notifications
You must be signed in to change notification settings - Fork 52
docs(Examples): use sources to render dedicated examples, separate contexts #644
Changes from all commits
bcb97e1
c93098a
19aada0
047834f
3526341
994b6e9
93e1bc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,49 @@ | ||
import * as _ from 'lodash/fp' | ||
import * as PropTypes from 'prop-types' | ||
import * as React from 'react' | ||
|
||
import { exampleContext, exampleKebabNameToFilename, parseExamplePath } from 'docs/src/utils' | ||
import SourceRender from 'react-source-render' | ||
|
||
import { ExampleSource } from 'docs/src/types' | ||
import { | ||
exampleSourcesContext, | ||
exampleKebabNameToSourceFilename, | ||
parseExamplePath, | ||
} from 'docs/src/utils' | ||
import PageNotFound from '../views/PageNotFound' | ||
import { babelConfig, importResolver } from './Playground/renderConfig' | ||
|
||
const examplePaths = exampleContext.keys() | ||
const examplePaths = exampleSourcesContext.keys() | ||
|
||
const ExternalExampleLayout: any = props => { | ||
const { exampleName } = props.match.params | ||
const exampleFilename = exampleKebabNameToFilename(exampleName) | ||
const exampleFilename = exampleKebabNameToSourceFilename(exampleName) | ||
|
||
const examplePath = _.find(path => { | ||
const { exampleName } = parseExamplePath(path) | ||
return exampleFilename === exampleName | ||
}, examplePaths) | ||
|
||
if (!examplePath) return <PageNotFound /> | ||
|
||
const ExampleComponent = exampleContext(examplePath).default | ||
if (!ExampleComponent) return <PageNotFound /> | ||
|
||
return <ExampleComponent /> | ||
const exampleSource: ExampleSource = exampleSourcesContext(examplePath) | ||
|
||
return ( | ||
<SourceRender | ||
babelConfig={babelConfig} | ||
source={exampleSource.js} | ||
renderHtml={false} | ||
resolver={importResolver} | ||
> | ||
<SourceRender.Consumer> | ||
{({ element, error }) => ( | ||
<> | ||
{element} | ||
{/* This block allows to see issues with examples as visual regressions. */} | ||
{error && <div style={{ fontSize: '5rem', color: 'red' }}>{error.toString()}</div>} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a little clumsy, but it will show us broken examples as visual regressions. |
||
</> | ||
)} | ||
</SourceRender.Consumer> | ||
</SourceRender> | ||
) | ||
} | ||
|
||
ExternalExampleLayout.propTypes = { | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* The Webpack Context for doc site example groups. | ||
*/ | ||
export const exampleIndexContext = require.context('docs/src/examples/', true, /index.tsx$/) | ||
|
||
/** | ||
* The Webpack Context for doc site example sources. | ||
*/ | ||
export const exampleSourcesContext = require.context( | ||
'docs/src/exampleSources/', | ||
true, | ||
/.source.json$/, | ||
) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as _ from 'lodash' | ||
|
||
/** | ||
* Converts kebab-cased-example-name back into the original filename. | ||
* @param {string} exampleKebabName | ||
*/ | ||
const exampleKebabNameToSourceFilename = (exampleKebabName: string) => { | ||
// button-example => ButtonExample.source.json | ||
// button-example-shorthand => ButtonExample.shorthand.source.json | ||
return `${_.startCase(exampleKebabName) | ||
.replace(/ /g, '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can be a bit more expressive here. The only source of the whitespace could be either leading or trailing ones of the initial kebab-cased name. So, we might eliminate them before transform will happen, like that: const trimmedExampleKebabName = exampleKebabName.trim()
return `${_.startCase(trimmedExampleKebabName) ... }...` While this makes code more efficient, I would put efficiency as the least worthy benefit in this case - the primary goal of this move is rather to improve explicitness of where the spaces (that regex aims to eliminate) might happen from. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proposed solution is not correct because _.startCase('foo-bar')
// => 'Foo Bar' |
||
.replace(/Shorthand$/, '.shorthand')}.source.json` | ||
} | ||
|
||
export default exampleKebabNameToSourceFilename |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still not sure why we are relying on the React Context functionality to render source code - but this is definitely not the point of this PR, we could address this moment later (if necessary)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm looking for a better solution there.