@lumx/react 2.2.12-alpha.2 → 2.2.12

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.
@@ -12,6 +12,7 @@ import { ClickAwayProvider } from '@lumx/react/utils/ClickAwayProvider';
12
12
 
13
13
  import { Comp, GenericProps, getRootClassName, handleBasicClasses, ValueOf } from '@lumx/react/utils';
14
14
  import { mergeRefs } from '@lumx/react/utils/mergeRefs';
15
+ import { useFocusWithin } from '@lumx/react/hooks/useFocusWithin';
15
16
 
16
17
  /**
17
18
  * Different possible placements for the popover.
@@ -81,6 +82,8 @@ export interface PopoverProps extends GenericProps {
81
82
  fitWithinViewportHeight?: boolean;
82
83
  /** Element to focus when opening the popover. */
83
84
  focusElement?: RefObject<HTMLElement>;
85
+ /** Whether the focus should go back on the anchor when popover closes and focus is within. */
86
+ focusAnchorOnClose?: boolean;
84
87
  /** Whether we put an arrow or not. */
85
88
  hasArrow?: boolean;
86
89
  /** Whether the popover is open or not. */
@@ -213,6 +216,7 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
213
216
  style,
214
217
  usePortal,
215
218
  zIndex,
219
+ focusAnchorOnClose = true,
216
220
  ...forwardedProps
217
221
  } = props;
218
222
  // eslint-disable-next-line react-hooks/rules-of-hooks
@@ -222,6 +226,42 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
222
226
  // eslint-disable-next-line react-hooks/rules-of-hooks
223
227
  const clickAwayRef = useRef<HTMLDivElement>(null);
224
228
 
229
+ /**
230
+ * Track whether the focus is currently set in the
231
+ * popover.
232
+ * */
233
+ // eslint-disable-next-line react-hooks/rules-of-hooks
234
+ const isFocusedWithin = useRef(false);
235
+
236
+ // eslint-disable-next-line react-hooks/rules-of-hooks
237
+ useFocusWithin({
238
+ element: popperElement || null,
239
+ onFocusIn: () => {
240
+ isFocusedWithin.current = true;
241
+ },
242
+ onFocusOut: () => {
243
+ isFocusedWithin.current = false;
244
+ },
245
+ });
246
+
247
+ /** Action on close */
248
+ const handleClose = () => {
249
+ if (!onClose) {
250
+ return;
251
+ }
252
+
253
+ /**
254
+ * If the focus is currently within the popover
255
+ * when the popover closes, reset the focus back to the anchor element
256
+ * unless specifically requested not to.
257
+ */
258
+ if (isFocusedWithin.current && focusAnchorOnClose) {
259
+ anchorRef.current?.focus();
260
+ }
261
+
262
+ onClose();
263
+ };
264
+
225
265
  const modifiers: any = [];
226
266
  const actualOffset: [number, number] = [offset?.along ?? 0, (offset?.away ?? 0) + (hasArrow ? ARROW_SIZE : 0)];
227
267
  modifiers.push({
@@ -267,7 +307,7 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
267
307
  }, [style, styles.popper, zIndex, fitWithinViewportHeight]);
268
308
 
269
309
  // eslint-disable-next-line react-hooks/rules-of-hooks
270
- useCallbackOnEscape(onClose, isOpen && closeOnEscape);
310
+ useCallbackOnEscape(handleClose, isOpen && closeOnEscape);
271
311
  // eslint-disable-next-line react-hooks/rules-of-hooks
272
312
  useFocus(focusElement?.current, isOpen && (state?.rects?.popper?.y ?? -1) >= 0);
273
313
 
@@ -286,7 +326,7 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
286
326
  style={popoverStyle}
287
327
  {...attributes.popper}
288
328
  >
289
- <ClickAwayProvider callback={closeOnClickAway && onClose} refs={clickAwayRefs}>
329
+ <ClickAwayProvider callback={closeOnClickAway && handleClose} refs={clickAwayRefs}>
290
330
  {hasArrow && <div ref={setArrowElement} className={`${CLASSNAME}__arrow`} style={styles.arrow} />}
291
331
  {children}
292
332
  </ClickAwayProvider>
@@ -29,6 +29,7 @@ exports[`<Select> Snapshots and structure should render correctly 1`] = `
29
29
  closeOnEscape={true}
30
30
  fitToAnchorWidth={true}
31
31
  fitWithinViewportHeight={true}
32
+ focusAnchorOnClose={true}
32
33
  isOpen={false}
33
34
  onClose={[Function]}
34
35
  placement="bottom-start"
@@ -30,6 +30,7 @@ exports[`<SelectMultiple> Snapshots and structure should render chips 1`] = `
30
30
  closeOnEscape={true}
31
31
  fitToAnchorWidth={true}
32
32
  fitWithinViewportHeight={true}
33
+ focusAnchorOnClose={true}
33
34
  isOpen={false}
34
35
  onClose={[Function]}
35
36
  placement="bottom-start"
@@ -72,6 +73,7 @@ exports[`<SelectMultiple> Snapshots and structure should render defaults 1`] = `
72
73
  closeOnEscape={true}
73
74
  fitToAnchorWidth={true}
74
75
  fitWithinViewportHeight={true}
76
+ focusAnchorOnClose={true}
75
77
  isOpen={false}
76
78
  onClose={[Function]}
77
79
  placement="bottom-start"
@@ -103,7 +103,6 @@ export function useTooltipOpen(delay: number | undefined, anchorElement: HTMLEle
103
103
  for (const [node, eventType, evenHandler] of events) {
104
104
  node.removeEventListener(eventType, evenHandler);
105
105
  }
106
- closeImmediately();
107
106
  };
108
107
  }, [anchorElement, delay]);
109
108
 
package/types.d.ts CHANGED
@@ -938,6 +938,8 @@ export interface PopoverProps extends GenericProps {
938
938
  fitWithinViewportHeight?: boolean;
939
939
  /** Element to focus when opening the popover. */
940
940
  focusElement?: RefObject<HTMLElement>;
941
+ /** Whether the focus should go back on the anchor when popover closes and focus is within. */
942
+ focusAnchorOnClose?: boolean;
941
943
  /** Whether we put an arrow or not. */
942
944
  hasArrow?: boolean;
943
945
  /** Whether the popover is open or not. */
@@ -1012,6 +1014,8 @@ export interface DropdownProps extends GenericProps {
1012
1014
  placement?: Placement;
1013
1015
  /** Whether the focus should be set on the list when the dropdown is open or not. */
1014
1016
  shouldFocusOnOpen?: boolean;
1017
+ /** Whether the focus should go back on the anchor when dropdown closes and focus is within. */
1018
+ focusAnchorOnClose?: boolean;
1015
1019
  /**
1016
1020
  * Z-axis position.
1017
1021
  * @see {@link PopoverProps#zIndex}
@@ -1474,6 +1478,8 @@ export interface ListProps extends GenericProps {
1474
1478
  isClickable?: boolean;
1475
1479
  /** Item padding size. */
1476
1480
  itemPadding?: Extract<Size, "big" | "huge">;
1481
+ /** Tab index of the list. Default to -1 */
1482
+ tabIndex?: number;
1477
1483
  /**
1478
1484
  * On list item selected callback.
1479
1485
  *