Skip to content

Commit a7ee722

Browse files
committed
feat(Storybook): revamped storybook examples and added recipes
1 parent 6d4a7da commit a7ee722

File tree

35 files changed

+584
-367
lines changed

35 files changed

+584
-367
lines changed

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ npm-debug.log
1717
.tmp
1818
.eslintignore
1919
yarn.lock
20-
stories
20+
examples
2121
.storybook
2222
test
2323
example

.storybook/addons.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import '@storybook/addon-options/register';
2+
import 'storybook-readme/register';
23
import '@storybook/addon-actions/register';

.storybook/config.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
import * as storybook from '@storybook/react';
22
import { setOptions } from '@storybook/addon-options';
3-
import { setDefaults } from '@storybook/addon-info';
43

54
setOptions({
65
name: 'React Intersection Observer',
76
url: 'https://github.com/researchgate/react-intersection-observer',
7+
downPanelInRight: true,
88
});
99

10-
setDefaults({
11-
source: false,
12-
propTables: null,
13-
styles: stylesheet => ({
14-
...stylesheet,
15-
infoBody: {
16-
...stylesheet.infoBody,
17-
background: '#fafafa',
18-
color: '#444',
19-
boxShadow: 'rgba(0,0,0,.18) 0 0 2px 0, rgba(0,0,0,.12) 0 1px 3px 0',
20-
border: 0,
21-
marginBottom: 50,
22-
},
23-
}),
24-
});
25-
26-
storybook.configure(() => require('../stories/index.js'), module);
10+
storybook.configure(() => require('../examples/index.js'), module);

.storybook/webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ module.exports = {
88
loaders: ['style-loader', 'css-loader'],
99
include: path.resolve(__dirname, '../'),
1010
},
11+
{
12+
test: /\.md$/,
13+
loaders: ['raw-loader'],
14+
},
1115
],
1216
},
1317
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import withIntersectionObserver from './WithIntersectionObserver';
2+
import Target from './Target';
3+
4+
export default withIntersectionObserver(0.99)(Target);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## Higher order component
2+
3+
### withIntersectionObserver.js
4+
```jsx
5+
import React, { Component } from 'react';
6+
import Observer from '@researchgate/react-intersection-observer';
7+
8+
export default threshold => BaseComponent => {
9+
const displayName = BaseComponent.displayName || BaseComponent.name || 'Component';
10+
11+
return class WithIntersectionObserver extends Component {
12+
static displayName = `withIntersectionObserver(${displayName})`;
13+
14+
state = {
15+
isIntersecting: false,
16+
};
17+
18+
handleChange = ({ isIntersecting, intersectionRatio }) => {
19+
this.setState({ isIntersecting: isIntersecting && intersectionRatio >= threshold });
20+
};
21+
22+
render() {
23+
return (
24+
<Observer onChange={this.handleChange} threshold={threshold}>
25+
<BaseComponent {...this.props} isVisible={this.state.isIntersecting} />
26+
</Observer>
27+
);
28+
}
29+
};
30+
};
31+
```
32+
33+
### Target.js
34+
```jsx
35+
import React, { Component } from 'react';
36+
37+
export default class Target extends Component {
38+
render() {
39+
return (
40+
<div className={`box ${this.props.isVisible ? 'visible' : 'transparent'}`}>
41+
{this.props.isVisible ? 'Visible' : 'Culled'}
42+
</div>
43+
);
44+
}
45+
}
46+
```
47+
48+
### Component.js
49+
```jsx
50+
import withIntersectionObserver from './WithIntersectionObserver';
51+
import Target from './Target';
52+
53+
export default withIntersectionObserver(0.99)(Target);
54+
```
55+
56+
### Example.js
57+
```jsx
58+
import React from 'react';
59+
import Component from './Component';
60+
61+
export default () =>
62+
<div>
63+
<div className="header visible">Higher Order Component - Occlusion Culling</div>
64+
<div className="body body--center">
65+
<Component />
66+
<Component />
67+
<Component />
68+
<Component />
69+
<Component />
70+
</div>
71+
</div>
72+
);
73+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React, { Component } from 'react';
2+
3+
export default class Target extends Component {
4+
render() {
5+
return (
6+
<div className={`box ${this.props.isVisible ? 'visible' : 'transparent'}`}>
7+
{this.props.isVisible ? 'Visible' : 'Culled'}
8+
</div>
9+
);
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { Component } from 'react';
2+
import Observer from '../../../src/IntersectionObserver';
3+
4+
export default threshold => BaseComponent => {
5+
const displayName = BaseComponent.displayName || BaseComponent.name || 'Component';
6+
7+
return class WithIntersectionObserver extends Component {
8+
static displayName = `withIntersectionObserver(${displayName})`;
9+
10+
state = {
11+
isIntersecting: false,
12+
};
13+
14+
handleChange = ({ isIntersecting, intersectionRatio }) => {
15+
this.setState({ isIntersecting: isIntersecting && intersectionRatio >= threshold });
16+
};
17+
18+
render() {
19+
return (
20+
<Observer onChange={this.handleChange} threshold={threshold}>
21+
<BaseComponent {...this.props} isVisible={this.state.isIntersecting} />
22+
</Observer>
23+
);
24+
}
25+
};
26+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import withReadme from 'storybook-readme/with-readme';
3+
import Readme from './README.md';
4+
import Component from './Component';
5+
6+
export default withReadme(Readme, () =>
7+
<div>
8+
<div className="header visible">Higher Order Component - Occlusion Culling</div>
9+
<div className="body body--center">
10+
<Component />
11+
<Component />
12+
<Component />
13+
<Component />
14+
<Component />
15+
</div>
16+
</div>
17+
);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
import { action } from '@storybook/addon-actions';
3+
import Observer from '../../../src/IntersectionObserver';
4+
5+
const tracked = action('tracked');
6+
7+
export default class AdImpression extends React.Component {
8+
state = {
9+
tracked: '',
10+
};
11+
12+
handleChange = event => {
13+
if (event.isIntersecting && event.intersectionRatio >= 0.5) {
14+
this.recordedTimeout = setTimeout(() => {
15+
this.setState({ tracked: 'ad--tracked' });
16+
tracked(`ad #${this.props.index}`);
17+
}, 1000);
18+
return;
19+
}
20+
clearTimeout(this.recordedTimeout);
21+
};
22+
23+
render() {
24+
return (
25+
<Observer onChange={this.handleChange} threshold={0.5}>
26+
<div className={`ad ${this.state.tracked}`} />
27+
</Observer>
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)