Skip to content

feat(core): add useTableRowExpansion plugin - #3441

Merged
cixzhang merged 2 commits into
meta:mainfrom
humbertovirtudes:navi/feat/table-row-expansion
Jul 6, 2026
Merged

cixzhang merged 2 commits into
meta:mainfrom
humbertovirtudes:navi/feat/table-row-expansion

Conversation

@humbertovirtudes

@humbertovirtudes humbertovirtudes commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds useTableRowExpansion — expand/collapse tree rows inline. Uses the inherited-columns pattern: child rows use the same columns as their parents, indented by depth (like a file tree). Clicking the chevron, the header expand-all toggle, or right-clicking → "Expand/Collapse row" toggles a row's children.

const [expandedKeys, setExpandedKeys] = useState<Set<string>>(new Set());

const {data, expansionConfig} = useTableRowExpansionState<Row>({
  baseData: tree,
  getChildren: item => item.children ?? [],
  getRowKey: item => item.id,
  expandedKeys,
  setExpandedKeys,
});

const expansion = useTableRowExpansion(expansionConfig);

<Table data={data} columns={columns} idKey="id" plugins={{expansion}} />;

Design

  • Headless: the consumer owns expandedKeys state. useTableRowExpansionState mirrors useTableSelectionState's footprint — it takes expandedKeys + setExpandedKeys and returns {data, expansionConfig}, deriving the flattened rows, per-row depth, expand/collapse handlers, and the expand-all state. Consumers no longer hand-roll the allExpandableKeys / isAllExpanded / onToggleExpandAll boilerplate.
  • Injects a __expansion chevron column; transformColumns wraps the first user column's renderCell to indent nested rows and render their inline chevron.
  • Optional expand-all header toggle (isAllExpanded + onToggleExpandAll, both derived by the state hook).
  • Optional hasRowClickExpansion to toggle by clicking anywhere in the row.
  • Optional getIsItemExpandable to gate which rows are expandable (e.g. folders only) — also scopes expand-all.
  • Contributes an Expand/Collapse row context-menu action on every cell of an expandable row.

Full-cell context menu fix

While wiring the context-menu action, right-click only opened near a cell's content — right-clicking a cell's padding (or a narrow cell) fell through to the browser's native menu. Fixed by making the context-menu trigger fill the entire cell:

  • Added a triggerXstyle prop to ContextMenu (styles the right-click target wrapper).
  • TableCell relocates its density padding onto the trigger and stretches it to fill the cell (inline-size/block-size: 100%, with block-size: 100% on the <td>), so a right-click anywhere in the cell — content and padding, edge to edge — opens the menu.

Validation

  • pnpm -F @astryxdesign/core build ✅, lint ✅ (0 errors)
  • Full Table + ContextMenu suites pass (175 tests), including new tests for expand-all / collapse-all and the full-cell trigger regression.
  • Storybook stories: InheritedColumns, LeafNodesNotExpandable, ExpandOnRowClick (all now use the simplified state API).
  • Docsite: useTableRowExpansion hook page + example block + doc.

Notes

  • Plugin config props follow the API conventions (is* state, has* toggle, on* handlers). expandOnRowClick was renamed to hasRowClickExpansion per the boolean-naming guidance.
  • Indentation is a fixed 24px per depth level (kept simple; no config override).

@vercel

vercel Bot commented Jul 2, 2026

Copy link
Copy Markdown

@humbertovirtudes is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 2, 2026
@humbertovirtudes
humbertovirtudes force-pushed the navi/feat/table-row-expansion branch from b464faf to f1cb0cf Compare July 3, 2026 15:19
@humbertovirtudes
humbertovirtudes marked this pull request as ready for review July 3, 2026 15:47
@vercel

vercel Bot commented Jul 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview, Comment Jul 6, 2026 3:32pm

Request Review

@cixzhang

cixzhang commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Nice! I'm excited to get this feature in OSS!

Small bug: the context menu seems to be broken when expand on row click is enabled. The context menu works in the other examples though so likely an issue with how we're resolving the combination of these features.

Screen.Recording.2026-07-03.at.6.33.45.PM.mov

Observing the code written in storybook, there's quite a lot of extra code for managing the "allExpanded" behavior. IIRC we have some utilities for this internally so I wonder if we can make that better here too. For example, these lines are repeated:

    // Compute expand-all state: collect all expandable keys from the full tree.
    const allExpandableKeys = useMemo(() => {
      const keys: string[] = [];
      function walk(items: FileNode[]) {
        for (const item of items) {
          const children = item.children ?? [];
          if (children.length > 0) {
            keys.push(item.id);
            walk(children);
          }
        }
      }
      walk(fileTree);
      return keys;
    }, []);
    const isAllExpanded = allExpandableKeys.length > 0 && allExpandableKeys.every(k => expandedKeys.has(k)) ? true : expandedKeys.size > 0 ? 'indeterminate' as const : false;
    const handleToggleExpandAll = useCallback((expand: boolean) => {
      if (expand) {
        setExpandedKeys(new Set(allExpandableKeys));
      } else {
        setExpandedKeys(new Set());
      }
    }, [allExpandableKeys]);

