1
- import { type ComponentPublicInstance , type Ref , computed , nextTick , watch } from 'vue'
1
+ import { type ComponentPublicInstance , type Ref , computed , nextTick , onBeforeUnmount , onMounted , ref , watch } from 'vue'
2
2
import { isVertical , set } from './helpers'
3
3
import { TRANSITIONS , VELOCITY_THRESHOLD } from './constants'
4
4
import type { DrawerDirection } from './types'
@@ -22,6 +22,30 @@ export function useSnapPoints({
22
22
onSnapPointChange,
23
23
direction,
24
24
} : useSnapPointsProps ) {
25
+ const windowDimensions = ref ( typeof window !== 'undefined'
26
+ ? {
27
+ innerWidth : window . innerWidth ,
28
+ innerHeight : window . innerHeight ,
29
+ }
30
+ : undefined )
31
+
32
+ function onResize ( ) {
33
+ windowDimensions . value = {
34
+ innerWidth : window . innerWidth ,
35
+ innerHeight : window . innerHeight ,
36
+ }
37
+ }
38
+
39
+ onMounted ( ( ) => {
40
+ if ( typeof window !== 'undefined' )
41
+ window . addEventListener ( 'resize' , onResize )
42
+ } )
43
+
44
+ onBeforeUnmount ( ( ) => {
45
+ if ( typeof window !== 'undefined' )
46
+ window . removeEventListener ( 'resize' , onResize )
47
+ } )
48
+
25
49
const isLastSnapPoint = computed (
26
50
( ) =>
27
51
( snapPoints . value
@@ -46,25 +70,24 @@ export function useSnapPoints({
46
70
const snapPointsOffset = computed (
47
71
( ) =>
48
72
snapPoints . value ?. map ( ( snapPoint ) => {
49
- const hasWindow = typeof window !== 'undefined'
50
73
const isPx = typeof snapPoint === 'string'
51
74
let snapPointAsNumber = 0
52
75
53
76
if ( isPx )
54
77
snapPointAsNumber = Number . parseInt ( snapPoint , 10 )
55
78
56
79
if ( isVertical ( direction . value ) ) {
57
- const height = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window . innerHeight : 0
80
+ const height = isPx ? snapPointAsNumber : windowDimensions . value ? snapPoint * windowDimensions . value . innerHeight : 0
58
81
59
- if ( hasWindow )
60
- return direction . value === 'bottom' ? window . innerHeight - height : - window . innerHeight + height
82
+ if ( windowDimensions . value )
83
+ return direction . value === 'bottom' ? windowDimensions . value . innerHeight - height : - windowDimensions . value . innerHeight + height
61
84
62
85
return height
63
86
}
64
- const width = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window . innerWidth : 0
87
+ const width = isPx ? snapPointAsNumber : windowDimensions . value ? snapPoint * windowDimensions . value . innerWidth : 0
65
88
66
- if ( hasWindow )
67
- return direction . value === 'right' ? window . innerWidth - width : - window . innerWidth + width
89
+ if ( windowDimensions . value )
90
+ return direction . value === 'right' ? windowDimensions . value . innerWidth - width : - windowDimensions . value . innerWidth + width
68
91
69
92
return width
70
93
} ) ?? [ ] ,
0 commit comments