Audit icons: add 1-word hover tooltips
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run

Todo: fe70b845
This commit is contained in:
Krzysztof Rudnicki 2026-11-22 18:20:57 +01:00
parent 5658331478
commit 9b8dc5cb26
3 changed files with 42 additions and 4 deletions

View File

@ -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 = '<a><span data-icon="nav.store"></span></a>'
decorateIcons(root)
const mark = root.querySelector<HTMLElement>('[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' } })

View File

@ -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<string, string> = { 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)

View File

@ -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),
)