Skip to content

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

TypeScript

fix(richtext): size a table's scroll wrapper from its container (#6994)

Cindy Zhangb599105a48fd
diff --git a/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts b/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts
index c95ff52326..3e98c837d5 100644
--- a/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts
+++ b/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts
@@ -259,6 +259,164 @@ const MARKDOWN = '[data-parity-surface="markdown"]';
const RICH_TEXT = '[data-parity-surface="richtext"]';
const TOGGLE = '[data-parity-surface="toggle"]';
+// A table must stay inside its own wrapper in the editor and the view, however
+// the host lays them out. Grid and flex items default to their content's
+// min-content width, so a wrapper that reports its table's width widens the
+// page; a shrink-to-fit host (a chat bubble, an inline-block) must still give
+// a small table its natural width. The Markdown Serializers story renders the
+// editor and the view; each case restyles the story's layout into one host.
+// `surfaces` names which of the story's two surfaces stay in the host: a flex
+// row holds one at a time, because the editor's toolbar has its own minimum
+// width that has nothing to do with tables.
+const TABLE_HOSTS = {
+ grid: {
+ layout: 'display:grid;gap:24px;max-width:720px',
+ view: '',
+ surfaces: 'both',
+ },
+ 'flex row with the editor': {
+ layout: 'display:flex;flex-direction:row;max-width:720px',
+ view: '',
+ surfaces: 'editor',
+ },
+ 'flex row with the view': {
+ layout: 'display:flex;flex-direction:row;max-width:720px',
+ view: '',
+ surfaces: 'view',
+ },
+ 'fit-content bubble': {
+ layout: 'display:block;max-width:720px',
+ view: 'width:fit-content;max-width:100%',
+ surfaces: 'both',
+ },
+ 'inline-block': {
+ layout: 'display:block;max-width:720px',
+ view: 'display:inline-block;width:auto;max-width:100%;vertical-align:top',
+ surfaces: 'both',
+ },
+} as const;
+
+const tableSource = (columnCount: number) => {
+ const columns = Array.from(
+ {length: columnCount},
+ (_, index) => `Column ${index + 1}`,
+ );
+ return [
+ `| ${columns.join(' | ')} |`,
+ `| ${columns.map(() => '---').join(' | ')} |`,
+ `| ${columns.map((_, index) => `value-${index + 1}`).join(' | ')} |`,
+ ].join('\n');
+};
+
+for (const viewport of [PHONE, {width: 1280, height: 900}] as const) {
+ for (const globals of [
+ 'colorMode:light;direction:ltr',
+ 'colorMode:dark;direction:rtl',
+ ]) {
+ test(`tables stay inside their wrappers in grid, flex, and shrink-to-fit hosts (${viewport.width}px, ${globals})`, async ({
+ page,
+ }) => {
+ await page.setViewportSize(viewport);
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=lab-richtexteditor--markdown-serializers&viewMode=story&globals=astryxTheme:neutral;${globals}`,
+ {waitUntil: 'load'},
+ );
+ for (const columnCount of [2, 20]) {
+ // Show every section so the story's textarea can take the table.
+ await page.evaluate(() => {
+ const layout = document.querySelector('[data-table-host-layout]');
+ for (const section of Array.from(layout?.children ?? [])) {
+ section.setAttribute('style', '');
+ }
+ });
+ await page.locator('textarea').fill(tableSource(columnCount));
+ await expect(page.locator('table:visible')).toHaveCount(2);
+ for (const [host, styles] of Object.entries(TABLE_HOSTS)) {
+ await page.evaluate(
+ ({layout: layoutStyle, view: viewStyle, surfaces}) => {
+ // The story lays its sections out in a grid; mark it once so later
+ // cases find it after its style changes.
+ let layout = document.querySelector<HTMLElement>(
+ '[data-table-host-layout]',
+ );
+ if (layout == null) {
+ layout =
+ document
+ .querySelector('textarea')
+ ?.closest<HTMLElement>('div[style*="grid"]') ?? null;
+ layout?.setAttribute('data-table-host-layout', '');
+ }
+ if (layout == null) {
+ throw new Error('Story layout not found');
+ }
+ layout.setAttribute('style', layoutStyle);
+ // Only the surfaces this host holds stay in it.
+ for (const section of Array.from(layout.children)) {
+ const table = section.querySelector('table');
+ const isView =
+ table?.closest('[contenteditable="false"]') != null;
+ const keep =
+ table != null &&
+ (surfaces === 'both' || (surfaces === 'view') === isView);
+ section.setAttribute('style', keep ? '' : 'display:none');
+ }
+ // The view itself is the shrink-to-fit box, as in a chat bubble.
+ const viewRoot = document
+ .querySelector('[contenteditable="false"] table')
+ ?.closest('[contenteditable]')?.parentElement;
+ viewRoot?.setAttribute('style', viewStyle);
+ },
+ styles,
+ );
+ await settle(page);
+ const layout = await page.evaluate(() => ({
+ pageOverflow:
+ document.documentElement.scrollWidth - window.innerWidth,
+ wrappers: [...document.querySelectorAll('table')]
+ .filter(element => element.getBoundingClientRect().width > 0)
+ .map(element => {
+ const wrapper = element.parentElement as HTMLElement;
+ return {
+ inView: element.closest('[contenteditable="false"]') != null,
+ clientWidth: wrapper.clientWidth,
+ scrollWidth: wrapper.scrollWidth,
+ };
+ }),
+ }));
+ const label = `${columnCount} columns, ${host} host`;
+ expect(layout.pageOverflow, label).toBeLessThanOrEqual(0);
+ expect(layout.wrappers, label).toHaveLength(
+ styles.surfaces === 'both' ? 2 : 1,
+ );
+ for (const wrapper of layout.wrappers) {
+ expect(wrapper.clientWidth, label).toBeGreaterThan(0);
+ expect(wrapper.clientWidth, label).toBeLessThanOrEqual(
+ viewport.width,
+ );
+ if (columnCount === 20 && viewport.width === PHONE.width) {
+ // A table wider than the phone scrolls inside its wrapper.
+ expect(wrapper.scrollWidth, label).toBeGreaterThan(
+ wrapper.clientWidth,
+ );
+ } else if (columnCount === 2) {
+ // A small table fits without scrolling.
+ expect(wrapper.scrollWidth, label).toBeLessThanOrEqual(
+ wrapper.clientWidth + 1,
+ );
+ }
+ }
+ if (columnCount === 2 && styles.view !== '') {
+ // A shrink-to-fit host gives the view's small table its natural
+ // width instead of stretching or collapsing it.
+ const view = layout.wrappers.find(wrapper => wrapper.inView);
+ expect(view?.clientWidth, label).toBeLessThan(viewport.width / 2);
+ }
+ }
+ }
+ });
+ }
+}
+
test('side by side: one fixture renders on both surfaces', async ({page}) => {
const errors = await openStory(page, STORY.sideBySide, DESKTOP);
await waitForDocument(page, MARKDOWN);
diff --git a/packages/richtext/src/editorTheme.ts b/packages/richtext/src/editorTheme.ts
index dabcb02299..7fc313bf6f 100644
--- a/packages/richtext/src/editorTheme.ts
+++ b/packages/richtext/src/editorTheme.ts
@@ -126,6 +126,12 @@ const editorTheme = stylex.create({
tableScrollableWrapper: {
overflowX: 'auto',
maxWidth: '100%',
+ // One grid track that may shrink to nothing: the wrapper asks its host
+ // for no minimum width, so a grid or flex host never grows to fit a wide
+ // table and the wrapper scrolls instead, while a shrink-to-fit host still
+ // sizes to the table's natural width.
+ display: 'grid',
+ gridTemplateColumns: 'minmax(0, 1fr)',
},
table: {
borderCollapse: 'collapse',
diff --git a/packages/richtext/src/markdownTable.test.tsx b/packages/richtext/src/markdownTable.test.tsx
index c921c843a1..b347951a88 100644
--- a/packages/richtext/src/markdownTable.test.tsx
+++ b/packages/richtext/src/markdownTable.test.tsx
@@ -174,6 +174,24 @@ describe('GFM tables', () => {
});
});
+ it('sizes the scroll wrapper from its container, not from the table', async () => {
+ const {container} = render(
+ <RichTextEditor
+ label="Notes"
+ defaultValue={markdownToEditorStateJSON(
+ '| Name | Role |\n| --- | --- |\n| Ada | Engineer |',
+ )}
+ />,
+ );
+ await waitFor(() => {
+ const wrapper = container.querySelector('table')?.parentElement;
+ expect(wrapper?.getAttribute('contenteditable')).toBeNull();
+ // jsdom keeps `display` but not grid track sizes; the browser test
+ // checks what the track does.
+ expect(wrapper).toHaveStyle({display: 'grid'});
+ });
+ });
+
it('renders a table from the editor in RichTextView, inside a scroll wrapper', async () => {
const {container} = render(
<RichTextView