An open source design system that's fully customizable and agent ready
TypeScript
fix(richtext): RichTextView draws every block in the theme's body type (#7220)
Cindy Zhanga073f05e7374
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 4adf63fb3b..5df6ee7f38 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"; thenif git diff --name-only "$BASE_SHA" HEAD \- | grep -qE '^(apps/storybook/stories/RichTextEditor\.(markdownParity|stories)|packages/richtext/src/__tests__/MarkdownParitySandbox)'; then+ | grep -qE '^(apps/storybook/stories/RichTextEditor\.(markdownParity|stories)|packages/richtext/src/__tests__/(MarkdownParitySandbox|RichTextView))'; thenshould_run=truefielse@@ -1240,8 +1240,11 @@ jobs:echo "Markdown, RichText, and the parity sandbox are not in PR scope; skipping parity evidence."exit 0fi+ # The RichTextView typography spec shares this scope, so it rides 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 \--project chromium \--output richtext-markdown-parity-outputdiff --git a/packages/richtext/src/RichTextView.tsx b/packages/richtext/src/RichTextView.tsxindex a78d995061..89f1196d42 100644--- a/packages/richtext/src/RichTextView.tsx+++ b/packages/richtext/src/RichTextView.tsx@@ -23,6 +23,11 @@ import * as stylex from '@stylexjs/stylex';import {sharedEditorTheme} from './editorTheme';import type {BaseProps} from '@astryxdesign/core';import {mergeProps} from '@astryxdesign/core/utils';+import {+ colorVars,+ typeScaleVars,+ typographyVars,+} from '@astryxdesign/core/theme/tokens.stylex';import {LexicalExtensionComposer} from '@lexical/react/LexicalExtensionComposer';import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';@@ -54,6 +59,13 @@ const styles = stylex.create({// Holds the code block headers and task checkboxes drawn over the// content.position: 'relative',+ // The document's body text, as core Markdown and the editor set it+ // (spec:AST-061 FR2). Without it, blocks the theme leaves unsized —+ // list items, quotes, table cells — take the host page's font.+ fontFamily: typographyVars['--font-family-body'],+ fontSize: typeScaleVars['--text-body-size'],+ lineHeight: typeScaleVars['--text-body-leading'],+ color: colorVars['--color-text-primary'],},});diff --git a/packages/richtext/src/__tests__/RichTextView.a11y.chromium.spec.ts b/packages/richtext/src/__tests__/RichTextView.a11y.chromium.spec.tsnew file mode 100644index 0000000000..8502c53936--- /dev/null+++ b/packages/richtext/src/__tests__/RichTextView.a11y.chromium.spec.ts@@ -0,0 +1,100 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file RichTextView.a11y.chromium.spec.ts+ * @input The built Storybook's "Markdown serializers" story, which renders one+ * document in a RichTextEditor and in a RichTextView.+ * @output Real-browser proof for spec:AST-061 FR2 on the view: every block's+ * text takes the theme's body type, not the host page's font, so the view and+ * the editor draw the same document the same size.+ *+ * Run after building Storybook:+ * pnpm storybook:build+ * pnpm exec playwright test packages/richtext --project chromium+ */++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--markdown-serializers';+const DOCUMENT = [+ 'Paragraph text',+ '',+ '- List item',+ ' - Nested item',+ '',+ '> Quoted text',+ '',+ '| Cell | Other |',+ '| --- | --- |',+ '| Body | Cell |',+ '',+].join('\n');++let storybook: StaticServer;++test.beforeAll(async () => {+ storybook = await serveStorybook(STORYBOOK_DIR);+});++test.afterAll(async () => {+ await storybook?.close();+});++for (const colorMode of ['light', 'dark'] as const) {+ test(`the view draws every block in the body type the editor uses (${colorMode})`, async ({+ page,+ }) => {+ // The story host's own font is 16px/24px, so inheriting it shows.+ await page.setViewportSize({width: 1240, height: 1000});+ await page.goto(+ `${storybook.origin}/iframe.html?id=${STORY}&viewMode=story&globals=astryxTheme:neutral;colorMode:${colorMode};direction:ltr`,+ {waitUntil: 'load'},+ );+ await page.locator('textarea').fill(DOCUMENT);+ const surfaces = page.locator('[data-lexical-editor]');+ await expect(surfaces).toHaveCount(2);+ await expect(surfaces.nth(1).locator('td, th')).toHaveCount(4);++ const read = (index: number) =>+ surfaces.nth(index).evaluate(root => {+ const textOf = (selector: string) => {+ const block = root.querySelector(selector);+ // The innermost element holding the block's words.+ let leaf: Element | null = block;+ while (leaf?.firstElementChild != null) {+ leaf = leaf.firstElementChild;+ }+ if (leaf == null) {+ return null;+ }+ const style = getComputedStyle(leaf);+ return `${style.fontFamily} ${style.fontSize}/${style.lineHeight} | ${style.color}`;+ };+ return {+ paragraph: textOf('p'),+ listItem: textOf('li'),+ quote: textOf('blockquote'),+ cell: textOf('td'),+ };+ });++ const editor = await read(0);+ const view = await read(1);+ expect(editor.paragraph).not.toBeNull();+ // Every block in the view matches the same block in the editor.+ expect(view).toEqual(editor);+ // And it is the body type, not the page's: every block shares the+ // paragraph's family, size, and leading. (A quote's color differs by+ // design.)+ const typeOf = (block: string | null) => block?.split(' | ')[0];+ for (const block of [view.listItem, view.quote, view.cell]) {+ expect(typeOf(block)).toBe(typeOf(view.paragraph));+ }+ });+}