An open source design system that's fully customizable and agent ready
TypeScript
docs(richtext): in-place editing keeps the reading width, so the switch does not reflow (#10429)
Cindy Zhang1ff81c7a90b7
diff --git a/.changeset/richtext-in-place-width.md b/.changeset/richtext-in-place-width.mdnew file mode 100644index 0000000000..eab20bda74--- /dev/null+++ b/.changeset/richtext-in-place-width.md@@ -0,0 +1,7 @@+---+'@astryxdesign/richtext': patch+---++[docs] RichTextEditor documents how to switch between reading and editing in place without reflow: set the field's inset and border outside the reading width with `xstyle`, so every paragraph keeps its line count across the switch. A standalone editor keeps its form width.++@cixzhangdiff --git a/apps/storybook/stories/RichTextEditor.markdownParity.tsx b/apps/storybook/stories/RichTextEditor.markdownParity.tsxindex 5a4ac986e0..7518a88c80 100644--- a/apps/storybook/stories/RichTextEditor.markdownParity.tsx+++ b/apps/storybook/stories/RichTextEditor.markdownParity.tsx@@ -39,6 +39,7 @@ import {Text} from '@astryxdesign/core/Text';import {ToggleButton} from '@astryxdesign/core/ToggleButton';import {defineTheme, Theme, useTheme} from '@astryxdesign/core/theme';import {+ borderVars,colorVars,spacingVars,typeScaleVars,@@ -384,11 +385,29 @@ function MarkdownRead({content}: {content: ParityContent}) {);}-function RichTextEdit({content}: {content: ParityContent}) {+/**+ * The in-place editing frame the RichTextEditor docs describe: the field's+ * inset and border sit outside the reading width, so the document wraps at the+ * same width in read and edit mode (spec:AST-061 FR9).+ */+const inPlaceStyles = stylex.create({+ editorFrame: {+ marginInline: `calc(-1 * (${spacingVars['--spacing-2']} + ${borderVars['--border-width']}))`,+ },+});++function RichTextEdit({+ content,+ isInPlace = false,+}: {+ content: ParityContent;+ isInPlace?: boolean;+}) {return (<RichTextEditorlabel="Document"isLabelHidden+ xstyle={isInPlace ? inPlaceStyles.editorFrame : undefined}defaultValue={content.editorState}toolbar={<RichTextEditorToolbar />}plugins={@@ -403,7 +422,8 @@ interface SurfaceDefinition {readonly label: string;/** The element whose children are the document's top-level blocks. */readonly contentRoot: (body: HTMLElement) => Element | null;- readonly render: (content: ParityContent) => ReactNode;+ /** `isInPlace` is set when the surface replaces the other one in place. */+ readonly render: (content: ParityContent, isInPlace?: boolean) => ReactNode;}/**@@ -420,7 +440,9 @@ const SURFACES = {richtext: {label: 'RichText, edit mode',contentRoot: body => body.querySelector('[contenteditable]'),- render: content => <RichTextEdit content={content} />,+ render: (content: ParityContent, isInPlace?: boolean) => (+ <RichTextEdit content={content} isInPlace={isInPlace} />+ ),},} satisfies Record<string, SurfaceDefinition>;@@ -754,7 +776,7 @@ function ToggleView({label={SURFACES[surface].label}bodyRef={bodyRef}isHosted={hostWidth === '680px'}>- {SURFACES[surface].render(content)}+ {SURFACES[surface].render(content, true)}</SurfaceSection><Measurementsleft={{label: 'Read (last measured)', geometry: read}}@@ -863,12 +885,6 @@ const WIDE_MARKER_SOURCE = [].join('\n');const wideMarkerStyles = stylex.create({- editorFrame: {- // The field's 8px inset and 1px border offset its document. Give the frame- // that space outside the common measure, rather than testing unequal widths.- width: `calc(100% + 2 * (${spacingVars['--spacing-2']} + 1px))`,- marginInline: `calc(-1 * (${spacingVars['--spacing-2']} + 1px))`,- },columns: {display: 'grid',gridTemplateColumns: 'repeat(auto-fit, minmax(min(100%, 320px), 1fr))',@@ -942,7 +958,7 @@ function NumberMarkerParity({source}: {source: string}) {label="Wide counters"isLabelHiddendefaultValue={state}- xstyle={wideMarkerStyles.editorFrame}+ xstyle={inPlaceStyles.editorFrame}/></div></div>diff --git a/docs/specs/AST-061-document-surface-parity/spec.md b/docs/specs/AST-061-document-surface-parity/spec.mdindex 0a894551f6..6806341598 100644--- a/docs/specs/AST-061-document-surface-parity/spec.md+++ b/docs/specs/AST-061-document-surface-parity/spec.md@@ -186,11 +186,10 @@ RichText adopts DEC-8: its package entry stops exporting `sharedEditorTheme`,gains `BaseProps` and a `ref` and renames `linkOpensInNewTab` to `hasNewTabLinks`; and itsLexical peer ranges move to the Lexical 0.52 minor.`Markdown` renders fenced code unwrapped, scrolling sideways, and adopts the-FR5 wrap; RichText already wraps fenced code. RichText's editing surface is-18px narrower than the reading surface at every width, the field's inset, so a-line that wraps close to the available width can take one more line in edit-mode than in read mode, against FR9; at phone width a long fenced code line-does.+FR5 wrap; RichText already wraps fenced code. A product that switches in+place keeps the editor's text as wide as the reading surface by setting the+field's inset and border outside it, as the RichTextEditor docs show; a+standalone editor keeps its form width.`Markdown`, through `component:CodeBlock`, and the RichText surfaces floatCopy over the code of a fence that names no language; both adopt the FR8corner cluster that `component:CodeBlock` FR11 places.diff --git a/packages/richtext/src/RichTextEditor.doc.mjs b/packages/richtext/src/RichTextEditor.doc.mjsindex f4faafd4f0..39c68ccd6d 100644--- a/packages/richtext/src/RichTextEditor.doc.mjs+++ b/packages/richtext/src/RichTextEditor.doc.mjs@@ -288,6 +288,11 @@ export const docs = {description:"The toolbar's glyphs are themeable. Each control resolves its icon from the core icon registry under a stable richtext:* key (see RICHTEXT_ICON_KEYS), falling back to a bundled inline SVG. A theme restyles any glyph without forking the toolbar by naming its key: defineTheme({icons: {'richtext:bold': <MyBoldIcon />}}). Undo and redo mirror under right-to-left direction, whichever glyph draws them.",},+ {+ guidance: true,+ description:+ "To switch between reading (Markdown or RichTextView) and editing the same document in place, give the editor's field its inset and border outside the reading width, so every paragraph keeps its line count across the switch. Define the frame with stylex.create, for example const inPlace = stylex.create({editorFrame: {marginInline: `calc(-1 * (${spacingVars['--spacing-2']} + ${borderVars['--border-width']}))`}}), with spacingVars and borderVars from @astryxdesign/core/theme/tokens.stylex, and pass xstyle={inPlace.editorFrame} to the RichTextEditor. The recipe assumes no status: a status icon reserves another spacing-6 at the inline end. The field then extends past the column on both sides; leave room for it in the surrounding padding. A standalone editor in a form keeps its default width, so its border lines up with the fields around it.",+ },{guidance: false,description:diff --git a/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts b/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.tsindex d51b13c685..91c33ec211 100644--- a/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts+++ b/packages/richtext/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts@@ -1852,13 +1852,37 @@ for (const viewport of [DESKTOP, PHONE]) {.locator('[data-parity-anchor]').evaluate(element => ({...(element as HTMLElement).dataset}));expect(await axeViolations(page)).toEqual([]);+ const editGeometry = await surfaceGeometry(page, edit);+ // spec:AST-061 FR9: edit mode offsets the whole document by one constant+ // amount, and every block keeps its top and height within 2px after it.+ // A paragraph that gained or lost a line changes height by a line.+ const readBlocks = readGeometry.blocks.filter(block => block.copy === 1);+ const editBlocks = new Map(+ editGeometry.blocks+ .filter(block => block.copy === 1)+ .map(block => [block.key, block]),+ );+ const pairs = readBlocks.flatMap(block => {+ const edited = editBlocks.get(block.key);+ return edited == null ? [] : [{block, edited}];+ });+ expect(pairs.length).toBeGreaterThan(0);+ const offset = pairs[0].edited.top - pairs[0].block.top;+ for (const {block, edited} of pairs) {+ expect+ .soft(Math.abs(edited.top - block.top - offset), `${block.key} top`)+ .toBeLessThanOrEqual(2);+ expect+ .soft(Math.abs(edited.height - block.height), `${block.key} height`)+ .toBeLessThanOrEqual(2);+ }await capture(page, `${name}--edit`, {story: STORY.toggle,viewport,globals: `neutral ${colorMode} ltr`,mode: 'edit',anchor,- geometry: await surfaceGeometry(page, edit),+ geometry: editGeometry,});// Back to read with the keyboard: the control is reachable without a pointer.