Skip to content

Add example #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .storybook/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ setAddon(chaptersAddon);
addDecorator(story => (
<Fragment>
<PageTitle />
<Spacer />
<Spacer dir="down" />
<div style={{ padding: '20px' }}>
{story()}
</div>
<Spacer dir="up" />
</Fragment>
));

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"postpublish": "npm run publish-storybook",
"prepublish": ". ./.scripts/prepublish.sh",
"publish-storybook": "bash .scripts/publish_storybook.sh",
"storybook": "start-storybook -p 9010",
"storybook": "NODE_ENV=development start-storybook -p 9010",
"test-watch": "npm run testonly -- --watch --watch-extensions js",
"test": "npm run lint && npm run testonly",
"testonly": "jest --config jest-config.js"
Expand Down Expand Up @@ -64,8 +64,11 @@
"react": "^16.0.0",
"react-aspect-ratio": "^1.0.3",
"react-dom": "^16.0.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-storybook-addon-chapters": "^3.1.3",
"react-test-renderer": "^16.0.0",
"recompose": "^0.30.0",
"storybook-addon-jsx": "^7.0.0"
},
"peerDependencies": {
Expand Down
60 changes: 58 additions & 2 deletions src/stories/chapters/enterCallback.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@ import React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import omit from 'lodash/omit';
import { BrowserRouter as Router } from 'react-router-dom';

import handleViewport, { customProps } from '../../index';
import { handleViewport, customProps } from '../../index';
import { Block } from '../common/themeComponent';
import RefBlock from '../common/ref';

const ViewportBlock = handleViewport(Block, {}, { disconnectOnLeave: false });
const ViewportBlockThreshold = handleViewport(
Block,
{ threshold: [0, 1.0] },
{ disconnectOnLeave: false }
);

const CustomAnchor = ({ forwardedRef, inViewport, ...restProps }) => {
const text = inViewport ? 'Link (in viewport)' : 'Link (not in viewport)';
return (
<a
href="https://github.com/roderickhsiao/react-in-viewport#readme"
{...omit(restProps, customProps)}
ref={forwardedRef}
style={{ padding: '20px 0' }}
style={{
padding: '20px 0',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
width: '400px',
height: '300px',
backgroundColor: inViewport ? '#217ac0' : '#ff9800',
color: '#fff'
}}
>
{text}
</a>
Expand Down Expand Up @@ -59,4 +76,43 @@ storiesOf('Enter and Leave callback', module)
]
}
]
})
.addWithChapters('Ref component', {
subtitle: 'Multiple layer HOC',
chapters: [
{
sections: [
{
title: 'onEnterViewport and onLeaveViewport callback',
sectionFn: () => (
<Router>
<RefBlock
onEnterViewport={() => action('callback')('onEnterViewport')}
onLeaveViewport={() => action('callback')('onLeaveViewport')}
/>
</Router>
)
}
]
}
]
})
.addWithChapters('Threshold', {
subtitle: 'Set different threshold',
chapters: [
{
sections: [
{
title: 'onEnterViewport and onLeaveViewport callback',
subtitle: 'threshold 1.0',
sectionFn: () => (
<ViewportBlockThreshold
onEnterViewport={() => action('callbackWithThreshold')('onEnterViewport')}
onLeaveViewport={() => action('callbackWithThreshold')('onLeaveViewport')}
/>
)
}
]
}
]
});
26 changes: 26 additions & 0 deletions src/stories/common/ref.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { compose, setDisplayName } from 'recompose';
import { withRouter } from 'react-router';

import { handleViewport } from '../../index';

import { Block } from './themeComponent';

// https://github.com/ReactTraining/react-router/issues/6056
const withRouterAndRef = Wrapped => {
const WithRouter = withRouter(({ forwardRef, ...otherProps }) => (
<Wrapped ref={forwardRef} {...otherProps} />
));
const WithRouterAndRef = React.forwardRef((props, ref) => (
<WithRouter {...props} forwardRef={ref} />
));
const name = Wrapped.displayName || Wrapped.name;
WithRouterAndRef.displayName = `withRouterAndRef(${name})`;
return WithRouterAndRef;
};

export default compose(
setDisplayName('DemoComponent'),
withRouterAndRef,
handleViewport
)(Block);
6 changes: 3 additions & 3 deletions src/stories/common/themeComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ export const Block = props => {

Block.displayName = 'Block';

export const Spacer = () => (
export const Spacer = ({ dir = 'down' }) => (
<div style={{ height: '100vh', padding: '20px' }}>
<p>
Scroll down to make component in viewport{' '}
{`Scroll ${dir} to make component in viewport`}{' '}
<span role="img" aria-label="down">
👇
{dir === 'down' ? '👇' : '☝️'}
</span>{' '}
</p>
</div>
Expand Down
Loading