|
1 | 1 | <script setup lang="ts"> |
2 | | -import { DialogRoot, type DialogRootEmits, type DialogRootProps, useForwardPropsEmits } from 'radix-vue' |
| 2 | +import { DialogRoot, useForwardPropsEmits, type DialogRootEmits, type DialogRootProps } from 'radix-vue'; |
3 | 3 |
|
4 | | -const props = defineProps<DialogRootProps & { class?: string }>() |
5 | | -const emits = defineEmits<DialogRootEmits>() |
| 4 | +const MOBILE_VIEWPORT = 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0' as const; |
6 | 5 |
|
7 | | -const forwarded = useForwardPropsEmits(props, emits) |
8 | | -const initialViewport = ref(document.querySelector('meta[name="viewport"]')?.getAttribute('content') ?? 'width=1300'); |
9 | | -const openListener = (opened: boolean) => { |
10 | | - /** |
11 | | - * Update meta tags for the root page when oepned |
12 | | - */ |
| 6 | +const props = defineProps<DialogRootProps & { class?: string }>(); |
| 7 | +const emits = defineEmits<DialogRootEmits>(); |
| 8 | +
|
| 9 | +const getViewport = (): string => { |
| 10 | + return document.querySelector('meta[name="viewport"]')?.getAttribute('content') ?? 'width=1300'; |
| 11 | +}; |
| 12 | +const updateViewport = (viewport: string): void => { |
| 13 | + const meta = document.querySelector('meta[name="viewport"]'); |
| 14 | + if (meta) { |
| 15 | + meta.setAttribute('content', viewport); |
| 16 | + } else { |
| 17 | + const meta = document.createElement('meta'); |
| 18 | + meta.name = 'viewport'; |
| 19 | + meta.content = viewport; |
| 20 | + document.head.appendChild(meta); |
| 21 | + } |
| 22 | +}; |
13 | 23 |
|
| 24 | +const forwarded = useForwardPropsEmits(props, emits); |
| 25 | +const initialViewport = ref(getViewport()); |
| 26 | +const openListener = (opened: boolean) => { |
14 | 27 | if (opened) { |
15 | | - document.querySelector('meta[name="viewport"]')?.setAttribute('content', 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0') |
| 28 | + updateViewport(MOBILE_VIEWPORT); |
16 | 29 | } else { |
17 | | - document.querySelector('meta[name="viewport"]')?.setAttribute('content', initialViewport.value); |
| 30 | + updateViewport(initialViewport.value); |
18 | 31 | } |
19 | | -} |
| 32 | +}; |
| 33 | +
|
| 34 | +onUnmounted(() => { |
| 35 | + updateViewport(initialViewport.value); |
| 36 | +}); |
20 | 37 | </script> |
21 | 38 |
|
22 | 39 | <template> |
|
0 commit comments