Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(lab): Schedule times read in order right-to-left (#7077)

Cindy Zhangaea0d3e03e9b
diff --git a/apps/storybook/stories/Schedule.stories.tsx b/apps/storybook/stories/Schedule.stories.tsx
index a1711ad9e9..18b22b5bf5 100644
--- a/apps/storybook/stories/Schedule.stories.tsx
+++ b/apps/storybook/stories/Schedule.stories.tsx
@@ -3,6 +3,7 @@
import {useMemo, useState} from 'react';
import type {Meta, StoryObj} from '@storybook/react';
import {Text} from '@astryxdesign/core';
+import {InternationalizationProvider} from '@astryxdesign/core/i18n';
import {
Schedule,
createScheduleDayView,
@@ -315,6 +316,33 @@ export const List: Story = {
},
};
+/**
+ * The list in a right-to-left locale: its 24-hour time ranges read start
+ * first, right to left.
+ */
+export const RightToLeftLocale: Story = {
+ render: () => {
+ const [date, setDate] = useState<Instant>(focusDate);
+ const view = useMemo(() => createScheduleListView(), []);
+
+ return (
+ <InternationalizationProvider locale="he-IL">
+ <div dir="rtl">
+ <Schedule
+ view={view}
+ events={events}
+ categories={categories}
+ date={date}
+ focusDate={focusDate}
+ onChangeDate={setDate}
+ timezoneID="America/Los_Angeles"
+ />
+ </div>
+ </InternationalizationProvider>
+ );
+ },
+};
+
export const AsyncLoader: Story = {
render: () => {
const [date, setDate] = useState<Instant>(focusDate);
diff --git a/packages/lab/src/Schedule/Schedule.test.tsx b/packages/lab/src/Schedule/Schedule.test.tsx
index f48ba41760..4fbc0832d4 100644
--- a/packages/lab/src/Schedule/Schedule.test.tsx
+++ b/packages/lab/src/Schedule/Schedule.test.tsx
@@ -263,6 +263,45 @@ describe('Schedule', () => {
expect(screen.getAllByText('11:00 PM - 2:00 AM')).toHaveLength(2);
});
+ it('isolates each painted time in the locale direction, whatever the layout direction', async () => {
+ const renderList = (locale: string) =>
+ render(
+ <InternationalizationProvider locale={locale} dir="rtl">
+ <div dir="rtl">
+ <Schedule
+ view={createScheduleListView({days: 7})}
+ events={events}
+ categories={categories}
+ date={Date.UTC(2026, 4, 13)}
+ timezoneID="UTC"
+ />
+ </div>
+ </InternationalizationProvider>,
+ );
+ const timesOf = async () => {
+ await waitFor(() => {
+ expect(screen.getByText('Visible sync')).toBeInTheDocument();
+ });
+ return Array.from(document.querySelectorAll('bdi')).map(time => [
+ time.textContent,
+ time.getAttribute('dir'),
+ ]);
+ };
+
+ const english = renderList('en-US');
+ expect(await timesOf()).toEqual([
+ ['All day', 'ltr'],
+ ['4:00 PM - 4:30 PM', 'ltr'],
+ ]);
+ english.unmount();
+
+ // A right-to-left locale keeps its 24-hour range start first.
+ renderList('he-IL');
+ const hebrew = await timesOf();
+ expect(hebrew.map(([, dir]) => dir)).toEqual(['rtl', 'rtl']);
+ expect(hebrew[1][0]).toMatch(/^16:00\s?-\s?16:30$/u);
+ });
+
it('renders weekly view with the same month title as monthly view', () => {
render(
<Schedule
diff --git a/packages/lab/src/Schedule/TimeGridView.tsx b/packages/lab/src/Schedule/TimeGridView.tsx
index 75093d01de..f92be78e95 100644
--- a/packages/lab/src/Schedule/TimeGridView.tsx
+++ b/packages/lab/src/Schedule/TimeGridView.tsx
@@ -58,6 +58,7 @@ import {
getEventCategory,
getMinutesSinceStartOfDay,
isEventInPast,
+ ScheduleTime,
styles,
} from './shared';
import {layoutTimedEvents, type TimedEventPlacement} from './timeGridLayout';
@@ -492,7 +493,7 @@ export function TimeGridView({
styles.timeLabel,
styles.timeLabelPosition(index + 1, hourHeight),
)}>
- {formatHour(hour, locale)}
+ <ScheduleTime>{formatHour(hour, locale)}</ScheduleTime>
</div>
))}
</div>
@@ -578,7 +579,7 @@ export function TimeGridView({
type="supporting"
color="inherit"
xstyle={styles.eventTime}>
- {timeLabel}
+ <ScheduleTime>{timeLabel}</ScheduleTime>
</Text>
</>
);
diff --git a/packages/lab/src/Schedule/__tests__/ScheduleTimeDirection.a11y.chromium.spec.ts b/packages/lab/src/Schedule/__tests__/ScheduleTimeDirection.a11y.chromium.spec.ts
new file mode 100644
index 0000000000..2c0e14f7e2
--- /dev/null
+++ b/packages/lab/src/Schedule/__tests__/ScheduleTimeDirection.a11y.chromium.spec.ts
@@ -0,0 +1,195 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file ScheduleTimeDirection.a11y.chromium.spec.ts
+ * @input The built Lab/Schedule week, month, and list stories in real Chromium
+ * @output Receipted evidence that every painted time — hour labels, event
+ * times, month chip times, and list rows — reads in order in both
+ * directions: a number paints before its AM or PM, and a range reads start
+ * first in its locale's direction (en-US in either layout, he-IL)
+ * @position Browser binding for the Schedule's direction support
+ * (`component:Schedule` AR6). jsdom does not run the bidirectional
+ * algorithm, so only a browser shows the painted order.
+ */
+
+import {expect, test, type Page} from '@playwright/test';
+import {
+ finishEvidence,
+ openStory,
+ record,
+ startEvidence,
+ WEEKLY,
+ WIDE,
+ type Evidence,
+} from './timeGridProbe';
+
+/** Each view, and whether it paints time ranges (month chips show starts). */
+const STORIES = [
+ ['week', WEEKLY, true],
+ ['month', 'lab-schedule--monthly', false],
+ ['list', 'lab-schedule--list', true],
+] as const;
+/** The list under a right-to-left locale with 24-hour times. */
+const RTL_LOCALE = 'lab-schedule--right-to-left-locale';
+
+let evidence: Evidence;
+test.beforeAll(async () => {
+ evidence = await startEvidence();
+});
+test.afterAll(async () => {
+ await finishEvidence(evidence, 'time-direction');
+});
+
+interface TimeReading {
+ readonly text: string;
+ /** Every number paints before its AM or PM. */
+ readonly meridiemInOrder: boolean;
+ /** For a range, which side its start paints on. */
+ readonly startSide: 'left' | 'right' | null;
+}
+
+/**
+ * Every visible time in the schedule: text nodes holding a time, skipping
+ * visually hidden text (read by assistive technology in logical order, never
+ * painted).
+ */
+function readTimes(page: Page): Promise<TimeReading[]> {
+ return page.evaluate(() => {
+ const root = document.querySelector('.astryx-schedule');
+ if (root == null) {
+ throw new Error('no Schedule in the story');
+ }
+ const isHidden = (element: Element | null): boolean => {
+ for (let node = element; node != null; node = node.parentElement) {
+ const style = getComputedStyle(node);
+ const rect = node.getBoundingClientRect();
+ if (
+ style.clipPath !== 'none' ||
+ style.clip !== 'auto' ||
+ (rect.width <= 1 && style.overflow === 'hidden')
+ ) {
+ return true;
+ }
+ }
+ return false;
+ };
+ const rectOf = (node: Node, from: number, to: number) => {
+ const range = document.createRange();
+ range.setStart(node, from);
+ range.setEnd(node, to);
+ return range.getBoundingClientRect();
+ };
+ const readings: Array<{
+ text: string;
+ meridiemInOrder: boolean;
+ startSide: 'left' | 'right' | null;
+ }> = [];
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
+ for (let node = walker.nextNode(); node != null; node = walker.nextNode()) {
+ const text = node.textContent ?? '';
+ const times = [
+ ...text.matchAll(/(\d{1,2}(?::\d{2})?)(?:\s?([AP]M))?/gu),
+ ].filter(match => match[2] != null || match[1].includes(':'));
+ if (times.length === 0 || isHidden(node.parentElement)) {
+ continue;
+ }
+ const boxes = times.map(match => {
+ const at = match.index ?? 0;
+ const number = rectOf(node, at, at + match[1].length);
+ const meridiemAt =
+ match[2] == null ? -1 : text.indexOf(match[2], at + match[1].length);
+ return {
+ number,
+ meridiem:
+ meridiemAt < 0 ? null : rectOf(node, meridiemAt, meridiemAt + 2),
+ };
+ });
+ if (boxes.some(box => box.number.width < 1)) {
+ continue;
+ }
+ const [start, end] = boxes;
+ readings.push({
+ text: text.trim(),
+ meridiemInOrder: boxes.every(
+ box =>
+ box.meridiem == null || box.number.right <= box.meridiem.left + 1,
+ ),
+ startSide:
+ end == null
+ ? null
+ : start.number.left < end.number.left
+ ? 'left'
+ : 'right',
+ });
+ }
+ return readings;
+ });
+}
+
+async function expectTimesInOrder(
+ page: Page,
+ name: string,
+ story: string,
+ direction: 'ltr' | 'rtl',
+ localeDirection: 'ltr' | 'rtl',
+ paintsRanges: boolean,
+) {
+ const times = await readTimes(page);
+ const ranges = times.filter(time => time.startSide != null);
+ // A range reads start first in its locale's direction: on the left for a
+ // left-to-right locale, on the right for a right-to-left one, whatever the
+ // layout direction.
+ const startSide = localeDirection === 'ltr' ? 'left' : 'right';
+ const outOfOrder = times
+ .filter(
+ time =>
+ !time.meridiemInOrder ||
+ (time.startSide != null && time.startSide !== startSide),
+ )
+ .map(time => time.text);
+ await record(evidence, page, name, story, direction, {
+ count: times.length,
+ ranges: ranges.length,
+ outOfOrder,
+ });
+ expect(times.length, `${name} paints times`).toBeGreaterThan(0);
+ if (paintsRanges) {
+ expect(ranges.length, `${name} paints ranges`).toBeGreaterThan(0);
+ }
+ expect(outOfOrder, `${name} times painted out of order`).toEqual([]);
+}
+
+for (const [view, story, paintsRanges] of STORIES) {
+ for (const [direction, globals] of [
+ ['ltr', undefined],
+ ['rtl', 'direction:rtl'],
+ ] as const) {
+ test(`every painted ${view} time reads in order (en-US, ${direction})`, async ({
+ page,
+ }) => {
+ await openStory(evidence, page, story, WIDE, globals);
+ await expectTimesInOrder(
+ page,
+ `time-direction-${view}-${direction}`,
+ story,
+ direction,
+ 'ltr',
+ paintsRanges,
+ );
+ });
+ }
+}
+
+test('a right-to-left locale reads its 24-hour ranges start first (he-IL)', async ({
+ page,
+}) => {
+ await openStory(evidence, page, RTL_LOCALE, WIDE);
+ await expectTimesInOrder(
+ page,
+ 'time-direction-list-he-IL',
+ RTL_LOCALE,
+ 'rtl',
+ 'rtl',
+ true,
+ );
+});
diff --git a/packages/lab/src/Schedule/__tests__/ScheduleTimeGridOverlap.a11y.chromium.spec.ts b/packages/lab/src/Schedule/__tests__/ScheduleTimeGridOverlap.a11y.chromium.spec.ts
index 229979bc64..376219a74a 100644
--- a/packages/lab/src/Schedule/__tests__/ScheduleTimeGridOverlap.a11y.chromium.spec.ts
+++ b/packages/lab/src/Schedule/__tests__/ScheduleTimeGridOverlap.a11y.chromium.spec.ts
@@ -199,7 +199,8 @@ test('narrow tracks keep the title and drop the time line', async ({page}) => {
titleVisible:
getComputedStyle(title).display !== 'none' &&
title.getBoundingClientRect().height > 0,
- timeDisplay: time == null ? null : getComputedStyle(time).display,
+ // Whether the time line paints, read through any inner markup.
+ timeVisible: time == null ? null : time.checkVisibility(),
};
});
});
@@ -212,8 +213,8 @@ test('narrow tracks keep the title and drop the time line', async ({page}) => {
// Five tracks in a 140px column are ~24px each: below the time label's
// room, so the time line is gone rather than squeezing the title.
if ((reading.blockWidth ?? 0) <= 72) {
- expect(reading.timeDisplay, `${reading.title} time line collapses`).toBe(
- 'none',
+ expect(reading.timeVisible, `${reading.title} time line collapses`).toBe(
+ false,
);
}
}
diff --git a/packages/lab/src/Schedule/shared.tsx b/packages/lab/src/Schedule/shared.tsx
index 4207c8cd99..e90cad6720 100644
--- a/packages/lab/src/Schedule/shared.tsx
+++ b/packages/lab/src/Schedule/shared.tsx
@@ -11,7 +11,7 @@
import {type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
-import type {Locale} from '@astryxdesign/core/i18n';
+import {getLocaleDirection, type Locale} from '@astryxdesign/core/i18n';
import {
borderVars,
colorVars,
@@ -188,6 +188,16 @@ export function ScheduleRangeMonthTitle({
);
}
+/**
+ * A painted time or time range, isolated in the locale's direction: it lays
+ * out as one unit whatever the layout direction, so "9:00 AM" never paints as
+ * "AM 9:00" and a right-to-left locale's range still reads start first.
+ */
+export function ScheduleTime({children}: {children: ReactNode}) {
+ const {locale} = useScheduleContext();
+ return <bdi dir={getLocaleDirection(locale)}>{children}</bdi>;
+}
+
export function EventPill({
event,
day,
@@ -215,7 +225,7 @@ export function EventPill({
)}>
{timeLabel != null && (
<Text type="supporting" color="inherit" xstyle={styles.eventTime}>
- {timeLabel}
+ <ScheduleTime>{timeLabel}</ScheduleTime>
</Text>
)}
<Text
@@ -253,7 +263,7 @@ export function MonthEventPill({
)}>
{timeLabel != null && (
<Text type="supporting" color="inherit" xstyle={styles.eventTime}>
- {timeLabel}
+ <ScheduleTime>{timeLabel}</ScheduleTime>
</Text>
)}
<Text
@@ -289,9 +299,11 @@ export function ListEventRow({
)}
/>
<span {...stylex.props(styles.listEventTime)}>
- {isDayEvent(event)
- ? 'All day'
- : formatEventTimeRange(event, timezoneID, locale)}
+ <ScheduleTime>
+ {isDayEvent(event)
+ ? 'All day'
+ : formatEventTimeRange(event, timezoneID, locale)}
+ </ScheduleTime>
</span>
<span
{...stylex.props(