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

+66
View File
@@ -0,0 +1,66 @@
// Every modal dialog in a Vue view is a UiDialog: no SFC template hand-writes
// `<dialog>`. Panels drawn as `role="dialog"` (key setup, the welcome
// launcher, the CCTV popup, Story Explore's filter) and the render failure's
// `alertdialog` are other controls.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync, readdirSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const SRC_ROOT = fileURLToPath(new URL('../../', import.meta.url));
const REPO_ROOT = path.resolve(SRC_ROOT, '..');
const KIT = 'src/ui/kit/UiDialog.vue';
/** Every SFC under src. */
function sfcFiles(directory = SRC_ROOT) {
const files = [];
for (const entry of readdirSync(directory, { withFileTypes: true })) {
const absolute = path.join(directory, entry.name);
if (entry.isDirectory()) files.push(...sfcFiles(absolute));
else if (entry.isFile() && entry.name.endsWith('.vue'))
files.push(absolute);
}
return files.sort();
}
/** The template block: everything outside script and style. */
function template(source) {
return source.replace(/<(script|style)\b[\s\S]*?<\/\1\s*>/g, '');
}
// An opening tag's attributes, quoted values may hold `>` (`() => x`).
const ATTRS = String.raw`(?:[^>"']|"[^"]*"|'[^']*')*`;
const HAND_WRITTEN_DIALOG = new RegExp(String.raw`<dialog\b${ATTRS}>`, 'gi');
/** Hand-written dialogs in an SFC's template. */
function handWrittenDialogs(source) {
return [...template(source).matchAll(HAND_WRITTEN_DIALOG)].map((m) =>
m[0].replace(/\s+/g, ' '),
);
}
test('the scan finds each hand-written dialog', () => {
for (const markup of [
'<template><dialog id="x" @cancel.prevent="on"><p /></dialog></template>',
'<template><dialog\n :open="a > b"\n class="x"\n>…</dialog></template>',
])
assert.equal(handWrittenDialogs(markup).length, 1, markup);
for (const markup of [
'<template><UiDialog :open="on" @dismiss="off"><p /></UiDialog></template>',
'<template><div role="dialog" aria-modal="true" /></template>',
"<script>const html = '<dialog open>';</script><template><p /></template>",
])
assert.equal(handWrittenDialogs(markup).length, 0, markup);
});
test('no SFC hand-writes a dialog; each is a UiDialog', () => {
const found = [];
for (const file of sfcFiles()) {
const relative = path.relative(REPO_ROOT, file).split(path.sep).join('/');
if (relative === KIT) continue;
for (const markup of handWrittenDialogs(readFileSync(file, 'utf8')))
found.push(`${relative}: ${markup}`);
}
assert.deepEqual(found, []);
});