Skip to content

Commit 6179f2c

Browse files
authored
fix: safari scrolling on window now works properly (#106)
Also reduces bundlesize
1 parent bd6429b commit 6179f2c

File tree

5 files changed

+37
-32
lines changed

5 files changed

+37
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ And while `scroll-into-view-if-needed` use the same default options as browsers
1515
## Install
1616

1717
```bash
18-
yarn add smooth-scroll-into-view-if-needed scroll-into-view-if-needed
18+
yarn add smooth-scroll-into-view-if-needed
1919
```
2020

2121
## Usage

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
"build:umd.min": "BABEL_ENV=umd NODE_ENV=production rollup -c -f umd -o umd/smooth-scroll-into-view-if-needed.min.js",
2727
"clean": "rimraf 'umd' 'es' 'typings'",
2828
"precommit": "lint-staged",
29-
"dev": "concurrently 'tsc --watch' 'yarn build:cjs --watch'",
29+
"dev": "concurrently 'tsc --noEmit --watch' 'yarn build:cjs --watch' 'yarn build:es --watch' 'yarn build:umd --watch' 'yarn build:umd.min --watch'",
3030
"prepublishOnly": "unset npm_config_cafile && yarn build",
3131
"typecheck": "tsc --noEmit"
3232
},
33-
"peerDependencies": {
34-
"scroll-into-view-if-needed": "^2.2.1"
33+
"dependencies": {
34+
"scroll-into-view-if-needed": "2.2.9"
3535
},
3636
"devDependencies": {
3737
"@babel/cli": "7.0.0-beta.51",
@@ -58,7 +58,6 @@
5858
"rollup-plugin-node-resolve": "3.3.0",
5959
"rollup-plugin-replace": "2.0.0",
6060
"rollup-plugin-terser": "1.0.1",
61-
"scroll-into-view-if-needed": "2.2.8",
6261
"semantic-release": "15.6.0",
6362
"typescript": "2.9.2"
6463
},

src/index.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface SmoothBehaviorOptions extends Options {
1414
}
1515

1616
// Memoize so we're much more friendly to non-dom envs
17-
let memoizedNow
17+
let memoizedNow: () => number
1818
const now = () => {
1919
if (!memoizedNow) {
2020
memoizedNow =
@@ -23,7 +23,19 @@ const now = () => {
2323
return memoizedNow()
2424
}
2525

26-
function step(context) {
26+
type Context = {
27+
scrollable: Element
28+
method: Function
29+
startTime: number
30+
startX: number
31+
startY: number
32+
x: number
33+
y: number
34+
duration: number
35+
ease: CustomEasing
36+
cb: Function
37+
}
38+
function step(context: Context) {
2739
const time = now()
2840
const elapsed = Math.min((time - context.startTime) / context.duration, 1)
2941

@@ -42,33 +54,26 @@ function step(context) {
4254
}
4355

4456
function smoothScroll(
45-
el,
46-
x,
47-
y,
48-
duration = 450,
49-
ease = t => 1 + --t * t * t * t * t,
50-
cb
57+
el: Element,
58+
x: number,
59+
y: number,
60+
duration = 600,
61+
ease: CustomEasing = t => 1 + --t * t * t * t * t,
62+
cb: Function
5163
) {
5264
let scrollable
5365
let startX
5466
let startY
5567
let method
5668

5769
// define scroll context
58-
if (el === document.documentElement) {
59-
scrollable = window
60-
startX = window.scrollX || window.pageXOffset
61-
startY = window.scrollY || window.pageYOffset
62-
method = (x, y) => window.scroll(x, y)
63-
} else {
64-
scrollable = el
65-
startX = el.scrollLeft
66-
startY = el.scrollTop
67-
method = (x, y) => {
68-
// @TODO use Element.scroll if it exists, as it is potentially better performing
69-
el.scrollLeft = x
70-
el.scrollTop = y
71-
}
70+
scrollable = el
71+
startX = el.scrollLeft
72+
startY = el.scrollTop
73+
method = (x: number, y: number) => {
74+
// @TODO use Element.scroll if it exists, as it is potentially better performing
75+
el.scrollLeft = x
76+
el.scrollTop = y
7277
}
7378

7479
// scroll looping over a frame
@@ -93,7 +98,7 @@ const shouldSmoothScroll = <T>(options: any): options is T => {
9398
function scroll(target: Element, options?: SmoothBehaviorOptions): Promise<any>
9499
function scroll<T>(target: Element, options: CustomBehaviorOptions<T>): T
95100
function scroll(target: Element, options: StandardBehaviorOptions): void
96-
function scroll<T>(target, options) {
101+
function scroll<T>(target: Element, options?: any) {
97102
if (shouldSmoothScroll<SmoothBehaviorOptions>(options)) {
98103
const overrides = options || {}
99104
// @TODO replace <any> in promise signatures with better information

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"skipLibCheck": true,
1313
"noUnusedLocals": true,
1414
"noUnusedParameters": true,
15-
15+
"strict": true,
16+
"noImplicitAny": true,
1617
"alwaysStrict": true,
1718
"lib": ["es5", "es2015.promise", "dom"]
1819
},

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3958,9 +3958,9 @@ sax@^1.2.4:
39583958
version "1.2.4"
39593959
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
39603960

3961-
3962-
version "2.2.8"
3963-
resolved "https://registry.yarnpkg.com/scroll-into-view-if-needed/-/scroll-into-view-if-needed-2.2.8.tgz#c22ffbddce5c8a31949ab3e01c27a6c29ba7b979"
3961+
3962+
version "2.2.9"
3963+
resolved "https://registry.yarnpkg.com/scroll-into-view-if-needed/-/scroll-into-view-if-needed-2.2.9.tgz#f6c95d60f2106893d549de6eef69cb6ce2ebe936"
39643964

39653965
39663966
version "15.6.0"

0 commit comments

Comments
 (0)