mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 09:42:21 +00:00
Vue kit: UiCloseButton for every close button in the views
UiCloseButton is a UiButton with a fixed glyph (× or the Material close symbol through UiIcon), a required accessible label and no tip unless the site passes a title. Attributes and classes pass through, so each site keeps its id, feature class, data and aria attributes, and its look. Its kit class, ui-close-button (close-button.css, loaded before the feature stylesheets), is Core's common look: a bare glyph with no padding, border or background of its own, and a pointer. The guide's close button shares the rule, and the copies of those declarations leave the feature rules. `bare` leaves the look off for Street Level's and the space mission's close buttons, whose skins keep the browser's padding. UiIcon takes size null to leave the glyph's size to the stylesheet. The sixteen hand-written close, dismiss and clear buttons in the SFCs are now UiCloseButtons; closeButtons.test.mjs pins that none remain. Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
1 parent
c58f9fbdc6
commit
1711b4b456
32 files changed
+317
-69
No files matched your search
@@ -0,0 +1,98 @@
|
||||
import { after, before, test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { installVueDom } from '../../testSupport/vueDom.mjs';
|
||||
|
||||
let mount;
|
||||
let UiCloseButton;
|
||||
let uninstall;
|
||||
before(async () => {
|
||||
uninstall = installVueDom();
|
||||
({ mount } = await import('@vue/test-utils'));
|
||||
UiCloseButton = (await import('./UiCloseButton.vue')).default;
|
||||
});
|
||||
after(() => uninstall());
|
||||
|
||||
function mountClose(props = {}, attrs = {}) {
|
||||
return mount(UiCloseButton, {
|
||||
attachTo: document.body,
|
||||
props: { label: 'Close card', ...props },
|
||||
attrs,
|
||||
});
|
||||
}
|
||||
|
||||
test('a close button is a button named by its label, with no tip of its own', () => {
|
||||
const wrapper = mountClose();
|
||||
const button = wrapper.get('button');
|
||||
assert.equal(button.attributes('type'), 'button');
|
||||
assert.equal(button.attributes('aria-label'), 'Close card');
|
||||
assert.equal(button.attributes('title'), undefined);
|
||||
assert.ok(button.classes().includes('ui-close-button'));
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
test('glyph="times" draws the × character', () => {
|
||||
const wrapper = mountClose();
|
||||
const button = wrapper.get('button');
|
||||
assert.equal(button.element.innerHTML, '×');
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
test('glyph="icon" draws the Material close symbol, sized by the stylesheet', () => {
|
||||
const wrapper = mountClose({ glyph: 'icon' });
|
||||
const icon = wrapper.get('button > span');
|
||||
assert.equal(wrapper.get('button').element.children.length, 1);
|
||||
assert.equal(icon.text(), 'close');
|
||||
assert.ok(icon.classes().includes('material-symbols-outlined'));
|
||||
assert.equal(icon.attributes('aria-hidden'), 'true');
|
||||
assert.equal(icon.attributes('style'), undefined);
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
test('attributes and classes pass through to the button', () => {
|
||||
const wrapper = mountClose(
|
||||
{},
|
||||
{
|
||||
id: 'gev-chat-close',
|
||||
class: 'data-toggle-chip',
|
||||
title: 'Clear reading',
|
||||
'data-action-id': 'clear',
|
||||
'aria-hidden': 'true',
|
||||
disabled: true,
|
||||
hidden: true,
|
||||
},
|
||||
);
|
||||
const button = wrapper.get('button');
|
||||
assert.equal(button.attributes('id'), 'gev-chat-close');
|
||||
assert.ok(button.classes().includes('data-toggle-chip'));
|
||||
assert.ok(button.classes().includes('ui-close-button'));
|
||||
assert.equal(button.attributes('title'), 'Clear reading');
|
||||
assert.equal(button.attributes('data-action-id'), 'clear');
|
||||
assert.equal(button.attributes('aria-hidden'), 'true');
|
||||
assert.equal(button.element.disabled, true);
|
||||
assert.equal(button.element.hidden, true);
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
test('bare leaves the kit look off', () => {
|
||||
const wrapper = mountClose({ bare: true }, { class: 'sl-viewer-x' });
|
||||
const classes = wrapper.get('button').classes();
|
||||
assert.ok(classes.includes('sl-viewer-x'));
|
||||
assert.equal(classes.includes('ui-close-button'), false);
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
test('a click is emitted with its event', async () => {
|
||||
const wrapper = mountClose();
|
||||
await wrapper.get('button').trigger('click');
|
||||
const clicks = wrapper.emitted('click');
|
||||
assert.equal(clicks.length, 1);
|
||||
assert.ok(clicks[0][0] instanceof Event);
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
test('a disabled close button emits no click', async () => {
|
||||
const wrapper = mountClose({}, { disabled: true });
|
||||
await wrapper.get('button').trigger('click');
|
||||
assert.equal(wrapper.emitted('click'), undefined);
|
||||
wrapper.unmount();
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
<script setup>
|
||||
import UiButton from './UiButton.vue';
|
||||
import UiIcon from './UiIcon.vue';
|
||||
|
||||
// Core's close, dismiss and clear button: a UiButton with a fixed glyph and
|
||||
// a required accessible name. `glyph` is `times` (the × character) or `icon`
|
||||
// (the Material `close` symbol, sized by the caller's stylesheet). Attributes
|
||||
// and classes pass through to the button, so each site keeps its id, its
|
||||
// feature class and its look; a `title` passed in is its tip, and without
|
||||
// one it has none. The kit class `ui-close-button` (close-button.css) is
|
||||
// Core's common look, a bare glyph: no padding, border or background of its
|
||||
// own, and a pointer. Its rule selects one class and loads before the
|
||||
// feature stylesheets, so a feature rule still sets the size, colour, hover
|
||||
// and any surface. `bare` leaves the kit look off, for a button whose skin
|
||||
// keeps the browser's own padding.
|
||||
const props = defineProps({
|
||||
/** The accessible name (aria-label). */
|
||||
label: { type: String, required: true },
|
||||
glyph: {
|
||||
type: String,
|
||||
default: 'times',
|
||||
validator: (value) => ['times', 'icon'].includes(value),
|
||||
},
|
||||
bare: { type: Boolean, default: false },
|
||||
});
|
||||
const emit = defineEmits(['click']);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<UiButton
|
||||
:class="{ 'ui-close-button': !props.bare }"
|
||||
:label="props.label"
|
||||
:native-title="false"
|
||||
@click="emit('click', $event)"
|
||||
><UiIcon v-if="props.glyph === 'icon'" name="close" :size="null" /><template
|
||||
v-else
|
||||
>×</template
|
||||
></UiButton
|
||||
>
|
||||
</template>
|
||||
@@ -1,5 +1,8 @@
|
||||
<script setup>
|
||||
/** A Material Symbols glyph; the name must be in index.html's `icon_names`. */
|
||||
/**
|
||||
* A Material Symbols glyph; the name must be in index.html's `icon_names`.
|
||||
* `size` is its font size in pixels; null leaves the size to the stylesheet.
|
||||
*/
|
||||
defineProps({
|
||||
name: { type: String, required: true },
|
||||
size: { type: Number, default: 18 },
|
||||
@@ -9,7 +12,7 @@ defineProps({
|
||||
<template>
|
||||
<span
|
||||
class="ui-icon material-symbols-outlined"
|
||||
:style="{ fontSize: `${size}px` }"
|
||||
:style="size == null ? undefined : { fontSize: `${size}px` }"
|
||||
aria-hidden="true"
|
||||
>{{ name }}</span
|
||||
>
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
// Every close, dismiss and clear button in a Vue view is a UiCloseButton: no
|
||||
// SFC template hand-writes a button whose only content is × or the Material
|
||||
// `close` symbol. Buttons built in script are not read here.
|
||||
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, '..');
|
||||
|
||||
/** 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 BUTTON = String.raw`(button|UiButton)`;
|
||||
const CLOSE_GLYPH = [
|
||||
String.raw`×|×|×|×`,
|
||||
String.raw`<span\b[^>]*\bmaterial-symbols-outlined\b[^>]*>\s*close\s*</span\s*>`,
|
||||
String.raw`<UiIcon${ATTRS}\bname="close"${ATTRS}/>`,
|
||||
].join('|');
|
||||
const HAND_WRITTEN_CLOSE = new RegExp(
|
||||
String.raw`<${BUTTON}\b${ATTRS}>\s*(?:${CLOSE_GLYPH})\s*</\1\s*>`,
|
||||
'gi',
|
||||
);
|
||||
|
||||
/** Hand-written close buttons in an SFC's template. */
|
||||
function handWrittenCloses(source) {
|
||||
return [...template(source).matchAll(HAND_WRITTEN_CLOSE)].map((m) =>
|
||||
m[0].replace(/\s+/g, ' '),
|
||||
);
|
||||
}
|
||||
|
||||
test('the scan finds each hand-written close glyph', () => {
|
||||
for (const markup of [
|
||||
'<template><button type="button" @click="emit(\'close\')">×</button></template>',
|
||||
'<template><button\n class="x"\n :title="a > b ? 1 : 2"\n >×</button\n></template>',
|
||||
'<template><UiButton class="x" :label="l">×</UiButton></template>',
|
||||
'<template><button type="button" class="x">\n <span class="material-symbols-outlined" aria-hidden="true">close</span>\n</button></template>',
|
||||
'<template><UiButton class="x"><UiIcon name="close" :size="14" /></UiButton></template>',
|
||||
])
|
||||
assert.equal(handWrittenCloses(markup).length, 1, markup);
|
||||
for (const markup of [
|
||||
'<template><UiCloseButton class="x" :label="l" /></template>',
|
||||
'<template><button type="button">{{ name }} ×</button></template>',
|
||||
'<template><button type="button">On<span class="material-symbols-outlined">close</span></button></template>',
|
||||
'<template><button type="button"><span class="material-symbols-outlined">close_fullscreen</span></button></template>',
|
||||
])
|
||||
assert.equal(handWrittenCloses(markup).length, 0, markup);
|
||||
});
|
||||
|
||||
test('no SFC hand-writes a close button; each is a UiCloseButton', () => {
|
||||
const found = [];
|
||||
for (const file of sfcFiles()) {
|
||||
const relative = path.relative(REPO_ROOT, file).split(path.sep).join('/');
|
||||
for (const markup of handWrittenCloses(readFileSync(file, 'utf8')))
|
||||
found.push(`${relative}: ${markup}`);
|
||||
}
|
||||
assert.deepEqual(found, []);
|
||||
});
|
||||
@@ -138,6 +138,17 @@ test('UiChip, UiEmpty, UiBadge and UiSegmented wear Core’s skins', () => {
|
||||
shares('.ui-segmented__item:disabled', '.gev-segment:disabled');
|
||||
});
|
||||
|
||||
test('UiCloseButton wears the bare glyph look the guide’s close shares', () => {
|
||||
const rule = shares('.ui-close-button', '.gev-guide-close');
|
||||
for (const declaration of [
|
||||
/padding:\s*0;/,
|
||||
/border:\s*0;/,
|
||||
/background:\s*none;/,
|
||||
/cursor:\s*pointer;/,
|
||||
])
|
||||
assert.match(rule.body, declaration);
|
||||
});
|
||||
|
||||
test('the kit’s skins load before the feature stylesheets that restyle them', () => {
|
||||
const at = (selector) => css.indexOf(selector);
|
||||
for (const [kit, feature] of [
|
||||
@@ -146,6 +157,15 @@ test('the kit’s skins load before the feature stylesheets that restyle them',
|
||||
['.ui-empty,', '.recent-imagery-readout .ri-strip-empty {'],
|
||||
['.ui-empty,', '#space-mission-panel-host .space-mission-roster-empty {'],
|
||||
['.ui-button--icon,', '#command-dock .panel-collapse-btn'],
|
||||
['.ui-close-button,', '.data-toggle-chip {'],
|
||||
['.ui-close-button,', '.geo-import-action {'],
|
||||
['.ui-close-button,', '.panel-surface-control {'],
|
||||
['.ui-close-button,', '.gev-voice-card-dismiss {'],
|
||||
['.ui-close-button,', '.bhote-event-icon-btn {'],
|
||||
['.ui-close-button,', '.entity-card__close {'],
|
||||
['.ui-close-button,', '.context-search__clear {'],
|
||||
['.ui-close-button,', '.key-setup-close {'],
|
||||
['.ui-close-button,', '.gev-guide-close {'],
|
||||
]) {
|
||||
assert.ok(
|
||||
at(kit) >= 0 && at(feature) > at(kit),
|
||||
|
||||
Reference in new issue
Block a user