Skip to content

Commit 9aefa38

Browse files
committed
feat: viewport watch refactor
1 parent ad5a537 commit 9aefa38

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

web/components/shadcn/sheet/Sheet.vue

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
<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';
33
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;
65
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+
};
1323
24+
const forwarded = useForwardPropsEmits(props, emits);
25+
const initialViewport = ref(getViewport());
26+
const openListener = (opened: boolean) => {
1427
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);
1629
} else {
17-
document.querySelector('meta[name="viewport"]')?.setAttribute('content', initialViewport.value);
30+
updateViewport(initialViewport.value);
1831
}
19-
}
32+
};
33+
34+
onUnmounted(() => {
35+
updateViewport(initialViewport.value);
36+
});
2037
</script>
2138

2239
<template>

0 commit comments

Comments
 (0)