Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(richtext): toolbar glyphs follow the active theme, and undo and redo mirror in RTL (#7216)

Cindy Zhangae5e6f90c328
diff --git a/packages/richtext/src/RichTextEditor.doc.mjs b/packages/richtext/src/RichTextEditor.doc.mjs
index 41a10b71e0..0590471d18 100644
--- a/packages/richtext/src/RichTextEditor.doc.mjs
+++ b/packages/richtext/src/RichTextEditor.doc.mjs
@@ -239,7 +239,7 @@ export const docs = {
{
guidance: true,
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 can restyle any glyph without forking the toolbar: registerIcons({'richtext:bold': <MyBoldIcon />}) from @astryxdesign/core/Icon. registerIcons now accepts arbitrary extension keys, and getExtendedIcon(key, fallback) resolves them; the same pattern any library can use to make its own icons theme-overridable.",
+ "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: false,
diff --git a/packages/richtext/src/RichTextEditor.test.tsx b/packages/richtext/src/RichTextEditor.test.tsx
index e07e5c1422..be45990d7b 100644
--- a/packages/richtext/src/RichTextEditor.test.tsx
+++ b/packages/richtext/src/RichTextEditor.test.tsx
@@ -41,6 +41,8 @@ import {
} from './markdownSerializers';
import {RichTextEditorToolbar} from './RichTextEditorToolbar';
import {registerIcons, resetIcons} from '@astryxdesign/core/Icon';
+import {Theme, defineTheme} from '@astryxdesign/core/theme';
+import {rtlStyles} from '@astryxdesign/core/utils';
import {InternationalizationProvider} from '@astryxdesign/core/i18n';
import {
RichTextEditorAutoLinkPlugin,
@@ -1462,6 +1464,46 @@ describe('RichTextEditorToolbar', () => {
resetIcons();
}
});
+
+ it('draws the glyphs of the theme it renders in', () => {
+ const theme = defineTheme({
+ name: 'richtext-toolbar-themed-icons',
+ icons: {
+ 'richtext:bold': <svg data-testid="theme-bold" />,
+ 'richtext:undo': <svg data-testid="theme-undo" />,
+ },
+ });
+ render(
+ <Theme theme={theme}>
+ <RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />
+ </Theme>,
+ );
+ expect(
+ screen
+ .getByRole('button', {name: 'Bold'})
+ .querySelector('[data-testid="theme-bold"]'),
+ ).toBeInTheDocument();
+ expect(
+ screen
+ .getByRole('button', {name: 'Undo'})
+ .querySelector('[data-testid="theme-undo"]'),
+ ).toBeInTheDocument();
+ });
+
+ it('wraps only undo and redo in the RTL mirror', () => {
+ const {container} = render(
+ <RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />,
+ );
+ // The atomic class only; dev builds add a debug name with a dot in it.
+ const mirrorClass = (stylex.props(rtlStyles.mirror).className ?? '')
+ .split(' ')
+ .find(name => !name.includes('__'));
+ expect(mirrorClass).toBeDefined();
+ const mirrored = [
+ ...container.getElementsByClassName(mirrorClass ?? ''),
+ ].map(element => element.closest('button')?.getAttribute('aria-label'));
+ expect(mirrored).toEqual(['Undo', 'Redo']);
+ });
});
describe('linkUtils', () => {
diff --git a/packages/richtext/src/RichTextEditorToolbar.tsx b/packages/richtext/src/RichTextEditorToolbar.tsx
index f57a3a474b..cb4fb3a193 100644
--- a/packages/richtext/src/RichTextEditorToolbar.tsx
+++ b/packages/richtext/src/RichTextEditorToolbar.tsx
@@ -26,11 +26,12 @@
* commands); the UI is built from Astryx primitives so it matches the theme.
*
* ICONS: Each control resolves its glyph through the core icon registry under a
- * stable `richtext:*` key (see {@link RICHTEXT_ICON_KEYS}), falling back to the
- * bundled inline SVGs below. A theme can restyle any glyph by registering its
- * own icon for that key — no need to fork the toolbar:
- * import {registerIcons} from '@astryxdesign/core/Icon';
- * registerIcons({'richtext:bold': <MyBoldIcon />});
+ * stable `richtext:*` key (see {@link RICHTEXT_ICON_KEYS}) for the active
+ * theme, falling back to the bundled inline SVGs below — the library seam
+ * `getExtendedIcon` documents. A theme restyles any glyph by naming its key:
+ * defineTheme({icons: {'richtext:bold': <MyBoldIcon />}});
+ * Undo and redo point backward and forward in time, so they mirror under RTL
+ * whichever glyph draws them.
*/
import {
@@ -75,6 +76,12 @@ import {
LayoutFooter,
} from '@astryxdesign/core/Layout';
import {getExtendedIcon} from '@astryxdesign/core/Icon';
+import {useThemeName} from '@astryxdesign/core/theme';
+import {rtlStyles} from '@astryxdesign/core/utils';
+import {
+ fontWeightVars,
+ typeScaleVars,
+} from '@astryxdesign/core/theme/tokens.stylex';
import {useTranslator} from '@astryxdesign/core/i18n';
import {
FORMAT_TEXT_COMMAND,
@@ -126,6 +133,18 @@ const toolbarDividerStyles = stylex.create({
},
});
+const glyphStyles = stylex.create({
+ // A transform needs a box: the wrapper is inline-flex so the mirror applies.
+ directional: {
+ display: 'inline-flex',
+ },
+ text: {
+ fontSize: typeScaleVars['--text-supporting-size'],
+ fontWeight: fontWeightVars['--font-weight-bold'],
+ fontVariantNumeric: 'tabular-nums',
+ },
+});
+
const toolbarScrollStyles = stylex.create({
actions: {
flex: '1 1 0%',
@@ -150,10 +169,10 @@ function isInsertLink(event: KeyboardEvent): boolean {
}
/**
- * Stable icon-registry keys for the toolbar's controls. Themes can override any
- * of these via `registerIcons({'richtext:bold': <MyIcon />})` from
- * `@astryxdesign/core/Icon`. Keys are namespaced (`richtext:*`) to avoid
- * collisions with the core semantic icon set.
+ * Stable icon-registry keys for the toolbar's controls. A theme overrides any
+ * of these by key: `defineTheme({icons: {'richtext:bold': <MyIcon />}})`.
+ * Keys are namespaced (`richtext:*`) to avoid collisions with the core
+ * semantic icon set.
*/
export const RICHTEXT_ICON_KEYS = {
bold: 'richtext:bold',
@@ -379,24 +398,39 @@ const defaultToolbarIcons: Record<string, ReactNode> = {
/** Renders a short text label as a toolbar glyph (for heading buttons). */
function TextGlyph({label}: {label: string}) {
return (
- <span
- aria-hidden="true"
- style={{
- fontSize: '0.75rem',
- fontWeight: 700,
- fontVariantNumeric: 'tabular-nums',
- }}>
+ <span aria-hidden="true" {...stylex.props(glyphStyles.text)}>
{label}
</span>
);
}
+type ToolbarIconName = keyof typeof RICHTEXT_ICON_KEYS;
+
/**
- * Resolve a toolbar glyph: prefer a theme-registered icon for the stable
- * `richtext:*` key, otherwise fall back to the bundled inline default.
+ * Resolve a toolbar glyph for the active theme: the theme's icon for the
+ * stable `richtext:*` key, then a globally registered one, then the bundled
+ * inline default.
*/
-function resolveIcon(name: keyof typeof RICHTEXT_ICON_KEYS): ReactNode {
- return getExtendedIcon(RICHTEXT_ICON_KEYS[name], defaultToolbarIcons[name]);
+function resolveIcon(
+ name: ToolbarIconName,
+ themeName: string | null | undefined,
+): ReactNode {
+ return getExtendedIcon(
+ RICHTEXT_ICON_KEYS[name],
+ defaultToolbarIcons[name],
+ themeName,
+ );
+}
+
+/** A glyph that points along the reading direction, mirrored under RTL. */
+function DirectionalGlyph({children}: {children: ReactNode}) {
+ return (
+ <span
+ aria-hidden="true"
+ {...stylex.props(glyphStyles.directional, rtlStyles.mirror)}>
+ {children}
+ </span>
+ );
}
export interface RichTextEditorToolbarProps {
@@ -482,6 +516,8 @@ export function RichTextEditorToolbar({
endContent,
}: RichTextEditorToolbarProps) {
const [editor] = useLexicalComposerContext();
+ const themeName = useThemeName();
+ const icon = (name: ToolbarIconName) => resolveIcon(name, themeName);
const t = useTranslator();
const label = labelFromProps ?? t('@astryx.richTextEditor.toolbar.label');
// Literal keys, so the catalog gate can verify every one.
@@ -815,27 +851,27 @@ export function RichTextEditorToolbar({
{
value: 'paragraph',
label: t('@astryx.richTextEditor.toolbar.paragraph'),
- icon: resolveIcon('paragraph'),
+ icon: icon('paragraph'),
},
...headingLevels.map(level => ({
value: level,
label: headingLabels[level],
- icon: resolveIcon(level),
+ icon: icon(level),
})),
{
value: 'bullet',
label: t('@astryx.richTextEditor.toolbar.bulletedList'),
- icon: resolveIcon('bullet'),
+ icon: icon('bullet'),
},
{
value: 'number',
label: t('@astryx.richTextEditor.toolbar.numberedList'),
- icon: resolveIcon('number'),
+ icon: icon('number'),
},
{
value: 'quote',
label: t('@astryx.richTextEditor.toolbar.blockQuote'),
- icon: resolveIcon('quote'),
+ icon: icon('quote'),
},
];
@@ -852,7 +888,7 @@ export function RichTextEditorToolbar({
xstyle={toolbarScrollStyles.actions}>
<IconButton
label={t('@astryx.richTextEditor.toolbar.undo')}
- icon={resolveIcon('undo')}
+ icon={<DirectionalGlyph>{icon('undo')}</DirectionalGlyph>}
variant="ghost"
tooltip={t('@astryx.richTextEditor.toolbar.undo')}
isDisabled={!isEditable || !canUndo}
@@ -860,7 +896,7 @@ export function RichTextEditorToolbar({
/>
<IconButton
label={t('@astryx.richTextEditor.toolbar.redo')}
- icon={resolveIcon('redo')}
+ icon={<DirectionalGlyph>{icon('redo')}</DirectionalGlyph>}
variant="ghost"
tooltip={t('@astryx.richTextEditor.toolbar.redo')}
isDisabled={!isEditable || !canRedo}
@@ -878,7 +914,7 @@ export function RichTextEditorToolbar({
size={size}
value={blockType}
options={blockOptions}
- startIcon={resolveIcon(blockType)}
+ startIcon={icon(blockType)}
isDisabled={!isEditable}
onChange={value => setBlock(value as BlockType)}
/>
@@ -891,7 +927,7 @@ export function RichTextEditorToolbar({
<ToggleButton
key={action.format}
label={inlineFormatLabels[action.format]}
- icon={resolveIcon(action.icon)}
+ icon={icon(action.icon)}
size={size}
isIconOnly
isPressed={activeFormats.has(action.format)}
@@ -903,7 +939,7 @@ export function RichTextEditorToolbar({
<ToggleButton
key="link"
label={t('@astryx.richTextEditor.toolbar.link')}
- icon={resolveIcon('link')}
+ icon={icon('link')}
size={size}
isIconOnly
isPressed={isLink || isLinkDialogOpen}