An open source design system that's fully customizable and agent ready
TypeScript
fix(Table): a column-resize handle is named by a node header's visible text (#9057)
Cindy Zhangbb18c817acf8
diff --git a/.changeset/resize-handle-names-node-header.md b/.changeset/resize-handle-names-node-header.mdnew file mode 100644index 0000000000..5a76c48ed3--- /dev/null+++ b/.changeset/resize-handle-names-node-header.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A `Table` column-resize handle is named by its column's visible text when the header is a node, such as `<span>Amount (EUR)</span>`, instead of by the column key. The key remains the fallback only for a header with no text.++@cixzhang @duytbpdiff --git a/packages/core/locales/en.json b/packages/core/locales/en.jsonindex 411102378d..3db897c2fc 100644--- a/packages/core/locales/en.json+++ b/packages/core/locales/en.json@@ -803,6 +803,10 @@"defaultMessage": "No data","description": "Fallback empty-state text in the table body when there are zero rows. Neutral tone (not error-y); consumers commonly override with something specific like \"No results\"."},+ "@astryx.table.columnResize.resizeColumn": {+ "defaultMessage": "Resize column {label}",+ "description": "Accessible name for a Table column-resize handle (a vertical separator between header cells). `label` is the column header's visible text, or the column key when the header has no text."+ },"@astryx.table.filter.allPlaceholder": {"defaultMessage": "All","description": "Placeholder on a per-column filter dropdown when nothing is selected, meaning \"no filter — all rows match\". Determiner form (as in \"all values\"), not the pronoun."diff --git a/packages/core/src/Table/plugins/columnResize/useTableColumnResize.test.tsx b/packages/core/src/Table/plugins/columnResize/useTableColumnResize.test.tsxindex dabd82dd47..8df9312c00 100644--- a/packages/core/src/Table/plugins/columnResize/useTableColumnResize.test.tsx+++ b/packages/core/src/Table/plugins/columnResize/useTableColumnResize.test.tsx@@ -574,6 +574,28 @@ describe('useTableColumnResize', () => {expect(handle).toHaveAttribute('aria-label', 'Resize column Name');});+ it("names a node header's handle by the header's visible text (AR1)", () => {+ function Glyph() {+ return <svg aria-hidden />;+ }+ render(+ <ResizeTable+ columns={[+ {key: 'name', header: <span>Amount (EUR)</span>},+ {key: 'role', header: <Glyph />},+ {key: 'id', header: 'ID'},+ ]}+ />,+ );+ const [amount, role] = getResizeHandles();+ expect(amount).toHaveAttribute(+ 'aria-label',+ 'Resize column Amount (EUR)',+ );+ // A header with no text it renders through children falls back to the key.+ expect(role).toHaveAttribute('aria-label', 'Resize column role');+ });+it('handle has aria-valuemin', () => {render(<ResizeTable minWidth={80} />);const handle = getResizeHandles()[0];diff --git a/packages/core/src/Table/plugins/columnResize/useTableColumnResize.tsx b/packages/core/src/Table/plugins/columnResize/useTableColumnResize.tsxindex 1fcdad6c8e..5d380c82fc 100644--- a/packages/core/src/Table/plugins/columnResize/useTableColumnResize.tsx+++ b/packages/core/src/Table/plugins/columnResize/useTableColumnResize.tsx@@ -29,6 +29,8 @@ import {DEFAULT_MIN_COLUMN_WIDTH,} from '../../columnUtils';import {observeResize} from '../../../utils/sharedResizeObserver';+import {getNodeText} from '../../../utils/getNodeText';+import {useTranslator} from '../../../i18n';// =============================================================================// Config Type@@ -384,6 +386,7 @@ function ResizeHandle({isDraggingRef,tableRef,}: ResizeHandleProps) {+ const t = useTranslator();const setTableDragging = useCallback((dragging: boolean) => {const table = tableRef.current;@@ -743,10 +746,11 @@ function ResizeHandle({],);- const ariaLabel =- typeof columnHeader === 'string'- ? `Resize column ${columnHeader}`- : `Resize column ${columnKey}`;+ // Named by the column's visible text, also when its header is a node; the+ // key is the fallback only for a header with no reachable text (AR1).+ const ariaLabel = t('@astryx.table.columnResize.resizeColumn', {+ label: getNodeText(columnHeader) || columnKey,+ });return (<divdiff --git a/packages/core/src/Toast/ToastViewport.tsx b/packages/core/src/Toast/ToastViewport.tsxindex a1dfdd9ff6..58d31223d5 100644--- a/packages/core/src/Toast/ToastViewport.tsx+++ b/packages/core/src/Toast/ToastViewport.tsx@@ -15,7 +15,6 @@*/import {- isValidElement,useCallback,useEffect,useLayoutEffect,@@ -28,6 +27,7 @@ import {createPortal} from 'react-dom';import * as stylex from '@stylexjs/stylex';import {spacingVars, durationVars, easeVars} from '../theme/tokens.stylex';import {mergeProps} from '../utils';+import {getNodeText} from '../utils/getNodeText';import {INTERACTIVE_SELECTORS} from '../hooks/useClickableContainer';import {useAnnounce} from '../hooks/useAnnounce';import {layerTextReset} from '../Layer/layerTextReset.stylex';@@ -172,29 +172,6 @@ const styles = stylex.create({},});-// Flatten a toast's rendered content (title, description, etc.) to the plain-// text that should be spoken by a screen reader. Only text is announced —-// interactive endContent is deliberately excluded (it is reachable via F6).-function getNodeText(node: ReactNode): string {- if (node == null || typeof node === 'boolean') {- return '';- }- if (typeof node === 'string') {- return node;- }- if (typeof node === 'number') {- return String(node);- }- if (Array.isArray(node)) {- return node.map(getNodeText).filter(Boolean).join(' ');- }- if (isValidElement(node)) {- const {children} = node.props as {children?: ReactNode};- return getNodeText(children);- }- return '';-}-export interface ToastViewportProps {position?: ToastPosition;/** Maximum number of visible toasts. @default 5 */@@ -438,6 +415,7 @@ export function ToastViewport({) {return;}+ // Only text is announced; interactive endContent is reachable via F6.const text = getNodeText(entry.options.body);if (text) {// Error toasts map to the assertive region (role="alert"); everythingdiff --git a/packages/core/src/utils/getNodeText.test.ts b/packages/core/src/utils/getNodeText.test.tsnew file mode 100644index 0000000000..802f67479c--- /dev/null+++ b/packages/core/src/utils/getNodeText.test.ts@@ -0,0 +1,24 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {createElement} from 'react';+import {describe, expect, it} from 'vitest';++import {getNodeText} from './getNodeText';++describe('getNodeText', () => {+ it('reads strings, numbers and element children, joined with spaces', () => {+ expect(getNodeText('Name')).toBe('Name');+ expect(getNodeText(42)).toBe('42');+ expect(getNodeText(createElement('span', null, 'Amount (EUR)'))).toBe(+ 'Amount (EUR)',+ );+ expect(getNodeText(['Total', createElement('b', null, 3)])).toBe('Total 3');+ });++ it('yields nothing for empty nodes and text a component renders from other props', () => {+ for (const node of [null, undefined, true, false, []])+ {expect(getNodeText(node)).toBe('');}+ const Labelled = ({label}: {label: string}) => label;+ expect(getNodeText(createElement(Labelled, {label: 'Hidden'}))).toBe('');+ });+});diff --git a/packages/core/src/utils/getNodeText.ts b/packages/core/src/utils/getNodeText.tsnew file mode 100644index 0000000000..a4582a8254--- /dev/null+++ b/packages/core/src/utils/getNodeText.ts@@ -0,0 +1,37 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file getNodeText.ts+ * @input Receives a React node+ * @output Exports getNodeText, the node's plain text+ * @position Shared utility; consumed where a rendered node also needs a+ * spoken name (Toast announcements, Table column-resize handle names)+ */++import {isValidElement} from 'react';+import type {ReactNode} from 'react';++/**+ * The plain text a node renders through strings, numbers and element+ * children, joined with spaces. Text a component renders internally, from+ * props other than `children`, is not reachable and yields `''`.+ */+export function getNodeText(node: ReactNode): string {+ if (node == null || typeof node === 'boolean') {+ return '';+ }+ if (typeof node === 'string') {+ return node;+ }+ if (typeof node === 'number') {+ return String(node);+ }+ if (Array.isArray(node)) {+ return node.map(getNodeText).filter(Boolean).join(' ');+ }+ if (isValidElement(node)) {+ const {children} = node.props as {children?: ReactNode};+ return getNodeText(children);+ }+ return '';+}