Skip to content

Commit fd10a5b

Browse files
author
Daniel Kurowski
committed
fix collision with native scroll-behavior
1 parent 388be3f commit fd10a5b

6 files changed

+92
-5
lines changed

dist/index.js

+32-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare class NativeScrollBehavior {
2+
private readonly rootEl;
3+
private originalValue;
4+
private constructor();
5+
static forWindowObject(): NativeScrollBehavior;
6+
remove(): void;
7+
restore(): void;
8+
}
9+
export declare const nativeScrollBehavior: NativeScrollBehavior;
10+
export {};

src/scrollers/NativeScrollBehavior.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
type ScrollBehavior = 'auto' | 'smooth' | 'inherit' | 'initial' | 'revert' | 'revert-layer' | 'unset';
2+
3+
class NativeScrollBehavior
4+
{
5+
private originalValue: ScrollBehavior|null = null;
6+
7+
private constructor(
8+
private readonly rootEl: HTMLHtmlElement,
9+
) {}
10+
11+
public static forWindowObject(): NativeScrollBehavior
12+
{
13+
return new this(
14+
document.documentElement as HTMLHtmlElement,
15+
);
16+
}
17+
18+
19+
public remove(): void
20+
{
21+
this.originalValue = window.getComputedStyle(this.rootEl).getPropertyValue('scrollBehavior') as ScrollBehavior;
22+
this.rootEl.style.scrollBehavior = 'unset';
23+
}
24+
25+
public restore(): void
26+
{
27+
if (this.originalValue === null) {
28+
return;
29+
}
30+
31+
this.rootEl.style.scrollBehavior = this.originalValue;
32+
}
33+
34+
}
35+
36+
// export singleton instance
37+
export const nativeScrollBehavior = NativeScrollBehavior.forWindowObject();

src/scrollers/scrollToElement.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import * as Velocity from 'velocity-animate';
22
import {EASE_IN_SKIP_OUT_EASING} from '../easing/bindEasingToVelocity';
3+
import {nativeScrollBehavior} from './NativeScrollBehavior';
34

45
export function scrollToElement(element: HTMLElement, onScrollFinishedCallback?: () => void): void
56
{
7+
nativeScrollBehavior.remove();
68
Velocity.animate(element, 'scroll', {
79
duration: 1200, // todo: different depending on offset from page top?
810
easing: EASE_IN_SKIP_OUT_EASING,
9-
complete: () => onScrollFinishedCallback !== undefined && onScrollFinishedCallback(),
11+
complete: () => {
12+
nativeScrollBehavior.restore();
13+
onScrollFinishedCallback !== undefined && onScrollFinishedCallback();
14+
},
1015
});
1116
}

src/scrollers/scrollToOffset.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import * as Velocity from 'velocity-animate';
22
import {EASE_IN_SKIP_OUT_EASING} from '../easing/bindEasingToVelocity';
3+
import {nativeScrollBehavior} from "./NativeScrollBehavior";
34

45
export function scrollToOffset(
56
topOffset: number,
67
onScrollFinishedCallback?: () => void,
78
): void
89
{
10+
nativeScrollBehavior.remove();
911
/**
1012
* Setting `<html>` as the element to scroll to with offset simulates `window.scrollTo()` behavior.
1113
* See last paragraph at http://velocityjs.org/#scroll
@@ -14,6 +16,9 @@ export function scrollToOffset(
1416
duration: 1200, // todo: different depending on offset from page top?
1517
offset: topOffset,
1618
easing: EASE_IN_SKIP_OUT_EASING,
17-
complete: onScrollFinishedCallback,
19+
complete: () => {
20+
nativeScrollBehavior.restore();
21+
onScrollFinishedCallback !== undefined && onScrollFinishedCallback();
22+
},
1823
});
1924
}

0 commit comments

Comments
 (0)