diff --git a/.changeset/date-announcements-live-region.md b/.changeset/date-announcements-live-region.md new file mode 100644 index 00000000..940ef206 --- /dev/null +++ b/.changeset/date-announcements-live-region.md @@ -0,0 +1,10 @@ +--- +'@wirecore/wirelayer-recipes': patch +'@wirecore/wirelayer-vue': patch +'@wirecore/wirelayer-react': patch +--- + +DatePicker, DateRangePicker and TimePicker: the parts announce their changes in the app's language again with Ark UI +5.40 (`@zag-js/live-region` 1.47), e.g. "Jahr, 1990" instead of "Year, 1990". Its live region has no id +`__live-region__` anymore and is created once when the field mounts; `translateDateAnnouncements` now finds the regions +by `data-live-announcer` and works with both versions. diff --git a/packages/parity/src/date-logic.test.ts b/packages/parity/src/date-logic.test.ts index cc5f0faa..ba389739 100644 --- a/packages/parity/src/date-logic.test.ts +++ b/packages/parity/src/date-logic.test.ts @@ -336,4 +336,41 @@ describe('segmented date fields', () => { root.remove(); outside.remove(); }); + + it('translateDateAnnouncements: the region of @zag-js/live-region 1.47 (one per field, no id, created first)', async () => { + const root = document.createElement('div'); + root.innerHTML = ''; + // Created when the field mounts, before the translation starts, and reused for every announcement + const region = document.createElement('span'); + region.dataset.liveAnnouncer = 'true'; + region.setAttribute('aria-live', 'assertive'); + region.setAttribute('role', 'alert'); + document.body.append(root, region); + const stop = translateDateAnnouncements( + () => root, + () => ({ segments: { year: 'Jahr', day: 'Tag' }, empty: 'Leer' }) + ); + const announce = async (text: string) => { + region.textContent = ''; + await Promise.resolve(); + region.textContent = text; + await Promise.resolve(); + return region.textContent; + }; + root.querySelector('button')!.focus(); + expect(await announce('Year, 1990')).toBe('Jahr, 1990'); + expect(await announce('Day, Empty')).toBe('Tag, Leer'); + // A region that comes later (another field mounts, or Ark creates it again after it was removed) + const later = region.cloneNode() as HTMLElement; + document.body.append(later); + await Promise.resolve(); + later.textContent = 'Year, Empty'; + await Promise.resolve(); + expect(later.textContent).toBe('Jahr, Leer'); + stop(); + expect(await announce('Year, Empty')).toBe('Year, Empty'); + root.remove(); + region.remove(); + later.remove(); + }); }); diff --git a/packages/parity/src/date-time.test.ts b/packages/parity/src/date-time.test.ts index 2edea558..42a673f4 100644 --- a/packages/parity/src/date-time.test.ts +++ b/packages/parity/src/date-time.test.ts @@ -220,8 +220,8 @@ describe.each(frameworks)('%s', (framework) => { // The parts announce their changes in the language of the field (Ark: in English) const announced: string[] = []; const recorder = new MutationObserver(() => { - const text = document.getElementById('__live-region__')?.textContent; - if (text) announced.push(text); + for (const region of document.querySelectorAll('[data-live-announcer]')) + if (region.textContent) announced.push(region.textContent); }); recorder.observe(document.body, { childList: true, subtree: true, characterData: true }); await userEvent.click(within(group).getByRole('spinbutton', { name: 'Jahr' })); diff --git a/packages/recipes/src/date-time.ts b/packages/recipes/src/date-time.ts index dc590568..8b9eb44d 100644 --- a/packages/recipes/src/date-time.ts +++ b/packages/recipes/src/date-time.ts @@ -995,20 +995,29 @@ export function translateDateAnnouncements( if (typeof MutationObserver === 'undefined' || typeof document === 'undefined') return () => {}; announcingFields.set(element, texts); if (!regionWatch) { - // Ark puts a new region (id __live-region__) into the body for every announcement and sets its text later + // Ark's regions in the body, which get their text later: up to @zag-js/live-region 1.46 a new one with the id + // __live-region__ for every announcement; from 1.47 one per field without an id, created when the field mounts + // (before this runs) and reused. Both have data-live-announcer. + const regionSelector = '[data-live-announcer], #__live-region__'; const region = new MutationObserver((records) => { - const target = records[0]?.target; - const element = target instanceof Element ? target : target?.parentElement; - const live = element?.closest('#__live-region__'); - if (live) translateAnnouncement(live); + const changed = new Set(); + for (const record of records) { + const target = record.target; + const element = target instanceof Element ? target : target.parentElement; + const live = element?.closest(regionSelector); + if (live) changed.add(live); + } + for (const live of changed) translateAnnouncement(live); }); + const watchRegion = (node: Node) => { + if (node instanceof Element && node.matches(regionSelector)) + region.observe(node, { childList: true, characterData: true, subtree: true }); + }; const body = new MutationObserver((records) => { - for (const record of records) - for (const node of record.addedNodes) - if (node instanceof Element && node.id === '__live-region__') - region.observe(node, { childList: true, characterData: true, subtree: true }); + for (const record of records) for (const node of record.addedNodes) watchRegion(node); }); body.observe(document.body, { childList: true }); + for (const node of document.body.children) watchRegion(node); regionWatch = { body, region }; } return () => {