Skip to content

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

TypeScript

fix(richtext): keep toolbar controls on a 4px gutter (#7391)

Ernest2c86d9dda12f
diff --git a/packages/richtext/src/RichTextEditor.doc.mjs b/packages/richtext/src/RichTextEditor.doc.mjs
index 0590471d18..a3f2f268dc 100644
--- a/packages/richtext/src/RichTextEditor.doc.mjs
+++ b/packages/richtext/src/RichTextEditor.doc.mjs
@@ -224,7 +224,7 @@ export const docs = {
{
guidance: true,
description:
- 'Add a formatting toolbar with toolbar={<RichTextEditorToolbar />}. The dedicated slot places it edge-to-edge at the top of the field, before the padded editing surface, with correct keyboard order. It uses small Astryx Toolbar controls: undo/redo, a divided block-format Selector for paragraphs/headings/lists/quotes, then divided inline ToggleButtons for bold/italic/underline/strikethrough/code and links. The complete action row scrolls horizontally when space is tight, keeping every control directly available without a More menu. The Selector keeps its default adaptive placement. Compose extra controls via endContent.',
+ 'Add a formatting toolbar with toolbar={<RichTextEditorToolbar />}. The dedicated slot places it edge-to-edge at the top of the field, before the padded editing surface, with a RichText-owned 4px control gutter and correct keyboard order. It uses small Astryx Toolbar controls: undo/redo, a divided block-format Selector for paragraphs/headings/lists/quotes, then divided inline ToggleButtons for bold/italic/underline/strikethrough/code and links. The complete action row scrolls horizontally when space is tight, keeping every control directly available without a More menu. The Selector keeps its default adaptive placement. Compose extra controls via endContent.',
},
{
guidance: true,
diff --git a/packages/richtext/src/RichTextEditor.test.tsx b/packages/richtext/src/RichTextEditor.test.tsx
index c4fba070af..6e6a7e395b 100644
--- a/packages/richtext/src/RichTextEditor.test.tsx
+++ b/packages/richtext/src/RichTextEditor.test.tsx
@@ -1314,7 +1314,11 @@ describe('RichTextEditorToolbar', () => {
Node.DOCUMENT_POSITION_FOLLOWING,
).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
expect(toolbarRegion?.parentElement).toBe(editor);
+ expect(toolbarRegion).toBeInstanceOf(HTMLElement);
expect(editorBody).toBeInstanceOf(HTMLElement);
+ // The compact gutter is emitted by StyleX; do not regress to a component
+ // inline-style escape hatch. Browser coverage below owns the geometry.
+ expect(toolbarRegion).not.toHaveAttribute('style');
// Vitest's JSDOM setup does not load compiled StyleX CSS. The WithToolbar
// story provides visual coverage for flush edges and preserved body inset;
// here we verify that the dedicated body receives its StyleX layout.
@@ -1421,7 +1425,7 @@ describe('RichTextEditorToolbar', () => {
const actionRow = screen.getByRole('group', {
name: 'Formatting actions',
});
- expect(actionRow).toHaveAttribute(EDGE_COMP_ATTR);
+ expect(actionRow).not.toHaveAttribute(EDGE_COMP_ATTR);
for (const name of [
'Bold',
'Italic',
diff --git a/packages/richtext/src/RichTextEditorToolbar.tsx b/packages/richtext/src/RichTextEditorToolbar.tsx
index e354d07ef5..1790142af2 100644
--- a/packages/richtext/src/RichTextEditorToolbar.tsx
+++ b/packages/richtext/src/RichTextEditorToolbar.tsx
@@ -9,7 +9,8 @@
* command constants, plus Astryx Toolbar / IconButton / Selector /
* ToggleButton / Divider / Dialog / TextInput / Button / Layout primitives.
* @output Exports RichTextEditorToolbar (a compact formatting toolbar with a
- * horizontally scrollable action row) and RichTextEditorToolbarProps.
+ * RichText-owned 4px control gutter and horizontally scrollable action row)
+ * 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.
@@ -70,7 +71,6 @@ import {TextInput} from '@astryxdesign/core/TextInput';
import {Button} from '@astryxdesign/core/Button';
import {Stack} from '@astryxdesign/core/Stack';
import {
- EDGE_COMP_ATTR,
HStack,
Layout,
LayoutContent,
@@ -81,6 +81,7 @@ import {useThemeName} from '@astryxdesign/core/theme';
import {rtlStyles} from '@astryxdesign/core/utils';
import {
fontWeightVars,
+ spacingVars,
typeScaleVars,
} from '@astryxdesign/core/theme/tokens.stylex';
import {useTranslator} from '@astryxdesign/core/i18n';
@@ -147,6 +148,15 @@ const glyphStyles = stylex.create({
});
const toolbarScrollStyles = stylex.create({
+ // Core Toolbar owns an 8px block inset. This transparent RichText-only
+ // surface overlaps 4px of that inset at each block edge, so its controls and
+ // following editor body retain a 4px visual gutter.
+ compactToolbar: {
+ '--astryx-section-padding-inline-start': spacingVars['--spacing-1'],
+ '--astryx-section-padding-inline-end': spacingVars['--spacing-1'],
+ marginBlockStart: `calc(-1 * ${spacingVars['--spacing-1']})`,
+ marginBlockEnd: `calc(-1 * ${spacingVars['--spacing-1']})`,
+ },
// The row scrolls when it runs out of room, so a control keeps its own
// width. A Selector's field may shrink, which squeezes its label to a
// sliver, so it sits in a slot that may not.
@@ -166,12 +176,6 @@ const toolbarScrollStyles = stylex.create({
},
});
-// The scroll row is this standalone toolbar's sole slot child. Opting it into
-// Toolbar's container-owned edge compensation gives the formatting surface a
-// 4px inline inset at `sm` without moving generic nested Toolbar content off
-// its surrounding Card/Section content line.
-const toolbarEdgeCompensationProps = {[EDGE_COMP_ATTR]: ''};
-
/**
* Whether `event` is the insert-link shortcut (Cmd/Ctrl+K). Uses Lexical's
* `isExactShortcutMatch`, which — unlike a loose `metaKey || ctrlKey` check —
@@ -894,9 +898,9 @@ export function RichTextEditorToolbar({
<Toolbar
label={label}
size={size}
+ xstyle={toolbarScrollStyles.compactToolbar}
startContent={
<HStack
- {...toolbarEdgeCompensationProps}
gap={1}
role="group"
aria-label={t('@astryx.richTextEditor.toolbar.actions')}