@@ -14,7 +14,7 @@ export interface SmoothBehaviorOptions extends Options {
14
14
}
15
15
16
16
// Memoize so we're much more friendly to non-dom envs
17
- let memoizedNow
17
+ let memoizedNow : ( ) => number
18
18
const now = ( ) => {
19
19
if ( ! memoizedNow ) {
20
20
memoizedNow =
@@ -23,7 +23,19 @@ const now = () => {
23
23
return memoizedNow ( )
24
24
}
25
25
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 ) {
27
39
const time = now ( )
28
40
const elapsed = Math . min ( ( time - context . startTime ) / context . duration , 1 )
29
41
@@ -42,33 +54,26 @@ function step(context) {
42
54
}
43
55
44
56
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
51
63
) {
52
64
let scrollable
53
65
let startX
54
66
let startY
55
67
let method
56
68
57
69
// 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
72
77
}
73
78
74
79
// scroll looping over a frame
@@ -93,7 +98,7 @@ const shouldSmoothScroll = <T>(options: any): options is T => {
93
98
function scroll ( target : Element , options ?: SmoothBehaviorOptions ) : Promise < any >
94
99
function scroll < T > ( target : Element , options : CustomBehaviorOptions < T > ) : T
95
100
function scroll ( target : Element , options : StandardBehaviorOptions ) : void
96
- function scroll < T > ( target , options ) {
101
+ function scroll < T > ( target : Element , options ?: any ) {
97
102
if ( shouldSmoothScroll < SmoothBehaviorOptions > ( options ) ) {
98
103
const overrides = options || { }
99
104
// @TODO replace <any> in promise signatures with better information
0 commit comments