@deque/cauldron-react 7.4.0 → 7.5.0-canary.792c4b83

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.
@@ -13,7 +13,14 @@ interface ActionMenuListProps {
13
13
  * (`<li>`) in the documented `TopBar`/`MenuBar` nesting — so the `ref` and
14
14
  * spread handlers type against the actual element without casts.
15
15
  */
16
- export type ActionMenuTriggerProps<E extends HTMLElement = HTMLButtonElement> = Pick<React.HTMLAttributes<E>, 'children' | 'id' | 'onClick' | 'onKeyDown' | 'aria-expanded' | 'aria-haspopup' | 'aria-controls'> & {
16
+ export type ActionMenuTriggerProps<E extends HTMLElement = HTMLButtonElement> = Pick<React.HTMLAttributes<E>, 'children' | 'id' | 'onClick' | 'onKeyDown' | 'aria-labelledby' | 'aria-expanded' | 'aria-haspopup' | 'aria-controls'> & {
17
+ /**
18
+ * Id for the element wrapping the trigger's visible label, e.g.
19
+ * `<span id={labelId}>Menu</span>`. Only provided with `renderInTrigger`,
20
+ * where the trigger contains the menu and would otherwise be named after
21
+ * every item in it. Not a DOM attribute — destructure it out of the props.
22
+ */
23
+ labelId?: string;
17
24
  ref: React.RefObject<E | null>;
18
25
  };
19
26
  export type ActionMenuTriggerFunction<E extends HTMLElement = HTMLButtonElement> = (props: ActionMenuTriggerProps<E>, open: boolean) => React.ReactElement;
package/lib/index.js CHANGED
@@ -1659,7 +1659,11 @@ var DialogContent = function (_a) {
1659
1659
  'text--align-left': align === 'left',
1660
1660
  'text--align-center': align === 'center',
1661
1661
  'text--align-right': align === 'right'
1662
- }), tabIndex: (context === null || context === void 0 ? void 0 : context.scrollable) ? -1 : undefined }, other), children));
1662
+ }),
1663
+ // A keyboard user can only scroll this region if it is tabbable (WCAG
1664
+ // 2.1.1). The rule allows that on role="tabpanel" only. See #680.
1665
+ // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
1666
+ tabIndex: (context === null || context === void 0 ? void 0 : context.scrollable) ? 0 : undefined }, other), children));
1663
1667
  };
1664
1668
  DialogContent.displayName = 'DialogContent';
