Skip to content

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

TypeScript

fix(core): ReactNode slots use isRenderable for 'renders nothing' (#9160)

Cindy Zhang7ef23f2c31a1
diff --git a/.changeset/slots-render-nothing-consistently.md b/.changeset/slots-render-nothing-consistently.md
new file mode 100644
index 0000000000..21c5aeacee
--- /dev/null
+++ b/.changeset/slots-render-nothing-consistently.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `DropdownMenuGroup`'s `title` and `Badge`'s `label` now use the shared `isRenderable` check, so `null`, `undefined`, `true`, `false` and `''` count as rendering nothing, and `0` still counts as content. A `DropdownMenuGroup` with `title={true}` no longer renders an empty heading that names the group. `Badge` is unchanged (thanks @vjeux, #8236). Other slots are unchanged.
+
+@cixzhang @duytbp
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 9a30b4ae8d..cd695eeda1 100644
--- a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json
+++ b/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json
@@ -1,5 +1,4 @@
{
- "packages/core/src/Badge/Badge.tsx": ["label"],
"packages/core/src/Chat/ChatLayout.tsx": ["children"],
"packages/core/src/Chat/ChatMessageList.tsx": ["children"],
"packages/core/src/Chat/ChatSystemMessage.tsx": ["icon"],
@@ -10,7 +9,6 @@
"call.target",
"latestCall.target"
],
- "packages/core/src/DropdownMenu/DropdownMenuGroup.tsx": ["title"],
"packages/core/src/Item/Item.tsx": [
"description",
"endContent",
diff --git a/packages/core/src/Badge/Badge.test.tsx b/packages/core/src/Badge/Badge.test.tsx
index 21f287ca82..70de7c77d4 100644
--- a/packages/core/src/Badge/Badge.test.tsx
+++ b/packages/core/src/Badge/Badge.test.tsx
@@ -1,6 +1,7 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
import {describe, it, expect} from 'vitest';
+import {createElement, Fragment, type ReactNode} from 'react';
import {render, screen} from '@testing-library/react';
import {Badge} from './Badge';
import {getForcedColorsRules} from '../__tests__/forcedColors';
@@ -104,6 +105,27 @@ describe('Badge', () => {
});
});
+ it('grows with its label only when the label renders something', () => {
+ const classOf = (label: ReactNode) =>
+ render(<Badge label={label} />).container.firstElementChild!.className;
+ const empty = classOf('');
+ const labelled = classOf('New');
+ expect(labelled).not.toBe(empty);
+ // React renders nothing for these, so the badge keeps its fixed height.
+ for (const [index, label] of [null, undefined, true, false, ''].entries()) {
+ expect(classOf(label), `nothing ${index}`).toBe(empty);
+ }
+ // These render (0 shows "0"); a container counts without looking inside.
+ for (const [index, label] of [
+ 0,
+ 'x',
+ [],
+ createElement(Fragment),
+ ].entries()) {
+ expect(classOf(label), `renders ${index}`).toBe(labelled);
+ }
+ });
+
it('renders astryx-* class names for theme targeting', () => {
const {container} = render(<Badge variant="success" label="Active" />);
const root = container.firstElementChild!;
diff --git a/packages/core/src/Badge/Badge.tsx b/packages/core/src/Badge/Badge.tsx
index 0b57ce8f55..c9e7f9bedc 100644
--- a/packages/core/src/Badge/Badge.tsx
+++ b/packages/core/src/Badge/Badge.tsx
@@ -26,6 +26,7 @@ import {
} from '../theme/tokens.stylex';
import {mergeProps} from '../utils';
import {themeProps} from '../utils/themeProps';
+import {isRenderable} from '../utils/isRenderable';
import type {BadgeVariantMap} from './index';
/**
@@ -249,7 +250,7 @@ export function Badge({
? label
: undefined;
// Whether the label renders anything, which is when the badge grows with it.
- const hasLabel = label !== '' && label != null && typeof label !== 'boolean';
+ const hasLabel = isRenderable(label);
return (
<span
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
index 2d9f2319dd..0d4b6c73a4 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -1641,6 +1641,41 @@ describe('DropdownMenuItem host attributes', () => {
});
describe('DropdownMenuGroup (compound mode)', () => {
+ it('renders a heading only for a title that renders something', () => {
+ const titles = ['', false, true, null, 0] as const;
+ render(
+ <>
+ <DropdownMenuGroup title="" data-testid="g0">
+ <DropdownMenuItem label="Row 0" onClick={() => {}} />
+ </DropdownMenuGroup>
+ <DropdownMenuGroup title={false} data-testid="g1">
+ <DropdownMenuItem label="Row 1" onClick={() => {}} />
+ </DropdownMenuGroup>
+ <DropdownMenuGroup title={true} data-testid="g2">
+ <DropdownMenuItem label="Row 2" onClick={() => {}} />
+ </DropdownMenuGroup>
+ <DropdownMenuGroup title={null} data-testid="g3">
+ <DropdownMenuItem label="Row 3" onClick={() => {}} />
+ </DropdownMenuGroup>
+ <DropdownMenuGroup title={0} data-testid="g4">
+ <DropdownMenuItem label="Row 4" onClick={() => {}} />
+ </DropdownMenuGroup>
+ </>,
+ );
+ // '', false, true and null render nothing, so no empty heading names the
+ // group.
+ for (const index of [0, 1, 2, 3]) {
+ expect(
+ screen.getByTestId(`g${index}`),
+ String(titles[index]),
+ ).not.toHaveAttribute('aria-labelledby');
+ }
+ // 0 renders "0" and names the group.
+ expect(screen.getByRole('group', {name: '0'})).toBe(
+ screen.getByTestId('g4'),
+ );
+ });
+
it('a focusable node in the heading is not reachable by the arrow keys', async () => {
// The public ReactNode type permits a focusable descendant, but the menu's
// roving focus model cannot reach it. Pin that shipped gap while the public
diff --git a/packages/core/src/DropdownMenu/DropdownMenuGroup.tsx b/packages/core/src/DropdownMenu/DropdownMenuGroup.tsx
index f5c59392de..c2b7c2253d 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuGroup.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuGroup.tsx
@@ -35,6 +35,7 @@ import {
import type {BaseProps} from '../BaseProps';
import {mergeProps} from '../utils';
import {themeProps} from '../utils/themeProps';
+import {isRenderable} from '../utils/isRenderable';
const styles = stylex.create({
heading: {
@@ -98,7 +99,7 @@ export function DropdownMenuGroup({
...rest
}: DropdownMenuGroupProps) {
const headingId = useId();
- const hasTitle = title != null && title !== '' && title !== false;
+ const hasTitle = isRenderable(title);
return (
<div