Skip to content
Snippets Groups Projects
Commit 41c40b01 authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

feat: add self-managed open-state for ADialog component

parent 2f4fc4ee
No related branches found
No related tags found
No related merge requests found
<template> <template>
<dialog <dialog
v-if="modelValue" v-if="isOpen"
ref="dialogEl" ref="dialogEl"
class="tw-shadow-xl tw-border tw-border-gray-200 tw-rounded tw-p-0 tw-bg-white tw-z-30 tw-flex tw-flex-col" class="tw-shadow-xl tw-border tw-border-gray-200 tw-rounded tw-p-0 tw-bg-white tw-z-30 tw-flex tw-flex-col"
style="max-width: 95%" style="max-width: 95%"
...@@ -34,17 +34,20 @@ ...@@ -34,17 +34,20 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { nextTick, ref, useSlots, watch } from 'vue' import { computed, nextTick, ref, useSlots, watch } from 'vue'
defineOptions({ compatConfig: { MODE: 3 } })
const titleClass = 'tw-text-lg tw-font-semibold tw-m-0' const titleClass = 'tw-text-lg tw-font-semibold tw-m-0'
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
modelValue: boolean modelValue?: boolean | undefined
isModal?: boolean isModal?: boolean
title?: string title?: string
}>(), }>(),
{ {
modelValue: undefined,
title: '', title: '',
isModal: false, isModal: false,
}, },
...@@ -53,15 +56,38 @@ const emit = defineEmits<{ ...@@ -53,15 +56,38 @@ const emit = defineEmits<{
(e: 'update:modelValue', value: boolean): void (e: 'update:modelValue', value: boolean): void
}>() }>()
const slots = useSlots() const slots = useSlots()
const localIsOpen = ref(false)
const dialogEl = ref<HTMLDialogElement>() const dialogEl = ref<HTMLDialogElement>()
const isOpen = computed({
get() {
return props.modelValue !== undefined ? props.modelValue : localIsOpen.value
},
set(value: boolean) {
if (props.modelValue !== undefined) {
emit('update:modelValue', value)
} else {
localIsOpen.value = value
}
},
})
function close() { function close() {
emit('update:modelValue', false) isOpen.value = false
}
function open() {
isOpen.value = true
} }
defineExpose({
close,
open,
hide: close,
show: open,
})
watch( watch(
() => props.modelValue, isOpen,
async (isOpen) => { async (isOpen) => {
if (isOpen && !dialogEl.value) { if (isOpen && !dialogEl.value) {
await nextTick() await nextTick()
...@@ -83,14 +109,6 @@ watch( ...@@ -83,14 +109,6 @@ watch(
) )
</script> </script>
<script lang="ts">
export default {
compatConfig: {
MODE: 3,
},
}
</script>
<style lang="postcss" scoped> <style lang="postcss" scoped>
dialog::backdrop { dialog::backdrop {
background: rgba(0, 0, 0, 0.5); background: rgba(0, 0, 0, 0.5);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment