An open source design system that's fully customizable and agent ready
TypeScript
fix(richtext): RichTextView keeps consumer styling and reports parse errors after render (#6978)
Cindy Zhangb2214a49c554
diff --git a/packages/richtext/src/RichTextEditor.test.tsx b/packages/richtext/src/RichTextEditor.test.tsxindex b42c4e4791..485f57f3f3 100644--- a/packages/richtext/src/RichTextEditor.test.tsx+++ b/packages/richtext/src/RichTextEditor.test.tsx@@ -16,7 +16,8 @@import {describe, it, expect, vi, beforeAll, afterAll} from 'vitest';import {render, screen, waitFor, fireEvent, act} from '@testing-library/react';import userEvent from '@testing-library/user-event';-import {createRef, useEffect} from 'react';+import {createRef, StrictMode, useEffect, useState} from 'react';+import * as stylex from '@stylexjs/stylex';import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';import type {EditorState, LexicalEditor} from 'lexical';import {@@ -1000,6 +1001,99 @@ describe('RichTextView', () => {expect(content).not.toHaveAttribute('aria-autocomplete');});+ it('keeps its own StyleX class alongside a consumer className', () => {+ // A JSX attribute after an object spread wins even when it is undefined,+ // so `className={className}` after `{...stylex.props(...)}` dropped every+ // generated class, including styles.root's `width: 100%`.+ const {container} = render(+ <RichTextView value={HELLO_STATE} className="consumer-class" />,+ );+ const root = container.firstElementChild;+ expect(root).toHaveClass('consumer-class');+ expect(root?.classList.length).toBeGreaterThan(1);+ });++ it('applies a consumer xstyle', () => {+ const overrides = stylex.create({custom: {opacity: 0.5}});+ const {container: withXstyle} = render(+ <RichTextView value={HELLO_STATE} xstyle={overrides.custom} />,+ );+ const {container: plain} = render(<RichTextView value={HELLO_STATE} />);+ const withClasses = withXstyle.firstElementChild?.className ?? '';+ expect(withClasses).not.toBe('');+ expect(withClasses).not.toBe(plain.firstElementChild?.className ?? '');+ });++ it('merges className and its own styles on the error fallback too', () => {+ const {container} = render(+ <RichTextView+ value={'{ not valid json'}+ className="consumer-class"+ errorFallback={<div>Unavailable</div>}+ />,+ );+ const root = container.firstElementChild;+ expect(root).toHaveClass('consumer-class');+ expect(root?.classList.length).toBeGreaterThan(1);+ });++ it('reports a parse error after render, so the callback can update state', () => {+ // A consumer commonly surfaces the failure from its own state. Called+ // during RichTextView's render, that update would land while React is+ // rendering a different component, which React reports as an error.+ const consoleError = vi+ .spyOn(console, 'error')+ .mockImplementation(() => {});+ function Host() {+ const [failure, setFailure] = useState('');+ return (+ <>+ <RichTextView+ value={'{ not valid json'}+ onParseError={error => setFailure(error.name)}+ />+ <output>{failure}</output>+ </>+ );+ }+ try {+ const {container} = render(<Host />);+ expect(container.querySelector('output')).toHaveTextContent(+ 'SyntaxError',+ );+ expect(consoleError).not.toHaveBeenCalled();+ } finally {+ consoleError.mockRestore();+ }+ });++ it('reports a parse error once under StrictMode', () => {+ const onParseError = vi.fn();+ render(+ <StrictMode>+ <RichTextView value={'{ not valid json'} onParseError={onParseError} />+ </StrictMode>,+ );+ expect(onParseError).toHaveBeenCalledTimes(1);+ expect(onParseError.mock.calls[0][0]).toBeInstanceOf(Error);+ });++ it('reports each distinct parse failure once across re-renders', () => {+ const onParseError = vi.fn();+ const {rerender} = render(+ <RichTextView value={'{ bad'} onParseError={onParseError} />,+ );+ expect(onParseError).toHaveBeenCalledTimes(1);++ // Re-rendering with the same bad value does not report it again…+ rerender(<RichTextView value={'{ bad'} onParseError={onParseError} />);+ expect(onParseError).toHaveBeenCalledTimes(1);++ // …but a different bad value is a new failure.+ rerender(<RichTextView value={'{ worse'} onParseError={onParseError} />);+ expect(onParseError).toHaveBeenCalledTimes(2);+ });+it('renders custom read-only plugins passed via the plugins prop', () => {render(<RichTextViewdiff --git a/packages/richtext/src/RichTextView.tsx b/packages/richtext/src/RichTextView.tsxindex 6138fb476a..5f8056bd13 100644--- a/packages/richtext/src/RichTextView.tsx+++ b/packages/richtext/src/RichTextView.tsx@@ -5,7 +5,7 @@/*** @file RichTextView.tsx* @input Uses React, Lexical (lexical + @lexical/react, composed through- * LexicalExtensionComposer), design tokens+ * LexicalExtensionComposer), mergeProps from core utils, design tokens* @output Exports RichTextView component and RichTextViewProps* @position Read-only renderer for serialized Lexical editor state, exposed to* assistive technology as document content rather than a form field;@@ -21,6 +21,7 @@ import {useEffect, useRef, useState, type ReactNode} from 'react';import * as stylex from '@stylexjs/stylex';import {sharedEditorTheme} from './editorTheme';import type {BaseProps} from '@astryxdesign/core';+import {mergeProps} from '@astryxdesign/core/utils';import {LexicalExtensionComposer} from '@lexical/react/LexicalExtensionComposer';import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';@@ -210,8 +211,26 @@ export function RichTextView({lastValueRef.current = value;}+ // A parse failure is recorded during render (below, or from Lexical's+ // `onError` while the composer builds the editor) but reported to the+ // consumer only after commit. Updating this component's own state during+ // render is allowed; running a consumer's callback is not — a callback that+ // updates its own component would do so during another component's render,+ // and StrictMode or a discarded concurrent render would repeat it or run it+ // for output that never commits. This Effect synchronizes the consumer's+ // `onParseError`, reporting each distinct failure exactly once.+ const pendingErrorRef = useRef<Error | null>(null);+ const reportedErrorRef = useRef<Error | null>(null);+ useEffect(() => {+ const error = pendingErrorRef.current;+ if (error !== null && error !== reportedErrorRef.current) {+ reportedErrorRef.current = error;+ onParseError?.(error);+ }+ });+const handleError = (error: Error) => {- onParseError?.(error);+ pendingErrorRef.current = error;setHasError(true);};@@ -234,9 +253,7 @@ export function RichTextView({extensionRef.current = null;return (<div- {...stylex.props(styles.root, xstyle)}- className={className}- style={style}+ {...mergeProps(stylex.props(styles.root, xstyle), className, style)}{...rest}>{errorFallback}</div>@@ -259,9 +276,7 @@ export function RichTextView({return (<div- {...stylex.props(styles.root, xstyle)}- className={className}- style={style}+ {...mergeProps(stylex.props(styles.root, xstyle), className, style)}{...rest}><LexicalExtensionComposerextension={extensionRef.current}