-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathModal.client.vue
More file actions
101 lines (89 loc) · 2.42 KB
/
Modal.client.vue
File metadata and controls
101 lines (89 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<script setup lang="ts">
const props = defineProps<{
modalTitle: string
modalSubtitle?: string
}>()
const dialogRef = useTemplateRef('dialogRef')
const emit = defineEmits<{
(e: 'transitioned'): void
}>()
const modalTitleId = computed(() => {
const id = getCurrentInstance()?.attrs.id
return id ? `${id}-title` : undefined
})
function handleModalClose() {
dialogRef.value?.close()
}
/**
* Emits `transitioned` once the dialog has finished its open opacity transition.
* This is used by consumers that need to run layout-sensitive logic (for example
* dispatching a resize) only after the modal is fully displayed.
*/
function onDialogTransitionEnd(event: TransitionEvent) {
const el = dialogRef.value
if (!el) return
if (!el.open) return
if (event.target !== el) return
if (event.propertyName !== 'opacity') return
emit('transitioned')
}
defineExpose({
showModal: () => dialogRef.value?.showModal(),
close: () => dialogRef.value?.close(),
})
</script>
<template>
<Teleport to="body">
<dialog
ref="dialogRef"
closedby="any"
class="w-[calc(100%-2rem)] bg-bg border border-border rounded-lg shadow-xl max-h-[90vh] overflow-y-auto overscroll-contain m-0 m-auto p-6 text-fg focus-visible:outline focus-visible:outline-accent/70"
:aria-labelledby="modalTitleId"
v-bind="$attrs"
@transitionend="onDialogTransitionEnd"
>
<!-- Modal top header section -->
<div class="flex items-center justify-between mb-6">
<div>
<h2 :id="modalTitleId" class="font-mono text-lg font-medium">
{{ modalTitle }}
</h2>
<p v-if="modalSubtitle" class="text-xs text-fg-subtle">{{ modalSubtitle }}</p>
</div>
<ButtonBase
type="button"
:aria-label="$t('common.close')"
@click="handleModalClose"
classicon="i-lucide:x"
/>
</div>
<!-- Modal body content -->
<slot />
</dialog>
</Teleport>
</template>
<style scoped>
/* Backdrop styling when any of the modals are open */
dialog:modal::backdrop {
@apply bg-bg-elevated/70;
}
dialog::backdrop {
pointer-events: none;
}
/* Modal transition styles */
dialog {
opacity: 0;
transition: opacity 200ms ease;
transition-behavior: allow-discrete;
}
dialog:modal {
opacity: 1;
transition: opacity 200ms ease;
transition-behavior: allow-discrete;
}
@starting-style {
dialog:modal {
opacity: 0;
}
}
</style>