Repository navigation
feat(lab): add imperative ref API to RichTextEditor - #4417
Conversation
|
Deployment failed with the following error: View Documentation: https://vercel.com/docs/accounts/team-members-and-roles |
PR Analysis ReportNo new or modified components detected. Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | View full report |
nynexman4464
left a comment
There was a problem hiding this comment.
Approve with nits. forwardRef correct, handle memoized on [editor], type exported from component index + lab barrel. Good coverage + story.
- Resolve or acknowledge before landing:
clear()calls$getRoot().clear(), leaving zero children, but docs say "single empty paragraph." Use$getRoot().clear(); $getRoot().append($createParagraphNode())+ assertgetChildrenSize() === 1. clear()/focus()aren't gated onisReadOnly/isDisabled.- No test asserts
clear()firesonChange.
— Reviewed with Navi on behalf of Alex Rock
|
Thanks for the review! All three nits addressed in 05f3e57:
Also synced the JSDoc + |
0c69f8b to
7a2ffc0
Compare
Exposes a `RichTextEditorRef` via `forwardRef` with focus(), clear(), getEditorState(), and getEditor() — so callers can drive the editor without wiring a custom plugin. The Lexical editor instance only exists inside the LexicalComposer, so the handle is wired by a small internal EditorRefBridge plugin that reads the editor via useLexicalComposerContext() and connects it to the outer ref with useImperativeHandle (same pattern as the existing AutoFocusOnMount plugin). - New RichTextEditorRef type, exported from index.ts + lab barrel - Updated JSDoc example + doc.mjs best-practice note - Added 6 unit tests (handle shape, focus, getEditorState, getEditor, clear)
Interactive story demonstrating the new ref handle — buttons wired to focus(), clear(), getEditorState() (prints text content), and getEditor(). Gives reviewers a live demo of the imperative API.
- clear() now resets to a single empty paragraph (root.clear() + append $createParagraphNode()) instead of leaving a childless root, matching the documented single empty paragraph behaviour - focus() and clear() are gated on editability — no-op when isReadOnly or isDisabled, so the imperative handle cannot mutate/focus a locked editor - add tests: clear() leaves getChildrenSize() === 1, clear() fires onChange, clear()/focus() are no-ops when read-only/disabled - sync JSDoc + doc.mjs with the read-only gating and single-paragraph reset
The @astryxdesign `source` resolve rule cascades into how those packages resolve their own imports, so @astryxdesign/lab resolved `import 'lexical'` with the `source` condition — pulling lexical's raw `src/*.ts` (which Next's Babel can't compile: `declare` class fields), failing the build-sandbox job. Add a higher-precedence webpack rule that forces lexical + @lexical/* to their default (built dist) conditions regardless of importer. Add next.test.mjs regression test asserting the rule shape, precedence, and non-overlap with the @astryxdesign source rule.
7a2ffc0 to
f3fe8af
Compare
The previous approach (a webpack module.rule keyed on the lexical resource) did NOT work: a rule's resolve.conditionNames govern how the MATCHED module resolves its own imports, so when @astryxdesign/lab imports 'lexical' the resolution runs in lab's context (source condition) and picks lexical's raw-TS source export — the rule keyed on the lexical resource never gets a chance to override it. Verified in Next's bundled webpack: lexical still resolved to src/*.ts. Restore the global 'source' condition (so @astryxdesign builds from source as on main) and pin lexical + every @lexical/* package — including every exports subpath — to its prebuilt dist via exact-match resolve.alias entries, resolved through Node's own resolver (ignores the webpack-only 'source' convention and honors exports-map renames like ReactProviderExtension -> LexicalReactProvider Extension). Aliases win over condition resolution regardless of issuer. Verified end-to-end: RichTextEditor's full lexical import graph (56 modules) resolves entirely to dist with no build errors. Rewrote next.test.mjs (7 tests).
|
Update on the My first attempt (a webpack Actual fix: keep the global Verified end-to-end in Next's actual bundled webpack: the RichTextEditor's full lexical import graph (56 modules) resolves entirely to dist with no build errors. Added a 7-test |
The imperative clear() used $getRoot/$createParagraphNode imported from the top-level 'lexical' package. In the sandbox's Next build a top-level 'lexical' value import forces Babel to transpile lexical's raw src/*.ts (which uses 'declare' class fields) and fails build-sandbox. Same class of issue fixed for the char counter in the sibling maxLength PR. Reset via editor.setEditorState(editor.parseEditorState(EMPTY_STATE)) instead — both are editor-instance methods, so no top-level 'lexical' value import is needed. Still resets to a single empty paragraph and notifies update listeners (onChange fires). Only the erased 'import type' from 'lexical' remains. Reverts the earlier withAstryx/next.js changes (and their test + changeset): that build-config approach was wrong — the sibling PR proves the scoped-rule next.js on main builds lexical fine once the component avoids the bare-lexical value import. next.js is restored to the baseline.
|
Correction + real fix (commit fb041dd): My earlier Root cause: the imperative Fix: Verified: built |
Adds `getMarkdown()` to the RichTextEditorRef handle. Serializes the current content to a Markdown string via $convertToMarkdownString, run inside a read context. Honors the same `transformers` the editor is configured with (the prop added in meta#4416), so custom transformers round-trip to Markdown — mirroring the Lexical playground, where one transformer array feeds shortcut typing, import, and export. The bridge now receives the memoized transformers array. `@lexical/markdown` is a subpackage (built dist), so this adds no top-level `lexical` value import — keeping the sandbox build safe. - Extended RichTextEditorRef + EditorRefBridge - Updated doc.mjs ref best-practice note - Added getMarkdown() button to the ImperativeRef storybook story - Added 3 unit tests (plain text, heading with default transformers, and custom empty-transformers set proving the prop is honored) Depends on the merged ref API (meta#4417) and transformers prop (meta#4416).
* feat(lab): add getMarkdown() to RichTextEditor imperative ref Adds `getMarkdown()` to the RichTextEditorRef handle. Serializes the current content to a Markdown string via $convertToMarkdownString, run inside a read context. Honors the same `transformers` the editor is configured with (the prop added in #4416), so custom transformers round-trip to Markdown — mirroring the Lexical playground, where one transformer array feeds shortcut typing, import, and export. The bridge now receives the memoized transformers array. `@lexical/markdown` is a subpackage (built dist), so this adds no top-level `lexical` value import — keeping the sandbox build safe. - Extended RichTextEditorRef + EditorRefBridge - Updated doc.mjs ref best-practice note - Added getMarkdown() button to the ImperativeRef storybook story - Added 3 unit tests (plain text, heading with default transformers, and custom empty-transformers set proving the prop is honored) Depends on the merged ref API (#4417) and transformers prop (#4416). * chore: re-trigger CI/Vercel deployment
What
Adds an imperative
refAPI to the experimentalRichTextEditor(@astryxdesign/lab) via a newRichTextEditorRefhandle:focus()— move focus into the editable surfaceclear()— empty the content (reset to a single empty paragraph)getEditorState()— read the currentEditorStategetEditor()— access the underlyingLexicalEditorfor advanced useWhy
Today the only way to imperatively drive the editor (focus it, clear it, read its state, reach the Lexical instance) is to write a custom plugin that calls
useLexicalComposerContext()and pass it via thepluginsslot. That's boilerplate for common operations. This exposes a first-classref, matching the ergonomics consumers expect from other Astryx inputs.One of the general-purpose gaps identified for EPS
RichTextArea→ AstryxRichTextEditorparity (EPS shipsfocus/clear/getEditorState/getEditoron its ref). Low-risk and additive.How
The
LexicalEditorinstance only exists inside theLexicalComposer, so the handle can't be wired from the outer component directly. A small internalEditorRefBridgeplugin (rendered inside the composer) reads the editor viauseLexicalComposerContext()and connects it to the outerrefwithuseImperativeHandle— the same pattern as the existingAutoFocusOnMountplugin.forwardRef<RichTextEditorRef, RichTextEditorProps>.clear()useseditor.update(() => $getRoot().clear()).Docs / exports / tests (SYNC contract)
RichTextEditorReffromRichTextEditor/index.tsand the@astryxdesign/labbarrel.doc.mjsbest-practice note.getEditorState,getEditor,clear).Testing
pnpm -F @astryxdesign/lab typecheck— cleaneslinton changed files — cleanvitest run RichTextEditor.test.tsx— 21/21 passing (incl. 6 new)human test
Screen.Recording.2026-07-27.at.4.47.35.PM.mov
Notes
focus()test asserts the call is wired + the root element is reachable rather than jsdomdocument.activeElement, because jsdom does not reliably reflect programmatic contenteditable focus (same limitation the existing autofocus path has).main.