diff --git a/web/src/icons/render.test.ts b/web/src/icons/render.test.ts index 99be42c..e9df467 100644 --- a/web/src/icons/render.test.ts +++ b/web/src/icons/render.test.ts @@ -4,7 +4,8 @@ import { describe, expect, it } from 'vitest' import { useIcons } from '../test/iconLedger' import { glyph } from '../ui/build' import { setWordsHidden } from './ledger' -import { decorateChrome, decorateIcons, iconFor, iconMark, iconWord } from './render' +import { ICONS } from './registry' +import { decorateChrome, decorateIcons, iconFor, iconHover, iconMark, iconWord } from './render' // vitest runs from web/ (vitest.config.ts); jsdom loads no CSS, so read the rule. const css = readFileSync(join(process.cwd(), 'src/styles/icons.css'), 'utf8') @@ -71,6 +72,13 @@ describe('glyph(id)', () => { expect(el.querySelector('[data-icon]')?.getAttribute('data-icon')).toBe('state.warn') expect(el.querySelector('.sr-only')?.textContent).toBe('Stale') }) + + it('glyph() carries the hover title only while no word is painted', () => { + const bare = glyph('cmd.check').querySelector('[data-icon]') + expect(bare?.getAttribute('title')).toBe(iconHover('cmd.check')) + useIcons({ 'cmd.check': { word: 'verify' } }) + expect(glyph('cmd.check').querySelector('[data-icon]')?.getAttribute('title')).toBeNull() + }) }) describe('icon pieces', () => { @@ -83,6 +91,27 @@ describe('icon pieces', () => { expect(el.textContent).toBe('🏪') }) + it('a glyph with no painted word carries a one-word hover title', () => { + expect(iconMark('cmd.check', '').getAttribute('title')).toBe(iconHover('cmd.check')) + useIcons({ 'nav.picks': { word: 'target' } }) + expect(iconMark('nav.picks', '').getAttribute('title')).toBeNull() + }) + + it('every icon hover is one word of at most 20 characters', () => { + for (const icon of ICONS) expect(iconHover(icon.id)).toMatch(/^[^\s.!?,;:]{1,20}$/) + }) + + it('decorateIcons sets the hover title and drops it once a word is painted', () => { + const root = document.createElement('div') + root.innerHTML = '' + decorateIcons(root) + const mark = root.querySelector('[data-icon]') + expect(mark?.getAttribute('title')).toBe(iconHover('nav.store')) + useIcons({ 'nav.store': { word: 'shop' } }) + decorateIcons(root) + expect(mark?.getAttribute('title')).toBeNull() + }) + it('iconWord is null without a word, a tagged span with one', () => { expect(iconWord('nav.store')).toBeNull() useIcons({ 'nav.store': { word: 'shop' } }) diff --git a/web/src/icons/render.ts b/web/src/icons/render.ts index 9b0dd30..0500a3a 100644 --- a/web/src/icons/render.ts +++ b/web/src/icons/render.ts @@ -25,9 +25,17 @@ export function iconFor(id: IconId): ResolvedIcon { return { id, glyph, word, spoken: entry.spoken } } +/** Hover-tooltip word for icons the user did NOT choose a word for. */ +export function iconHover(id: IconId): string { + return sentenceCase(iconEntry(id).words[0]) +} + /** The painted glyph alone, tagged with its id (decorateIcons can refresh it). */ export function iconMark(id: IconId, className: string): HTMLSpanElement { - return h('span', { class: className, 'aria-hidden': 'true', 'data-icon': id }, iconFor(id).glyph) + const { glyph, word } = iconFor(id) + const attrs: Record = { class: className, 'aria-hidden': 'true', 'data-icon': id } + if (!word) attrs.title = iconHover(id) + return h('span', attrs, glyph) } /** The word span that goes right after the icon, or null when it has none. */ @@ -59,6 +67,7 @@ export function decorateIcons(root: ParentNode): void { const next = el.nextElementSibling if (next?.classList.contains('icon-word')) next.remove() el.textContent = iconFor(id).glyph + if (!iconFor(id).word) el.title = iconHover(id); else el.removeAttribute('title') const word = iconWord(id) if (word) el.after(word) el.parentElement?.classList.toggle('has-word', word !== null) diff --git a/web/src/ui/build.ts b/web/src/ui/build.ts index 974ce54..a2bd5e8 100644 --- a/web/src/ui/build.ts +++ b/web/src/ui/build.ts @@ -4,7 +4,7 @@ import { explain } from '../api/errors' import type { IconId } from '../icons/registry' -import { iconFor, iconInline, iconMark, iconWord } from '../icons/render' +import { iconFor, iconHover, iconInline, iconMark, iconWord } from '../icons/render' import { h, type Child } from '../lib/dom' export type Tone = 'info' | 'success' | 'warning' | 'danger' @@ -98,7 +98,7 @@ export function glyph(id: IconId, spoken?: string, className = 'glyph'): HTMLSpa return h( 'span', { class: word ? `${className} has-word` : className }, - h('span', { 'aria-hidden': 'true', 'data-icon': id }, painted), + h('span', word ? { 'aria-hidden': 'true', 'data-icon': id } : { 'aria-hidden': 'true', 'data-icon': id, title: iconHover(id) }, painted), word, h('span', { class: 'sr-only' }, spoken ?? name), )