An open source design system that's fully customizable and agent ready
TypeScript
feat(richtext): the toolbar takes base props, and its new-tab option is hasNewTabLinks (#7561)
Cindy Zhang6a40f150418f
diff --git a/apps/storybook/stories/RichTextEditor.playground.tsx b/apps/storybook/stories/RichTextEditor.playground.tsxindex b81338451d..640b0884b8 100644--- a/apps/storybook/stories/RichTextEditor.playground.tsx+++ b/apps/storybook/stories/RichTextEditor.playground.tsx@@ -488,7 +488,7 @@ export function RichTextEditorPlayground() {const [hasToolbar, setHasToolbar] = useState(true);const [hasInsertControls, setHasInsertControls] = useState(true);const [hasLink, setHasLink] = useState(true);- const [linkOpensInNewTab, setLinkOpensInNewTab] = useState(true);+ const [hasNewTabLinks, setHasNewTabLinks] = useState(true);const [hasAutoLink, setHasAutoLink] = useState(true);const [hasMarkdownShortcuts, setHasMarkdownShortcuts] = useState(true);const [hasMarkdownPlugins, setHasMarkdownPlugins] = useState(true);@@ -673,7 +673,7 @@ export function RichTextEditorPlayground() {<RichTextEditorToolbarsize={toolbarSize}hasLink={hasLink}- linkOpensInNewTab={linkOpensInNewTab}+ hasNewTabLinks={hasNewTabLinks}endContent={hasInsertControls ? <InsertControls /> : null}/>) : undefined@@ -799,8 +799,8 @@ export function RichTextEditorPlayground() {<Switchsize="sm"label="Links open in a new tab"- value={linkOpensInNewTab}- onChange={setLinkOpensInNewTab}+ value={hasNewTabLinks}+ onChange={setHasNewTabLinks}isDisabled={!hasToolbar || !hasLink}disabledMessage="Turn the toolbar's link button on first."/>diff --git a/packages/richtext/src/RichTextEditor.doc.mjs b/packages/richtext/src/RichTextEditor.doc.mjsindex 2139655363..ca6cd7d983 100644--- a/packages/richtext/src/RichTextEditor.doc.mjs+++ b/packages/richtext/src/RichTextEditor.doc.mjs@@ -229,7 +229,7 @@ export const docs = {{guidance: true,description:- 'Links: the toolbar Link button (on by default; disable with hasLink={false}) and Cmd/Ctrl+K open an Astryx Dialog. The form preserves the Lexical selection while focus moves into the URL input and supports add, update, remove, Escape, and focus return. Pass promptForUrl only when integrating an existing synchronous URL flow. Entered URLs are sanitized (only http/https/mailto/tel are written; javascript:/data: are rejected). Links open in a new tab by default: target=_blank and rel=noopener noreferrer are written into the link node data; set linkOpensInNewTab={false} for same-tab links.',+ 'Links: the toolbar Link button (on by default; disable with hasLink={false}) and Cmd/Ctrl+K open an Astryx Dialog. The form preserves the Lexical selection while focus moves into the URL input and supports add, update, remove, Escape, and focus return. Pass promptForUrl only when integrating an existing synchronous URL flow. Entered URLs are sanitized (only http/https/mailto/tel are written; javascript:/data: are rejected). Links open in a new tab by default: target=_blank and rel=noopener noreferrer are written into the link node data; set hasNewTabLinks={false} for same-tab links. The toolbar takes base props and passes them to the Astryx Toolbar it renders: ref, className, style and xstyle on its root; data-*, other aria-* attributes and DOM handlers on the toolbar element (name it with label, not aria-label).',},{guidance: true,diff --git a/packages/richtext/src/RichTextEditor.test.tsx b/packages/richtext/src/RichTextEditor.test.tsxindex 6e6a7e395b..98f8120045 100644--- a/packages/richtext/src/RichTextEditor.test.tsx+++ b/packages/richtext/src/RichTextEditor.test.tsx@@ -1845,7 +1845,41 @@ describe('RichTextEditorToolbar — new-tab links', () => {});});- it('omits new-tab attributes when linkOpensInNewTab is false', async () => {+ it('passes base props and the ref through to the Astryx Toolbar', () => {+ const ref = createRef<HTMLDivElement>();+ const onKeyDown = vi.fn();+ render(+ <RichTextEditor+ label="Notes"+ toolbar={+ <RichTextEditorToolbar+ ref={ref}+ className="product-toolbar"+ style={{marginBlockEnd: 3}}+ data-testid="toolbar-root"+ aria-describedby="toolbar-help"+ onKeyDown={onKeyDown}+ />+ }+ />,+ );+ // Base props land where core Toolbar puts them: the ref and styling on+ // its root, every other base prop on the role="toolbar" element inside.+ const toolbar = screen.getByRole('toolbar');+ const root = ref.current;+ expect(root).not.toBeNull();+ expect(root?.contains(toolbar)).toBe(true);+ expect(root).toHaveClass('product-toolbar');+ expect(root?.style.marginBlockEnd).toBe('3px');+ expect(screen.getByTestId('toolbar-root')).toBe(toolbar);+ expect(toolbar).toHaveAttribute('aria-describedby', 'toolbar-help');+ // The toolbar's own label and role survive the spread.+ expect(toolbar).toHaveAccessibleName('Text formatting');+ fireEvent.keyDown(toolbar, {key: 'x'});+ expect(onKeyDown).toHaveBeenCalledTimes(1);+ });++ it('omits new-tab attributes when hasNewTabLinks is false', async () => {let editor!: LexicalEditor;const promptForUrl = vi.fn(() => 'example.com');render(@@ -1854,7 +1888,7 @@ describe('RichTextEditorToolbar — new-tab links', () => {toolbar={<RichTextEditorToolbarpromptForUrl={promptForUrl}- linkOpensInNewTab={false}+ hasNewTabLinks={false}/>}plugins={<CaptureEditor onReady={e => (editor = e)} />}diff --git a/packages/richtext/src/RichTextEditorToolbar.tsx b/packages/richtext/src/RichTextEditorToolbar.tsxindex 1790142af2..a52a0962c3 100644--- a/packages/richtext/src/RichTextEditorToolbar.tsx+++ b/packages/richtext/src/RichTextEditorToolbar.tsx@@ -13,7 +13,9 @@* and RichTextEditorToolbarProps.* @position Experimental (richtext). Drop into RichTextEditor's `toolbar` slot to* add a flush top formatting toolbar. Themed via Astryx Toolbar, Selector,- * IconButton, and ToggleButton, so it inherits the active theme.+ * IconButton, and ToggleButton, so it inherits the active theme. Base props+ * pass through to the Astryx Toolbar exactly as Toolbar places them+ * (spec:AST-061/DEC-8).** SYNC: When modified, update:* - /packages/richtext/src/index.ts (exports)@@ -42,6 +44,7 @@ import {useState,type FormEvent,type ReactNode,+ type Ref,} from 'react';import * as stylex from '@stylexjs/stylex';import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';@@ -61,6 +64,7 @@ import {} from '@lexical/list';import {$getNearestNodeOfType, mergeRegister} from '@lexical/utils';import {TOGGLE_LINK_COMMAND, $isLinkNode, $createLinkNode} from '@lexical/link';+import type {BaseProps} from '@astryxdesign/core';import {Toolbar} from '@astryxdesign/core/Toolbar';import {IconButton} from '@astryxdesign/core/IconButton';import {Selector, type SelectorOptionType} from '@astryxdesign/core/Selector';@@ -451,7 +455,15 @@ function DirectionalGlyph({children}: {children: ReactNode}) {);}-export interface RichTextEditorToolbarProps {+export interface RichTextEditorToolbarProps extends BaseProps<HTMLDivElement> {+ /**+ * Ref to the toolbar's root element. Base props pass through to the+ * underlying Astryx `Toolbar`: the ref, `className`, `style`, and `xstyle`+ * reach its root, and `data-*`, other `aria-*` attributes, and DOM+ * handlers reach the `role="toolbar"` element inside it. The toolbar names+ * itself, so pass `label` rather than `aria-label`.+ */+ ref?: Ref<HTMLDivElement>;/*** Accessible label for the toolbar element.* @default 'Text formatting', translated for the active locale@@ -495,7 +507,7 @@ export interface RichTextEditorToolbarProps {* create same-tab links.* @default true*/- linkOpensInNewTab?: boolean;+ hasNewTabLinks?: boolean;/*** Extra items rendered at the end of the toolbar (after a divider). Use this* to compose product-specific controls (e.g. mentions, AI) alongside the@@ -530,8 +542,11 @@ export function RichTextEditorToolbar({size = 'sm',hasLink = true,promptForUrl,- linkOpensInNewTab = true,+ hasNewTabLinks = true,endContent,+ xstyle,+ ref,+ ...rest}: RichTextEditorToolbarProps) {const [editor] = useLexicalComposerContext();const themeName = useThemeName();@@ -680,7 +695,7 @@ export function RichTextEditorToolbar({}restoreLinkSelection();- const linkAttributes = linkOpensInNewTab+ const linkAttributes = hasNewTabLinks? {target: '_blank', rel: 'noopener noreferrer'}: {};const handled = editor.dispatchCommand(TOGGLE_LINK_COMMAND, {@@ -699,7 +714,7 @@ export function RichTextEditorToolbar({}return true;},- [editor, linkOpensInNewTab, restoreLinkSelection],+ [editor, hasNewTabLinks, restoreLinkSelection],);const toggleLink = useCallback(() => {@@ -896,9 +911,11 @@ export function RichTextEditorToolbar({return (<><Toolbar+ {...rest}+ ref={ref}label={label}size={size}- xstyle={toolbarScrollStyles.compactToolbar}+ xstyle={[toolbarScrollStyles.compactToolbar, xstyle]}startContent={<HStackgap={1}