1
1
import cookie from 'cookiejs'
2
- import { computed , type Ref } from 'vue-demi'
2
+ import { computed , type MaybeRefOrGetter , type Ref , toRef , toValue } from 'vue-demi'
3
3
4
- import type { ViewportOptions , ViewportQuery } from '.. /types'
4
+ import type { ViewportOptions , ViewportQuery } from './types'
5
5
6
6
export const STATE_KEY = 'viewportState'
7
7
8
- export function createViewportManager ( options : ViewportOptions , state : Ref < string > ) {
8
+ export function createViewportManager ( options : MaybeRefOrGetter < ViewportOptions > , state : Ref < string > ) {
9
+ options = toRef ( options )
10
+
9
11
const breakpoint = computed < string > ( {
10
12
get ( ) {
11
- return state . value || options . fallbackBreakpoint
13
+ return state . value || options . value . fallbackBreakpoint
12
14
} ,
13
15
14
16
set ( newBreakpoint ) {
15
17
state . value = newBreakpoint
16
18
17
- if ( typeof window !== 'undefined' && options . cookie . name ) {
18
- cookie . set ( options . cookie . name , state . value , options . cookie )
19
+ if ( typeof window !== 'undefined' && options . value . cookie . name ) {
20
+ cookie . set ( options . value . cookie . name , state . value , options . value . cookie )
19
21
}
20
22
} ,
21
23
} )
22
24
23
25
const queries = computed < Record < string , ViewportQuery > > ( ( ) => {
24
- const breakpoints = options . breakpoints || { }
26
+ const breakpoints = options . value . breakpoints || { }
25
27
const breakpointsKeys = Object . keys ( breakpoints ) . sort ( ( a , b ) => breakpoints [ a ] - breakpoints [ b ] )
26
28
27
29
const output : Record < string , ViewportQuery > = { }
@@ -36,7 +38,7 @@ export function createViewportManager(options: ViewportOptions, state: Ref<strin
36
38
37
39
let mediaQuery = ''
38
40
39
- if ( options . feature === 'minWidth' ) {
41
+ if ( options . value . feature === 'minWidth' ) {
40
42
if ( i > 0 ) {
41
43
mediaQuery = `(min-width: ${ size } px)`
42
44
} else {
@@ -86,7 +88,7 @@ export function createViewportManager(options: ViewportOptions, state: Ref<strin
86
88
* @param searchBreakpoint - Breakpoint to search.
87
89
*/
88
90
function breakpointValue ( searchBreakpoint : string ) {
89
- const breakpoints = options . breakpoints || { }
91
+ const breakpoints = toValue ( options ) . breakpoints || { }
90
92
91
93
return breakpoints [ searchBreakpoint ]
92
94
}
0 commit comments