Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/date-announcements-live-region.md
Original file line number Diff line number Diff line change
@@ -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.
37 changes: 37 additions & 0 deletions packages/parity/src/date-logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<button>part</button>';
// 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();
});
});
4 changes: 2 additions & 2 deletions packages/parity/src/date-time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }));
Expand Down
27 changes: 18 additions & 9 deletions packages/recipes/src/date-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Element>();
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 () => {
Expand Down
Loading