@@ -14,6 +14,11 @@ declare global {
14
14
width : number
15
15
}
16
16
}
17
+ // tslint:disable-next-line
18
+ type visualViewport = {
19
+ height : number
20
+ width : number
21
+ }
17
22
18
23
// @TODO better declaration of possible shadowdom hosts
19
24
interface Element {
@@ -41,21 +46,13 @@ export interface Options {
41
46
skipOverflowHiddenElements ?: boolean
42
47
}
43
48
44
- // memoize for perf
45
- let viewport : HTMLElement
46
-
47
49
// return the current viewport depending on wether quirks mode is active or not
48
50
function getViewport ( ) {
49
- const doc = document
50
-
51
- if ( ! viewport ) {
52
- viewport =
53
- ( doc . compatMode !== 'CSS1Compat' &&
54
- ( doc . scrollingElement as HTMLElement ) ) ||
55
- doc . documentElement
56
- }
57
-
58
- return viewport
51
+ return (
52
+ ( document . compatMode !== 'CSS1Compat' &&
53
+ ( document . scrollingElement as HTMLElement ) ) ||
54
+ document . documentElement
55
+ )
59
56
}
60
57
61
58
// @TODO better shadowdom test, 11 = document fragment
@@ -68,26 +65,23 @@ function isElement(el: any) {
68
65
}
69
66
70
67
function canOverflow (
71
- el : Element ,
72
- axis : 'overflowY' | 'overflowX' ,
68
+ overflow : string | null ,
73
69
skipOverflowHiddenElements ?: boolean
74
70
) {
75
- const overflowValue = getComputedStyle ( el , null ) [ axis ]
76
-
77
- if ( skipOverflowHiddenElements && overflowValue === 'hidden' ) {
71
+ if ( skipOverflowHiddenElements && overflow === 'hidden' ) {
78
72
return false
79
73
}
80
74
81
- return overflowValue !== 'visible' && overflowValue !== 'clip'
75
+ return overflow !== 'visible' && overflow !== 'clip'
82
76
}
83
77
84
78
function isScrollable ( el : Element , skipOverflowHiddenElements ?: boolean ) {
79
+ const style = getComputedStyle ( el )
85
80
return (
86
- el === getViewport ( ) ||
87
81
( el . clientHeight < el . scrollHeight &&
88
- canOverflow ( el , ' overflowY' , skipOverflowHiddenElements ) ) ||
82
+ canOverflow ( style . overflowY , skipOverflowHiddenElements ) ) ||
89
83
( el . clientWidth < el . scrollWidth &&
90
- canOverflow ( el , ' overflowX' , skipOverflowHiddenElements ) )
84
+ canOverflow ( style . overflowX , skipOverflowHiddenElements ) )
91
85
)
92
86
}
93
87
@@ -266,33 +260,25 @@ export default (target: Element, options: Options): CustomScrollAction[] => {
266
260
// Workaround Chrome's behavior on clientHeight/clientWidth after introducing visualViewport
267
261
// https://www.quirksmode.org/blog/archives/2016/02/chrome_change_b.html
268
262
const viewport = getViewport ( )
269
- const viewportWidth = window . visualViewport
270
- ? window . visualViewport . width
271
- : Math . min ( viewport . clientWidth , window . innerWidth )
272
- const viewportHeight = window . visualViewport
273
- ? window . visualViewport . height
274
- : Math . min ( viewport . clientHeight , window . innerHeight )
275
- const viewportX = window . scrollX || window . pageXOffset
276
- const viewportY = window . scrollY || window . pageYOffset
277
-
278
- // @TODO remove duplicate results
263
+ const viewportWidth = innerWidth
264
+ const viewportHeight = innerHeight
265
+ const viewportX = scrollX
266
+ const viewportY = scrollY
267
+
279
268
// These values mutate as we loop through and generate scroll coordinates
280
269
let targetBlock : number =
281
- block === 'start '
282
- ? targetRect . top
270
+ block === 'center '
271
+ ? targetRect . top + targetRect . height / 2
283
272
: block === 'end'
284
273
? targetRect . bottom
285
- : block === 'nearest'
286
- ? targetRect . top
287
- : targetRect . top + targetRect . height / 2 // block === 'center
274
+ : targetRect . top // block === 'start' || block === 'nearest'
275
+
288
276
let targetInline : number =
289
- inline === 'start'
290
- ? targetRect . left
291
- : inline === 'center'
292
- ? targetRect . left + targetRect . width / 2
293
- : inline === 'end'
294
- ? targetRect . right
295
- : targetRect . left // inline === 'nearest && inline === 'start
277
+ inline === 'center'
278
+ ? targetRect . left + targetRect . width / 2
279
+ : inline === 'end'
280
+ ? targetRect . right
281
+ : targetRect . left // inline === 'start || inline === 'nearest
296
282
297
283
// Collect new scroll positions
298
284
const computations = frames . reduce < CustomScrollAction [ ] > ( ( results , frame ) => {
0 commit comments