Skip to content

Commit beeb7cb

Browse files
Juha Karttunensapegin
Juha Karttunen
authored andcommitted
Docs: Add example of using Context (styleguidist#1411)
Closes styleguidist#1374
1 parent d1e528f commit beeb7cb

File tree

7 files changed

+82
-1
lines changed

7 files changed

+82
-1
lines changed

docs/Documenting.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ class ModalExample extends React.Component {
310310
;<ModalExample />
311311
````
312312

313+
If a component consumes React Context, you need a context provider in the example or in a custom `Wrapper` component. See [ThemeButton example](https://github.com/styleguidist/react-styleguidist/tree/master/examples/sections/src/components/ThemeButton).
314+
313315
> **Note:** If you need a more complex demo it’s often a good idea to define it in a separate JavaScript file and `import` it in Markdown.
314316

315317
## Limitations

examples/sections/src/ThemeContext.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
3+
/**
4+
* Context that stores selected application theme: 'light' | 'dark'
5+
*/
6+
export default React.createContext('light');
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
An example of using React Context with a component:
2+
3+
```jsx
4+
import ThemeContext from '../../ThemeContext.js'
5+
6+
initialState = { theme: 'light' }
7+
const toggleTheme = () => {
8+
setState({ theme: state.theme === 'light' ? 'dark' : 'light' })
9+
}
10+
;<>
11+
<div
12+
style={{
13+
backgroundColor: '#ccc',
14+
margin: '-16px -16px 16px -16px',
15+
padding: '16px'
16+
}}
17+
>
18+
<button onClick={toggleTheme}>Theme: {state.theme}</button>
19+
</div>
20+
<div>
21+
<ThemeContext.Provider value={state.theme}>
22+
<ThemeButton>Themable button</ThemeButton>
23+
</ThemeContext.Provider>
24+
</div>
25+
</>
26+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.ThemeButton {
2+
padding: 0.5em 1.5em;
3+
border: 1px solid currentColor;
4+
border-radius: 0.3em;
5+
font-size: 16px;
6+
font-weight: bold;
7+
text-align: center;
8+
vertical-align: middle;
9+
cursor: pointer;
10+
}
11+
12+
.ThemeButton--light {
13+
color: #111133;
14+
background-color: #eeeeff;
15+
}
16+
17+
.ThemeButton--dark {
18+
color: #eeeeff;
19+
background-color: #111133;
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import ThemeContext from '../../ThemeContext';
4+
import './ThemeButton.css';
5+
6+
/**
7+
* Button that is themed by a context value
8+
*/
9+
export default class ThemeButton extends Component {
10+
render() {
11+
const className = this.context === 'light' ? 'ThemeButton ThemeButton--light' : 'ThemeButton ThemeButton--dark';
12+
return <button className={className}>{this.props.children}</button>;
13+
}
14+
}
15+
16+
/** ThemeContext {string} */
17+
ThemeButton.contextType = ThemeContext;
18+
ThemeButton.propTypes = {
19+
/**
20+
* ThemeButton label
21+
*/
22+
children: PropTypes.string.isRequired,
23+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './ThemeButton';

examples/sections/styleguide.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ module.exports = {
3939
sections: [
4040
{
4141
name: 'Buttons',
42-
components: () => ['./src/components/Button/Button.js'],
42+
components: () => [
43+
'./src/components/Button/Button.js',
44+
'./src/components/ThemeButton/ThemeButton.js',
45+
],
4346
exampleMode: 'expand', // 'hide' | 'collapse' | 'expand'
4447
usageMode: 'hide', // 'hide' | 'collapse' | 'expand'
4548
},

0 commit comments

Comments
 (0)