Skip to content

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

TypeScript

fix(core N–Z): slots use isRenderable; the remaining checks are allowed with a reason (#9206)

Cindy Zhang0135af462b16
diff --git a/.changeset/slots-render-nothing-n-z.md b/.changeset/slots-render-nothing-n-z.md
new file mode 100644
index 0000000000..e294451d6b
--- /dev/null
+++ b/.changeset/slots-render-nothing-n-z.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] More slots treat `''`, `false` and `true` as empty, the way React renders them, instead of drawing an empty wrapper: TreeList's `header` (a header that renders nothing no longer names the tree, so it keeps its `aria-label`), TreeList items' `startContent` and `endContent`, TopNavMegaMenu's `items` and `featured`, Table header `after` content, and OverflowList's measured overflow indicator. `0` still counts as content and shows "0".
+
+@czarandy @humbertovirtudes @rubyycheung
diff --git a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json b/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json
index de758a82ce..4dfc9cc699 100644
--- a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json
+++ b/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json
@@ -1,27 +1,4 @@
{
- "packages/core/src/OverflowList/OverflowList.tsx": ["measureIndicator"],
- "packages/core/src/Pagination/Pagination.tsx": ["computedTotalPages"],
- "packages/core/src/PowerSearch/PowerSearch.tsx": ["resultCountText"],
- "packages/core/src/Table/BaseTable.tsx": ["after"],
- "packages/core/src/Table/plugins/sortable/useTableSortable.tsx": [
- "rank",
- "rank"
- ],
- "packages/core/src/TextArea/TextArea.tsx": ["maxLength"],
- "packages/core/src/ToggleButton/ToggleButton.tsx": ["children"],
- "packages/core/src/TopNav/TopNavMegaMenu.tsx": [
- "featured",
- "featured",
- "items"
- ],
- "packages/core/src/TreeList/TreeList.tsx": ["header"],
- "packages/core/src/TreeList/TreeListItem.tsx": [
- "description",
- "endContent",
- "renderedChildren",
- "startContent"
- ],
- "packages/core/src/utils/getNodeText.ts": ["node"],
"packages/richtext/src/RichTextEditor.tsx": ["maxLength", "toolbar"],
"packages/richtext/src/RichTextEditorToolbar.tsx": ["endContent"]
}
diff --git a/packages/core/src/OverflowList/OverflowList.tsx b/packages/core/src/OverflowList/OverflowList.tsx
index c709ac8530..b277855df4 100644
--- a/packages/core/src/OverflowList/OverflowList.tsx
+++ b/packages/core/src/OverflowList/OverflowList.tsx
@@ -27,6 +27,7 @@ import {
useState,
} from 'react';
import type {BaseProps} from '../BaseProps';
+import {isRenderable} from '../utils/isRenderable';
import type {SpacingStep} from '../utils/types';
import * as stylex from '@stylexjs/stylex';
import {mergeProps} from '../utils';
@@ -370,7 +371,7 @@ export function OverflowList({
inert
{...stylex.props(styles.measureContainer, gapStyles[gap])}>
{childArray}
- {measureIndicator != null && (
+ {isRenderable(measureIndicator) && (
<div {...stylex.props(styles.measureIndicator)}>
{measureIndicator}
</div>
diff --git a/packages/core/src/Pagination/Pagination.tsx b/packages/core/src/Pagination/Pagination.tsx
index 3e161fc2d8..a5b15917f0 100644
--- a/packages/core/src/Pagination/Pagination.tsx
+++ b/packages/core/src/Pagination/Pagination.tsx
@@ -768,6 +768,7 @@ export function Pagination({
{inputLabelText}
</span>
{inputNode}
+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `computedTotalPages` is a page count, not a slot */}
{computedTotalPages != null && (
<span
{...mergeProps(
diff --git a/packages/core/src/PowerSearch/PowerSearch.tsx b/packages/core/src/PowerSearch/PowerSearch.tsx
index 986d508fc9..932f7af6b8 100644
--- a/packages/core/src/PowerSearch/PowerSearch.tsx
+++ b/packages/core/src/PowerSearch/PowerSearch.tsx
@@ -1084,6 +1084,7 @@ export function PowerSearch({
// Build combined endContent from resultCount + endContent props
const combinedEndContent = useMemo((): React.ReactNode => {
const resultCountNode =
+ // eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `resultCountText` is a formatted string, not a slot
resultCountText != null ? (
<span {...stylex.props(resultCountStyles.text)}>{resultCountText}</span>
) : null;
diff --git a/packages/core/src/Table/BaseTable.tsx b/packages/core/src/Table/BaseTable.tsx
index 92ce3b827c..d57896b377 100644
--- a/packages/core/src/Table/BaseTable.tsx
+++ b/packages/core/src/Table/BaseTable.tsx
@@ -15,6 +15,7 @@
*/
import {memo, useRef, type ReactElement, type ReactNode, type Ref} from 'react';
+import {isRenderable} from '../utils/isRenderable';
import * as stylex from '@stylexjs/stylex';
import {spacingVars} from '../theme/tokens.stylex';
import type {
@@ -470,7 +471,7 @@ function BaseTableInner<T extends Record<string, unknown>>({
const headerInner = hasSlots ? (
<>
{before}
- {after != null ? (
+ {isRenderable(after) ? (
<div {...stylex.props(styles.headerLabelRow)}>
{resolvedContent}
{after}
diff --git a/packages/core/src/Table/plugins/sortable/useTableSortable.tsx b/packages/core/src/Table/plugins/sortable/useTableSortable.tsx
index 543aa8d6a5..3c4547dd99 100644
--- a/packages/core/src/Table/plugins/sortable/useTableSortable.tsx
+++ b/packages/core/src/Table/plugins/sortable/useTableSortable.tsx
@@ -303,12 +303,14 @@ function SortHeaderButton<T extends Record<string, unknown>>({
color={direction != null ? 'accent' : 'secondary'}
/>
</span>
+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `rank` is the column's sort position, a number */}
{rank != null && (
<span {...stylex.props(sortStyles.rank)} aria-hidden="true">
{rank}
</span>
)}
</button>
+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `rank` is the column's sort position, a number */}
{rank != null && (
// Hidden, so it adds nothing to the button's or the header's name;
// aria-describedby still reads it.
diff --git a/packages/core/src/TextArea/TextArea.tsx b/packages/core/src/TextArea/TextArea.tsx
index 767b56d248..b4b49277cc 100644
--- a/packages/core/src/TextArea/TextArea.tsx
+++ b/packages/core/src/TextArea/TextArea.tsx
@@ -691,6 +691,7 @@ export function TextArea({
{statusIcon}
</span>
)}
+ {/* 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/core/src/ToggleButton/ToggleButton.tsx b/packages/core/src/ToggleButton/ToggleButton.tsx
index 2aab8e39c6..e7109fc089 100644
--- a/packages/core/src/ToggleButton/ToggleButton.tsx
+++ b/packages/core/src/ToggleButton/ToggleButton.tsx
@@ -319,6 +319,7 @@ export function ToggleButton({
// isIconOnly prop is the source of truth for icon-only rendering.
const labelContent =
+ // eslint-disable-next-line @astryx/no-nullish-jsx-guard -- switching to isRenderable would show `label` for children of '', false or true, a visible change that needs a decision
children != null ? (
<span {...stylex.props(labelStyles.wrapper)}>
<span {...stylex.props(isPressed && labelStyles.pressed)}>
diff --git a/packages/core/src/TopNav/TopNavMegaMenu.test.tsx b/packages/core/src/TopNav/TopNavMegaMenu.test.tsx
index 8aa951f261..0100751639 100644
--- a/packages/core/src/TopNav/TopNavMegaMenu.test.tsx
+++ b/packages/core/src/TopNav/TopNavMegaMenu.test.tsx
@@ -9,11 +9,11 @@
* SYNC: When TopNavMegaMenu changes, update tests to match new behavior
*/
-import {createRef} from 'react';
+import {createRef, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {focusOutlineStyles} from '../utils/focusOutline.stylex';
import {describe, it, expect, vi, beforeAll, afterAll, afterEach} from 'vitest';
-import {render, screen, act, fireEvent} from '@testing-library/react';
+import {render, screen, act, fireEvent, within} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {TopNavMegaMenu} from './TopNavMegaMenu';
import {TopNavMegaMenuItem} from './TopNavMegaMenuItem';
@@ -1151,3 +1151,50 @@ describe('TopNavMegaMenu ref', () => {
expect(ref.current).toBe(screen.getByRole('button', {name: /Products/}));
});
});
+
+// A slot is empty for exactly the values React renders as nothing.
+describe('TopNavMegaMenu empty slots', () => {
+ it.each([
+ ['an empty string', ''],
+ ['false', false],
+ ['true', true],
+ ['null', null],
+ ])('renders no featured wrapper in the drawer for %s', (_name, featured) => {
+ const section = (element: HTMLElement) => {
+ const trigger = within(element).getByRole('button', {name: 'Products'});
+ // Both renders share an id, so look the group up inside this render.
+ return element
+ .querySelector(`[id="${trigger.getAttribute('aria-controls')}"]`)!
+ .innerHTML.replace(/(id|aria-[a-z]+)="[^"]*"/g, '$1');
+ };
+ const {container: withFeatured} = render(
+ <TopNavRenderContext value="drawer">
+ <TopNavMegaMenu
+ label="Products"
+ items={<TopNavMegaMenuItem title="Analytics" href="/analytics" />}
+ featured={featured as ReactNode}
+ />
+ </TopNavRenderContext>,
+ );
+ const {container: without} = render(
+ <TopNavRenderContext value="drawer">
+ <TopNavMegaMenu
+ label="Products"
+ items={<TopNavMegaMenuItem title="Analytics" href="/analytics" />}
+ />
+ </TopNavRenderContext>,
+ );
+ expect(section(withFeatured)).toBe(section(without));
+ });
+
+ it('renders 0 as featured content, as React does', async () => {
+ const user = userEvent.setup();
+ render(
+ <TopNavRenderContext value="drawer">
+ <TopNavMegaMenu label="Products" featured={0} />
+ </TopNavRenderContext>,
+ );
+ await user.click(screen.getByRole('button', {name: 'Products'}));
+ expect(screen.getByText('0')).toBeInTheDocument();
+ });
+});
diff --git a/packages/core/src/TopNav/TopNavMegaMenu.tsx b/packages/core/src/TopNav/TopNavMegaMenu.tsx
index b760383550..ada98b46cb 100644
--- a/packages/core/src/TopNav/TopNavMegaMenu.tsx
+++ b/packages/core/src/TopNav/TopNavMegaMenu.tsx
@@ -38,6 +38,7 @@ import React, {
type ReactNode,
} from 'react';
import * as stylex from '@stylexjs/stylex';
+import {isRenderable} from '../utils/isRenderable';
import {
colorVars,
spacingVars,
@@ -538,14 +539,14 @@ function DefaultMegaMenu({
{...stylex.props(styles.panelContainer)}>
<div {...stylex.props(styles.panelContent)}>
{/* Menu items section */}
- {items != null && (
+ {isRenderable(items) && (
<Grid columns={2} gap={2} xstyle={styles.menuWrapper}>
{items}
</Grid>
)}
{/* Featured section */}
- {featured != null && (
+ {isRenderable(featured) && (
<div {...stylex.props(styles.featured)}>{featured}</div>
)}
</div>
@@ -630,7 +631,7 @@ function DrawerMegaMenu({
{items}
{/* Featured card */}
- {featured != null && (
+ {isRenderable(featured) && (
<div {...stylex.props(styles.drawerFeatured)}>{featured}</div>
)}
</div>
diff --git a/packages/core/src/TreeList/TreeList.test.tsx b/packages/core/src/TreeList/TreeList.test.tsx
index 531cbab4b4..740c528770 100644
--- a/packages/core/src/TreeList/TreeList.test.tsx
+++ b/packages/core/src/TreeList/TreeList.test.tsx
@@ -1735,3 +1735,64 @@ describe('TreeList', () => {
expect(items[0]).toHaveAttribute('tabindex', '0');
});
});
+
+// A slot is empty for exactly the values React renders as nothing.
+describe('TreeList empty slots', () => {
+ it.each([
+ ['an empty string', ''],
+ ['false', false],
+ ['true', true],
+ ['null', null],
+ ])(
+ 'a header of %s names nothing; the tree keeps its own label',
+ (_name, header) => {
+ render(
+ <TreeList
+ items={simpleItems}
+ header={header as ReactNode}
+ aria-label="Files"
+ />,
+ );
+ const tree = screen.getByRole('tree');
+ expect(tree).not.toHaveAttribute('aria-labelledby');
+ expect(tree).toHaveAccessibleName('Files');
+ },
+ );
+
+ it.each([
+ ['an empty string', ''],
+ ['false', false],
+ ['true', true],
+ ['null', null],
+ ])('renders no start or end wrapper for %s', (_name, value) => {
+ const html = (element: HTMLElement) =>
+ element.innerHTML.replace(/(id|aria-[a-z]+)="[^"]*"/g, '$1');
+ const {container: withSlots} = render(
+ <TreeList
+ items={[
+ {
+ id: 'a',
+ label: 'Item A',
+ startContent: value as ReactNode,
+ endContent: value as ReactNode,
+ },
+ ]}
+ />,
+ );
+ const {container: without} = render(
+ <TreeList items={[{id: 'a', label: 'Item A'}]} />,
+ );
+ expect(html(withSlots)).toBe(html(without));
+ });
+
+ it('renders 0 as a header and in the start and end slots, as React does', () => {
+ render(
+ <TreeList
+ header={0}
+ items={[{id: 'a', label: 'Item A', startContent: 0, endContent: 0}]}
+ />,
+ );
+ expect(screen.getAllByText('0')).toHaveLength(3);
+ expect(screen.getByRole('tree')).toHaveAttribute('aria-labelledby');
+ });
+});
diff --git a/packages/core/src/TreeList/TreeList.tsx b/packages/core/src/TreeList/TreeList.tsx
index 3bcaf8033f..fa2eb9602c 100644
--- a/packages/core/src/TreeList/TreeList.tsx
+++ b/packages/core/src/TreeList/TreeList.tsx
@@ -25,6 +25,7 @@ import {
type ReactNode,
} from 'react';
import * as stylex from '@stylexjs/stylex';
+import {isRenderable} from '../utils/isRenderable';
import {spacingVars} from '../theme/tokens.stylex';
import {mergeProps} from '../utils';
import type {BaseProps} from '../BaseProps';
@@ -241,6 +242,8 @@ export function TreeList({
...restProps
}: TreeListProps) {
const headerId = useId();
+ // A header that renders nothing names nothing, so the tree keeps its own label.
+ const hasHeader = isRenderable(header);
// Expanded keys from data: recomputed whenever items change.
const expandedKeysFromProps = useMemo(
@@ -597,7 +600,7 @@ export function TreeList({
style,
)}
{...restProps}>
- {header != null && (
+ {hasHeader && (
<div id={headerId} {...stylex.props(styles.header)}>
{header}
</div>
@@ -611,8 +614,8 @@ export function TreeList({
<ul
ref={treeRef}
role="tree"
- aria-label={header != null ? undefined : ariaLabel}
- aria-labelledby={header != null ? headerId : ariaLabelledby}
+ aria-label={hasHeader ? undefined : ariaLabel}
+ aria-labelledby={hasHeader ? headerId : ariaLabelledby}
onFocus={handleFocus}
{...stylex.props(styles.list)}>
{renderItems(items, 0, [])}
diff --git a/packages/core/src/TreeList/TreeListItem.tsx b/packages/core/src/TreeList/TreeListItem.tsx
index 1e2c8a588d..9d9108cee4 100644
--- a/packages/core/src/TreeList/TreeListItem.tsx
+++ b/packages/core/src/TreeList/TreeListItem.tsx
@@ -15,6 +15,7 @@
*/
import {useId, useMemo, useRef, type ReactNode} from 'react';
+import {isRenderable} from '../utils/isRenderable';
import * as stylex from '@stylexjs/stylex';
import {
colorVars,
@@ -520,6 +521,7 @@ export function TreeListItem({
)}>
{label}
</span>
+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `description` is a string field: an empty string still describes the item */}
{description != null && (
<span
id={descriptionId}
@@ -601,7 +603,7 @@ export function TreeListItem({
const innerContent = (
<>
{chevron}
- {startContent != null && (
+ {isRenderable(startContent) && (
<span {...stylex.props(styles.startContent)}>{startContent}</span>
)}
{href != null && isDisabled ? (
@@ -649,7 +651,7 @@ export function TreeListItem({
) : (
<span {...stylex.props(styles.content)}>{labelAndDescription}</span>
)}
- {endContent != null && (
+ {isRenderable(endContent) && (
<span {...stylex.props(styles.endContent)}>{endContent}</span>
)}
</>
@@ -728,7 +730,7 @@ export function TreeListItem({
<div {...stylex.props(styles.indentedContent)}>{innerContent}</div>
</div>
</div>
- {(isExpanded || isExiting) && renderedChildren != null && (
+ {(isExpanded || isExiting) && isRenderable(renderedChildren) && (
<ul
role="group"
data-tree-exiting={isExiting ? '' : undefined}
diff --git a/packages/core/src/utils/getNodeText.ts b/packages/core/src/utils/getNodeText.ts
index a4582a8254..5837951eb6 100644
--- a/packages/core/src/utils/getNodeText.ts
+++ b/packages/core/src/utils/getNodeText.ts
@@ -9,6 +9,7 @@
*/
import {isValidElement} from 'react';
+import {isRenderable} from './isRenderable';
import type {ReactNode} from 'react';
/**
@@ -17,7 +18,7 @@ import type {ReactNode} from 'react';
* props other than `children`, is not reachable and yields `''`.
*/
export function getNodeText(node: ReactNode): string {
- if (node == null || typeof node === 'boolean') {
+ if (!isRenderable(node)) {
return '';
}
if (typeof node === 'string') {