Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 55 additions & 23 deletions src/runtime/components/Modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,28 @@ export interface ModalProps extends DialogRootProps {
}

export interface ModalEmits extends DialogRootEmits {
'after:leave': []
'enter': []
'leave': []
'after:enter': []
'after:leave': []
'close:prevent': []
}

export interface ModalSlots {
default(props: { open: boolean }): any
content(props: { close: () => void }): any
header(props: { close: () => void }): any
title(props?: {}): any
description(props?: {}): any
actions(props?: {}): any
close(props: { close: () => void, ui: { [K in keyof Required<Modal['slots']>]: (props?: Record<string, any>) => string } }): any
body(props: { close: () => void }): any
footer(props: { close: () => void }): any
default(props: { open: boolean, transitioning: boolean }): any
content(props: { close: () => void, transitioning: boolean }): any
header(props: { close: () => void, transitioning: boolean }): any
title(props: { transitioning: boolean }): any
description(props: { transitioning: boolean }): any
actions(props: { transitioning: boolean }): any
close(props: { close: () => void, ui: { [K in keyof Required<Modal['slots']>]: (props?: Record<string, any>) => string }, transitioning: boolean }): any
body(props: { close: () => void, transitioning: boolean }): any
footer(props: { close: () => void, transitioning: boolean }): any
}
</script>

<script setup lang="ts">
import { computed, toRef } from 'vue'
import { computed, toRef, ref } from 'vue'
import { DialogRoot, DialogTrigger, DialogPortal, DialogOverlay, DialogContent, DialogTitle, DialogDescription, DialogClose, VisuallyHidden, useForwardPropsEmits } from 'reka-ui'
import { reactivePick } from '@vueuse/core'
import { useAppConfig } from '#imports'
Expand All @@ -96,6 +98,8 @@ const slots = defineSlots<ModalSlots>()
const { t } = useLocale()
const appConfig = useAppConfig() as Modal['AppConfig']

const transitioning = ref(false)

const rootProps = useForwardPropsEmits(reactivePick(props, 'open', 'defaultOpen', 'modal'), emits)
const portalProps = usePortal(toRef(() => props.portal))
const contentProps = toRef(() => props.content)
Expand Down Expand Up @@ -123,54 +127,82 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.modal || {})
transition: props.transition,
fullscreen: props.fullscreen
}))

function handleAfterEnter() {
transitioning.value = false
emits('after:enter')
}

function handleAfterLeave() {
transitioning.value = false
emits('after:leave')
}

function handleEnter() {
transitioning.value = true
emits('enter')
}

function handleLeave() {
transitioning.value = true
emits('leave')
}
</script>

<!-- eslint-disable vue/no-template-shadow -->
<template>
<DialogRoot v-slot="{ open, close }" v-bind="rootProps">
<DialogTrigger v-if="!!slots.default" as-child :class="props.class">
<slot :open="open" />
<slot :open="open" :transitioning="transitioning" />
</DialogTrigger>

<DialogPortal v-bind="portalProps">
<DialogOverlay v-if="overlay" :class="ui.overlay({ class: props.ui?.overlay })" />

<DialogContent :class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })" v-bind="contentProps" @after-enter="emits('after:enter')" @after-leave="emits('after:leave')" v-on="contentEvents">
<DialogContent
:class="ui.content({ class: [!slots.default && props.class, props.ui?.content] })"
v-bind="contentProps"
@after-enter="handleAfterEnter"
@after-leave="handleAfterLeave"
@enter="handleEnter"
@leave="handleLeave"
v-on="contentEvents"
>
<VisuallyHidden v-if="!!slots.content && ((title || !!slots.title) || (description || !!slots.description))">
<DialogTitle v-if="title || !!slots.title">
<slot name="title">
<slot name="title" :transitioning="transitioning">
{{ title }}
</slot>
</DialogTitle>

<DialogDescription v-if="description || !!slots.description">
<slot name="description">
<slot name="description" :transitioning="transitioning">
{{ description }}
</slot>
</DialogDescription>
</VisuallyHidden>

<slot name="content" :close="close">
<slot name="content" :close="close" :transitioning="transitioning">
<div v-if="!!slots.header || (title || !!slots.title) || (description || !!slots.description) || (props.close || !!slots.close)" :class="ui.header({ class: props.ui?.header })">
<slot name="header" :close="close">
<slot name="header" :close="close" :transitioning="transitioning">
<div :class="ui.wrapper({ class: props.ui?.wrapper })">
<DialogTitle v-if="title || !!slots.title" :class="ui.title({ class: props.ui?.title })">
<slot name="title">
<slot name="title" :transitioning="transitioning">
{{ title }}
</slot>
</DialogTitle>

<DialogDescription v-if="description || !!slots.description" :class="ui.description({ class: props.ui?.description })">
<slot name="description">
<slot name="description" :transitioning="transitioning">
{{ description }}
</slot>
</DialogDescription>
</div>

<slot name="actions" />
<slot name="actions" :transitioning="transitioning" />

<DialogClose v-if="props.close || !!slots.close" as-child>
<slot name="close" :close="close" :ui="ui">
<slot name="close" :close="close" :ui="ui" :transitioning="transitioning">
<UButton
v-if="props.close"
:icon="closeIcon || appConfig.ui.icons.close"
Expand All @@ -186,11 +218,11 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.modal || {})
</div>

<div v-if="!!slots.body" :class="ui.body({ class: props.ui?.body })">
<slot name="body" :close="close" />
<slot name="body" :close="close" :transitioning="transitioning" />
</div>

<div v-if="!!slots.footer" :class="ui.footer({ class: props.ui?.footer })">
<slot name="footer" :close="close" />
<slot name="footer" :close="close" :transitioning="transitioning" />
</div>
</slot>
</DialogContent>
Expand Down