For the expansion state plugin if we can match similar API footprint as useTableSelectionState that would help keep these shapes consistent. You may also want to run the current API through the API Guidance wiki page in our github to catch any other issues like boolean prop naming conventions.

@cixzhang cixzhang left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! Back to you for the comment.

@humbertovirtudes
humbertovirtudes marked this pull request as draft July 4, 2026 06:01
Expand/collapse tree rows inline using the inherited-columns pattern —
child rows use the same columns as their parents, indented by depth.

- Headless: consumer owns expandedKeys state; plugin provides the UI
  (chevron column, indentation, inline chevrons for nested rows).
- useTableRowExpansionState helper flattens a tree into the visible rows.
- Optional expand-all header toggle (isAllExpanded + onToggleExpandAll).
- Optional expandOnRowClick to toggle by clicking anywhere in a row.
- Contributes an Expand/Collapse context-menu action on every cell.
- Storybook stories, docsite example block + doc, and unit tests.
…ext menu

Addresses review feedback on useTableRowExpansion:

- useTableRowExpansionState now mirrors useTableSelectionState: it takes
  expandedKeys + setExpandedKeys and returns {data, expansionConfig}, deriving
  the expand/collapse handlers and expand-all state internally. Consumers no
  longer hand-roll allExpandableKeys / isAllExpanded / onToggleExpandAll.
- Renamed expandOnRowClick -> hasRowClickExpansion (has-prefix per the API
  conventions for feature toggles).
- Fix: right-click context menu now opens across the entire cell. The trigger
  wrapper fills the cell and carries the density padding (relocated off the
  <td>), so right-clicking near a cell/row edge no longer falls through to the
  browser's native menu. Added a triggerXstyle prop to ContextMenu.
- Simplified all stories, the docsite block, and tests to the new state API;
  added expand-all / collapse-all and full-cell trigger regression tests.
@humbertovirtudes
humbertovirtudes force-pushed the navi/feat/table-row-expansion branch from 8dc68f1 to 65b2e93 Compare July 5, 2026 04:12
@humbertovirtudes
humbertovirtudes marked this pull request as ready for review July 5, 2026 05:59
@github-actions
github-actions Bot requested a review from cixzhang July 5, 2026 06:00
@humbertovirtudes

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both addressed in the latest push:

Context menu bug — the trigger only wrapped the cell content, so right-clicking a cell's padding (or a narrow cell) fell through to the browser's native menu. Fixed by making the trigger fill the whole cell (density padding relocated onto the trigger, 100% inline/block size), so a right-click anywhere in the cell — edge to edge — opens the menu. Added a triggerXstyle prop to ContextMenu for this.

Expand-all boilerplate — moved into useTableRowExpansionState, matching the useTableSelectionState footprint. It now takes expandedKeys + setExpandedKeys and returns {data, expansionConfig}, deriving onToggle / getDepth / isAllExpanded / onToggleExpandAll internally. The stories dropped from ~40 lines of setup to ~10:

const {data, expansionConfig} = useTableRowExpansionState({
  baseData: tree, getChildren, getRowKey, expandedKeys, setExpandedKeys,
});
const expansion = useTableRowExpansion(expansionConfig);

Also ran the API through the conventions page and renamed expandOnRowClick → hasRowClickExpansion (has-prefix for feature toggles).

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great turnaround — thank you Humberto. The useTableRowExpansionState hook mirrors useTableSelectionState nicely and takes all that expand-all/tree-walking boilerplate out of the consumer, and the full-cell right-click fix is clean. Excited to get this in.

One note, not blocking: triggerXstyle on ContextMenu is a new public escape hatch, and it's a little at odds with the rest of the Layer family — HoverCard and Tooltip intentionally use a display: contents trigger wrapper and don't expose trigger styling (the trigger is meant to be the child). Since the only consumer here is TableCell, I'd rather not grow ContextMenu's public API for it long-term. Let's land this as-is so the fix ships, and I'll file a follow-up to migrate ContextMenu's trigger to display: contents like Tooltip/HoverCard — best done alongside the positioning rework in #3465, since both touch how the trigger/menu anchor. Happy to pair on that.

This branch was successfully deployed

1 active deployment
Preview — 65b2e936 Deployed Jul 6, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants