-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix collision with native scroll-behavior
- Loading branch information
Daniel Kurowski
committed
Jan 30, 2024
1 parent
ea2d4c6
commit 1c12698
Showing
3 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
type ScrollBehavior = 'auto' | 'smooth' | 'inherit' | 'initial' | 'revert' | 'revert-layer' | 'unset'; | ||
|
||
class NativeScrollBehavior | ||
{ | ||
private originalValue: ScrollBehavior|null = null; | ||
|
||
private constructor( | ||
private readonly rootEl: HTMLHtmlElement, | ||
) {} | ||
|
||
public static forWindowObject(): NativeScrollBehavior | ||
{ | ||
return new this( | ||
document.documentElement as HTMLHtmlElement, | ||
); | ||
} | ||
|
||
|
||
public remove(): void | ||
{ | ||
this.originalValue = window.getComputedStyle(this.rootEl).getPropertyValue('scrollBehavior') as ScrollBehavior; | ||
this.rootEl.style.scrollBehavior = 'unset'; | ||
} | ||
|
||
public restore(): void | ||
{ | ||
if (this.originalValue === null) { | ||
return; | ||
} | ||
|
||
this.rootEl.style.scrollBehavior = this.originalValue; | ||
} | ||
|
||
} | ||
|
||
// export singleton instance | ||
export const nativeScrollBehavior = NativeScrollBehavior.forWindowObject(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import * as Velocity from 'velocity-animate'; | ||
import {EASE_IN_SKIP_OUT_EASING} from '../easing/bindEasingToVelocity'; | ||
import {nativeScrollBehavior} from './NativeScrollBehavior'; | ||
|
||
export function scrollToElement(element: HTMLElement, onScrollFinishedCallback?: () => void): void | ||
{ | ||
nativeScrollBehavior.remove(); | ||
Velocity.animate(element, 'scroll', { | ||
duration: 1200, // todo: different depending on offset from page top? | ||
easing: EASE_IN_SKIP_OUT_EASING, | ||
complete: () => onScrollFinishedCallback !== undefined && onScrollFinishedCallback(), | ||
complete: () => { | ||
nativeScrollBehavior.restore(); | ||
onScrollFinishedCallback !== undefined && onScrollFinishedCallback(); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters