Skip to content

Commit 2204fd3

Browse files
committed
refactor: use modal component for ChartModal
1 parent 46b32bb commit 2204fd3

3 files changed

Lines changed: 21 additions & 83 deletions

File tree

app/components/ChartModal.vue

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,14 @@
1-
<script setup lang="ts">
2-
const open = defineModel<boolean>('open', { default: false })
3-
4-
function close() {
5-
open.value = false
6-
}
7-
8-
function handleKeydown(event: KeyboardEvent) {
9-
if (event.key === 'Escape') {
10-
close()
11-
}
12-
}
13-
</script>
1+
<script setup lang="ts"></script>
142

153
<template>
16-
<Teleport to="body">
17-
<Transition
18-
enter-active-class="transition-opacity duration-200"
19-
leave-active-class="transition-opacity duration-200"
20-
enter-from-class="opacity-0"
21-
leave-to-class="opacity-0"
22-
>
23-
<div
24-
v-if="open"
25-
class="fixed inset-0 z-50 flex items-center justify-center p-0 sm:p-4"
26-
@keydown="handleKeydown"
27-
>
28-
<!-- Backdrop -->
29-
<button
30-
type="button"
31-
class="absolute inset-0 bg-black/60 cursor-default"
32-
:aria-label="$t('common.close_modal')"
33-
@click="open = false"
34-
/>
35-
36-
<div
37-
class="relative w-full h-full sm:h-auto bg-bg sm:border sm:border-border sm:rounded-lg shadow-xl sm:max-h-[90vh] overflow-y-auto overscroll-contain sm:max-w-3xl"
38-
role="dialog"
39-
aria-modal="true"
40-
aria-labelledby="chart-modal-title"
41-
>
42-
<div class="p-4 sm:p-6">
43-
<div class="flex items-center justify-between mb-4 sm:mb-6">
44-
<h2 id="chart-modal-title" class="font-mono text-lg font-medium">
45-
<slot name="title" />
46-
</h2>
47-
<button
48-
type="button"
49-
class="text-fg-subtle hover:text-fg transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 rounded"
50-
:aria-label="$t('common.close')"
51-
@click="open = false"
52-
>
53-
<span class="i-carbon-close block w-5 h-5" aria-hidden="true" />
54-
</button>
55-
</div>
56-
<div class="font-mono text-sm">
57-
<slot />
58-
</div>
59-
</div>
60-
61-
<slot name="after" v-bind="{ close }" />
62-
</div>
63-
</div>
64-
</Transition>
65-
</Teleport>
4+
<Modal
5+
:modalTitle="$t('package.downloads.modal_title')"
6+
id="chart-modal"
7+
class="h-full sm:h-min sm:border sm:border-border sm:rounded-lg shadow-xl sm:max-h-[90-vh] sm:max-w-3xl"
8+
>
9+
<div class="font-mono text-sm">
10+
<slot />
11+
</div>
12+
<slot name="after" />
13+
</Modal>
6614
</template>

app/components/Modal.client.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function handleModalClose() {
3333
</button>
3434
</div>
3535
<!-- Modal body content -->
36-
<slot></slot>
36+
<slot />
3737
</dialog>
3838
</Teleport>
3939
</template>

app/components/PackageWeeklyDownloadStats.vue

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref, computed, onMounted, watch } from 'vue'
2+
import { computed, onMounted, watch } from 'vue'
33
import { VueUiSparkline } from 'vue-data-ui/vue-ui-sparkline'
44
import { useCssVariables } from '../composables/useColors'
55
import { OKLCH_NEUTRAL_FALLBACK, lightenOklch } from '../utils/colors'
@@ -8,7 +8,12 @@ const { packageName } = defineProps<{
88
packageName: string
99
}>()
1010
11-
const showModal = shallowRef(false)
11+
function showChartModal() {
12+
const chartModal = document.querySelector<HTMLDialogElement>('#chart-modal')
13+
if (chartModal) {
14+
chartModal.showModal()
15+
}
16+
}
1217
1318
const { data: packument } = usePackage(() => packageName)
1419
const createdIso = computed(() => packument.value?.time?.created ?? null)
@@ -195,7 +200,7 @@ const config = computed(() => {
195200
<template #actions>
196201
<button
197202
type="button"
198-
@click="showModal = true"
203+
@click="showChartModal"
199204
class="link-subtle font-mono text-sm inline-flex items-center gap-1.5 ms-auto shrink-0 self-center focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 rounded"
200205
:title="$t('package.downloads.analyze')"
201206
>
@@ -242,28 +247,13 @@ const config = computed(() => {
242247
</CollapsibleSection>
243248
</div>
244249

245-
<ChartModal v-model:open="showModal">
246-
<template #title>{{ $t('package.downloads.modal_title') }}</template>
247-
250+
<ChartModal>
248251
<PackageDownloadAnalytics
249252
:weeklyDownloads="weeklyDownloads"
250253
:inModal="true"
251254
:packageName="packageName"
252255
:createdIso="createdIso"
253256
/>
254-
255-
<template #after="{ close }">
256-
<div class="sm:hidden flex justify-center">
257-
<button
258-
type="button"
259-
@click="close"
260-
class="w-12 h-12 bg-bg-elevated border border-border rounded-full shadow-lg flex items-center justify-center text-fg-muted hover:text-fg transition-colors"
261-
:aria-label="$t('common.close')"
262-
>
263-
<span class="w-5 h-5 i-carbon:close" aria-hidden="true" />
264-
</button>
265-
</div>
266-
</template>
267257
</ChartModal>
268258
</template>
269259

0 commit comments

Comments
 (0)