Skip to content

Commit 075e3e6

Browse files
committed
initial commit
1 parent ecf89c0 commit 075e3e6

File tree

7 files changed

+2268
-2
lines changed

7 files changed

+2268
-2
lines changed

.babelrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"modules": false
7+
}
8+
],
9+
"stage-2"
10+
]
11+
}

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules
2+
/dist/*
3+
/yarn-error.log
4+
/coverage
5+
*.sublime-project
6+
*.sublime-workspace

README.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
1-
# react-scroll-into-view-if-needed
2-
A thin react component wrapper around scroll-into-view-if-needed
1+
<h3 align="center">
2+
React Scroll Into View If Needed
3+
</h3>
4+
5+
## Description
6+
7+
Just a thin component wrapper around the fantastic [scroll-into-view-if-needed](https://www.npmjs.com/package/scroll-into-view-if-needed) polyfill.
8+
9+
## Install
10+
11+
`yarn add react-scroll-into-view-if-needed`
12+
13+
or
14+
15+
`npm install react-scroll-into-view-if-needed --save`
16+
17+
## Example
18+
19+
```js
20+
import ScrollIntoViewIfNeeded from 'react-scroll-into-view-if-needed';
21+
22+
const MyComponent = () => (
23+
<div style={{ paddingTop: 2000 }}>
24+
<ScrollIntoViewIfNeeded>
25+
<div>Hello</div>
26+
</ScrollIntoViewIfNeeded>
27+
</div>
28+
);
29+
```
30+
31+
## Props
32+
33+
#### active
34+
> Default: `true`
35+
36+
The `active` prop allows controll of _when_ to scroll to the component. By default, it will attempt to scroll as soon as it is mounted, but you can set this prop to manually control when to trigger the scroll behavior.
37+
38+
#### elementType
39+
> Default: `'div'`
40+
41+
Set the wrapper component type. See the React [createElement](https://reactjs.org/docs/react-api.html#createelement) api
42+
43+
#### options
44+
> Default: `{ duration: 250, easing: 'easeOut' }`
45+
46+
The `options` prop simply passes options to `scroll-into-view-if-needed`. See all the possible options in their [API documentation](https://www.npmjs.com/package/scroll-into-view-if-needed#api)

package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "react-scroll-into-view-if-needed",
3+
"version": "1.0.0",
4+
"description": "A thin component wrapper around scroll-into-view-if-needed",
5+
"main": "dist/umd/index.js",
6+
"module": "dist/es/index.js",
7+
"repository": "[email protected]:icd2k3/react-scroll-into-view-if-needed.git",
8+
"author": "JUSTIN SCHRADER <[email protected]>",
9+
"license": "MIT",
10+
"peerDependencies": {
11+
"react": "^16.3.0"
12+
},
13+
"dependencies": {
14+
"scroll-into-view-if-needed": "^1.5.0"
15+
},
16+
"devDependencies": {
17+
"babel-cli": "^6.26.0",
18+
"babel-core": "^6.26.0",
19+
"babel-eslint": "^8.2.3",
20+
"babel-plugin-external-helpers": "^6.22.0",
21+
"babel-preset-env": "^1.6.1",
22+
"babel-preset-stage-2": "^6.24.1",
23+
"prop-types": "^15.6.1",
24+
"react": "^16.3.2",
25+
"rollup": "^0.58.0",
26+
"rollup-plugin-babel": "^3.0.3",
27+
"rollup-plugin-commonjs": "^9.1.0",
28+
"rollup-plugin-node-resolve": "^3.3.0"
29+
},
30+
"scripts": {
31+
"prepublishOnly": "yarn build",
32+
"build": "rollup -c"
33+
}
34+
}

rollup.config.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import babel from 'rollup-plugin-babel';
2+
import commonjs from 'rollup-plugin-commonjs';
3+
import resolve from 'rollup-plugin-node-resolve';
4+
5+
const pkg = require('./package.json');
6+
7+
const external = Object.keys(pkg.peerDependencies);
8+
9+
const globals = {
10+
react: 'React',
11+
};
12+
13+
const config = {
14+
input: 'src/index.js',
15+
plugins: [
16+
babel({
17+
exclude: 'node_modules/**',
18+
plugins: ['external-helpers'],
19+
}),
20+
resolve({
21+
module: true,
22+
jsnext: true,
23+
main: true,
24+
}),
25+
commonjs(),
26+
],
27+
external,
28+
output: [
29+
{
30+
exports: 'named',
31+
file: pkg.main,
32+
format: 'umd',
33+
globals,
34+
name: 'react-scroll-into-view-if-needed',
35+
sourcemap: true,
36+
},
37+
{
38+
exports: 'named',
39+
file: pkg.module,
40+
format: 'es',
41+
globals,
42+
sourcemap: true,
43+
},
44+
],
45+
};
46+
47+
export default config;

src/index.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { createElement, createRef, PureComponent } from 'react';
2+
import PropTypes from 'prop-types';
3+
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed';
4+
5+
export default class ScrollIntoViewIfNeeded extends PureComponent {
6+
static propTypes = {
7+
active: PropTypes.bool,
8+
children: PropTypes.node.isRequired,
9+
elementType: PropTypes.string,
10+
options: PropTypes.shape({
11+
boundary: PropTypes.node,
12+
centerIfNeeded: PropTypes.bool,
13+
duration: PropTypes.number,
14+
easing: PropTypes.oneOf([
15+
'ease',
16+
'easeIn',
17+
'easeOut',
18+
'easeInOut',
19+
'linear',
20+
]),
21+
offset: PropTypes.shape(),
22+
top: PropTypes.number,
23+
right: PropTypes.number,
24+
bottom: PropTypes.number,
25+
left: PropTypes.number,
26+
}),
27+
};
28+
29+
static defaultProps = {
30+
active: true,
31+
elementType: 'div',
32+
options: {
33+
duration: 250,
34+
easing: 'easeOut',
35+
},
36+
};
37+
38+
constructor(props) {
39+
super(props);
40+
this.node = createRef();
41+
}
42+
43+
componentDidMount() {
44+
const { active } = this.props;
45+
if (active) {
46+
this.handleScrollIntoViewIfNeeded();
47+
}
48+
}
49+
50+
componentDidUpdate({ active }) {
51+
const { active: isNowActive } = this.props;
52+
if (!active && isNowActive) {
53+
this.handleScrollIntoViewIfNeeded();
54+
}
55+
}
56+
57+
handleScrollIntoViewIfNeeded = () => {
58+
const { options } = this.props;
59+
const { current: node } = this.node;
60+
scrollIntoViewIfNeeded(node, options);
61+
};
62+
63+
render() {
64+
const { elementType, children } = this.props;
65+
return createElement(elementType, { ref: this.node, ...this.props }, children);
66+
}
67+
}

0 commit comments

Comments
 (0)