1665
1669
  var DialogFooter = function (_a) {
@@ -3783,6 +3787,20 @@ var Listbox = React.forwardRef(function (_a, ref) {
3783
3787
  var listboxRef = useSharedRef(ref);
3784
3788
  var isControlled = typeof value !== 'undefined';
3785
3789
  var isActiveControlled = typeof controlledActiveOption !== 'undefined';
3790
+ var hasActiveChangeHandler = typeof onActiveChange === 'function';
3791
+ React.useEffect(function () {
3792
+ if (process.env.NODE_ENV !== 'production' &&
3793
+ isActiveControlled &&
3794
+ hasActiveChangeHandler) {
3795
+ console.warn('Listbox received both `activeOption` and `onActiveChange`. Do not ' +
3796
+ 'write the option reported by `onActiveChange` back into ' +
3797
+ '`activeOption`: the two copies then race, and a stale downward ' +
3798
+ 'sync can revert a newer value and re-render without settling. ' +
3799
+ 'Let Listbox own the active option, or use `activeOption` only as ' +
3800
+ 'a one-shot request. ' +
3801
+ 'See https://github.com/dequelabs/cauldron/issues/2512');
3802
+ }
3803
+ }, [isActiveControlled, hasActiveChangeHandler]);
3786
3804
  React.useLayoutEffect(function () {
3787
3805
  if (!isControlled && selectedOptions.length > 0) {
3788
3806
  return;
@@ -3924,7 +3942,7 @@ var Listbox = React.forwardRef(function (_a, ref) {
3924
3942
  var firstOption = !focusDisabledOptions
3925
3943
  ? options.find(function (option) { return !isDisabledOption(option); })
3926
3944
  : options[0];
3927
- if (firstOption) {
3945
+ if (firstOption && firstOption.element !== (activeOption === null || activeOption === void 0 ? void 0 : activeOption.element)) {
3928
3946
  setActiveOption(firstOption);
3929
3947
  }
3930
3948
  return;
@@ -3933,7 +3951,7 @@ var Listbox = React.forwardRef(function (_a, ref) {
3933
3951
  var lastOption = !focusDisabledOptions
3934
3952
  ? tslib.__spreadArray([], tslib.__read(options), false).reverse().find(function (option) { return !isDisabledOption(option); })
3935
3953
  : options[options.length - 1];
3936
- if (lastOption) {
3954
+ if (lastOption && lastOption.element !== (activeOption === null || activeOption === void 0 ? void 0 : activeOption.element)) {
3937
3955
  setActiveOption(lastOption);
3938
3956
  }
3939
3957
  return;
@@ -5154,21 +5172,24 @@ function useMnemonics(_a) {
5154
5172
 
5155
5173
  var ActionList = React.forwardRef(function (_a, ref) {
5156
5174
  var _b = _a.selectionType, selectionType = _b === void 0 ? null : _b, onAction = _a.onAction, className = _a.className, children = _a.children, props = tslib.__rest(_a, ["selectionType", "onAction", "className", "children"]);
5157
- var activeElement = React.useRef(null);
5175
+ // Listbox owns the active option. ActionList must not mirror it back down
5176
+ // as a controlled prop: the two copies then race, and a downward sync
5177
+ // carrying an already-stale value can revert a newer one and oscillate.
5178
+ // `activeOption` below is only a one-shot mnemonic request, never a
5179
+ // mirror. See cauldron#2512; cauldron#2522 tracks making `activeOption`
5180
+ // properly controlled so this constraint lives in the interface instead.
5158
5181
  var _c = tslib.__read(React.useState(), 2), activeOption = _c[0], setActiveOption = _c[1];
5159
- var handleActiveChange = React.useCallback(function (value) {
5160
- activeElement.current = value === null || value === void 0 ? void 0 : value.element;
5161
- setActiveOption(value);
5162
- }, []);
5163
5182
  var handleAction = React.useCallback(function (key, event) {
5164
5183
  if (typeof onAction === 'function') {
5165
5184
  onAction(key, event);
5166
5185
  }
5167
5186
  }, [onAction]);
5187
+ // A new object every match, even for the same element: this is a one-shot
5188
+ // request to Listbox rather than a mirror of its state, so re-using the
5189
+ // previous object would leave the controlled prop unchanged and the
5190
+ // request would never reach Listbox.
5168
5191
  var handleMnemonicMatch = React.useCallback(function (element) {
5169
- setActiveOption(function (prev) {
5170
- return (prev === null || prev === void 0 ? void 0 : prev.element) === element ? prev : { element: element };
5171
- });
5192
+ setActiveOption({ element: element });
5172
5193
  }, []);
5173
5194
  var containerRef = useMnemonics({
5174
5195
  onMatch: handleMnemonicMatch,
@@ -5185,7 +5206,7 @@ var ActionList = React.forwardRef(function (_a, ref) {
5185
5206
  /* Listbox comes with an explicit role of "listbox", but we want to either
5186
5207
  * use the role from props, or default to the intrinsic role */
5187
5208
  // eslint-disable-next-line jsx-a11y/aria-role
5188
- role: undefined, "aria-multiselectable": undefined, className: classNames__default["default"]('ActionList', className), activeOption: activeOption }, props, { onActiveChange: handleActiveChange, navigation: "bound" }),
5209
+ role: undefined, "aria-multiselectable": undefined, className: classNames__default["default"]('ActionList', className), activeOption: activeOption }, props, { navigation: "bound" }),
5189
5210
  React__default["default"].createElement(ActionListProvider, { role: props.role || 'list', onAction: handleAction, selectionType: selectionType }, children)));
5190
5211
  });
5191
5212
  ActionList.displayName = 'ActionList';
@@ -5341,8 +5362,9 @@ var ActionMenuComponent = React.forwardRef(function (_a, ref) {
5341
5362
  var actionMenuRef = useSharedRef(ref);
5342
5363
  var _f = actionMenuList.props, actionListRef = _f.ref, actionListOnAction = _f.onAction;
5343
5364
  var actionMenuListRef = useSharedRef(actionListRef !== null && actionListRef !== void 0 ? actionListRef : null);
5344
- var _g = tslib.__read(nextId.useId(1, 'menu-trigger'), 1), triggerId = _g[0];
5345
- var _h = tslib.__read(nextId.useId(1, 'menu'), 1), menuId = _h[0];
5365
+ var _g = tslib.__read(nextId.useId(1, 'menu-label'), 1), labelId = _g[0];
5366
+ var _h = tslib.__read(nextId.useId(1, 'menu-trigger'), 1), triggerId = _h[0];
5367
+ var _j = tslib.__read(nextId.useId(1, 'menu'), 1), menuId = _j[0];
5346
5368
  var handleTriggerClick = React.useCallback(function (event) {
5347
5369
  // istanbul ignore else
5348
5370
  if (!event.defaultPrevented) {
@@ -5411,18 +5433,41 @@ var ActionMenuComponent = React.forwardRef(function (_a, ref) {
5411
5433
  (_b = triggerRef.current) === null || _b === void 0 ? void 0 : _b.focus();
5412
5434
  }
5413
5435
  }, [open]);
5436
+ // Triggers predating `labelId` render no label element. Since
5437
+ // `aria-labelledby` is not chained, naming the menu with such a trigger
5438
+ // would resolve back to the menu's own items, so it is left unnamed.
5439
+ var _k = tslib.__read(React.useState(false), 2), hasLabelElement = _k[0], setHasLabelElement = _k[1];
5440
+ React.useEffect(function () {
5441
+ setHasLabelElement(renderInTrigger ? !!document.getElementById(labelId) : false);
5442
+ }, [renderInTrigger, labelId]);
5443
+ React.useEffect(function () {
5444
+ // istanbul ignore next
5445
+ if (renderInTrigger &&
5446
+ !document.getElementById(labelId) &&
5447
+ process.env.NODE_ENV !== 'production') {
5448
+ console.warn("renderInTrigger should render id=\"".concat(labelId, "\" on the element wrapping the trigger's visible label, otherwise the trigger and the menu are named after the entire submenu."));
5449
+ }
5450
+ }, [renderInTrigger, labelId]);
5451
+ var labelledBy = triggerId;
5452
+ if (renderInTrigger) {
5453
+ labelledBy = hasLabelElement ? labelId : undefined;
5454
+ }
5414
5455
  var hidden = renderInTrigger && !open;
5415
5456
  var overlay = (React__default["default"].createElement(AnchoredOverlay, tslib.__assign({ ref: actionMenuRef, role: "presentation", className: classNames__default["default"]('ActionMenu', className), open: open, onOpenChange: setOpen, target: triggerRef, placement: placement, offset: 4, portal: portal, style: tslib.__assign({ display: !open ? 'none' : undefined }, style), "aria-hidden": hidden, onBlur: handleOverlayBlur }, props), React__default["default"].cloneElement(actionMenuList, {
5416
5457
  ref: actionMenuListRef,
5417
5458
  id: menuId,
5418
5459
  role: 'menu',
5419
5460
  onAction: handleAction,
5420
- 'aria-labelledby': triggerId,
5461
+ // aria-labelledby is not chained, so pointing at a trigger that
5462
+ // contains this menu would resolve to the menu's own items.
5463
+ 'aria-labelledby': labelledBy,
5421
5464
  focusStrategy: focusStrategy,
5422
5465
  focusDisabledOptions: true,
5423
5466
  hidden: hidden
5424
5467
  })));
5425
- var triggerProps = React.useMemo(function () {
5468
+ // Spread onto a prerendered trigger element below, so this must stay
5469
+ // DOM-safe: no labelId.
5470
+ var baseTriggerProps = React.useMemo(function () {
5426
5471
  return {
5427
5472
  ref: triggerRef,
5428
5473
  id: triggerId,
@@ -5432,7 +5477,7 @@ var ActionMenuComponent = React.forwardRef(function (_a, ref) {
5432
5477
  'aria-haspopup': 'menu',
5433
5478
  'aria-controls': menuId
5434
5479
  };
5435
- }, [handleTriggerClick, open, menuId]);
5480
+ }, [handleTriggerClick, handleTriggerKeyDown, open, triggerId, menuId]);
5436
5481
  if (renderInTrigger) {
5437
5482
  // istanbul ignore next
5438
5483
  if (portal && process.env.NODE_ENV !== 'production') {
@@ -5442,11 +5487,14 @@ var ActionMenuComponent = React.forwardRef(function (_a, ref) {
5442
5487
  if (React__default["default"].isValidElement(trigger)) {
5443
5488
  console.warn('renderInTrigger requires the use of a trigger function, rather than a prerendered trigger ReactElement.');
5444
5489
  }
5445
- triggerProps.children = overlay;
5446
5490
  }
5491
+ // Derived rather than mutated onto the memoized object, so that toggling
5492
+ // `renderInTrigger` off cannot leave a stale `children` behind.
5493
+ var triggerProps = renderInTrigger
5494
+ ? tslib.__assign(tslib.__assign({}, baseTriggerProps), { children: overlay, 'aria-labelledby': labelledBy }) : baseTriggerProps;
5447
5495
  var actionMenuTrigger = React__default["default"].isValidElement(trigger)
5448
5496
  ? React__default["default"].cloneElement(trigger, triggerProps)
5449
- : trigger(tslib.__assign(tslib.__assign({}, triggerProps), { children: overlay }), open);
5497
+ : trigger(tslib.__assign(tslib.__assign(tslib.__assign({}, triggerProps), { children: overlay }), (renderInTrigger && { labelId: labelId })), open);
5450
5498
  return (React__default["default"].createElement(React__default["default"].Fragment, null,
5451
5499
  React__default["default"].createElement(ClickOutsideListener$1, { onClickOutside: handleClickOutside, mouseEvent: open ? undefined : false, touchEvent: open ? undefined : false, target: triggerProps.ref }, actionMenuTrigger),
5452
5500
  renderInTrigger ? null : overlay));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deque/cauldron-react",
3
- "version": "7.4.0",
3
+ "version": "7.5.0-canary.792c4b83",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Fully accessible react components library for Deque Cauldron",
6
6
  "homepage": "https://cauldron.dequelabs.com/",