Skip to content

Commit b06fcab

Browse files
scottyeckKyleAMathews
authored andcommitted
Styleguide example (#3304)
* Adds plop boilerplate * Adds base stuff * Configurable background colors * Rename props * Add a comment * Add a component README * Tweaks * Can query successfully * Adds components and index * Add prop types * Adds component index * Link back to component index * Showing props / methods * Add size prop to Button * Remove log * We are querying markdown * Feeds example html into renderer * Parsing / rendering HTML * Starts parsing nodes * It works * Add prop types * Adds size example to README * Sets up component index * Writes dyanmic component index to the cache * Renaming for clarity * Add description * Renaming * Possible strategy for ensuring durability of components in library previews * Using a particular prism theme * Fix proptypes * POC styling editor * Converts to table * new name
1 parent dadf533 commit b06fcab

File tree

19 files changed

+653
-0
lines changed

19 files changed

+653
-0
lines changed

examples/styleguide/.eslintrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"env": {
3+
"browser": true
4+
},
5+
"globals": {
6+
"graphql": false
7+
}
8+
}

examples/styleguide/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public
2+
.cache
3+
node_modules

examples/styleguide/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Gatsby Styleguide Example
2+
3+
> A living styleguide proof-of-concept built using Gatsby. Inspired by [react-styleguidist](https://react-styleguidist.js.org/).
4+
5+
https://styleguide.gatsbyjs.org
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const path = require(`path`)
2+
3+
module.exports = {
4+
plugins: [
5+
{
6+
resolve: `gatsby-source-filesystem`,
7+
options: {
8+
path: path.join(__dirname, `src/components`),
9+
name: `components`,
10+
},
11+
},
12+
{
13+
resolve: `gatsby-transformer-react-docgen`,
14+
},
15+
{
16+
resolve: `gatsby-transformer-remark`,
17+
},
18+
],
19+
}

examples/styleguide/gatsby-node.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const path = require(`path`)
2+
const fs = require(`fs`)
3+
const appRootDir = require(`app-root-dir`).get()
4+
5+
const componentPageTemplate = path.resolve(
6+
`src/templates/ComponentPage/index.js`
7+
)
8+
const tableOfContentsTemplate = path.resolve(`src/templates/TOC/index.js`)
9+
10+
exports.createPages = ({ graphql, boundActionCreators }) => {
11+
const { createPage } = boundActionCreators
12+
13+
return new Promise((resolve, reject) => {
14+
resolve(
15+
Promise.all([
16+
graphql(`
17+
{
18+
allComponentMetadata {
19+
edges {
20+
node {
21+
id
22+
displayName
23+
description {
24+
text
25+
}
26+
props {
27+
name
28+
type {
29+
value
30+
raw
31+
name
32+
}
33+
description {
34+
text
35+
}
36+
required
37+
}
38+
}
39+
}
40+
}
41+
}
42+
`),
43+
graphql(`
44+
{
45+
allMarkdownRemark(
46+
filter: { fileAbsolutePath: { regex: "/README.md/" } }
47+
) {
48+
edges {
49+
node {
50+
fileAbsolutePath
51+
html
52+
}
53+
}
54+
}
55+
}
56+
`),
57+
])
58+
.then(([docgenResult, markdownResult]) => {
59+
const errors = docgenResult.errors || markdownResult.errors
60+
if (errors) {
61+
reject(new Error(errors))
62+
return
63+
}
64+
65+
const allComponents = docgenResult.data.allComponentMetadata.edges.map(
66+
(edge, i) =>
67+
Object.assign({}, edge.node, {
68+
path: `/components/${edge.node.displayName.toLowerCase()}/`,
69+
html: markdownResult.data.allMarkdownRemark.edges[i].node.html,
70+
})
71+
)
72+
73+
const exportFileContents =
74+
allComponents
75+
.reduce((accumulator, { id, displayName }) => {
76+
const absolutePath = id.replace(/ absPath of.*$/, ``)
77+
accumulator.push(
78+
`export { default as ${displayName} } from "${absolutePath}"`
79+
)
80+
return accumulator
81+
}, [])
82+
.join(`\n`) + `\n`
83+
84+
fs.writeFileSync(
85+
path.join(appRootDir, `.cache/components.js`),
86+
exportFileContents
87+
)
88+
89+
allComponents.forEach(data => {
90+
const { path } = data
91+
const context = Object.assign({}, data, {
92+
allComponents,
93+
})
94+
createPage({
95+
path,
96+
component: componentPageTemplate,
97+
context,
98+
})
99+
})
100+
101+
createPage({
102+
path: `/components/`,
103+
component: tableOfContentsTemplate,
104+
context: {
105+
allComponents,
106+
},
107+
})
108+
})
109+
.catch(err => {
110+
console.log(err)
111+
throw new Error(err)
112+
})
113+
)
114+
})
115+
}

examples/styleguide/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "styleguide",
3+
"private": true,
4+
"description": "Gatsby example site demoing living-style-guide",
5+
"author": "[email protected]",
6+
"dependencies": {
7+
"app-root-dir": "^1.0.2",
8+
"gatsby": "latest",
9+
"gatsby-link": "latest",
10+
"gatsby-source-filesystem": "^1.5.11",
11+
"gatsby-transformer-react-docgen": "^1.0.11",
12+
"gatsby-transformer-remark": "^1.7.25",
13+
"glamor": "^2.20.40",
14+
"html-to-react": "^1.3.1",
15+
"react-live": "^1.7.1"
16+
},
17+
"license": "MIT",
18+
"main": "n/a",
19+
"scripts": {
20+
"develop": "gatsby develop",
21+
"build": "gatsby build"
22+
}
23+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import React from "react"
2+
import PropTypes from "prop-types"
3+
import { css } from "glamor"
4+
5+
const blue = `blue`
6+
const orange = `orange`
7+
const green = `green`
8+
9+
const colors = {
10+
[blue]: {
11+
primary: `#A0CED9`,
12+
hover: `#92BCC6`,
13+
},
14+
[orange]: {
15+
primary: `#EAB69B`,
16+
hover: `#E8AF91`,
17+
},
18+
[green]: {
19+
primary: `#ADF7B6`,
20+
hover: `#9EE1A6`,
21+
},
22+
}
23+
24+
const sm = `sm`
25+
const md = `md`
26+
const lg = `lg`
27+
28+
const sizes = {
29+
[sm]: {
30+
fontSize: `14px`,
31+
padding: `12px 20px`,
32+
minWidth: `160px`,
33+
},
34+
[md]: {
35+
fontSize: `18px`,
36+
padding: `16px 24px`,
37+
minWidth: `200px`,
38+
},
39+
[lg]: {
40+
fontSize: `22px`,
41+
padding: `20px 28px`,
42+
minWidth: `260px`,
43+
},
44+
}
45+
46+
const styles = ({ backgroundColor, size }) => {
47+
const backgroundColorConfig =
48+
colors[backgroundColor] || colors[Button.defaultProps.backgroundColor]
49+
const sizeConfig = sizes[size] || sizes[Button.defaultProps.size]
50+
return css({
51+
backgroundColor: backgroundColorConfig.primary,
52+
...sizeConfig,
53+
color: `rgba(36, 47, 60, 0.66)`,
54+
display: `inline-block`,
55+
borderRadius: `3px`,
56+
border: 0,
57+
cursor: `pointer`,
58+
"&:hover": {
59+
backgroundColor: backgroundColorConfig.hover,
60+
},
61+
})
62+
}
63+
64+
/**
65+
* The `<Button>` is a foundational trigger component for capturing
66+
* and guiding user-interaction.
67+
*/
68+
const Button = ({ backgroundColor, size, ...rest }) => (
69+
<button className={styles({ backgroundColor, size })} {...rest} />
70+
)
71+
72+
Button.propTypes = {
73+
/** The color to use as the background */
74+
backgroundColor: PropTypes.oneOf(Object.keys(colors)),
75+
/** The size of the button */
76+
size: PropTypes.oneOf(Object.keys(sizes)),
77+
}
78+
79+
Button.defaultProps = {
80+
backgroundColor: blue,
81+
size: md,
82+
}
83+
84+
export default Button
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
The basic button.
2+
3+
```
4+
<Button>Get Started</Button>
5+
```
6+
7+
Colors are configurable.
8+
9+
```
10+
<div>
11+
<div><Button backgroundColor="blue">Get Started</Button></div>
12+
<div><Button backgroundColor="green">Get Started</Button></div>
13+
<div><Button backgroundColor="orange">Get Started</Button></div>
14+
</div>
15+
```
16+
17+
Sizes are also configurable.
18+
19+
```
20+
<div>
21+
<div><Button size="sm">Get Started</Button></div>
22+
<div><Button size="md">Get Started</Button></div>
23+
<div><Button size="lg">Get Started</Button></div>
24+
</div>
25+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./Button"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react"
2+
import { Route, Redirect } from "react-router-dom"
3+
4+
class Home extends React.Component {
5+
render() {
6+
return (
7+
<Route exact path="/" render={() => <Redirect to="/components/" />} />
8+
)
9+
}
10+
}
11+
12+
export default Home

0 commit comments

Comments
 (0)