Skip to content

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

TypeScript

docs(Table): a block that puts row selection in each row's leading mark (#10395)

Cindy Zhangfed78ba24749
diff --git a/.changeset/table-leading-mark-selection.md b/.changeset/table-leading-mark-selection.md
new file mode 100644
index 0000000000..60dbd0c68a
--- /dev/null
+++ b/.changeset/table-leading-mark-selection.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[docs] New block, "Table — Selection in the Leading Mark": row selection with no checkbox column. Each row shows its avatar, and the checkbox takes the avatar's slot while a mouse is over it, on keyboard focus, and on every row once anything is selected. On touch screens the checkbox always shows. It uses the existing `useTableSelectionState` and renders the checkboxes in the name column, so no new Table API is involved.
+
+@cixzhang
diff --git a/apps/sandbox/scripts/next-export-routes.json b/apps/sandbox/scripts/next-export-routes.json
index 6f58e5cbe3..b032dae38e 100644
--- a/apps/sandbox/scripts/next-export-routes.json
+++ b/apps/sandbox/scripts/next-export-routes.json
@@ -602,6 +602,7 @@
"/templates/TableGroupedRowsTable/",
"/templates/TableInCard/",
"/templates/TableInlineFilterTable/",
+ "/templates/TableLeadingMarkSelection/",
"/templates/TablePaginatedTable/",
"/templates/TableResizableTable/",
"/templates/TableRichCellTable/",
diff --git a/internal/shadcn-registry/routes.lock.json b/internal/shadcn-registry/routes.lock.json
index 8a7022a404..a1f8877aa2 100644
--- a/internal/shadcn-registry/routes.lock.json
+++ b/internal/shadcn-registry/routes.lock.json
@@ -3891,6 +3891,11 @@
"path": "examples/use-table-row-status/semantic-and-custom-markers",
"aliases": []
},
+ {
+ "name": "example-use-table-selection-state-table-selection-in-the-leading-mark",
+ "path": "examples/use-table-selection-state/table-selection-in-the-leading-mark",
+ "aliases": []
+ },
{
"name": "example-use-table-selection-table-bulk-actions",
"path": "examples/use-table-selection/table-bulk-actions",
diff --git a/packages/cli/api/build/tree.test.mjs b/packages/cli/api/build/tree.test.mjs
index e04a8e2bce..e8dc501df5 100644
--- a/packages/cli/api/build/tree.test.mjs
+++ b/packages/cli/api/build/tree.test.mjs
@@ -120,7 +120,7 @@ const PINNED = [
'TableGridDividersTable',
'TableGroupedRowsTable',
'TableShowcase',
- 'TableStripedTable',
+ 'TableLeadingMarkSelection',
],
],
[
diff --git a/packages/cli/assets/templates/blocks/components/Table/TableLeadingMarkSelection.doc.mjs b/packages/cli/assets/templates/blocks/components/Table/TableLeadingMarkSelection.doc.mjs
new file mode 100644
index 0000000000..325ea79d54
--- /dev/null
+++ b/packages/cli/assets/templates/blocks/components/Table/TableLeadingMarkSelection.doc.mjs
@@ -0,0 +1,15 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
+export const doc = {
+ type: 'block',
+ exampleFor: 'useTableSelectionState',
+ alsoExampleFor: ['Table'],
+ name: 'Table — Selection in the Leading Mark',
+ displayName: 'Table — Selection in the Leading Mark',
+ description:
+ 'Selection without a checkbox column: each item shows its avatar, and the checkbox takes the avatar slot while a mouse is over it, on keyboard focus, and everywhere once anything is selected; on touch screens it always shows. The state comes from useTableSelectionState, and the checkboxes render in the name column instead of the useTableSelection column, so checked items get no aria-selected or highlight; add them if needed.',
+ isReady: true,
+ aspectRatio: 16 / 9,
+ componentsUsed: ['Table', 'CheckboxInput', 'Avatar'],
+};
diff --git a/packages/cli/assets/templates/blocks/components/Table/TableLeadingMarkSelection.tsx b/packages/cli/assets/templates/blocks/components/Table/TableLeadingMarkSelection.tsx
new file mode 100644
index 0000000000..301df7d838
--- /dev/null
+++ b/packages/cli/assets/templates/blocks/components/Table/TableLeadingMarkSelection.tsx
@@ -0,0 +1,134 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+import {useState} from 'react';
+import * as stylex from '@stylexjs/stylex';
+import {Avatar} from '@astryxdesign/core/Avatar';
+import {CheckboxInput} from '@astryxdesign/core/CheckboxInput';
+import {HStack} from '@astryxdesign/core/HStack';
+import {useContainerReveal} from '@astryxdesign/core/hooks';
+import {
+ Table,
+ proportional,
+ useTableSelectionState,
+} from '@astryxdesign/core/Table';
+import type {TableColumn} from '@astryxdesign/core/Table';
+import {colorVars, spacingVars} from '@astryxdesign/core/theme/tokens.stylex';
+import {mergeProps} from '@astryxdesign/core/utils';
+
+interface Automation extends Record<string, unknown> {
+ id: string;
+ name: string;
+ owner: string;
+ schedule: string;
+}
+
+const automations: Automation[] = [
+ {id: '1', name: 'Nightly backup', owner: 'Alice', schedule: 'Daily, 2:00'},
+ {id: '2', name: 'Weekly digest', owner: 'Bob', schedule: 'Mondays, 9:00'},
+ {id: '3', name: 'Cache warmup', owner: 'Diana', schedule: 'Hourly'},
+ {id: '4', name: 'Usage report', owner: 'Eve', schedule: 'Monthly, 1st'},
+];
+
+const styles = stylex.create({
+ mark: {
+ display: 'grid',
+ placeItems: 'center',
+ flexShrink: 0,
+ },
+ // A flex layer is exactly as tall as the 24px avatar; a block layer
+ // follows the taller line box and lets the avatar's edge show under the
+ // checkbox.
+ layer: {
+ gridArea: '1 / 1',
+ display: 'flex',
+ },
+ // The checkbox covers the avatar while it shows, so the two never read as
+ // one mark.
+ checkbox: {
+ borderRadius: '50%',
+ backgroundColor: colorVars['--color-background-surface'],
+ },
+ name: {
+ paddingInlineStart: spacingVars['--spacing-1'],
+ },
+});
+
+export default function TableLeadingMarkSelection() {
+ const [selectedKeys, setSelectedKeys] = useState<Set<string>>(new Set());
+ // The mark reveals its checkbox on hover and keyboard focus, and always
+ // shows it on touch.
+ const {getContainerProps, getContentRevealProps} = useContainerReveal();
+ const {selectionConfig, selectionState} = useTableSelectionState<Automation>({
+ data: automations,
+ idKey: 'id',
+ selectedKeys,
+ setSelectedKeys,
+ });
+
+ const columns: TableColumn<Automation>[] = [
+ {
+ key: 'name',
+ header: (
+ <HStack gap={2} vAlign="center">
+ <CheckboxInput
+ label="Select all automations"
+ isLabelHidden
+ value={
+ selectionConfig.getIsAllSelected()
+ ? true
+ : selectionConfig.getIsIndeterminate?.()
+ ? 'indeterminate'
+ : false
+ }
+ onChange={checked =>
+ selectionConfig.onSelectAll({isAllSelected: checked})
+ }
+ />
+ Automation
+ </HStack>
+ ),
+ width: proportional(2),
+ renderCell: item => {
+ const isSelected = selectionConfig.getIsItemSelected(item);
+ return (
+ <HStack gap={2} vAlign="center">
+ <span
+ {...mergeProps(stylex.props(styles.mark), getContainerProps())}>
+ {selectionState.hasSelection ? null : (
+ <span {...stylex.props(styles.layer)}>
+ <Avatar name={item.owner} size="sm" tooltip={false} />
+ </span>
+ )}
+ <span
+ {...mergeProps(
+ stylex.props(styles.layer, styles.checkbox),
+ getContentRevealProps({
+ isLayoutPreserved: true,
+ forceVisibility: selectionState.hasSelection
+ ? 'shown'
+ : undefined,
+ }),
+ )}>
+ <CheckboxInput
+ label={`Select ${item.name}`}
+ isLabelHidden
+ value={isSelected}
+ onChange={checked =>
+ selectionConfig.onSelectItem({item, isSelected: checked})
+ }
+ />
+ </span>
+ </span>
+ <span {...stylex.props(styles.name)}>{item.name}</span>
+ </HStack>
+ );
+ },
+ },
+ {key: 'owner', header: 'Owner', width: proportional(1)},
+ {key: 'schedule', header: 'Schedule', width: proportional(1)},
+ ];
+
+ return <Table data={automations} columns={columns} idKey="id" />;
+}