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:
Sameh KhamisandManjunath Paparaju committed 2026-11-19 01:16:46 -08:00
1 parent 2d7b7be890
commit ec4a5b6afe
10 files changed
+388 -42

No files matched your search

@@ -139,6 +139,30 @@ test('Browse opens a modal on the search field, packs last; Escape returns to Br
assert.equal(document.activeElement, $('#data-library-browse'));
});
test('a backdrop click and a close the browser made itself return to Browse', async (t_) => {
const { owner } = mount(t_);
owner.open();
await settle();
$('#data-library .data-library-intro').click();
await settle();
assert.equal($('#data-library').open, true, 'a click inside keeps it open');
$('#data-library').click();
await settle();
assert.equal($('#data-library').open, false);
assert.equal(document.activeElement, $('#data-library-browse'));
owner.open();
await settle();
assert.equal(document.activeElement, $('#data-library input[type="search"]'));
// An Escape the page may not cancel: the browser closes it first.
$('#data-library').close();
await settle();
assert.equal($('#data-library').open, false);
assert.equal(document.activeElement, $('#data-library-browse'));
owner.open();
await settle();
assert.equal($('#data-library').open, true, 'the owner closed it too');
});
test('filters narrow the list as the viewer types and picks', async (t_) => {
const { owner } = mount(t_);
owner.open();
+21 -31
View File
@@ -5,6 +5,7 @@ import { formatNumber, t, tn } from '../../../i18n/index.js';
import UiButton from '../../kit/UiButton.vue';
import UiCheckbox from '../../kit/UiCheckbox.vue';
import UiCloseButton from '../../kit/UiCloseButton.vue';
import UiDialog from '../../kit/UiDialog.vue';
import UiSelect from '../../kit/UiSelect.js';
import {
libraryStatTiles,
@@ -24,27 +25,18 @@ const { controls } = storeToRefs(useDataLibraryStore());
const root = useTemplateRef('root');
const search = useTemplateRef('search');
watch(
() => props.view.open,
async (open, wasOpen) => {
await nextTick();
const dialog = root.value;
if (!dialog) return;
if (open) {
if (!dialog.open) dialog.showModal?.();
search.value?.focus();
return;
}
if (dialog.open) dialog.close();
// The browser may already have closed it (Escape is not always
// cancelable); either way focus returns to Browse, unless the viewer
// has moved it somewhere else meanwhile.
const active = document.activeElement;
if (wasOpen && (!active || active === document.body || dialog.contains(active)))
document.getElementById('data-library-browse')?.focus();
},
{ immediate: true },
);
// UiDialog opens and closes the dialog as `view.open` says, once the content
// has rendered; Escape and a click on the backdrop dismiss.
const onOpened = () => search.value?.focus();
// The browser may already have closed it (Escape is not always cancelable);
// either way focus returns to Browse, unless the viewer has moved it
// somewhere else meanwhile.
const onClosed = () => {
const dialog = root.value?.element;
const active = document.activeElement;
if (!active || active === document.body || dialog?.contains(active))
document.getElementById('data-library-browse')?.focus();
};
watch(
() => props.view.focusSearch,
async () => {
@@ -81,24 +73,22 @@ const resetLabel = computed(() =>
? t('Confirm: remove every added entry from Data Layers')
: t('Remove every added entry from Data Layers'),
);
// A click on the backdrop (the dialog box itself, outside its content)
// dismisses, like Escape.
const onDialogClick = (event) => {
if (event.target === event.currentTarget) controls.value?.close();
};
</script>
<template>
<dialog
<UiDialog
id="data-library"
ref="root"
class="data-library-dialog"
data-panel-surface
aria-labelledby="data-library-title"
aria-describedby="data-library-intro"
@cancel.prevent="controls?.close()"
@close="view.open && controls?.close()"
@click="onDialogClick"
:open="view.open"
backdrop-dismiss
@dismiss="controls?.close()"
@browser-close="controls?.close()"
@opened="onOpened"
@closed="onClosed"
>
<header class="data-library-header" data-panel-header>
<h2 id="data-library-title" class="panel-title panel-surface-title">
@@ -188,5 +178,5 @@ const onDialogClick = (event) => {
/>
</ul>
</div>
</dialog>
</UiDialog>
</template>
+6 -10
View File
@@ -1,21 +1,17 @@
<script setup>
import { storeToRefs } from 'pinia';
import { computed, onBeforeUnmount, onMounted, shallowRef, useTemplateRef } from 'vue';
import { computed, shallowRef } from 'vue';
import { t, tn } from '../../../i18n/index.js';
import UiDialog from '../../kit/UiDialog.vue';
import { vStatusText } from '../../rail/statusText.js';
import { useSceneSharingStore } from '../../stores/sceneSharing.js';
// One open scene sharing dialog, as src/ui/sceneSharing.js presents it. The
// parent keys this by dialog, so each one opens as a fresh modal.
// parent keys this by dialog, so each one opens as a fresh modal: UiDialog
// opens it on mount and closes it on unmount.
const props = defineProps({ view: { type: Object, required: true } });
const { controls } = storeToRefs(useSceneSharingStore());
const root = useTemplateRef('root');
onMounted(() => root.value?.showModal?.());
onBeforeUnmount(() => {
if (root.value?.open) root.value.close();
});
const bodyItems = computed(() => props.view.items.filter((item) => item.place === 'body'));
const footerItems = computed(() => props.view.items.filter((item) => item.place === 'footer'));
// Typing reaches the item before any republish, so a field is rewritten only
@@ -46,7 +42,7 @@ const chooserStyle = (focused) => ({ padding: '3px 9px', border: '1px solid #397
</script>
<template>
<dialog ref="root" data-director-dialog class="director-sharing-dialog" :aria-label="view.title" :style="dialogStyle" @cancel.prevent="controls?.cancel()">
<UiDialog open data-director-dialog class="director-sharing-dialog" :aria-label="view.title" :style="dialogStyle" @dismiss="controls?.cancel()">
<h2 style="margin-bottom: 12px">{{ view.title }}</h2>
<div>
<template v-for="item in bodyItems" :key="item.id">
@@ -60,5 +56,5 @@ const chooserStyle = (focused) => ({ padding: '3px 9px', border: '1px solid #397
<div>
<button v-for="item in footerItems" :key="item.id" type="button" :style="buttonStyle" :disabled="item.disabled" :autofocus="item.autofocus || undefined" v-bind="item.hook ? { [item.hook]: '' } : {}" @click="controls?.click(item.id)">{{ item.label }}</button>
</div>
</dialog>
</UiDialog>
</template>