Skip to content

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

TypeScript

fix(richtext): keep the block-format selector readable when the toolbar runs out of room (#7217)

Cindy Zhangaea7a8dc61c7
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 5df6ee7f38..a7acba54f8 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1228,7 +1228,7 @@ jobs:
# with the base. If the base cannot be fetched, run the evidence.
if git fetch --quiet --depth=1 origin "$BASE_SHA"; then
if git diff --name-only "$BASE_SHA" HEAD \
- | grep -qE '^(apps/storybook/stories/RichTextEditor\.(markdownParity|stories)|packages/richtext/src/__tests__/(MarkdownParitySandbox|RichTextView))'; then
+ | grep -qE '^(apps/storybook/stories/RichTextEditor\.(markdownParity|stories)|packages/richtext/src/__tests__/(MarkdownParitySandbox|RichTextView|RichTextEditorToolbar))'; then
should_run=true
fi
else
@@ -1240,11 +1240,12 @@ jobs:
echo "Markdown, RichText, and the parity sandbox are not in PR scope; skipping parity evidence."
exit 0
fi
- # The RichTextView typography spec shares this scope, so it rides the
- # same gate and output directory.
+ # The RichTextView typography and RichText toolbar layout specs share
+ # this scope, so they ride the same gate and output directory.
pnpm exec playwright test \
packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts \
packages/richtext/src/__tests__/RichTextView.a11y.chromium.spec.ts \
+ packages/richtext/src/__tests__/RichTextEditorToolbar.a11y.chromium.spec.ts \
--project chromium \
--output richtext-markdown-parity-output
diff --git a/packages/richtext/src/RichTextEditorToolbar.tsx b/packages/richtext/src/RichTextEditorToolbar.tsx
index cb4fb3a193..9635696951 100644
--- a/packages/richtext/src/RichTextEditorToolbar.tsx
+++ b/packages/richtext/src/RichTextEditorToolbar.tsx
@@ -146,6 +146,13 @@ const glyphStyles = stylex.create({
});
const toolbarScrollStyles = stylex.create({
+ // The row scrolls when it runs out of room, so a control keeps its own
+ // width. A Selector's field may shrink, which squeezes its label to a
+ // sliver, so it sits in a slot that may not.
+ fixedSlot: {
+ display: 'flex',
+ flexShrink: 0,
+ },
actions: {
flex: '1 1 0%',
width: 0,
@@ -907,17 +914,19 @@ export function RichTextEditorToolbar({
aria-label={t('@astryx.richTextEditor.toolbar.historyDivider')}
xstyle={toolbarDividerStyles.vertical}
/>
- <Selector
- label={t('@astryx.richTextEditor.toolbar.blockFormat')}
- isLabelHidden
- variant="ghost"
- size={size}
- value={blockType}
- options={blockOptions}
- startIcon={icon(blockType)}
- isDisabled={!isEditable}
- onChange={value => setBlock(value as BlockType)}
- />
+ <div {...stylex.props(toolbarScrollStyles.fixedSlot)}>
+ <Selector
+ label={t('@astryx.richTextEditor.toolbar.blockFormat')}
+ isLabelHidden
+ variant="ghost"
+ size={size}
+ value={blockType}
+ options={blockOptions}
+ startIcon={icon(blockType)}
+ isDisabled={!isEditable}
+ onChange={value => setBlock(value as BlockType)}
+ />
+ </div>
<Divider
orientation="vertical"
aria-label={t('@astryx.richTextEditor.toolbar.formatDivider')}
diff --git a/packages/richtext/src/__tests__/RichTextEditorToolbar.a11y.chromium.spec.ts b/packages/richtext/src/__tests__/RichTextEditorToolbar.a11y.chromium.spec.ts
new file mode 100644
index 0000000000..4c47c915fc
--- /dev/null
+++ b/packages/richtext/src/__tests__/RichTextEditorToolbar.a11y.chromium.spec.ts
@@ -0,0 +1,112 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file RichTextEditorToolbar.a11y.chromium.spec.ts
+ * @input The built Storybook's "With toolbar" RichTextEditor story.
+ * @output Real-browser proof that the toolbar's action row scrolls at phone
+ * widths instead of squeezing a control: the block-format selector keeps its
+ * whole label, no control overlaps its neighbour, and every control keeps a
+ * 24px target.
+ *
+ * Run after building Storybook:
+ * pnpm storybook:build
+ * pnpm exec playwright test packages/richtext --project chromium
+ */
+
+import AxeBuilder from '@axe-core/playwright';
+import {expect, test} from '@playwright/test';
+import {
+ DEFAULT_STORYBOOK_DIR,
+ serveStorybook,
+ type StaticServer,
+} from '@astryxdesign/a11y-spec/storybook';
+
+const STORYBOOK_DIR = process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR;
+const STORY = 'lab-richtexteditor--with-toolbar';
+
+let storybook: StaticServer;
+
+test.beforeAll(async () => {
+ storybook = await serveStorybook(STORYBOOK_DIR);
+});
+
+test.afterAll(async () => {
+ await storybook?.close();
+});
+
+for (const width of [320, 390, 1240]) {
+ for (const direction of ['ltr', 'rtl'] as const) {
+ test(`the action row scrolls instead of squeezing a control (${width}px, ${direction})`, async ({
+ page,
+ }) => {
+ await page.setViewportSize({width, height: 700});
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=${STORY}&viewMode=story&globals=astryxTheme:neutral;colorMode:light;direction:${direction}`,
+ {waitUntil: 'load'},
+ );
+ const toolbar = page.getByRole('toolbar', {name: 'Text formatting'});
+ await expect(toolbar).toBeVisible();
+
+ const layout = await toolbar.evaluate(root => {
+ const row = root.querySelector('[role="group"]') as HTMLElement;
+ const combobox = root.querySelector('[role="combobox"]') as HTMLElement;
+ const label = [...combobox.querySelectorAll('span')].find(
+ span => span.textContent === 'Paragraph',
+ );
+ const controls = [...row.children]
+ .filter(child => child.getAttribute('role') !== 'separator')
+ .map(child => child.getBoundingClientRect())
+ .filter(box => box.width > 0)
+ .sort((a, b) => a.left - b.left);
+ let overlap = 0;
+ for (let index = 1; index < controls.length; index++) {
+ overlap = Math.max(
+ overlap,
+ controls[index - 1].right - controls[index].left,
+ );
+ }
+ return {
+ labelWidth: label?.getBoundingClientRect().width ?? 0,
+ labelNeeds: label?.scrollWidth ?? -1,
+ overlap,
+ rowOverflow: row.scrollWidth - row.clientWidth,
+ rowRoom: row.clientWidth,
+ pageOverflow:
+ document.documentElement.scrollWidth -
+ document.documentElement.clientWidth,
+ shortest: Math.min(...controls.map(box => box.height)),
+ };
+ });
+
+ expect(layout.labelNeeds, 'label found').toBeGreaterThan(0);
+ expect(
+ layout.labelWidth,
+ 'the selector shows its whole label',
+ ).toBeGreaterThanOrEqual(layout.labelNeeds - 0.5);
+ expect(
+ layout.overlap,
+ 'no control overlaps the next',
+ ).toBeLessThanOrEqual(0.5);
+ expect(
+ layout.shortest,
+ 'every control is a 24px target',
+ ).toBeGreaterThanOrEqual(24);
+ expect(
+ layout.pageOverflow,
+ 'the page never scrolls sideways',
+ ).toBeLessThanOrEqual(0);
+ if (width < 400) {
+ expect(
+ layout.rowOverflow,
+ 'at phone width the row scrolls',
+ ).toBeGreaterThan(0);
+ }
+
+ const targets = await new AxeBuilder({page})
+ .include('[role="toolbar"]')
+ .withRules(['target-size'])
+ .analyze();
+ expect(targets.violations).toEqual([]);
+ });
+ }
+}