Skip to content

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

TypeScript

fix(richtext): toolbar slots use isRenderable; the nullish-guard baseline is deleted (#9265)

Cindy Zhang66d66876e6a2
diff --git a/.changeset/richtext-slots-render-nothing.md b/.changeset/richtext-slots-render-nothing.md
new file mode 100644
index 0000000000..94ea4e636b
--- /dev/null
+++ b/.changeset/richtext-slots-render-nothing.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/richtext': patch
+---
+
+[fix] RichTextEditor's `toolbar` now treats `''` as empty, as it already treated `null`, `undefined` and booleans, so `toolbar=""` no longer adds the toolbar's top border to the editing surface. RichTextEditorToolbar's `endContent` treats `''`, `false` and `true` as empty, so it no longer adds a trailing separator. `0` still counts as content and shows "0".
+
+@ernestt @potatowagon
diff --git a/eslint.config.js b/eslint.config.js
index 5b3767d5b2..bba91b4868 100644
--- a/eslint.config.js
+++ b/eslint.config.js
@@ -47,19 +47,6 @@ const internalBarrelBaseline = JSON.parse(
),
);
-// Guarded values whose bare nullish JSX guard or hand-written isRenderable
-// check predates making `@astryx/no-nullish-jsx-guard` fail strict lint, by
-// repo-relative file, once per check. The list only shrinks.
-const nullishGuardBaseline = JSON.parse(
- readFileSync(
- path.join(
- import.meta.dirname,
- 'internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json',
- ),
- 'utf8',
- ),
-);
-
// Private `data-astryx-*` attributes written by hand before
// `@astryx/no-raw-private-attribute` arrived, by repo-relative file, once per
// site. The list only shrinks.
@@ -912,11 +899,9 @@ export default defineConfig(
},
},
// A slot's presence is decided by `isRenderable`, never a bare `!= null`
- // guard or a hand-written copy of its checks. The checks that predate this
- // block are listed in the baseline, once per check of each value, and that
- // many stay allowed until migrated; one more is reported (an error in
- // strict mode, so a pull request that adds one fails lint, while main stays
- // green).
+ // guard or a hand-written copy of its checks (an error in strict mode). A
+ // check of a string, number or data value says so with a disable comment
+ // for this rule that gives its reason.
{
files: nullishGuardScope.files,
ignores: nullishGuardScope.ignores,
@@ -924,10 +909,7 @@ export default defineConfig(
'@astryx': astryxEslintPlugin,
},
rules: {
- '@astryx/no-nullish-jsx-guard': [
- reactSeverity,
- {baseline: nullishGuardBaseline, root: import.meta.dirname},
- ],
+ '@astryx/no-nullish-jsx-guard': reactSeverity,
},
},
// A private `data-astryx-*` attribute is written once, in its owner module
diff --git a/internal/eslint-plugin-astryx/README.md b/internal/eslint-plugin-astryx/README.md
index 6e3434098b..875f2e96eb 100644
--- a/internal/eslint-plugin-astryx/README.md
+++ b/internal/eslint-plugin-astryx/README.md
@@ -599,7 +599,7 @@ if (rendered == null || typeof rendered === 'boolean') return null; // flagged
const hasLabel = isRenderable(label); // good
```
-The presets ship it as a warning. The repository config enforces it in core and richtext with `{baseline, root}`: each value's checks that predated enforcement are listed per file in `no-nullish-jsx-guard.baseline.json`, once per check, and that many stay allowed until migrated; one more check of a value in that file is an error in strict mode. A chain that also tests the value against another type (`typeof value === 'string'`, `isValidElement`, `Array.isArray`, `instanceof`) is a type switch and is not flagged. After migrating call sites, rewrite the baseline with `node internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.mjs` (`--check` verifies it). For a JSX guard, the rule also provides an ESLint suggestion that rewrites the comparison to `isRenderable(value)` (add the import manually).
+The presets ship it as a warning. The repository config enforces it in core and richtext, where it is an error in strict mode. A check of a string, number or data value, or a documented `false` opt-out, says so with `// eslint-disable-next-line @astryx/no-nullish-jsx-guard -- <reason>`. The rule still takes `{baseline, root}` (each value's allowed checks per file) for a codebase that adopts it with existing call sites. A chain that also tests the value against another type (`typeof value === 'string'`, `isValidElement`, `Array.isArray`, `instanceof`) is a type switch and is not flagged. For a JSX guard, the rule also provides an ESLint suggestion that rewrites the comparison to `isRenderable(value)` (add the import manually).
See: https://github.com/meta/astryx/issues/2538
diff --git a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json b/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json
deleted file mode 100644
index 4dfc9cc699..0000000000
--- a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json
+++ /dev/null
@@ -1,4 +0,0 @@
-{
- "packages/richtext/src/RichTextEditor.tsx": ["maxLength", "toolbar"],
- "packages/richtext/src/RichTextEditorToolbar.tsx": ["endContent"]
-}
diff --git a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.mjs b/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.mjs
deleted file mode 100644
index 8141616036..0000000000
--- a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.mjs
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright (c) Meta Platforms, Inc. and affiliates.
-
-/**
- * @file no-nullish-jsx-guard.baseline.mjs
- * @description Writes (or, with --check, verifies) the baseline that
- * `@astryx/no-nullish-jsx-guard` allows: each guarded value, per file, once
- * per bare nullish JSX guard or hand-written isRenderable check of it today.
- * Run it after migrating checks to isRenderable, or after merging main into a
- * branch that changes the rule, so the list only names checks that still
- * exist.
- *
- * node internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.mjs [--check]
- */
-
-import path from 'node:path';
-import {scope} from './no-nullish-jsx-guard.js';
-import {runRuleBaseline} from './rule-baseline.mjs';
-
-await runRuleBaseline({
- ruleId: '@astryx/no-nullish-jsx-guard',
- scope,
- targets: ['packages/core/src', 'packages/richtext/src'],
- baselineFile: path.join(
- import.meta.dirname,
- 'no-nullish-jsx-guard.baseline.json',
- ),
- entryFromMessage: message =>
- /`!?isRenderable\(([^`]*)\)`/.exec(message)?.[1] ?? null,
- unique: false,
- noun: 'guarded-value checks',
-});
diff --git a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.js b/internal/eslint-plugin-astryx/no-nullish-jsx-guard.js
index 1da903085a..d7bf7a7e84 100644
--- a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.js
+++ b/internal/eslint-plugin-astryx/no-nullish-jsx-guard.js
@@ -58,7 +58,6 @@
* baseline lists, per file, each guarded value once per check that predates
* enforcement. That many checks of the value stay allowed until migrated;
* one more is reported.
- * `no-nullish-jsx-guard.baseline.mjs` writes it.
*
* Known limitations (intentional, to keep precision high):
* - Bare assignments like `const hasFoo = foo != null` are not flagged;
@@ -71,8 +70,7 @@
import {baselineAllowance} from './rule-baseline.mjs';
/**
- * Where the repository config enforces the rule with its baseline; the
- * baseline script lints the same files.
+ * Where the repository config enforces the rule.
*/
export const scope = {
files: [
diff --git a/packages/richtext/src/RichTextEditor.test.tsx b/packages/richtext/src/RichTextEditor.test.tsx
index 05be3fff8d..a7b512f40b 100644
--- a/packages/richtext/src/RichTextEditor.test.tsx
+++ b/packages/richtext/src/RichTextEditor.test.tsx
@@ -14,9 +14,22 @@
*/
import {describe, it, expect, vi, beforeAll, afterAll} from 'vitest';
-import {render, screen, waitFor, fireEvent, act} from '@testing-library/react';
+import {
+ render,
+ screen,
+ waitFor,
+ fireEvent,
+ act,
+ within,
+} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
-import {createRef, StrictMode, useEffect, useState} from 'react';
+import {
+ createRef,
+ StrictMode,
+ useEffect,
+ useState,
+ type ReactNode,
+} from 'react';
import * as stylex from '@stylexjs/stylex';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import type {EditorState, LexicalEditor} from 'lexical';
@@ -1328,9 +1341,19 @@ describe('markdown serializers', () => {
});
describe('RichTextEditorToolbar', () => {
- it('treats a false conditional toolbar as absent', () => {
+ // A slot is empty for exactly the values React renders as nothing.
+ it.each([
+ ['false', false],
+ ['an empty string', ''],
+ ['true', true],
+ ['null', null],
+ ])('treats a %s conditional toolbar as absent', (_name, value) => {
const {container, rerender} = render(
- <RichTextEditor label="Notes" toolbar={false} tabEscapeHint="" />,
+ <RichTextEditor
+ label="Notes"
+ toolbar={value as ReactNode}
+ tabEscapeHint=""
+ />,
);
const conditionalBodyClass = container.querySelector(
'.astryx-rich-text-editor',
@@ -2256,3 +2279,43 @@ describe('RichTextEditor translation', () => {
);
});
});
+
+describe('RichTextEditorToolbar end content', () => {
+ it.each([
+ ['an empty string', ''],
+ ['false', false],
+ ['true', true],
+ ['null', null],
+ ])('adds no divider for %s', (_name, value) => {
+ const html = () =>
+ within(
+ screen.getByRole('toolbar', {name: 'Text formatting'}),
+ ).queryAllByRole('separator').length;
+ const {unmount} = render(
+ <RichTextEditor
+ label="Notes"
+ toolbar={<RichTextEditorToolbar endContent={value as ReactNode} />}
+ />,
+ );
+ const withEnd = html();
+ unmount();
+ render(
+ <RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />,
+ );
+ expect(withEnd).toBe(html());
+ });
+
+ it('renders 0 as end content, as React does', () => {
+ render(
+ <RichTextEditor
+ label="Notes"
+ toolbar={<RichTextEditorToolbar endContent={0} />}
+ />,
+ );
+ expect(
+ within(screen.getByRole('toolbar', {name: 'Text formatting'})).getByText(
+ '0',
+ ),
+ ).toBeInTheDocument();
+ });
+});
diff --git a/packages/richtext/src/RichTextEditor.tsx b/packages/richtext/src/RichTextEditor.tsx
index 2ce42ba809..3272fb5ab9 100644
--- a/packages/richtext/src/RichTextEditor.tsx
+++ b/packages/richtext/src/RichTextEditor.tsx
@@ -54,7 +54,12 @@ import type {BaseProps} from '@astryxdesign/core';
import {useInputStatusIcon} from '@astryxdesign/core/hooks';
import {VisuallyHidden} from '@astryxdesign/core/VisuallyHidden';
import {useTranslator} from '@astryxdesign/core/i18n';
-import {mergeProps, themeProps, type SizeValue} from '@astryxdesign/core/utils';
+import {
+ isRenderable,
+ mergeProps,
+ themeProps,
+ type SizeValue,
+} from '@astryxdesign/core/utils';
import {useSize} from '@astryxdesign/core/SizeContext';
import {LexicalExtensionComposer} from '@lexical/react/LexicalExtensionComposer';
@@ -599,7 +604,7 @@ export const RichTextEditor = function RichTextEditor({
}
const hasTabEscapeHint = editable && tabEscapeHint !== '';
- const hasToolbar = toolbar != null && typeof toolbar !== 'boolean';
+ const hasToolbar = isRenderable(toolbar);
const {statusIcon, describedBy: statusTooltipDescribedBy} =
useInputStatusIcon({
@@ -755,6 +760,7 @@ export const RichTextEditor = function RichTextEditor({
<VisuallyHidden id={tabEscapeHintID}>{tabEscapeHint}</VisuallyHidden>
)}
</div>
+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `maxLength` is a number; 0 still shows its counter */}
{maxLength != null && (
<div
id={counterID}
diff --git a/packages/richtext/src/RichTextEditorToolbar.tsx b/packages/richtext/src/RichTextEditorToolbar.tsx
index 1e90fe4114..bbae191f6b 100644
--- a/packages/richtext/src/RichTextEditorToolbar.tsx
+++ b/packages/richtext/src/RichTextEditorToolbar.tsx
@@ -82,7 +82,7 @@ import {
} from '@astryxdesign/core/Layout';
import {getExtendedIcon} from '@astryxdesign/core/Icon';
import {useThemeName} from '@astryxdesign/core/theme';
-import {rtlStyles} from '@astryxdesign/core/utils';
+import {isRenderable, rtlStyles} from '@astryxdesign/core/utils';
import {
fontWeightVars,
spacingVars,
@@ -988,7 +988,7 @@ export function RichTextEditorToolbar({
onPressedChange={toggleLink}
/>
)}
- {endContent != null && (
+ {isRenderable(endContent) && (
<>
<Divider orientation="vertical" />
{endContent}