An open source design system that's fully customizable and agent ready
TypeScript
fix(core): accessible name for rich-label CheckboxList items (#3998)
Baraa Hamodi351a2f5224f9
diff --git a/.changeset/checkboxlistitem-name.md b/.changeset/checkboxlistitem-name.mdnew file mode 100644index 0000000000..85e7da9216--- /dev/null+++ b/.changeset/checkboxlistitem-name.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] CheckboxList: items with rich (non-string) labels can now provide an accessibleLabel so their checkbox no longer announces as the literal "Checkbox".++@bhamodidiff --git a/packages/core/src/CheckboxList/CheckboxList.test.tsx b/packages/core/src/CheckboxList/CheckboxList.test.tsxindex ed780c050f..8fbfa3ec95 100644--- a/packages/core/src/CheckboxList/CheckboxList.test.tsx+++ b/packages/core/src/CheckboxList/CheckboxList.test.tsx@@ -445,6 +445,71 @@ describe('CheckboxListItem standalone mode', () => {});});+describe('CheckboxListItem accessible name', () => {+ it('names the checkbox from a string label', () => {+ render(+ <CheckboxList label="Preferences" value={[]} onChange={() => {}}>+ <CheckboxListItem label="Option A" value="a" />+ </CheckboxList>,+ );+ expect(+ screen.getByRole('checkbox', {name: 'Option A'}),+ ).toBeInTheDocument();+ });++ it('names the checkbox from aria-label when the label is a ReactNode', () => {+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});+ render(+ <CheckboxList label="Plans" value={[]} onChange={() => {}}>+ <CheckboxListItem+ label={+ <span>+ Pro plan <em>(recommended)</em>+ </span>+ }+ aria-label="Pro plan"+ value="pro"+ />+ </CheckboxList>,+ );+ expect(+ screen.getByRole('checkbox', {name: 'Pro plan'}),+ ).toBeInTheDocument();+ // A named checkbox needs no dev guidance.+ expect(warnSpy).not.toHaveBeenCalled();+ warnSpy.mockRestore();+ });++ it('warns once when a ReactNode label has no aria-label', () => {+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});+ const richLabel = (+ <span>+ Pro plan <em>(recommended)</em>+ </span>+ );+ const {rerender} = render(+ <CheckboxList label="Plans" value={[]} onChange={() => {}}>+ <CheckboxListItem label={richLabel} value="pro" />+ </CheckboxList>,+ );+ // Falls back to the generic name, and tells the developer how to fix it.+ expect(+ screen.getByRole('checkbox', {name: 'Checkbox'}),+ ).toBeInTheDocument();+ expect(warnSpy).toHaveBeenCalledTimes(1);+ expect(String(warnSpy.mock.calls[0]?.[0])).toContain('aria-label');++ // Warn once per item instance — re-renders don't repeat it.+ rerender(+ <CheckboxList label="Plans" value={['pro']} onChange={() => {}}>+ <CheckboxListItem label={richLabel} value="pro" />+ </CheckboxList>,+ );+ expect(warnSpy).toHaveBeenCalledTimes(1);+ warnSpy.mockRestore();+ });+});+describe('CheckboxListItem ARIA props', () => {it('conveys checked state via the inner checkbox, not aria-checked on the listitem', () => {render(@@ -575,7 +640,7 @@ describe('CheckboxListItem ARIA props', () => {expect(changeAction).toHaveBeenCalledWith(['a']);});- it('forwards arbitrary aria attributes to the list item DOM element', () => {+ it('forwards arbitrary aria attributes to the list item, but aria-label names the checkbox', () => {render(<List><CheckboxListItem@@ -586,8 +651,13 @@ describe('CheckboxListItem ARIA props', () => {</List>,);const item = screen.getByRole('listitem');+ // Arbitrary aria-* still forward to the row...expect(item).toHaveAttribute('aria-describedby', 'help-text');- expect(item).toHaveAttribute('aria-label', 'custom label');+ // ...but aria-label names the checkbox control, not the row.+ expect(item).not.toHaveAttribute('aria-label');+ expect(+ screen.getByRole('checkbox', {name: 'custom label'}),+ ).toBeInTheDocument();});describe('disabledMessage', () => {diff --git a/packages/core/src/CheckboxList/CheckboxListItem.doc.mjs b/packages/core/src/CheckboxList/CheckboxListItem.doc.mjsindex c448e132a6..1d7ae7c743 100644--- a/packages/core/src/CheckboxList/CheckboxListItem.doc.mjs+++ b/packages/core/src/CheckboxList/CheckboxListItem.doc.mjs@@ -7,14 +7,22 @@ export const docs = {subComponentOf: 'CheckboxList',displayName: 'Checkbox List Item',isHiddenFromOverview: true,- description: 'Individual checkbox item with label, description, and end content slot. Works in collection mode (inside CheckboxList) or standalone mode (inside List).',+ description:+ 'Individual checkbox item with label, description, and end content slot. Works in collection mode (inside CheckboxList) or standalone mode (inside List).',props: [{name: 'label',- type: 'string',- description: 'Primary text label for the item.',+ type: 'ReactNode',+ description:+ 'Primary text label for the item. A plain string names the checkbox automatically; a rich (ReactNode) label should be paired with aria-label so screen readers get a concise name.',required: true,},+ {+ name: 'aria-label',+ type: 'string',+ description:+ 'Plain-text accessible name for the checkbox when label is a ReactNode. Applied to the checkbox control. Without it, rich-label items all announce as the generic "Checkbox" to screen readers.',+ },{name: 'value',type: 'string',@@ -69,13 +77,24 @@ export const docs = {description: 'Direct check handler (standalone mode only).',},],+ examples: [+ {+ label: 'Rich label with an accessible name',+ code: `<CheckboxListItem+ label={<span>Pro plan <Badge label="Recommended" /></span>}+ aria-label="Pro plan"+ value="pro"+/>`,+ },+ ],};export const docsZh = {name: 'CheckboxListItem',isHiddenFromOverview: true,displayName: 'Checkbox List Item',- description: '单个复选框选项,包含标签、描述和尾部内容插槽。可在集合模式或独立模式下使用。',+ description:+ '单个复选框选项,包含标签、描述和尾部内容插槽。可在集合模式或独立模式下使用。',props: [{name: 'label',@@ -83,6 +102,12 @@ export const docsZh = {description: '选项的主要文本标签。',required: true,},+ {+ name: 'aria-label',+ type: 'string',+ description:+ '当 label 是 ReactNode 时,复选框的纯文本无障碍名称。缺少它时,富标签选项都会向屏幕阅读器播报为通用的 "Checkbox"。',+ },{name: 'value',type: 'string',@@ -128,9 +153,12 @@ export const docsDense = {name: 'CheckboxListItem',isHiddenFromOverview: true,displayName: 'Checkbox List Item',- description: 'Individual checkbox item w/ label, description, end content slot.',+ description:+ 'Individual checkbox item w/ label, description, end content slot.',propDescriptions: {label: 'Primary text label for item.',+ 'aria-label':+ 'Plain-text checkbox name when label is a ReactNode. Without it, rich-label items all announce as "Checkbox".',value: 'Identity key (required inside CheckboxList).',description: 'Secondary text below label.',endContent: 'Content rendered after label area.',diff --git a/packages/core/src/CheckboxList/CheckboxListItem.tsx b/packages/core/src/CheckboxList/CheckboxListItem.tsxindex 23087dfeb0..37ec776d69 100644--- a/packages/core/src/CheckboxList/CheckboxListItem.tsx+++ b/packages/core/src/CheckboxList/CheckboxListItem.tsx@@ -16,7 +16,7 @@* - /packages/cli/templates/blocks/components/CheckboxList/ (showcase blocks)*/-import {use, type MouseEvent, type ReactNode} from 'react';+import {use, useEffect, useRef, type MouseEvent, type ReactNode} from 'react';import * as stylex from '@stylexjs/stylex';import type {StyleXStyles} from '@stylexjs/stylex';import {colorVars} from '../theme/tokens.stylex';@@ -51,6 +51,25 @@ export interface CheckboxListItemProps extends BaseProps<HTMLLIElement> {* child components control their own text behavior).*/label: ReactNode;+ /**+ * Plain-text accessible name for the checkbox when `label` is a ReactNode.+ *+ * A string `label` names the checkbox automatically. A rich (ReactNode)+ * `label` cannot, so pass a concise string equivalent via the standard+ * `aria-label` — otherwise the checkbox falls back to the generic name+ * "Checkbox" and every rich-label item in a list announces identically to+ * screen readers. Applied to the checkbox control, not the row.+ *+ * @example+ * ```+ * <CheckboxListItem+ * label={<span>Pro plan <Badge label="Recommended" /></span>}+ * aria-label="Pro plan"+ * value="pro"+ * />+ * ```+ */+ 'aria-label'?: string;/*** Identity key for collection mode (REQUIRED inside CheckboxList).* Throws a runtime error if missing when used inside CheckboxList.@@ -119,6 +138,7 @@ export interface CheckboxListItemProps extends BaseProps<HTMLLIElement> {*/export function CheckboxListItem({label,+ 'aria-label': ariaLabel,value,description,endContent,@@ -142,6 +162,34 @@ export function CheckboxListItem({);}+ // Accessible name for the (visually hidden) checkbox label. A string+ // `label` names it directly; a rich label needs `aria-label`.+ const checkboxLabel =+ ariaLabel ??+ (typeof label === 'string'+ ? label+ : t('@astryx.checkboxList.item.checkbox'));++ // Dev-time guardrail: a rich label without `aria-label` leaves the+ // checkbox with the generic name "Checkbox". Warn once per item instance+ // (in an effect) rather than on every render.+ const warnedUnnamedCheckboxRef = useRef(false);+ useEffect(() => {+ if (+ typeof label !== 'string' &&+ ariaLabel == null &&+ !warnedUnnamedCheckboxRef.current+ ) {+ warnedUnnamedCheckboxRef.current = true;+ console.warn(+ 'CheckboxListItem: `label` is a ReactNode, so the checkbox falls ' ++ 'back to the generic accessible name "Checkbox". Pass ' ++ '`aria-label` with a concise string equivalent of the visible ' ++ 'label so screen readers can tell items apart.',+ );+ }+ }, [label, ariaLabel]);+// Density from list context for checkbox sizingconst listCtx = use(ListContext);const density = listCtx?.density ?? 'balanced';@@ -226,11 +274,7 @@ export function CheckboxListItem({style={style}startContent={<CheckboxInput- label={- typeof label === 'string'- ? label- : t('@astryx.checkboxList.item.checkbox')- }+ label={checkboxLabel}isLabelHiddenvalue={resolvedChecked}onChange={() => handleToggle()}