@deque/cauldron-react 7.6.0-canary.f34365cc → 7.6.0-canary.f3f1b3ab

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cauldron.css CHANGED
@@ -27,7 +27,6 @@
27
27
  }
28
28
 
29
29
  /* Dark Theme */
30
-
31
30
  .cauldron--theme-dark {
32
31
  --expandcollapse-trigger-color: var(--white);
33
32
  --expandcollapse-panel-color: var(--accent-light);
@@ -38,7 +37,6 @@
38
37
  animation: none !important;
39
38
  }
40
39
  }
41
-
42
40
  a.Button--primary,
43
41
  a.Button--secondary,
44
42
  a.Button--thin {
@@ -53,7 +51,6 @@ a.Button--secondary {
53
51
  a.Button--thin {
54
52
  padding: 0 var(--space-small);
55
53
  }
56
-
57
54
  .Icon {
58
55
  display: inline-block;
59
56
  vertical-align: middle;
@@ -66,7 +63,6 @@ a.Button--thin {
66
63
  }
67
64
 
68
65
  /* Icon--down is the default orientation */
69
-
70
66
  .Icon__down {
71
67
  }
72
68
 
@@ -14,15 +14,6 @@ interface CascadeOptions {
14
14
  cascadeSelect: boolean;
15
15
  cascadeDeselect: boolean;
16
16
  }
17
- /**
18
- * Toggle a single node, applying cascade per the options (multiple mode only):
19
- * - selecting + `cascadeSelect`: also selects all (non-disabled) descendants.
20
- * - deselecting + `cascadeDeselect`: also deselects all descendants.
21
- *
22
- * In `single` mode only the clicked node is selected (clicking it again clears
23
- * it); cascade does not apply.
24
- */
25
- export declare function toggleSelection(nodes: TreeViewNode[], previous: Set<Key>, key: Key, selectionMode: 'single' | 'multiple', { cascadeSelect, cascadeDeselect }: CascadeOptions): Set<Key>;
26
17
  /**
27
18
  * Apply react-aria's proposed selection while enforcing cascade. Each key
28
19
  * react-aria added cascades to its descendants when `cascadeSelect` is set; each
@@ -2,8 +2,17 @@ import React from 'react';
2
2
  import { Cauldron } from '../../types';
3
3
  import { TreeViewNode } from './types';
4
4
  export type { TreeViewNode } from './types';
5
- type TreeViewProps = Cauldron.LabelProps & {
5
+ /** Seeds the initial scroll math. Keep in step with `--tree-view-row-height`,
6
+ * which sizes a virtualized row — see the spacing-parity test. */
7
+ export declare const ESTIMATED_ROW_HEIGHT = 36;
8
+ /** Keep in step with `--space-quarter` / `--space-half`, which `.TreeView` uses
9
+ * for its flex spacing — see the spacing-parity test. */
10
+ export declare const ROW_GAP = 2;
11
+ export declare const LIST_PADDING = 4;
12
+ type TreeViewProps = Cauldron.LabelProps & Omit<React.HTMLAttributes<HTMLDivElement>, 'children' | 'role' | 'dangerouslySetInnerHTML'> & {
6
13
  items: TreeViewNode[];
14
+ /** Runs when an item is activated by a click, Space or Enter. Each of these
15
+ * also toggles the item's selection. Without selection, Space does not act. */
7
16
  onAction?: (key: string) => void;
8
17
  selectionMode?: 'none' | 'single' | 'multiple';
9
18
  /** When true (multiple selection only), selecting a parent also selects all
@@ -13,7 +22,15 @@ type TreeViewProps = Cauldron.LabelProps & {
13
22
  * all of its descendants. */
14
23
  cascadeDeselect?: boolean;
15
24
  defaultExpandedKeys?: string[];
16
- className?: string;
25
+ /** Render only the rows in view. Use for long lists, where rendering every
26
+ * row makes selection changes slow enough to look frozen. Needs a height —
27
+ * from `height`, `style`, or CSS — or no rows render. Toggling this at
28
+ * runtime remounts the tree, which resets expansion and scroll position. */
29
+ virtualized?: boolean;
30
+ /** Height of the scroll region, taking precedence over `style.height`. Only
31
+ * applies when `virtualized` is set. A relative unit (`"20rem"`) keeps the
32
+ * visible row count stable at zoom. */
33
+ height?: number | string;
17
34
  };
18
35
  declare const TreeView: React.ForwardRefExoticComponent<TreeViewProps & React.RefAttributes<HTMLDivElement>>;
19
36
  export default TreeView;
package/lib/index.js CHANGED
@@ -517,7 +517,7 @@ var Accordion = function (_a) {
517
517
  var panelProps = panelElement.props;
518
518
  return (React__default["default"].createElement("div", tslib.__assign({ className: "Accordion" }, props),
519
519
  React__default["default"].createElement(ExpandCollapsePanel, tslib.__assign({ id: panelProps.id || "".concat(elementId, "-panel"), open: open, onToggle: onToggle, animationTiming: animationTiming }, panelProps),
520
- React__default["default"].createElement(PanelTrigger$1, tslib.__assign({ iconCollapsed: "triangle-right", iconExpanded: "triangle-down", className: classNames__default["default"]('Accordion__trigger', trigger.props.className), "aria-controls": panelProps.id || "".concat(elementId, "-panel"), heading: trigger.props.heading }, trigger.props), trigger),
520
+ React__default["default"].createElement(PanelTrigger$1, tslib.__assign({ iconCollapsed: "triangle-right", iconExpanded: "triangle-down", "aria-controls": panelProps.id || "".concat(elementId, "-panel"), heading: trigger.props.heading }, trigger.props, { className: classNames__default["default"]('Accordion__trigger', trigger.props.className) }), trigger),
521
521
  panelElement)));
522
522
  };
523
523
  Accordion.displayName = 'Accordion';
@@ -1420,7 +1420,7 @@ function createFocusTrap(targetElement, initialFocusElement) {
1420
1420
  suspended: false
1421
1421
  };
1422
1422
  var focusListener = function (event) {
1423
- var _a, _b, _c, _d;
1423
+ var _a, _b;
1424
1424
  var eventTarget = event.target;
1425
1425
  var elementContainsTarget = elementContains(targetElement, eventTarget);
1426
1426
  if (focusTrapMetadata.suspended) {
@@ -1438,24 +1438,26 @@ function createFocusTrap(targetElement, initialFocusElement) {
1438
1438
  return;
1439
1439
  }
1440
1440
  var focusableElements = Array.from((targetElement === null || targetElement === void 0 ? void 0 : targetElement.querySelectorAll(focusable__default["default"])) || []);
1441
+ // Tab stops such as contenteditable and video[controls] can report tabIndex -1 without a tabindex attribute.
1442
+ var wrapTargets = focusableElements.filter(function (element) { return !(element.hasAttribute('tabindex') && element.tabIndex < 0); });
1441
1443
  // If focus reaches the trap guards, we need to wrap focus around to the leading
1442
1444
  // or trailing focusable element depending on which guard obtained focus
1443
- if (focusableElements.length && eventTarget === startGuard) {
1444
- (_a = focusableElements.reverse()[0]) === null || _a === void 0 ? void 0 : _a.focus();
1445
+ if (wrapTargets.length && eventTarget === startGuard) {
1446
+ wrapTargets[wrapTargets.length - 1].focus();
1445
1447
  return;
1446
1448
  }
1447
- else if (focusableElements.length && eventTarget === endGuard) {
1448
- (_b = focusableElements[0]) === null || _b === void 0 ? void 0 : _b.focus();
1449
+ else if (wrapTargets.length && eventTarget === endGuard) {
1450
+ wrapTargets[0].focus();
1449
1451
  return;
1450
1452
  }
1451
1453
  // If focus somehow escaped the trap, we need to try to restore focus to
1452
1454
  // to a suitable focusable element within the focus trap target. Otherwise
1453
1455
  // we'll need to focus on an alternative within the container.
1454
1456
  if (elementContains(targetElement, focusTrapMetadata.lastFocusedElement)) {
1455
- (_c = focusTrapMetadata.lastFocusedElement) === null || _c === void 0 ? void 0 : _c.focus();
1457
+ (_a = focusTrapMetadata.lastFocusedElement) === null || _a === void 0 ? void 0 : _a.focus();
1456
1458
  }
1457
1459
  else if (focusableElements.length) {
1458
- (_d = focusableElements[0]) === null || _d === void 0 ? void 0 : _d.focus();
1460
+ (_b = focusableElements[0]) === null || _b === void 0 ? void 0 : _b.focus();
1459
1461
  }
1460
1462
  else {
1461
1463
  // if there are no focusable elements, just focus the container
@@ -5604,45 +5606,6 @@ function collectDescendantKeys(node) {
5604
5606
  function collectDisabledKeys(nodes) {
5605
5607
  return nodes.flatMap(function (node) { return tslib.__spreadArray(tslib.__spreadArray([], tslib.__read((node.disabled ? [node.id] : [])), false), tslib.__read((node.children ? collectDisabledKeys(node.children) : [])), false); });
5606
5608
  }
5607
- /**
5608
- * Toggle a single node, applying cascade per the options (multiple mode only):
5609
- * - selecting + `cascadeSelect`: also selects all (non-disabled) descendants.
5610
- * - deselecting + `cascadeDeselect`: also deselects all descendants.
5611
- *
5612
- * In `single` mode only the clicked node is selected (clicking it again clears
5613
- * it); cascade does not apply.
5614
- */
5615
- function toggleSelection(nodes, previous, key, selectionMode, _a) {
5616
- var cascadeSelect = _a.cascadeSelect, cascadeDeselect = _a.cascadeDeselect;
5617
- if (selectionMode === 'single') {
5618
- var result_1 = new Set();
5619
- if (!previous.has(key)) {
5620
- result_1.add(key);
5621
- }
5622
- return result_1;
5623
- }
5624
- var result = new Set(previous);
5625
- var node = findNode(nodes, key);
5626
- var descendants = node ? collectDescendantKeys(node) : [];
5627
- var disabled = new Set(collectDisabledKeys(nodes));
5628
- if (!previous.has(key)) {
5629
- result.add(key);
5630
- if (cascadeSelect) {
5631
- descendants.forEach(function (k) {
5632
- if (!disabled.has(k)) {
5633
- result.add(k);
5634
- }
5635
- });
5636
- }
5637
- }
5638
- else {
5639
- result.delete(key);
5640
- if (cascadeDeselect) {
5641
- descendants.forEach(function (k) { return result.delete(k); });
5642
- }
5643
- }
5644
- return result;
5645
- }
5646
5609
  /**
5647
5610
  * Apply react-aria's proposed selection while enforcing cascade. Each key
5648
5611
  * react-aria added cascades to its descendants when `cascadeSelect` is set; each
@@ -5681,9 +5644,17 @@ function applyCascade(nodes, previous, next, selectionMode, _a) {
5681
5644
  return result;
5682
5645
  }
5683
5646
 
5647
+ /** Seeds the initial scroll math. Keep in step with `--tree-view-row-height`,
5648
+ * which sizes a virtualized row — see the spacing-parity test. */
5649
+ var ESTIMATED_ROW_HEIGHT = 36;
5650
+ /** Keep in step with `--space-quarter` / `--space-half`, which `.TreeView` uses
5651
+ * for its flex spacing — see the spacing-parity test. */
5652
+ var ROW_GAP = 2;
5653
+ var LIST_PADDING = 4;
5684
5654
  var TreeView = React.forwardRef(function (_a, ref) {
5685
- var items = _a.items, onAction = _a.onAction, _b = _a.selectionMode, selectionMode = _b === void 0 ? 'none' : _b, _c = _a.cascadeSelect, cascadeSelect = _c === void 0 ? false : _c, _d = _a.cascadeDeselect, cascadeDeselect = _d === void 0 ? false : _d, defaultExpandedKeys = _a.defaultExpandedKeys, className = _a.className, other = tslib.__rest(_a, ["items", "onAction", "selectionMode", "cascadeSelect", "cascadeDeselect", "defaultExpandedKeys", "className"]);
5686
- var _e = tslib.__read(React.useState(new Set()), 2), selectedKeys = _e[0], setSelectedKeys = _e[1];
5655
+ var items = _a.items, onAction = _a.onAction, _b = _a.selectionMode, selectionMode = _b === void 0 ? 'none' : _b, _c = _a.cascadeSelect, cascadeSelect = _c === void 0 ? false : _c, _d = _a.cascadeDeselect, cascadeDeselect = _d === void 0 ? false : _d, defaultExpandedKeys = _a.defaultExpandedKeys, _e = _a.virtualized, virtualized = _e === void 0 ? false : _e, height = _a.height, className = _a.className, style = _a.style, other = tslib.__rest(_a, ["items", "onAction", "selectionMode", "cascadeSelect", "cascadeDeselect", "defaultExpandedKeys", "virtualized", "height", "className", "style"]);
5656
+ var treeRef = useSharedRef(ref);
5657
+ var _f = tslib.__read(React.useState(new Set()), 2), selectedKeys = _f[0], setSelectedKeys = _f[1];
5687
5658
  var cascade = { cascadeSelect: cascadeSelect, cascadeDeselect: cascadeDeselect };
5688
5659
  // Cascade only applies to multiple selection.
5689
5660
  var isCascade = selectionMode === 'multiple' && (cascadeSelect || cascadeDeselect);
@@ -5697,24 +5668,74 @@ var TreeView = React.forwardRef(function (_a, ref) {
5697
5668
  : selection;
5698
5669
  });
5699
5670
  };
5700
- // When `onAction` is set, react-aria treats a row press as an action rather
5701
- // than a selection, so we toggle selection here ourselves.
5702
- var handleAction = function (key) {
5703
- if (selectionMode !== 'none') {
5704
- setSelectedKeys(function (prev) {
5705
- return toggleSelection(items, toKeySet(prev, items), key, selectionMode, cascade);
5706
- });
5707
- }
5708
- onAction === null || onAction === void 0 ? void 0 : onAction(key);
5709
- };
5671
+ // react-aria only reports a press as an action while nothing is selected, so
5672
+ // activation is driven here and react-aria is left to own selection. Click,
5673
+ // Space and Enter all do the same thing: toggle the checkbox and run the action.
5674
+ // Without selection react-aria handles the action itself (see the Tree props).
5675
+ React.useEffect(function () {
5676
+ var node = treeRef.current;
5677
+ if (!node || !onAction || selectionMode === 'none')
5678
+ return;
5679
+ var rowKey = function (target) {
5680
+ var _a;
5681
+ var row = (_a = target === null || target === void 0 ? void 0 : target.closest) === null || _a === void 0 ? void 0 : _a.call(target, '[role="row"][data-key]');
5682
+ if (!row || row.getAttribute('aria-disabled') === 'true')
5683
+ return null;
5684
+ return row.getAttribute('data-key');
5685
+ };
5686
+ var handleKeyDown = function (event) {
5687
+ if (event.key !== 'Enter' && event.key !== ' ')
5688
+ return;
5689
+ if (event.repeat || event.defaultPrevented)
5690
+ return;
5691
+ var key = rowKey(event.target);
5692
+ if (key)
5693
+ onAction(key);
5694
+ };
5695
+ var handleClick = function (event) {
5696
+ var _a;
5697
+ var target = event.target;
5698
+ // The chevron expands rather than activates, and a click on the
5699
+ // checkbox box or label is re-dispatched on the checkbox itself.
5700
+ if ((_a = target === null || target === void 0 ? void 0 : target.closest) === null || _a === void 0 ? void 0 : _a.call(target, '.TreeView__chevron, label[for], .Checkbox__overlay'))
5701
+ return;
5702
+ var key = rowKey(target);
5703
+ if (key)
5704
+ onAction(key);
5705
+ };
5706
+ node.addEventListener('keydown', handleKeyDown);
5707
+ node.addEventListener('click', handleClick);
5708
+ return function () {
5709
+ node.removeEventListener('keydown', handleKeyDown);
5710
+ node.removeEventListener('click', handleClick);
5711
+ };
5712
+ }, [onAction, selectionMode, virtualized]);
5710
5713
  // Disabled nodes are non-selectable; react-aria disables them via disabledKeys.
5711
5714
  var disabledKeys = React.useMemo(function () { return collectDisabledKeys(items); }, [items]);
5712
- // Cascade requires controlling selection. Without it, preserve the original
5713
- // behavior: selection is only controlled when an `onAction` handler is set.
5714
- var selectionProps = selectionMode !== 'none' && (isCascade || onAction)
5715
+ // Always control selection: react-aria's own state is lost if the tree
5716
+ // remounts (which toggling `virtualized` does), taking the user's checkboxes
5717
+ // with it.
5718
+ var selectionProps = selectionMode !== 'none'
5715
5719
  ? { selectedKeys: selectedKeys, onSelectionChange: handleSelectionChange }
5716
5720
  : {};
5717
- return (React__default["default"].createElement(reactAriaComponents.Tree, tslib.__assign({ ref: ref, className: classNames__default["default"]('TreeView', className), selectionMode: selectionMode, defaultExpandedKeys: defaultExpandedKeys }, (disabledKeys.length > 0 ? { disabledKeys: disabledKeys } : {}), (onAction ? { onAction: handleAction } : {}), selectionProps, other), items.map(function (item) { return (React__default["default"].createElement(TreeViewItem, tslib.__assign({ key: item.id }, item))); })));
5721
+ // The `height` prop outranks `style.height`, but only when it is actually
5722
+ // set — spreading an undefined `height` would wipe the caller's own.
5723
+ var mergedStyle = virtualized && height !== undefined ? tslib.__assign(tslib.__assign({}, style), { height: height }) : style;
5724
+ var tree = (React__default["default"].createElement(reactAriaComponents.Tree, tslib.__assign({ ref: treeRef, className: classNames__default["default"]('TreeView', className, {
5725
+ 'TreeView--virtualized': virtualized
5726
+ }), selectionMode: selectionMode, defaultExpandedKeys: defaultExpandedKeys }, (disabledKeys.length > 0 ? { disabledKeys: disabledKeys } : {}), (onAction && selectionMode === 'none'
5727
+ ? { onAction: function (key) { return onAction(String(key)); } }
5728
+ : {}), selectionProps, other, { style: mergedStyle }), items.map(function (item) { return (React__default["default"].createElement(TreeViewItem, tslib.__assign({ key: item.id }, item))); })));
5729
+ // Rows are absolutely positioned here, so the list layout owns their spacing
5730
+ // rather than `.TreeView`'s flex `gap`/`padding`.
5731
+ if (virtualized) {
5732
+ return (React__default["default"].createElement(reactAriaComponents.Virtualizer, { layout: reactAriaComponents.ListLayout, layoutOptions: {
5733
+ estimatedRowHeight: ESTIMATED_ROW_HEIGHT,
5734
+ gap: ROW_GAP,
5735
+ padding: LIST_PADDING
5736
+ } }, tree));
5737
+ }
5738
+ return tree;
5718
5739
  });
5719
5740
  TreeView.displayName = 'TreeView';
5720
5741
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deque/cauldron-react",
3
- "version": "7.6.0-canary.f34365cc",
3
+ "version": "7.6.0-canary.f3f1b3ab",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Fully accessible react components library for Deque Cauldron",
6
6
  "homepage": "https://cauldron.dequelabs.com/",
@@ -14,12 +14,12 @@
14
14
  "lib/"
15
15
  ],
16
16
  "dependencies": {
17
- "@floating-ui/dom": "^1.0.0",
17
+ "@floating-ui/dom": "^1.8.0",
18
18
  "@floating-ui/react-dom": "^2.1.2",
19
19
  "classnames": "^2.2.6",
20
20
  "focusable": "^2.3.0",
21
21
  "keyname": "^0.1.0",
22
- "react-aria-components": "^1.19.0",
22
+ "react-aria-components": "^1.21.1",
23
23
  "react-id-generator": "^3.0.1",
24
24
  "react-syntax-highlighter": "^16.1.1",
25
25
  "tslib": "^2.4.0"
@@ -37,7 +37,7 @@
37
37
  "@rollup/plugin-typescript": "^11.1.2",
38
38
  "@svgr/rollup": "^6.1.2",
39
39
  "@testing-library/dom": "^10.4.0",
40
- "@testing-library/jest-dom": "^7.0.0",
40
+ "@testing-library/jest-dom": "^7.0.1",
41
41
  "@testing-library/react": "^16.0.1",
42
42
  "@testing-library/user-event": "^14.6.7",
43
43
  "@types/classnames": "^2.2.10",
@@ -56,7 +56,7 @@
56
56
  "jest-axe": "^10.0.0",
57
57
  "jest-environment-jsdom": "^29.7.0",
58
58
  "nyc": "^15.0.1",
59
- "postcss": "^8.5.15",
59
+ "postcss": "^8.5.28",
60
60
  "postcss-cli": "^11.0.1",
61
61
  "postcss-import": "^16.1.1",
62
62
  "postcss-loader": "^8.2.1",