1+ import {
2+ $ ,
3+ isServer ,
4+ detectOS ,
5+ getEventListenerOptions ,
6+ } from './utils'
7+
8+ type OverflowHiddenPcStyleType = 'overflow' | 'boxSizing' | 'paddingRight'
9+ type OverflowHiddenMobileStyleType = 'top' | 'width' | 'height' | 'overflow' | 'position'
10+
111let lockedNum = 0
212let initialClientY = 0
3- let unLockCallback = null
13+ let unLockCallback : any = null
414let documentListenerAdded = false
515
6- const isServer = typeof window === 'undefined'
7- const lockedElements = [ ]
8-
9- const $ = ! isServer && document . querySelector . bind ( document )
10-
11- let eventListenerOptions
12- if ( ! isServer ) {
13- const testEvent = '__TUA_BSL_TEST_PASSIVE__'
14- const passiveTestOptions = {
15- get passive ( ) {
16- eventListenerOptions = { passive : false }
17- } ,
18- }
19- window . addEventListener ( testEvent , null , passiveTestOptions )
20- window . removeEventListener ( testEvent , null , passiveTestOptions )
21- }
22-
23- const detectOS = ( ) => {
24- const ua = navigator . userAgent
25- const ipad = / ( i P a d ) .* O S \s ( [ \d _ ] + ) / . test ( ua )
26- const iphone = ! ipad && / ( i P h o n e \s O S ) \s ( [ \d _ ] + ) / . test ( ua )
27- const android = / ( A n d r o i d ) ; ? [ \s / ] + ( [ \d . ] + ) ? / . test ( ua )
28-
29- const os = android ? 'android' : 'ios'
30- const ios = iphone || ipad
31-
32- return { os, ios, ipad, iphone, android }
33- }
16+ const lockedElements : HTMLElement [ ] = [ ]
17+ const eventListenerOptions = getEventListenerOptions ( { passive : false } )
3418
3519const setOverflowHiddenPc = ( ) => {
3620 const $body = $ ( 'body' )
@@ -42,7 +26,7 @@ const setOverflowHiddenPc = () => {
4226 $body . style . paddingRight = `${ scrollBarWidth } px`
4327
4428 return ( ) => {
45- ; [ 'overflow' , 'boxSizing' , 'paddingRight' ] . forEach ( ( x ) => {
29+ ; [ 'overflow' , 'boxSizing' , 'paddingRight' ] . forEach ( ( x : OverflowHiddenPcStyleType ) => {
4630 $body . style [ x ] = bodyStyle [ x ] || ''
4731 } )
4832 }
@@ -68,21 +52,21 @@ const setOverflowHiddenMobile = () => {
6852 $html . style . height = htmlStyle . height || ''
6953 $html . style . overflow = htmlStyle . overflow || ''
7054
71- ; [ 'top' , 'width' , 'height' , 'overflow' , 'position' ] . forEach ( ( x ) => {
55+ ; [ 'top' , 'width' , 'height' , 'overflow' , 'position' ] . forEach ( ( x : OverflowHiddenMobileStyleType ) => {
7256 $body . style [ x ] = bodyStyle [ x ] || ''
7357 } )
7458
7559 window . scrollTo ( 0 , scrollTop )
7660 }
7761}
7862
79- const preventDefault = ( event ) => {
63+ const preventDefault = ( event : TouchEvent ) => {
8064 if ( ! event . cancelable ) return
8165
8266 event . preventDefault ( )
8367}
8468
85- const handleScroll = ( event , targetElement ) => {
69+ const handleScroll = ( event : TouchEvent , targetElement : HTMLElement ) => {
8670 const clientY = event . targetTouches [ 0 ] . clientY - initialClientY
8771
8872 if ( targetElement ) {
@@ -99,7 +83,7 @@ const handleScroll = (event, targetElement) => {
9983 return true
10084}
10185
102- const checkTargetElement = ( targetElement ) => {
86+ const checkTargetElement = ( targetElement ?: HTMLElement ) => {
10387 if ( targetElement ) return
10488 if ( targetElement === null ) return
10589 if ( process . env . NODE_ENV === 'production' ) return
@@ -110,8 +94,8 @@ const checkTargetElement = (targetElement) => {
11094 )
11195}
11296
113- const lock = ( targetElement ) => {
114- if ( isServer ) return
97+ const lock = ( targetElement ?: HTMLElement ) => {
98+ if ( isServer ( ) ) return
11599
116100 checkTargetElement ( targetElement )
117101
@@ -136,30 +120,38 @@ const lock = (targetElement) => {
136120 documentListenerAdded = true
137121 }
138122 } else if ( lockedNum <= 0 ) {
139- unLockCallback = detectOS ( ) . android ? setOverflowHiddenMobile ( ) : setOverflowHiddenPc ( )
123+ unLockCallback = detectOS ( ) . android
124+ ? setOverflowHiddenMobile ( )
125+ : setOverflowHiddenPc ( )
140126 }
141127
142128 lockedNum += 1
143129}
144130
145- const unlock = ( targetElement ) => {
146- if ( isServer ) return
131+ const unlock = ( targetElement ?: HTMLElement ) => {
132+ if ( isServer ( ) ) return
147133
148134 checkTargetElement ( targetElement )
149135 lockedNum -= 1
150136
151137 if ( lockedNum > 0 ) return
152- if ( ! detectOS ( ) . ios ) {
153- lockedNum <= 0 && typeof unLockCallback === 'function' && unLockCallback ( )
138+ if (
139+ ! detectOS ( ) . ios &&
140+ typeof unLockCallback === 'function'
141+ ) {
142+ unLockCallback ( )
154143 return
155144 }
156145
157146 // iOS
158- const index = lockedElements . indexOf ( targetElement )
159- if ( index !== - 1 ) {
160- targetElement . ontouchmove = null
161- targetElement . ontouchstart = null
162- lockedElements . splice ( index , 1 )
147+ if ( targetElement ) {
148+ const index = lockedElements . indexOf ( targetElement )
149+
150+ if ( index !== - 1 ) {
151+ targetElement . ontouchmove = null
152+ targetElement . ontouchstart = null
153+ lockedElements . splice ( index , 1 )
154+ }
163155 }
164156
165157 if ( documentListenerAdded ) {
0 commit comments