@@ -14,6 +14,11 @@ declare global {
1414 width : number
1515 }
1616 }
17+ // tslint:disable-next-line
18+ type visualViewport = {
19+ height : number
20+ width : number
21+ }
1722
1823 // @TODO better declaration of possible shadowdom hosts
1924 interface Element {
@@ -41,21 +46,13 @@ export interface Options {
4146 skipOverflowHiddenElements ?: boolean
4247}
4348
44- // memoize for perf
45- let viewport : HTMLElement
46-
4749// return the current viewport depending on wether quirks mode is active or not
4850function 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+ )
5956}
6057
6158// @TODO better shadowdom test, 11 = document fragment
@@ -68,26 +65,23 @@ function isElement(el: any) {
6865}
6966
7067function canOverflow (
71- el : Element ,
72- axis : 'overflowY' | 'overflowX' ,
68+ overflow : string | null ,
7369 skipOverflowHiddenElements ?: boolean
7470) {
75- const overflowValue = getComputedStyle ( el , null ) [ axis ]
76-
77- if ( skipOverflowHiddenElements && overflowValue === 'hidden' ) {
71+ if ( skipOverflowHiddenElements && overflow === 'hidden' ) {
7872 return false
7973 }
8074
81- return overflowValue !== 'visible' && overflowValue !== 'clip'
75+ return overflow !== 'visible' && overflow !== 'clip'
8276}
8377
8478function isScrollable ( el : Element , skipOverflowHiddenElements ?: boolean ) {
79+ const style = getComputedStyle ( el )
8580 return (
86- el === getViewport ( ) ||
8781 ( el . clientHeight < el . scrollHeight &&
88- canOverflow ( el , ' overflowY' , skipOverflowHiddenElements ) ) ||
82+ canOverflow ( style . overflowY , skipOverflowHiddenElements ) ) ||
8983 ( el . clientWidth < el . scrollWidth &&
90- canOverflow ( el , ' overflowX' , skipOverflowHiddenElements ) )
84+ canOverflow ( style . overflowX , skipOverflowHiddenElements ) )
9185 )
9286}
9387
@@ -266,33 +260,25 @@ export default (target: Element, options: Options): CustomScrollAction[] => {
266260 // Workaround Chrome's behavior on clientHeight/clientWidth after introducing visualViewport
267261 // https://www.quirksmode.org/blog/archives/2016/02/chrome_change_b.html
268262 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+
279268 // These values mutate as we loop through and generate scroll coordinates
280269 let targetBlock : number =
281- block === 'start '
282- ? targetRect . top
270+ block === 'center '
271+ ? targetRect . top + targetRect . height / 2
283272 : block === 'end'
284273 ? targetRect . bottom
285- : block === 'nearest'
286- ? targetRect . top
287- : targetRect . top + targetRect . height / 2 // block === 'center
274+ : targetRect . top // block === 'start' || block === 'nearest'
275+
288276 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
296282
297283 // Collect new scroll positions
298284 const computations = frames . reduce < CustomScrollAction [ ] > ( ( results , frame ) => {
0 commit comments