Skip to content

Commit 440f8d3

Browse files
committed
FaModal 组件 provide/inject 改为组件传参方式
1 parent ec5aeee commit 440f8d3

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/ui/components/FaModal/dialog/DialogContent.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { computed } from 'vue'
1313
import { cn } from '@/utils'
1414
1515
const props = defineProps<DialogContentProps & {
16+
modalId: string
1617
class?: HTMLAttributes['class']
1718
open?: boolean
1819
maximize?: boolean
@@ -54,8 +55,6 @@ watch(showOverlay, (val) => {
5455
isLocked.value = false
5556
}
5657
})
57-
58-
const id = inject('ModalId')
5958
</script>
6059

6160
<template>
@@ -73,7 +72,7 @@ const id = inject('ModalId')
7372
>
7473
<div
7574
v-if="showOverlay"
76-
:data-modal-id="id"
75+
:data-modal-id="props.modalId"
7776
:class="cn('fixed inset-0 z-2000 data-[state=closed]:animate-out data-[state=open]:animate-in bg-black/50 data-[state=open]:fade-in-0 data-[state=closed]:fade-out-0', {
7877
'backdrop-blur-sm': props.overlayBlur,
7978
})"

src/ui/components/FaModal/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface ModalProps {
3030
headerClass?: HTMLAttributes['class']
3131
contentClass?: HTMLAttributes['class']
3232
footerClass?: HTMLAttributes['class']
33+
modalId?: string
3334
}
3435
export interface ModalEmits {
3536
'update:modelValue': [value: boolean]
@@ -66,6 +67,7 @@ export function useFaModal() {
6667
closeOnClickOverlay: false,
6768
contentClass: 'py-0 min-h-auto',
6869
footerClass: 'p-4',
70+
modalId: useId(),
6971
onClosed: () => {
7072
app.unmount()
7173
},
@@ -87,6 +89,7 @@ export function useFaModal() {
8789
closeOnClickOverlay: false,
8890
contentClass: 'py-0 min-h-auto',
8991
footerClass: 'p-4',
92+
modalId: useId(),
9093
onClosed: () => {
9194
app.unmount()
9295
},
@@ -108,6 +111,7 @@ export function useFaModal() {
108111
closeOnClickOverlay: false,
109112
contentClass: 'py-0 min-h-auto',
110113
footerClass: 'p-4',
114+
modalId: useId(),
111115
onClosed: () => {
112116
app.unmount()
113117
},
@@ -129,6 +133,7 @@ export function useFaModal() {
129133
closeOnClickOverlay: false,
130134
contentClass: 'py-0 min-h-auto',
131135
footerClass: 'p-4',
136+
modalId: useId(),
132137
onClosed: () => {
133138
app.unmount()
134139
},
@@ -150,6 +155,7 @@ export function useFaModal() {
150155
contentClass: 'py-0 min-h-auto',
151156
footerClass: 'p-4',
152157
showCancelButton: true,
158+
modalId: useId(),
153159
onClosed: () => {
154160
app.unmount()
155161
},

src/ui/components/FaModal/index.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ defineExpose({
6060
areaRef: dialogAreaRef,
6161
})
6262
63-
const id = useId()
64-
provide('ModalId', id)
65-
63+
const modalId = useId()
6664
const isOpen = ref(props.modelValue)
6765
const isMaximize = ref(props.maximize)
6866
@@ -122,7 +120,7 @@ function handleFocusOutside(e: Event) {
122120
}
123121
124122
function handleClickOutside(e: Event) {
125-
if (!props.closeOnClickOverlay || (e.target as HTMLElement).dataset.modalId !== id) {
123+
if (!props.closeOnClickOverlay || (e.target as HTMLElement).dataset.modalId !== modalId) {
126124
e.preventDefault()
127125
e.stopPropagation()
128126
}
@@ -154,6 +152,7 @@ function handleAnimationEnd() {
154152
<Dialog :modal="false" :open="isOpen" @update:open="updateOpen">
155153
<DialogContent
156154
ref="dialogContentRef"
155+
:modal-id="modalId"
157156
:open="isOpen"
158157
:closable="props.closable"
159158
:overlay="props.overlay"

0 commit comments

Comments
 (0)