mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Vue kit: UiDialog for the Data Library and scene sharing dialogs
UiDialog is a native <dialog> its owner opens and closes with open: true opens it with showModal() (on mount, for one mounted open), false closes it, and unmounting closes it. Each change is applied once the content has rendered and then emitted as opened or closed, so the Data Library still focuses its search field on open and returns focus to Browse on close. Escape's cancel is prevented and emitted as dismiss; backdropDismiss makes a click on the dialog itself a dismiss too, as the Data Library's was. When the browser closes the dialog on its own while open is still true, browserClose tells the owner, as the Data Library's close listener did; closing on unmount reports nothing. A DOM without showModal() leaves it closed. Attributes, classes, styles and listeners go to the dialog, and element is exposed. The two dialogs share no declarations, so there are no kit rules. dialogs.test.mjs fails if a view writes a <dialog> by hand; the panels drawn as role="dialog" are other controls. Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
1 parent
2d7b7be890
commit
ec4a5b6afe
10 files changed
+388
-42
No files matched your search
@@ -0,0 +1,72 @@
|
||||
<script setup>
|
||||
import { onBeforeUnmount, onMounted, useTemplateRef, watch } from 'vue';
|
||||
|
||||
// Core's modal dialog: a native `<dialog>` the owner opens and closes with
|
||||
// `open`. True opens it with showModal() (once mounted, so a dialog mounted
|
||||
// open opens straight away), false closes it, and unmounting closes it.
|
||||
// Each change is applied after the content has rendered and is then emitted
|
||||
// (`opened`, `closed`), so the owner can move focus into the content it
|
||||
// shows or back to the control that opened it. Escape's `cancel` is
|
||||
// prevented and emitted as `dismiss`, so the owner decides; with
|
||||
// `backdropDismiss`, a click on the backdrop (the dialog element itself,
|
||||
// outside its content) is a `dismiss` too. When the browser closes the
|
||||
// dialog on its own while `open` is still true (an Escape it does not let
|
||||
// the page cancel), `browserClose` tells the owner. A DOM without
|
||||
// showModal() (a test's) leaves the dialog closed rather than failing.
|
||||
// Attributes, classes, styles and listeners pass through to the dialog; the
|
||||
// content is the default slot. `element` is the dialog element.
|
||||
const props = defineProps({
|
||||
open: { type: Boolean, default: false },
|
||||
backdropDismiss: { type: Boolean, default: false },
|
||||
});
|
||||
const emit = defineEmits(['dismiss', 'browserClose', 'opened', 'closed']);
|
||||
const element = useTemplateRef('element');
|
||||
let unmounting = false;
|
||||
|
||||
function show() {
|
||||
const dialog = element.value;
|
||||
if (!dialog) return;
|
||||
if (!dialog.open) dialog.showModal?.();
|
||||
emit('opened');
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (props.open) show();
|
||||
});
|
||||
watch(
|
||||
() => props.open,
|
||||
(open) => {
|
||||
if (open) return show();
|
||||
const dialog = element.value;
|
||||
if (!dialog) return;
|
||||
// The browser may already have closed it.
|
||||
if (dialog.open) dialog.close();
|
||||
emit('closed');
|
||||
},
|
||||
{ flush: 'post' },
|
||||
);
|
||||
onBeforeUnmount(() => {
|
||||
unmounting = true;
|
||||
if (element.value?.open) element.value.close();
|
||||
});
|
||||
|
||||
function onCancel(event) {
|
||||
event.preventDefault();
|
||||
emit('dismiss');
|
||||
}
|
||||
function onClose() {
|
||||
if (!unmounting && props.open) emit('browserClose');
|
||||
}
|
||||
function onClick(event) {
|
||||
if (props.backdropDismiss && event.target === event.currentTarget)
|
||||
emit('dismiss');
|
||||
}
|
||||
|
||||
defineExpose({ element });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<dialog ref="element" @cancel="onCancel" @close="onClose" @click="onClick">
|
||||
<slot />
|
||||
</dialog>
|
||||
</template>
|
||||
Reference in new issue
Block a user