@lumx/react 3.6.7-alpha.6 → 3.6.7

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/package.json CHANGED
@@ -7,8 +7,8 @@
7
7
  },
8
8
  "dependencies": {
9
9
  "@juggle/resize-observer": "^3.2.0",
10
- "@lumx/core": "^3.6.7-alpha.6",
11
- "@lumx/icons": "^3.6.7-alpha.6",
10
+ "@lumx/core": "^3.6.7",
11
+ "@lumx/icons": "^3.6.7",
12
12
  "@popperjs/core": "^2.5.4",
13
13
  "body-scroll-lock": "^3.1.5",
14
14
  "classnames": "^2.3.2",
@@ -112,5 +112,5 @@
112
112
  "build:storybook": "storybook build"
113
113
  },
114
114
  "sideEffects": false,
115
- "version": "3.6.7-alpha.6"
115
+ "version": "3.6.7"
116
116
  }
@@ -60,6 +60,8 @@ export interface PopoverProps extends GenericProps, HasTheme {
60
60
  placement?: Placement;
61
61
  /** Whether the popover should be rendered into a DOM node that exists outside the DOM hierarchy of the parent component. */
62
62
  usePortal?: boolean;
63
+ /** The element in which the focus trap should be set. Default to popover. */
64
+ focusTrapZoneElement?: RefObject<HTMLElement>;
63
65
  /** Z-axis position. */
64
66
  zIndex?: number;
65
67
  /** On close callback (on click away or Escape pressed). */
@@ -115,6 +117,7 @@ const _InnerPopover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, ref
115
117
  boundaryRef,
116
118
  fitToAnchorWidth,
117
119
  fitWithinViewportHeight,
120
+ focusTrapZoneElement,
118
121
  offset,
119
122
  placement,
120
123
  style,
@@ -146,12 +149,13 @@ const _InnerPopover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, ref
146
149
  });
147
150
 
148
151
  const unmountSentinel = useRestoreFocusOnClose({ focusAnchorOnClose, anchorRef, parentElement }, popperElement);
152
+ const focusZoneElement = focusTrapZoneElement?.current || popoverRef?.current;
149
153
 
150
154
  useCallbackOnEscape(onClose, isOpen && closeOnEscape);
151
155
 
152
156
  /** Only set focus within if the focus trap is disabled as they interfere with one another. */
153
157
  useFocus(focusElement?.current, !withFocusTrap && isOpen && isPositioned);
154
- useFocusTrap(withFocusTrap && isOpen && popoverRef?.current, focusElement?.current);
158
+ useFocusTrap(withFocusTrap && isOpen && focusZoneElement, focusElement?.current);
155
159
 
156
160
  const clickAwayRefs = useRef([popoverRef, anchorRef]);
157
161
  const mergedRefs = useMergeRefs<HTMLDivElement>(setPopperElement, ref, popoverRef);
@@ -26,13 +26,17 @@ export function useFocusTrap(focusZoneElement: HTMLElement | Falsy, focusElement
26
26
  return undefined;
27
27
  }
28
28
 
29
+ // Use the shadow root as focusZoneElement when available
30
+ const focusZoneElementOrShadowRoot = focusZoneElement.shadowRoot || focusZoneElement;
31
+
29
32
  // Trap 'Tab' key down focus switch into the focus zone.
30
33
  const trapTabFocusInFocusZone = (evt: KeyboardEvent) => {
31
34
  const { key } = evt;
32
35
  if (key !== 'Tab') {
33
36
  return;
34
37
  }
35
- const focusable = getFirstAndLastFocusable(focusZoneElement);
38
+
39
+ const focusable = getFirstAndLastFocusable(focusZoneElementOrShadowRoot);
36
40
 
37
41
  // Prevent focus switch if no focusable available.
38
42
  if (!focusable.first) {
@@ -40,13 +44,17 @@ export function useFocusTrap(focusZoneElement: HTMLElement | Falsy, focusElement
40
44
  return;
41
45
  }
42
46
 
47
+ const activeElement = focusZoneElement.shadowRoot
48
+ ? focusZoneElement.shadowRoot.activeElement
49
+ : document.activeElement;
50
+
43
51
  if (
44
52
  // No previous focus
45
- !document.activeElement ||
53
+ !activeElement ||
46
54
  // Previous focus is at the end of the focus zone.
47
- (!evt.shiftKey && document.activeElement === focusable.last) ||
55
+ (!evt.shiftKey && activeElement === focusable.last) ||
48
56
  // Previous focus is outside the focus zone
49
- !focusZoneElement.contains(document.activeElement)
57
+ !focusZoneElementOrShadowRoot.contains(activeElement)
50
58
  ) {
51
59
  focusable.first.focus();
52
60
  evt.preventDefault();
@@ -57,7 +65,7 @@ export function useFocusTrap(focusZoneElement: HTMLElement | Falsy, focusElement
57
65
  // Focus order reversed
58
66
  evt.shiftKey &&
59
67
  // Previous focus is at the start of the focus zone.
60
- document.activeElement === focusable.first
68
+ activeElement === focusable.first
61
69
  ) {
62
70
  focusable.last.focus();
63
71
  evt.preventDefault();
@@ -70,12 +78,12 @@ export function useFocusTrap(focusZoneElement: HTMLElement | Falsy, focusElement
70
78
  };
71
79
 
72
80
  // SETUP:
73
- if (focusElement && focusZoneElement.contains(focusElement)) {
81
+ if (focusElement && focusZoneElementOrShadowRoot.contains(focusElement)) {
74
82
  // Focus the given element.
75
83
  focusElement.focus({ preventScroll: true });
76
84
  } else {
77
85
  // Focus the first focusable element in the zone.
78
- getFirstAndLastFocusable(focusZoneElement).first?.focus({ preventScroll: true });
86
+ getFirstAndLastFocusable(focusZoneElementOrShadowRoot).first?.focus({ preventScroll: true });
79
87
  }
80
88
  FOCUS_TRAPS.register(focusTrap);
81
89
 
@@ -6,7 +6,7 @@ import { getFocusableElements } from './getFocusableElements';
6
6
  * @param parentElement The element in which to search focusable elements.
7
7
  * @return first and last focusable elements
8
8
  */
9
- export function getFirstAndLastFocusable(parentElement: HTMLElement) {
9
+ export function getFirstAndLastFocusable(parentElement: HTMLElement | ShadowRoot) {
10
10
  const focusableElements = getFocusableElements(parentElement);
11
11
 
12
12
  // First non disabled element.
@@ -2,6 +2,6 @@ import { DISABLED_SELECTOR, TABBABLE_ELEMENTS_SELECTOR } from './constants';
2
2
 
3
3
  const isNotDisabled = (element: HTMLElement) => !element.matches(DISABLED_SELECTOR);
4
4
 
5
- export function getFocusableElements(element: HTMLElement): HTMLElement[] {
5
+ export function getFocusableElements(element: HTMLElement | ShadowRoot): HTMLElement[] {
6
6
  return Array.from(element.querySelectorAll<HTMLElement>(TABBABLE_ELEMENTS_SELECTOR)).filter(isNotDisabled);
7
7
  }