Vue kit: UiCheckbox for the Visual, voice, Data Library and welcome checkboxes

UiCheckbox is a label wrapping a native checkbox and its words; trailing
puts the box after them, as the Visual panel's rows have it. v-model shows
the value and emits the checked state, and change is emitted with its event;
without a modelValue the box is the browser's own, as the welcome card's
"Don't show this again" is. restore puts the box back to modelValue once a
change has rendered without the owner taking it, which is what the voice
panel's v-checked directive did for a refused preference. Attributes go to
the label; inputId and inputAttrs to the box.

checkbox.css holds the centred flex row, moved out of the two labels that
hold only a checkbox (first-run.css, library.css) and loaded before them.
Each box keeps its skin's size, margin and accent colour. checkboxes.test.mjs
fails if a view writes a checkbox by hand; Story Explore's filters are not
converted yet.

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju committed 2026-11-19 01:05:12 -08:00
1 parent 52dd576769
commit 2d7b7be890
18 files changed
+385 -41

No files matched your search

+162
View File
@@ -0,0 +1,162 @@
import { after, before, test } from 'node:test';
import assert from 'node:assert/strict';
import { installVueDom } from '../../testSupport/vueDom.mjs';
let mount;
let nextTick;
let UiCheckbox;
let uninstall;
before(async () => {
uninstall = installVueDom();
({ mount } = await import('@vue/test-utils'));
({ nextTick } = await import('vue'));
UiCheckbox = (await import('./UiCheckbox.vue')).default;
});
after(() => uninstall());
/** A checkbox whose v-model writes back into its props, as a parent's would. */
function bound(props = {}, attrs = {}) {
const wrapper = mount(UiCheckbox, {
attachTo: document.body,
props: {
modelValue: false,
...props,
'onUpdate:modelValue': (value) => wrapper.setProps({ modelValue: value }),
},
attrs,
slots: { default: '<span class="words">Scope</span>' },
});
return wrapper;
}
/** Tick the box as a person does: flip it, then the change event. */
async function tick(input) {
input.element.checked = !input.element.checked;
await input.trigger('change');
}
test('a checkbox is a label wrapping a native box, then its words', () => {
const wrapper = bound();
const root = wrapper.get('label');
assert.ok(root.classes().includes('ui-checkbox'));
const input = wrapper.get('input');
assert.equal(input.attributes('type'), 'checkbox');
assert.ok(input.classes().includes('ui-checkbox__input'));
assert.equal(input.attributes('id'), undefined);
const elements = [...root.element.children];
assert.deepEqual(
elements.map((el) => el.tagName),
['INPUT', 'SPAN'],
);
wrapper.unmount();
});
test('trailing puts the box after its words', () => {
const wrapper = bound({ trailing: true });
assert.deepEqual(
[...wrapper.get('label').element.children].map((el) => el.tagName),
['SPAN', 'INPUT'],
);
wrapper.unmount();
});
test('attributes and classes go to the label; inputId and inputAttrs to the box', () => {
const wrapper = bound(
{
inputId: 'scope-toggle',
inputAttrs: { 'aria-describedby': 'note', 'data-hook': '' },
},
{ class: 'pp-scope-toggle', title: 'Scope', id: 'scope-row' },
);
const root = wrapper.get('label');
assert.ok(root.classes().includes('pp-scope-toggle'));
assert.equal(root.attributes('title'), 'Scope');
assert.equal(root.attributes('id'), 'scope-row');
const input = wrapper.get('input');
assert.equal(input.attributes('id'), 'scope-toggle');
assert.equal(input.attributes('aria-describedby'), 'note');
assert.equal(input.attributes('data-hook'), '');
assert.equal(input.attributes('title'), undefined);
assert.ok(!input.classes().includes('pp-scope-toggle'));
wrapper.unmount();
});
test('v-model shows the value and reports each change', async () => {
const wrapper = bound({ modelValue: true });
const input = wrapper.get('input');
assert.equal(input.element.checked, true);
await tick(input);
assert.deepEqual(wrapper.emitted('update:modelValue').at(-1), [false]);
assert.equal(wrapper.emitted('change').at(-1)[0].target, input.element);
assert.equal(input.element.checked, false);
await tick(input);
assert.deepEqual(wrapper.emitted('update:modelValue').at(-1), [true]);
// A parent that resets its value resets the box.
await wrapper.setProps({ modelValue: false });
assert.equal(input.element.checked, false);
wrapper.unmount();
});
test('without restore a change the owner ignores stays ticked, as :checked did', async () => {
const wrapper = mount(UiCheckbox, { props: { modelValue: false } });
const input = wrapper.get('input');
await tick(input);
await nextTick();
assert.deepEqual(wrapper.emitted('update:modelValue'), [[true]]);
assert.equal(input.element.checked, true);
wrapper.unmount();
});
test('restore puts a refused change back and keeps an accepted one', async () => {
// Refused: the owner keeps its value.
const refused = mount(UiCheckbox, {
props: { modelValue: true, restore: true },
});
const box = refused.get('input');
await tick(box);
assert.deepEqual(refused.emitted('update:modelValue'), [[false]]);
await nextTick();
assert.equal(box.element.checked, true);
refused.unmount();
// Accepted: the owner takes the change before the box is checked again.
const accepted = bound({ modelValue: true, restore: true });
const input = accepted.get('input');
await tick(input);
await nextTick();
assert.equal(input.element.checked, false);
assert.equal(accepted.props('modelValue'), false);
accepted.unmount();
});
test('without a modelValue the box is the browser’s own', async () => {
const wrapper = mount(UiCheckbox, {
props: { inputAttrs: { 'data-first-run-suppress': '' } },
});
const input = wrapper.get('input');
assert.equal(input.element.checked, false);
await tick(input);
assert.equal(input.element.checked, true);
assert.equal(wrapper.emitted('change').length, 1);
// Rendering again leaves what was ticked alone.
await wrapper.setProps({ inputId: 'suppress' });
assert.equal(input.attributes('id'), 'suppress');
assert.equal(input.element.checked, true);
wrapper.unmount();
});
test('a change listener sees the box as the event’s current target', async () => {
let seen = null;
const wrapper = mount(UiCheckbox, {
attrs: {
onChange: (event) => {
seen = event.currentTarget;
},
},
});
const input = wrapper.get('input');
await tick(input);
assert.equal(seen, input.element);
// The label itself does not hear the box's change a second time.
assert.equal(wrapper.emitted('change').length, 1);
wrapper.unmount();
});
+57
View File
@@ -0,0 +1,57 @@
<script setup>
import { nextTick, ref } from 'vue';
// Core's checkbox: a `<label>` wrapping a native `<input type="checkbox">`
// and the default slot, which names it. The box comes first, or after the
// slot with `trailing`. Attributes and classes pass through to the label, so
// each site keeps its feature class and tip; the input takes `inputId` and
// `inputAttrs` (other attributes it needs, such as aria-describedby or a
// data hook). With v-model the box shows `modelValue` and a change emits the
// checked state; `change` is emitted with the event too. Without a
// modelValue the box is the browser's own and keeps whatever is ticked.
// `restore` puts the box back to `modelValue` after a change its owner did
// not take, so a refused preference shows the applied value again. The kit
// classes `ui-checkbox` and `ui-checkbox__input` (checkbox.css) are the
// label's centred flex row and the box, which keeps each skin's look.
const props = defineProps({
modelValue: { type: Boolean, default: undefined },
inputId: { type: String, default: undefined },
inputAttrs: { type: Object, default: undefined },
trailing: { type: Boolean, default: false },
restore: { type: Boolean, default: false },
});
const emit = defineEmits(['update:modelValue', 'change']);
const input = ref(null);
function onChange(event) {
emit('update:modelValue', event.target.checked);
emit('change', event);
if (!props.restore) return;
// The owner has answered once the update it caused has rendered.
nextTick(() => {
const box = input.value;
if (
box &&
props.modelValue !== undefined &&
box.checked !== props.modelValue
)
box.checked = props.modelValue;
});
}
</script>
<template>
<label class="ui-checkbox">
<slot v-if="props.trailing" />
<input
ref="input"
type="checkbox"
:id="props.inputId"
class="ui-checkbox__input"
v-bind="props.inputAttrs"
:checked="props.modelValue"
@change="onChange"
/>
<slot v-if="!props.trailing" />
</label>
</template>
+70
View File
@@ -0,0 +1,70 @@
// Every checkbox in a Vue view is a UiCheckbox: no SFC template hand-writes
// `<input type="checkbox">`. Story Explore's filters are not converted yet.
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 NOT_YET = new Set([
'src/ui/kit/UiCheckbox.vue',
'src/ui/views/stories/ExploreScreen.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_CHECKBOX = new RegExp(
String.raw`<input\b${ATTRS}\btype="checkbox"${ATTRS}>`,
'gi',
);
/** Hand-written checkboxes in an SFC's template. */
function handWrittenCheckboxes(source) {
return [...template(source).matchAll(HAND_WRITTEN_CHECKBOX)].map((m) =>
m[0].replace(/\s+/g, ' '),
);
}
test('the scan finds each hand-written checkbox', () => {
for (const markup of [
'<template><input type="checkbox" id="x" @change="on" /></template>',
'<template><input\n :checked="a > b"\n type="checkbox"\n/></template>',
])
assert.equal(handWrittenCheckboxes(markup).length, 1, markup);
for (const markup of [
'<template><UiCheckbox input-id="x" v-model="on">On</UiCheckbox></template>',
'<template><input type="search" /></template>',
'<script>const html = \'<input type="checkbox">\';</script><template><p /></template>',
])
assert.equal(handWrittenCheckboxes(markup).length, 0, markup);
});
test('no SFC hand-writes a checkbox; each is a UiCheckbox', () => {
const found = [];
for (const file of sfcFiles()) {
const relative = path.relative(REPO_ROOT, file).split(path.sep).join('/');
if (NOT_YET.has(relative)) continue;
for (const markup of handWrittenCheckboxes(readFileSync(file, 'utf8')))
found.push(`${relative}: ${markup}`);
}
assert.deepEqual(found, []);
});
+29
View File
@@ -188,6 +188,31 @@ test('UiSearchField owns the row and bare input both search skins drew', () => {
}
});
test('UiCheckbox owns the centred row the checkbox-only labels drew', () => {
const label = ruleWith('.ui-checkbox');
assert.deepEqual(label.selectors, ['.ui-checkbox']);
assert.match(label.body, /display:\s*flex;/);
assert.match(label.body, /align-items:\s*center;/);
// The box keeps each skin's size, margin and accent colour.
assert.ok(
!all.some((rule) =>
rule.selectors.some((s) => s.startsWith('.ui-checkbox__input')),
),
);
// Labels that hold only a checkbox leave the row to the kit; the Visual
// and voice rows share their classes with buttons and other labels, so
// theirs stay.
for (const selector of ['.first-run-suppress', '.data-library-check']) {
const feature = ruleWith(selector);
for (const property of ['display', 'align-items'])
assert.doesNotMatch(
feature.body,
new RegExp(`(^|[\\s;])${property}:`),
`${selector} leaves ${property} to the kit`,
);
}
});
test('the kit’s skins load before the feature stylesheets that restyle them', () => {
const at = (selector) => css.indexOf(selector);
for (const [kit, feature] of [
@@ -209,6 +234,10 @@ test('the kit’s skins load before the feature stylesheets that restyle them',
['.ui-search-field {', '.layer-finder__field {'],
['.ui-search-field__input {', '.context-search__input {'],
['.ui-search-field__input {', '.layer-finder__field input {'],
['.ui-checkbox {', '.pp-toggle-btn {'],
['.ui-checkbox {', '.gev-voice-preference {'],
['.ui-checkbox {', '.data-library-check {'],
['.ui-checkbox {', '.first-run-suppress {'],
]) {
assert.ok(
at(kit) >= 0 && at(feature) > at(kit),