An open source design system that's fully customizable and agent ready
TypeScript
fix(core A–M): slots use isRenderable; the remaining checks are allowed with a reason (#9204)
Cindy Zhang3e735de86c18
diff --git a/.changeset/slots-render-nothing-a-m.md b/.changeset/slots-render-nothing-a-m.mdnew file mode 100644index 0000000000..5b3d24b5de--- /dev/null+++ b/.changeset/slots-render-nothing-a-m.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: ChatSystemMessage's `icon`, Item's `startContent`, `endContent` and `description` (so a ListItem or menu row gains no blank lane), and MetadataList's `title` and MetadataListItem's `icon`. `0` still counts as content and shows "0".++@czarandydiff --git a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json b/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.jsonindex cd695eeda1..de758a82ce 100644--- a/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json+++ b/internal/eslint-plugin-astryx/no-nullish-jsx-guard.baseline.json@@ -1,22 +1,4 @@{- "packages/core/src/Chat/ChatLayout.tsx": ["children"],- "packages/core/src/Chat/ChatMessageList.tsx": ["children"],- "packages/core/src/Chat/ChatSystemMessage.tsx": ["icon"],- "packages/core/src/Chat/ChatToolCalls.tsx": [- "call.additions",- "call.deletions",- "call.duration",- "call.target",- "latestCall.target"- ],- "packages/core/src/Item/Item.tsx": [- "description",- "endContent",- "startContent"- ],- "packages/core/src/Markdown/Markdown.tsx": ["rendered"],- "packages/core/src/MetadataList/MetadataList.tsx": ["title"],- "packages/core/src/MetadataList/MetadataListItem.tsx": ["icon"],"packages/core/src/OverflowList/OverflowList.tsx": ["measureIndicator"],"packages/core/src/Pagination/Pagination.tsx": ["computedTotalPages"],"packages/core/src/PowerSearch/PowerSearch.tsx": ["resultCountText"],diff --git a/packages/core/src/Chat/ChatLayout.tsx b/packages/core/src/Chat/ChatLayout.tsxindex 295825a914..d239344391 100644--- a/packages/core/src/Chat/ChatLayout.tsx+++ b/packages/core/src/Chat/ChatLayout.tsx@@ -255,6 +255,7 @@ const styles = stylex.create({// =============================================================================function hasVisibleContent(children: ReactNode): boolean {+ // eslint-disable-next-line @astryx/no-nullish-jsx-guard -- component:ChatLayout FR5 names exactly null, undefined, false and an empty array as empty children; '' and true are notif (children == null || children === false) {return false;}diff --git a/packages/core/src/Chat/ChatMessageList.tsx b/packages/core/src/Chat/ChatMessageList.tsxindex c11b51c368..655f35369a 100644--- a/packages/core/src/Chat/ChatMessageList.tsx+++ b/packages/core/src/Chat/ChatMessageList.tsx@@ -254,6 +254,7 @@ export function ChatMessageList({}, [layoutContext]);const hasChildren =+ // eslint-disable-next-line @astryx/no-nullish-jsx-guard -- component:ChatMessageList FR4 shows the empty state only for nullish, false or an empty top-level array childrenchildren != null &&children !== false &&!(Array.isArray(children) && children.length === 0);diff --git a/packages/core/src/Chat/ChatSystemMessage.test.tsx b/packages/core/src/Chat/ChatSystemMessage.test.tsxindex 1d37107cd2..fb2fc35280 100644--- a/packages/core/src/Chat/ChatSystemMessage.test.tsx+++ b/packages/core/src/Chat/ChatSystemMessage.test.tsx@@ -1,6 +1,6 @@// Copyright (c) Meta Platforms, Inc. and affiliates.-import {createRef, Fragment} from 'react';+import {createRef, Fragment, type ReactNode} from 'react';import {describe, expect, it} from 'vitest';import {render, screen} from '@testing-library/react';import * as stylex from '@stylexjs/stylex';@@ -186,4 +186,25 @@ describe('ChatSystemMessage', () => {}},);++ // A slot is empty for exactly the values React renders as nothing.+ it.each([+ ['an empty string', ''],+ ['false', false],+ ['true', true],+ ['null', null],+ ])('renders no icon wrapper for %s', (_name, icon) => {+ const {container: withIcon} = render(+ <ChatSystemMessage icon={icon as ReactNode}>Notice</ChatSystemMessage>,+ );+ const {container: without} = render(+ <ChatSystemMessage>Notice</ChatSystemMessage>,+ );+ expect(withIcon.innerHTML).toBe(without.innerHTML);+ });++ it('renders 0 as an icon, as React does', () => {+ render(<ChatSystemMessage icon={0}>Notice</ChatSystemMessage>);+ expect(screen.getByText('0')).toBeInTheDocument();+ });});diff --git a/packages/core/src/Chat/ChatSystemMessage.tsx b/packages/core/src/Chat/ChatSystemMessage.tsxindex 262f030baf..d34edb3d08 100644--- a/packages/core/src/Chat/ChatSystemMessage.tsx+++ b/packages/core/src/Chat/ChatSystemMessage.tsx@@ -29,6 +29,7 @@ import {typographyVars,} from '../theme/tokens.stylex';import {mergeProps} from '../utils';+import {isRenderable} from '../utils/isRenderable';import {Divider} from '../Divider';import type {BaseProps} from '../BaseProps';import {themeProps} from '../utils/themeProps';@@ -152,7 +153,9 @@ export function ChatSystemMessage({)}{...rest}><span {...stylex.props(styles.content)}>- {icon != null && <span {...stylex.props(styles.icon)}>{icon}</span>}+ {isRenderable(icon) && (+ <span {...stylex.props(styles.icon)}>{icon}</span>+ )}{children}</span></div>diff --git a/packages/core/src/Chat/ChatToolCalls.tsx b/packages/core/src/Chat/ChatToolCalls.tsxindex b3be4fd84e..ccff909967 100644--- a/packages/core/src/Chat/ChatToolCalls.tsx+++ b/packages/core/src/Chat/ChatToolCalls.tsx@@ -465,6 +465,7 @@ function CallRow({{call.node != null && (<Badge label={call.node} variant="neutral" xstyle={styles.nodePill} />)}+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `target` is a string field: an empty string still marks a target */}{call.target != null && (<span {...stylex.props(styles.callLabel)}>{call.target}</span>)}@@ -472,11 +473,13 @@ function CallRow({call.deletions != null ||call.stats != null) && (<span {...stylex.props(styles.stats)}>+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `additions` is a count: 0 renders "+0" */}{call.additions != null && (<span {...stylex.props(styles.statsAdditions)}>+{call.additions}</span>)}+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `deletions` is a count: 0 renders "-0" */}{call.deletions != null && (<span {...stylex.props(styles.statsDeletions)}>-{call.deletions}@@ -485,6 +488,7 @@ function CallRow({{call.stats}</span>)}+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `duration` is a string field, not a slot */}{call.duration != null && status === 'complete' && (<span {...stylex.props(styles.callDuration)}>{call.duration}</span>)}@@ -657,6 +661,7 @@ export function ChatToolCalls(props: ChatToolCallsProps) {<VisuallyHidden>{latestStatusAnnouncement}</VisuallyHidden></span><span {...stylex.props(styles.callName)}>{latestCall.name}</span>+ {/* eslint-disable-next-line @astryx/no-nullish-jsx-guard -- `target` is a string field: an empty string still marks a target */}{latestCall.target != null && (<span {...stylex.props(styles.callLabel)}>{latestCall.target}diff --git a/packages/core/src/Item/Item.test.tsx b/packages/core/src/Item/Item.test.tsxindex e124c5d34b..729e46bf48 100644--- a/packages/core/src/Item/Item.test.tsx+++ b/packages/core/src/Item/Item.test.tsx@@ -9,7 +9,7 @@* SYNC: When Item component changes, update tests to match new behavior*/-import {use, useRef} from 'react';+import {use, useRef, type ReactNode} from 'react';import {afterEach,beforeAll,@@ -1543,3 +1543,68 @@ describe('swipeActions', () => {}});});++// A slot is empty for exactly the values React renders as nothing.+describe('Item empty slots', () => {+ it.each([+ ['an empty string', ''],+ ['false', false],+ ['true', true],+ ['null', null],+ ])('renders no start or end wrapper for %s', (_name, value) => {+ const {container: withSlots} = render(+ <Item+ label="Item"+ startContent={value as ReactNode}+ endContent={value as ReactNode}+ />,+ );+ const {container: without} = render(<Item label="Item" />);+ expect(withSlots.innerHTML).toBe(without.innerHTML);+ });++ it.each([+ ['false', false],+ ['true', true],+ ['null', null],+ ])('renders no description wrapper for %s', (_name, description) => {+ const {container: withDescription} = render(+ <Item label="Item" description={description as ReactNode} />,+ );+ const {container: without} = render(<Item label="Item" />);+ expect(withDescription.innerHTML).toBe(without.innerHTML);+ });++ it.each([+ ['an empty string', ''],+ ['false', false],+ ])(+ 'keeps the stacked layout for an inline row whose description is %s',+ (_name, description) => {+ const {container: withDescription} = render(+ <Item+ label="Item"+ layout="inline"+ description={description as ReactNode}+ />,+ );+ const {container: without} = render(+ <Item label="Item" layout="inline" />,+ );+ expect(withDescription.innerHTML).toBe(without.innerHTML);+ },+ );++ it('renders no description element for an empty string', () => {+ render(<Item label="Item" description="" />);+ const label = screen.getByText('Item');+ expect(label.parentElement?.children).toHaveLength(1);+ });++ it('renders 0 in the start, end and description slots, as React does', () => {+ render(+ <Item label="Item" startContent={0} endContent={0} description={0} />,+ );+ expect(screen.getAllByText('0')).toHaveLength(3);+ });+});diff --git a/packages/core/src/Item/Item.tsx b/packages/core/src/Item/Item.tsxindex c1b3e5fa97..316a0397a6 100644--- a/packages/core/src/Item/Item.tsx+++ b/packages/core/src/Item/Item.tsx@@ -718,7 +718,7 @@ export function Item({// Inline rows are one line by definition, so the description always// ellipsizes there — a ReactNode description cannot wrap the row open.- const isInline = layout === 'inline' && description != null;+ const isInline = layout === 'inline' && hasRenderableDescription;const descriptionTruncateStyle =descriptionLines != null@@ -742,9 +742,9 @@ export function Item({)}>{label}</span>- {description != null && (+ {hasRenderableDescription && (<span- id={hasRenderableDescription ? descriptionID : undefined}+ id={descriptionID}{...stylex.props(styles.description,isInline && styles.inlineDescription,@@ -772,7 +772,7 @@ export function Item({const content = (<>- {startContent != null && (+ {isRenderable(startContent) && (<span{...stylex.props(styles.startContent,@@ -830,7 +830,7 @@ export function Item({</span>)}- {endContent != null && (+ {isRenderable(endContent) && (<span{...stylex.props(styles.endContent,diff --git a/packages/core/src/Markdown/Markdown.tsx b/packages/core/src/Markdown/Markdown.tsxindex 768853b91f..f010c70f3c 100644--- a/packages/core/src/Markdown/Markdown.tsx+++ b/packages/core/src/Markdown/Markdown.tsx@@ -1592,6 +1592,7 @@ function renderBlock(}try {const rendered = renderer.render({node: proposal.node});+ // eslint-disable-next-line @astryx/no-nullish-jsx-guard -- a plugin renderer's '' is its output; only null, undefined or a boolean asks for the readable fallbackif (rendered == null || typeof rendered === 'boolean') {return fallback;}diff --git a/packages/core/src/MetadataList/MetadataList.test.tsx b/packages/core/src/MetadataList/MetadataList.test.tsxindex e72686f9b2..aa8fc3fec8 100644--- a/packages/core/src/MetadataList/MetadataList.test.tsx+++ b/packages/core/src/MetadataList/MetadataList.test.tsx@@ -1,5 +1,6 @@// Copyright (c) Meta Platforms, Inc. and affiliates.+import type {ReactNode} from 'react';import {describe, it, expect, vi} from 'vitest';import {render, screen, fireEvent} from '@testing-library/react';import userEvent from '@testing-library/user-event';@@ -380,4 +381,54 @@ describe('MetadataList pass-through props', () => {expect(rootEl).toHaveAttribute('data-columns', '2');expect(rootEl).toHaveAttribute('data-orientation', 'horizontal');});++ it.each([+ ['an empty string', ''],+ ['false', false],+ ['true', true],+ ['null', null],+ ])('renders no title wrapper for %s', (_name, title) => {+ const {container: withTitle} = render(+ <MetadataList title={title as ReactNode}>+ <MetadataListItem label="Owner">Ana</MetadataListItem>+ </MetadataList>,+ );+ const {container: without} = render(+ <MetadataList>+ <MetadataListItem label="Owner">Ana</MetadataListItem>+ </MetadataList>,+ );+ // The list's generated id differs between the two renders.+ const html = (element: HTMLElement) =>+ element.innerHTML.replace(/id="[^"]*"/g, 'id');+ expect(html(withTitle)).toBe(html(without));+ });++ it.each([+ ['an empty string', ''],+ ['false', false],+ ['true', true],+ ['null', null],+ ])('renders no item icon wrapper for %s', (_name, icon) => {+ const {container: withIcon} = render(+ <MetadataListItem label="Owner" icon={icon as ReactNode}>+ Ana+ </MetadataListItem>,+ );+ const {container: without} = render(+ <MetadataListItem label="Owner">Ana</MetadataListItem>,+ );+ expect(withIcon.innerHTML).toBe(without.innerHTML);+ });++ it('renders 0 as a title and as an item icon, as React does', () => {+ render(+ <MetadataList title={0}>+ <MetadataListItem label="Owner" icon={0}>+ Ana+ </MetadataListItem>+ </MetadataList>,+ );+ expect(screen.getAllByText('0')).toHaveLength(2);+ });});diff --git a/packages/core/src/MetadataList/MetadataList.tsx b/packages/core/src/MetadataList/MetadataList.tsxindex bf84783da8..f8dccfd027 100644--- a/packages/core/src/MetadataList/MetadataList.tsx+++ b/packages/core/src/MetadataList/MetadataList.tsx@@ -30,6 +30,7 @@ import {} from './MetadataListContext';import type {BaseProps} from '../BaseProps';import {mergeProps} from '../utils';+import {isRenderable} from '../utils/isRenderable';import {themeProps} from '../utils/themeProps';import {useTranslator} from '../i18n';@@ -238,8 +239,9 @@ export function MetadataList({? childrenArray.slice(0, effectiveMax): childrenArray;- const titleContent =- title != null ? <div {...stylex.props(styles.title)}>{title}</div> : null;+ const titleContent = isRenderable(title) ? (+ <div {...stylex.props(styles.title)}>{title}</div>+ ) : null;// Determine grid style based on columns and label positionconst getGridStyle = () => {diff --git a/packages/core/src/MetadataList/MetadataListItem.tsx b/packages/core/src/MetadataList/MetadataListItem.tsxindex 5dbb1a3737..64e0b93942 100644--- a/packages/core/src/MetadataList/MetadataListItem.tsx+++ b/packages/core/src/MetadataList/MetadataListItem.tsx@@ -27,6 +27,7 @@ import {import {MetadataListContext} from './MetadataListContext';import type {BaseProps} from '../BaseProps';import {mergeProps} from '../utils';+import {isRenderable} from '../utils/isRenderable';import {themeProps} from '../utils/themeProps';// =============================================================================@@ -153,7 +154,7 @@ export function MetadataListItem({const labelContent = (<>- {icon != null && (+ {isRenderable(icon) && (<span {...stylex.props(styles.iconWrapper)}>{icon}</span>)}{label}