Skip to content

Commit aca2328

Browse files
committed
Upgrade to latest scroll-into-view-if-needed
1 parent 2a000b0 commit aca2328

File tree

6 files changed

+137
-70
lines changed

6 files changed

+137
-70
lines changed

README.md

+52-9
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,69 @@ The standalone version does **not** come bundled with [scroll-into-view-if-neede
3939

4040
`import ScrollIntoViewIfNeeded from 'react-scroll-into-view-if-needed/dist/umd/standalone.js'`
4141

42-
## Optional Props
43-
The following props are supported by the component.
44-
45-
You can _also_ pass standard html attributes like `className`, `id`, etc...
46-
4742
#### active
4843
> Type: `boolean`
4944
> Default: `true`
5045
5146
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 from the parent component.
5247

48+
```js
49+
class Example extends React.PureComponent {
50+
constructor(props) {
51+
super(props);
52+
this.state = { active: false };
53+
}
54+
55+
handleScrollToggle = () => this.setState({ active: !this.state.active });
56+
57+
render() {
58+
const { active } = this.state;
59+
60+
return (
61+
<div>
62+
<button onClick={this.handleScrollToggle}>Scroll</button>
63+
<div style={{ paddingTop: 2000 }}>
64+
<ScrollIntoViewIfNeeded active={active}>
65+
<div>Hello</div>
66+
</ScrollIntoViewIfNeeded>
67+
</div>
68+
</div>
69+
);
70+
}
71+
}
72+
```
73+
74+
#### options
75+
> Type: `object`
76+
> Default: `{ behavior: 'smooth', scrollMode: 'if-needed' }`
77+
> Full list of options [here](https://www.npmjs.com/package/scroll-into-view-if-needed#api)
78+
79+
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).
80+
81+
```js
82+
<ScrollIntoViewIfNeeded options={{
83+
scrollMode: 'always',
84+
}}>
85+
<div>Hello</div>
86+
</ScrollIntoViewIfNeeded>
87+
```
88+
5389
#### elementType
5490
> Type: `string`
5591
> Default: `'div'`
5692
5793
Set the wrapper component type. For example, this could also be `'footer'`, `'button'`, etc... See the React [createElement](https://reactjs.org/docs/react-api.html#createelement) api.
5894

59-
#### options
60-
> Type: `object`
61-
> Default: `{ duration: 250, easing: 'easeOut' }`
95+
```js
96+
<ScrollIntoViewIfNeeded elementType="span">
97+
<div>Hello</div>
98+
</ScrollIntoViewIfNeeded>
99+
```
62100

63-
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).
101+
#### className, id, etc
102+
103+
You can also pass normal element attributes like `className` to the component.
104+
105+
## Upgrading from v1
64106

107+
This project has been updated along with `scroll-into-view-if-needed` and accepts the same `options`. Check out the [migration guide](https://www.npmjs.com/package/scroll-into-view-if-needed#breaking-api-changes-from-v1) over there!

package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-scroll-into-view-if-needed",
3-
"version": "1.0.3",
3+
"version": "2.0.0",
44
"description": "A thin component wrapper around scroll-into-view-if-needed",
55
"main": "dist/umd/index.js",
66
"module": "dist/es/index.js",
@@ -20,14 +20,14 @@
2020
"dependencies": {},
2121
"devDependencies": {
2222
"babel-cli": "^6.26.0",
23-
"babel-core": "^6.26.0",
23+
"babel-core": "^6.26.3",
2424
"babel-eslint": "^8.2.3",
2525
"babel-jest": "^22.4.3",
2626
"babel-plugin-external-helpers": "^6.22.0",
2727
"babel-preset-env": "^1.6.1",
2828
"babel-preset-react": "^6.24.1",
2929
"babel-preset-stage-2": "^6.24.1",
30-
"coveralls": "^3.0.0",
30+
"coveralls": "^3.0.1",
3131
"cross-env": "^5.1.4",
3232
"enzyme": "^3.3.0",
3333
"enzyme-adapter-react-16": "^1.1.1",
@@ -40,11 +40,11 @@
4040
"prop-types": "^15.6.1",
4141
"react": "^16.3.2",
4242
"react-dom": "^16.3.2",
43-
"rollup": "^0.58.1",
44-
"rollup-plugin-babel": "^3.0.3",
45-
"rollup-plugin-commonjs": "^9.1.0",
43+
"rollup": "^0.58.2",
44+
"rollup-plugin-babel": "^3.0.4",
45+
"rollup-plugin-commonjs": "^9.1.3",
4646
"rollup-plugin-node-resolve": "^3.3.0",
47-
"scroll-into-view-if-needed": "^1.5.0"
47+
"scroll-into-view-if-needed": "^2.1.5"
4848
},
4949
"scripts": {
5050
"build": "rollup -c && cross-env BUILD_TYPE=standalone rollup -c",

src/__tests__/__snapshots__/index.test.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ ShallowWrapper {
99
elementType="div"
1010
options={
1111
Object {
12-
"duration": 250,
13-
"easing": "easeOut",
12+
"behavior": "smooth",
13+
"scrollMode": "if-needed",
1414
}
1515
}
1616
>
@@ -86,8 +86,8 @@ ShallowWrapper {
8686
elementType="div"
8787
options={
8888
Object {
89-
"duration": 250,
90-
"easing": "easeOut",
89+
"behavior": "smooth",
90+
"scrollMode": "if-needed",
9191
}
9292
}
9393
>

src/__tests__/index.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ test('Render with no props', () => {
1515
expect(wrapper).toMatchSnapshot();
1616
expect(mockScroll).toHaveBeenCalledTimes(1);
1717
expect(mockScroll).toHaveBeenCalledWith(null, {
18-
duration: 250,
19-
easing: 'easeOut',
18+
behavior: 'smooth',
19+
scrollMode: 'if-needed',
2020
});
2121
});
2222

@@ -71,10 +71,10 @@ test('Expect elementType to be respected', () => {
7171

7272
test('Expect custom options to be passed to `scroll-into-view-if-needed`', () => {
7373
shallow(
74-
<ScrollIntoViewIfNeeded options={{ centerIfNeeded: true }}>
74+
<ScrollIntoViewIfNeeded options={{ block: 'center' }}>
7575
<MockChild />
7676
</ScrollIntoViewIfNeeded>,
7777
);
7878

79-
expect(mockScroll).toHaveBeenLastCalledWith(null, { centerIfNeeded: true });
79+
expect(mockScroll).toHaveBeenLastCalledWith(null, { block: 'center' });
8080
});

src/index.js

+29-17
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,43 @@ export default class ScrollIntoViewIfNeeded extends PureComponent {
1010
elementType: PropTypes.string,
1111
// this shape should mirror the scroll-into-view-if-needed options
1212
options: PropTypes.shape({
13-
boundary: PropTypes.node,
14-
centerIfNeeded: PropTypes.bool,
15-
duration: PropTypes.number,
16-
easing: PropTypes.oneOf([
17-
'ease',
18-
'easeIn',
19-
'easeOut',
20-
'easeInOut',
21-
'linear',
13+
behavior: PropTypes.oneOfType([
14+
PropTypes.oneOf([
15+
'auto',
16+
'smooth',
17+
'instant',
18+
]),
19+
PropTypes.func,
20+
]),
21+
block: PropTypes.oneOf([
22+
'center',
23+
'end',
24+
'nearest',
25+
'start',
26+
]),
27+
inline: PropTypes.oneOf([
28+
'center',
29+
'end',
30+
'nearest',
31+
'start',
32+
]),
33+
scrollMode: PropTypes.oneOf([
34+
'always',
35+
'if-needed',
36+
]),
37+
boundary: PropTypes.oneOfType([
38+
PropTypes.element,
39+
PropTypes.func,
2240
]),
23-
offset: PropTypes.shape({
24-
top: PropTypes.number,
25-
right: PropTypes.number,
26-
bottom: PropTypes.number,
27-
left: PropTypes.number,
28-
}),
2941
}),
3042
};
3143

3244
static defaultProps = {
3345
active: true,
3446
elementType: 'div',
3547
options: {
36-
duration: 250,
37-
easing: 'easeOut',
48+
behavior: 'smooth',
49+
scrollMode: 'if-needed',
3850
},
3951
};
4052

yarn.lock

+41-29
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,6 @@ align-text@^0.1.1, align-text@^0.1.3:
142142
longest "^1.0.1"
143143
repeat-string "^1.5.2"
144144

145-
amator@1.0.1:
146-
version "1.0.1"
147-
resolved "https://registry.yarnpkg.com/amator/-/amator-1.0.1.tgz#0fd3663fef5f3334d088cf68a70ba74398aa0e26"
148-
dependencies:
149-
bezier-easing "^2.0.3"
150-
151145
amdefine@>=0.0.4:
152146
version "1.0.1"
153147
resolved "https://registry.yarnpkg.com/amdefine/-/amdefine-1.0.1.tgz#4a5282ac164729e93619bcfd3ad151f817ce91f5"
@@ -394,6 +388,30 @@ babel-core@^6.0.0, babel-core@^6.26.0:
394388
slash "^1.0.0"
395389
source-map "^0.5.6"
396390

391+
babel-core@^6.26.3:
392+
version "6.26.3"
393+
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.26.3.tgz#b2e2f09e342d0f0c88e2f02e067794125e75c207"
394+
dependencies:
395+
babel-code-frame "^6.26.0"
396+
babel-generator "^6.26.0"
397+
babel-helpers "^6.24.1"
398+
babel-messages "^6.23.0"
399+
babel-register "^6.26.0"
400+
babel-runtime "^6.26.0"
401+
babel-template "^6.26.0"
402+
babel-traverse "^6.26.0"
403+
babel-types "^6.26.0"
404+
babylon "^6.18.0"
405+
convert-source-map "^1.5.1"
406+
debug "^2.6.9"
407+
json5 "^0.5.1"
408+
lodash "^4.17.4"
409+
minimatch "^3.0.4"
410+
path-is-absolute "^1.0.1"
411+
private "^0.1.8"
412+
slash "^1.0.0"
413+
source-map "^0.5.7"
414+
397415
babel-eslint@^8.2.3:
398416
version "8.2.3"
399417
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-8.2.3.tgz#1a2e6681cc9bc4473c32899e59915e19cd6733cf"
@@ -1056,10 +1074,6 @@ bcrypt-pbkdf@^1.0.0:
10561074
dependencies:
10571075
tweetnacl "^0.14.3"
10581076

1059-
bezier-easing@^2.0.3:
1060-
version "2.0.3"
1061-
resolved "https://registry.yarnpkg.com/bezier-easing/-/bezier-easing-2.0.3.tgz#cb493fddb7f8920ecca00973344ce0518885f17e"
1062-
10631077
binary-extensions@^1.0.0:
10641078
version "1.11.0"
10651079
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.11.0.tgz#46aa1751fb6a2f93ee5e689bb1087d4b14c6c205"
@@ -1367,7 +1381,7 @@ contains-path@^0.1.0:
13671381
version "0.1.0"
13681382
resolved "https://registry.yarnpkg.com/contains-path/-/contains-path-0.1.0.tgz#fe8cf184ff6670b6baef01a9d4861a5cbec4120a"
13691383

1370-
convert-source-map@^1.4.0, convert-source-map@^1.5.0:
1384+
convert-source-map@^1.4.0, convert-source-map@^1.5.0, convert-source-map@^1.5.1:
13711385
version "1.5.1"
13721386
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.5.1.tgz#b8278097b9bc229365de5c62cf5fcaed8b5599e5"
13731387

@@ -1387,9 +1401,9 @@ core-util-is@1.0.2, core-util-is@~1.0.0:
13871401
version "1.0.2"
13881402
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
13891403

1390-
coveralls@^3.0.0:
1391-
version "3.0.0"
1392-
resolved "https://registry.yarnpkg.com/coveralls/-/coveralls-3.0.0.tgz#22ef730330538080d29b8c151dc9146afde88a99"
1404+
coveralls@^3.0.1:
1405+
version "3.0.1"
1406+
resolved "https://registry.yarnpkg.com/coveralls/-/coveralls-3.0.1.tgz#12e15914eaa29204e56869a5ece7b9e1492d2ae2"
13931407
dependencies:
13941408
js-yaml "^3.6.1"
13951409
lcov-parse "^0.0.10"
@@ -3842,7 +3856,7 @@ pretty-format@^22.4.3:
38423856
ansi-regex "^3.0.0"
38433857
ansi-styles "^3.2.0"
38443858

3845-
private@^0.1.6, private@^0.1.7:
3859+
private@^0.1.6, private@^0.1.7, private@^0.1.8:
38463860
version "0.1.8"
38473861
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"
38483862

@@ -4225,15 +4239,15 @@ rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.5.4, rimraf@^2.6.1:
42254239
dependencies:
42264240
glob "^7.0.5"
42274241

4228-
rollup-plugin-babel@^3.0.3:
4229-
version "3.0.3"
4230-
resolved "https://registry.yarnpkg.com/rollup-plugin-babel/-/rollup-plugin-babel-3.0.3.tgz#63adedc863130327512a4a9006efc2241c5b7c15"
4242+
rollup-plugin-babel@^3.0.4:
4243+
version "3.0.4"
4244+
resolved "https://registry.yarnpkg.com/rollup-plugin-babel/-/rollup-plugin-babel-3.0.4.tgz#41b3e762fe64450dd61da3105a2cf7ad76be4edc"
42314245
dependencies:
42324246
rollup-pluginutils "^1.5.0"
42334247

4234-
rollup-plugin-commonjs@^9.1.0:
4235-
version "9.1.0"
4236-
resolved "https://registry.yarnpkg.com/rollup-plugin-commonjs/-/rollup-plugin-commonjs-9.1.0.tgz#468341aab32499123ee9a04b22f51d9bf26fdd94"
4248+
rollup-plugin-commonjs@^9.1.3:
4249+
version "9.1.3"
4250+
resolved "https://registry.yarnpkg.com/rollup-plugin-commonjs/-/rollup-plugin-commonjs-9.1.3.tgz#37bfbf341292ea14f512438a56df8f9ca3ba4d67"
42374251
dependencies:
42384252
estree-walker "^0.5.1"
42394253
magic-string "^0.22.4"
@@ -4262,9 +4276,9 @@ rollup-pluginutils@^2.0.1:
42624276
estree-walker "^0.3.0"
42634277
micromatch "^2.3.11"
42644278

4265-
rollup@^0.58.1:
4266-
version "0.58.1"
4267-
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.58.1.tgz#5e2e05ceb103f770868b12c4048c22d3903fa2dd"
4279+
rollup@^0.58.2:
4280+
version "0.58.2"
4281+
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.58.2.tgz#2feddea8c0c022f3e74b35c48e3c21b3433803ce"
42684282
dependencies:
42694283
"@types/estree" "0.0.38"
42704284
"@types/node" "*"
@@ -4324,11 +4338,9 @@ sax@^1.2.4:
43244338
version "1.2.4"
43254339
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
43264340

4327-
scroll-into-view-if-needed@^1.5.0:
4328-
version "1.5.0"
4329-
resolved "https://registry.yarnpkg.com/scroll-into-view-if-needed/-/scroll-into-view-if-needed-1.5.0.tgz#39e8ce15cb74348a897ee96110e848fed50aaf95"
4330-
dependencies:
4331-
amator "1.0.1"
4341+
scroll-into-view-if-needed@^2.1.5:
4342+
version "2.1.5"
4343+
resolved "https://registry.yarnpkg.com/scroll-into-view-if-needed/-/scroll-into-view-if-needed-2.1.5.tgz#0ded69275e0172c71f523722d497aef44f6c6bad"
43324344

43334345
"semver@2 || 3 || 4 || 5", semver@^5.3.0, semver@^5.4.1:
43344346
version "5.5.0"

0 commit comments

Comments
 (0)