@radix-ui/react-focus-scope 1.1.14 → 1.1.15

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/dist/index.d.mts CHANGED
@@ -15,6 +15,15 @@ interface FocusScopeProps extends PrimitiveDivProps {
15
15
  * @defaultValue false
16
16
  */
17
17
  trapped?: boolean;
18
+ /**
19
+ * A list of nodes that should be treated as part of the focus scope even
20
+ * though they don't live within the scope's DOM subtree (eg. portalled
21
+ * content of a nested, non-modal layer). When the scope is `trapped`, focus
22
+ * is allowed to move into these branches without being reclaimed.
23
+ *
24
+ * See: https://github.com/radix-ui/primitives/issues/3423
25
+ */
26
+ branches?: HTMLElement[];
18
27
  /**
19
28
  * Event handler called when auto-focusing on mount.
20
29
  * Can be prevented.
@@ -27,5 +36,31 @@ interface FocusScopeProps extends PrimitiveDivProps {
27
36
  onUnmountAutoFocus?: (event: Event) => void;
28
37
  }
29
38
  declare const FocusScope: React.ForwardRefExoticComponent<FocusScopeProps & React.RefAttributes<HTMLDivElement>>;
39
+ /**
40
+ * Lets portalled content that is a React descendant of a (modal) layer — but rendered outside of
41
+ * that layer's DOM subtree — register itself as a "branch" of the layer's focus scope.
42
+ *
43
+ * A modal container (eg. `Dialog`) creates a registry via `useFocusScopeBranchRegistry`, renders a
44
+ * `FocusScopeBranchProvider` around its children, and passes the collected `nodes` to its trapped
45
+ * `FocusScope` (so focus isn't reclaimed from the branch) and, if applicable, to its `RemoveScroll`
46
+ * `shards` (so the branch remains scrollable). Nested layers (eg. a non-modal `Popover`) register
47
+ * their content node with `useFocusScopeBranch`.
48
+ *
49
+ * See: https://github.com/radix-ui/primitives/issues/3423
50
+ */
51
+ interface FocusScopeBranchRegistry {
52
+ add: (node: HTMLElement) => void;
53
+ remove: (node: HTMLElement) => void;
54
+ }
55
+ declare const FocusScopeBranchProvider: React.Provider<FocusScopeBranchRegistry | null>;
56
+ declare function useFocusScopeBranchRegistry(): {
57
+ readonly nodes: HTMLElement[];
58
+ readonly registry: FocusScopeBranchRegistry;
59
+ };
60
+ /**
61
+ * Registers `node` as a branch of the nearest ancestor `FocusScopeBranchProvider`. No-ops when
62
+ * there is no ancestor registry (eg. the layer isn't nested inside a modal layer).
63
+ */
64
+ declare function useFocusScopeBranch(node: HTMLElement | null): void;
30
65
 
31
- export { FocusScope, type FocusScopeProps, FocusScope as Root };
66
+ export { FocusScope, FocusScopeBranchProvider, type FocusScopeBranchRegistry, type FocusScopeProps, FocusScope as Root, useFocusScopeBranch, useFocusScopeBranchRegistry };
package/dist/index.d.ts CHANGED
@@ -15,6 +15,15 @@ interface FocusScopeProps extends PrimitiveDivProps {
15
15
  * @defaultValue false
16
16
  */
17
17
  trapped?: boolean;
18
+ /**
19
+ * A list of nodes that should be treated as part of the focus scope even
20
+ * though they don't live within the scope's DOM subtree (eg. portalled
21
+ * content of a nested, non-modal layer). When the scope is `trapped`, focus
22
+ * is allowed to move into these branches without being reclaimed.
23
+ *
24
+ * See: https://github.com/radix-ui/primitives/issues/3423
25
+ */
26
+ branches?: HTMLElement[];
18
27
  /**
19
28
  * Event handler called when auto-focusing on mount.
20
29
  * Can be prevented.
@@ -27,5 +36,31 @@ interface FocusScopeProps extends PrimitiveDivProps {
27
36
  onUnmountAutoFocus?: (event: Event) => void;
28
37
  }
29
38
  declare const FocusScope: React.ForwardRefExoticComponent<FocusScopeProps & React.RefAttributes<HTMLDivElement>>;
39
+ /**
40
+ * Lets portalled content that is a React descendant of a (modal) layer — but rendered outside of
41
+ * that layer's DOM subtree — register itself as a "branch" of the layer's focus scope.
42
+ *
43
+ * A modal container (eg. `Dialog`) creates a registry via `useFocusScopeBranchRegistry`, renders a
44
+ * `FocusScopeBranchProvider` around its children, and passes the collected `nodes` to its trapped
45
+ * `FocusScope` (so focus isn't reclaimed from the branch) and, if applicable, to its `RemoveScroll`
46
+ * `shards` (so the branch remains scrollable). Nested layers (eg. a non-modal `Popover`) register
47
+ * their content node with `useFocusScopeBranch`.
48
+ *
49
+ * See: https://github.com/radix-ui/primitives/issues/3423
50
+ */
51
+ interface FocusScopeBranchRegistry {
52
+ add: (node: HTMLElement) => void;
53
+ remove: (node: HTMLElement) => void;
54
+ }
55
+ declare const FocusScopeBranchProvider: React.Provider<FocusScopeBranchRegistry | null>;
56
+ declare function useFocusScopeBranchRegistry(): {
57
+ readonly nodes: HTMLElement[];
58
+ readonly registry: FocusScopeBranchRegistry;
59
+ };
60
+ /**
61
+ * Registers `node` as a branch of the nearest ancestor `FocusScopeBranchProvider`. No-ops when
62
+ * there is no ancestor registry (eg. the layer isn't nested inside a modal layer).
63
+ */
64
+ declare function useFocusScopeBranch(node: HTMLElement | null): void;
30
65
 
31
- export { FocusScope, type FocusScopeProps, FocusScope as Root };
66
+ export { FocusScope, FocusScopeBranchProvider, type FocusScopeBranchRegistry, type FocusScopeProps, FocusScope as Root, useFocusScopeBranch, useFocusScopeBranchRegistry };
package/dist/index.js CHANGED
@@ -33,7 +33,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
33
33
  var index_exports = {};
34
34
  __export(index_exports, {
35
35
  FocusScope: () => FocusScope,
36
- Root: () => FocusScope
36
+ FocusScopeBranchProvider: () => FocusScopeBranchProvider,
37
+ Root: () => FocusScope,
38
+ useFocusScopeBranch: () => useFocusScopeBranch,
39
+ useFocusScopeBranchRegistry: () => useFocusScopeBranchRegistry
37
40
  });
38
41
  module.exports = __toCommonJS(index_exports);
39
42
 
@@ -51,6 +54,7 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
51
54
  const {
52
55
  loop = false,
53
56
  trapped = false,
57
+ branches,
54
58
  onMountAutoFocus: onMountAutoFocusProp,
55
59
  onUnmountAutoFocus: onUnmountAutoFocusProp,
56
60
  ...scopeProps
@@ -60,6 +64,18 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
60
64
  const onUnmountAutoFocus = (0, import_react_use_callback_ref.useCallbackRef)(onUnmountAutoFocusProp);
61
65
  const lastFocusedElementRef = React.useRef(null);
62
66
  const composedRefs = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, setContainer);
67
+ const branchesRef = React.useRef(branches);
68
+ React.useEffect(() => {
69
+ branchesRef.current = branches;
70
+ });
71
+ const isTargetInScope = React.useCallback(
72
+ (target) => {
73
+ if (!target) return false;
74
+ if (container?.contains(target)) return true;
75
+ return Boolean(branchesRef.current?.some((branch) => branch.contains(target)));
76
+ },
77
+ [container]
78
+ );
63
79
  const focusScope = React.useRef({
64
80
  paused: false,
65
81
  pause() {
@@ -74,7 +90,7 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
74
90
  let handleFocusIn2 = function(event) {
75
91
  if (focusScope.paused || !container) return;
76
92
  const target = event.target;
77
- if (container.contains(target)) {
93
+ if (isTargetInScope(target)) {
78
94
  lastFocusedElementRef.current = target;
79
95
  } else {
80
96
  focus(lastFocusedElementRef.current, { select: true });
@@ -83,7 +99,7 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
83
99
  if (focusScope.paused || !container) return;
84
100
  const relatedTarget = event.relatedTarget;
85
101
  if (relatedTarget === null) return;
86
- if (!container.contains(relatedTarget)) {
102
+ if (!isTargetInScope(relatedTarget)) {
87
103
  focus(lastFocusedElementRef.current, { select: true });
88
104
  }
89
105
  }, handleMutations2 = function(mutations) {
@@ -107,7 +123,7 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
107
123
  mutationObserver.disconnect();
108
124
  };
109
125
  }
110
- }, [trapped, container, focusScope.paused]);
126
+ }, [trapped, container, focusScope.paused, isTargetInScope]);
111
127
  React.useEffect(() => {
112
128
  if (container) {
113
129
  focusScopesStack.add(focusScope);
@@ -167,6 +183,29 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
167
183
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_primitive.Primitive.div, { tabIndex: -1, ...scopeProps, ref: composedRefs, onKeyDown: handleKeyDown });
168
184
  }, "FocusScope")
169
185
  );
186
+ var FocusScopeBranchContext = React.createContext(null);
187
+ var FocusScopeBranchProvider = FocusScopeBranchContext.Provider;
188
+ function useFocusScopeBranchRegistry() {
189
+ const [nodes, setNodes] = React.useState([]);
190
+ const registry = React.useMemo(
191
+ () => ({
192
+ add: /* @__PURE__ */ __name((node) => setNodes((prev) => prev.includes(node) ? prev : [...prev, node]), "add"),
193
+ remove: /* @__PURE__ */ __name((node) => setNodes((prev) => prev.filter((current) => current !== node)), "remove")
194
+ }),
195
+ []
196
+ );
197
+ return { nodes, registry };
198
+ }
199
+ __name(useFocusScopeBranchRegistry, "useFocusScopeBranchRegistry");
200
+ function useFocusScopeBranch(node) {
201
+ const registry = React.useContext(FocusScopeBranchContext);
202
+ React.useEffect(() => {
203
+ if (!node || !registry) return;
204
+ registry.add(node);
205
+ return () => registry.remove(node);
206
+ }, [node, registry]);
207
+ }
208
+ __name(useFocusScopeBranch, "useFocusScopeBranch");
170
209
  function focusFirst(candidates, { select = false } = {}) {
171
210
  const previouslyFocusedElement = document.activeElement;
172
211
  for (const candidate of candidates) {
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts", "../src/focus-scope.tsx"],
4
- "sourcesContent": ["'use client';\nexport {\n FocusScope,\n //\n Root,\n} from './focus-scope';\nexport type { FocusScopeProps } from './focus-scope';\n", "import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { Primitive } from '@radix-ui/react-primitive';\nimport { useCallbackRef } from '@radix-ui/react-use-callback-ref';\n\nconst AUTOFOCUS_ON_MOUNT = 'focusScope.autoFocusOnMount';\nconst AUTOFOCUS_ON_UNMOUNT = 'focusScope.autoFocusOnUnmount';\nconst EVENT_OPTIONS = { bubbles: false, cancelable: true };\n\ntype FocusableTarget = HTMLElement | { focus(): void };\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype FocusScopeElement = React.ComponentRef<typeof Primitive.div>;\ntype PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;\ninterface FocusScopeProps extends PrimitiveDivProps {\n /**\n * When `true`, tabbing from last item will focus first tabbable\n * and shift+tab from first item will focus last tababble.\n * @defaultValue false\n */\n loop?: boolean;\n\n /**\n * When `true`, focus cannot escape the focus scope via keyboard,\n * pointer, or a programmatic focus.\n * @defaultValue false\n */\n trapped?: boolean;\n\n /**\n * Event handler called when auto-focusing on mount.\n * Can be prevented.\n */\n onMountAutoFocus?: (event: Event) => void;\n\n /**\n * Event handler called when auto-focusing on unmount.\n * Can be prevented.\n */\n onUnmountAutoFocus?: (event: Event) => void;\n}\n\nconst FocusScope = /* @__PURE__ */ React.forwardRef<FocusScopeElement, FocusScopeProps>(\n function FocusScope(props, forwardedRef) {\n const {\n loop = false,\n trapped = false,\n onMountAutoFocus: onMountAutoFocusProp,\n onUnmountAutoFocus: onUnmountAutoFocusProp,\n ...scopeProps\n } = props;\n const [container, setContainer] = React.useState<HTMLElement | null>(null);\n const onMountAutoFocus = useCallbackRef(onMountAutoFocusProp);\n const onUnmountAutoFocus = useCallbackRef(onUnmountAutoFocusProp);\n const lastFocusedElementRef = React.useRef<HTMLElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, setContainer);\n\n const focusScope = React.useRef({\n paused: false,\n pause() {\n this.paused = true;\n },\n resume() {\n this.paused = false;\n },\n }).current;\n\n // Takes care of trapping focus if focus is moved outside programmatically for example\n React.useEffect(() => {\n if (trapped) {\n function handleFocusIn(event: FocusEvent) {\n if (focusScope.paused || !container) return;\n const target = event.target as HTMLElement | null;\n if (container.contains(target)) {\n lastFocusedElementRef.current = target;\n } else {\n focus(lastFocusedElementRef.current, { select: true });\n }\n }\n\n function handleFocusOut(event: FocusEvent) {\n if (focusScope.paused || !container) return;\n const relatedTarget = event.relatedTarget as HTMLElement | null;\n\n // A `focusout` event with a `null` `relatedTarget` will happen in at least two cases:\n //\n // 1. When the user switches app/tabs/windows/the browser itself loses focus.\n // 2. In Google Chrome, when the focused element is removed from the DOM.\n //\n // We let the browser do its thing here because:\n //\n // 1. The browser already keeps a memory of what's focused for when the page gets refocused.\n // 2. In Google Chrome, if we try to focus the deleted focused element (as per below), it\n // throws the CPU to 100%, so we avoid doing anything for this reason here too.\n if (relatedTarget === null) return;\n\n // If the focus has moved to an actual legitimate element (`relatedTarget !== null`)\n // that is outside the container, we move focus to the last valid focused element inside.\n if (!container.contains(relatedTarget)) {\n focus(lastFocusedElementRef.current, { select: true });\n }\n }\n\n // When the focused element gets removed from the DOM, browsers move focus\n // back to the document.body. In this case, we move focus to the container\n // to keep focus trapped correctly.\n function handleMutations(mutations: MutationRecord[]) {\n const focusedElement = document.activeElement as HTMLElement | null;\n if (focusedElement !== document.body) return;\n for (const mutation of mutations) {\n if (mutation.removedNodes.length > 0) focus(container);\n }\n }\n\n document.addEventListener('focusin', handleFocusIn);\n document.addEventListener('focusout', handleFocusOut);\n const mutationObserver = new MutationObserver(handleMutations);\n if (container) mutationObserver.observe(container, { childList: true, subtree: true });\n\n return () => {\n document.removeEventListener('focusin', handleFocusIn);\n document.removeEventListener('focusout', handleFocusOut);\n mutationObserver.disconnect();\n };\n }\n }, [trapped, container, focusScope.paused]);\n\n React.useEffect(() => {\n if (container) {\n focusScopesStack.add(focusScope);\n const previouslyFocusedElement = document.activeElement as HTMLElement | null;\n const hasFocusedCandidate = container.contains(previouslyFocusedElement);\n\n if (!hasFocusedCandidate) {\n const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);\n container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);\n container.dispatchEvent(mountEvent);\n if (!mountEvent.defaultPrevented) {\n focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });\n if (document.activeElement === previouslyFocusedElement) {\n focus(container);\n }\n }\n }\n\n return () => {\n container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);\n\n // We hit a react bug (fixed in v17) with focusing in unmount.\n // We need to delay the focus a little to get around it for now.\n // See: https://github.com/facebook/react/issues/17894\n setTimeout(() => {\n const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);\n container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);\n container.dispatchEvent(unmountEvent);\n if (!unmountEvent.defaultPrevented) {\n focus(previouslyFocusedElement ?? document.body, { select: true });\n }\n // we need to remove the listener after we `dispatchEvent`\n container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);\n\n focusScopesStack.remove(focusScope);\n }, 0);\n };\n }\n }, [container, onMountAutoFocus, onUnmountAutoFocus, focusScope]);\n\n // Takes care of looping focus (when tabbing whilst at the edges)\n const handleKeyDown = React.useCallback(\n (event: React.KeyboardEvent) => {\n if (!loop && !trapped) return;\n if (focusScope.paused) return;\n\n const isTabKey = event.key === 'Tab' && !event.altKey && !event.ctrlKey && !event.metaKey;\n const focusedElement = document.activeElement as HTMLElement | null;\n\n if (isTabKey && focusedElement) {\n const container = event.currentTarget as HTMLElement;\n const [first, last] = getTabbableEdges(container);\n const hasTabbableElementsInside = first && last;\n\n // we can only wrap focus if we have tabbable edges\n if (!hasTabbableElementsInside) {\n if (focusedElement === container) event.preventDefault();\n } else {\n if (!event.shiftKey && focusedElement === last) {\n event.preventDefault();\n if (loop) focus(first, { select: true });\n } else if (event.shiftKey && focusedElement === first) {\n event.preventDefault();\n if (loop) focus(last, { select: true });\n }\n }\n }\n },\n [loop, trapped, focusScope.paused],\n );\n\n return (\n <Primitive.div tabIndex={-1} {...scopeProps} ref={composedRefs} onKeyDown={handleKeyDown} />\n );\n },\n);\n\n/* -------------------------------------------------------------------------------------------------\n * Utils\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * Attempts focusing the first element in a list of candidates.\n * Stops when focus has actually moved.\n */\nfunction focusFirst(candidates: HTMLElement[], { select = false } = {}) {\n const previouslyFocusedElement = document.activeElement;\n for (const candidate of candidates) {\n focus(candidate, { select });\n if (document.activeElement !== previouslyFocusedElement) return;\n }\n}\n\n/**\n * Returns the first and last tabbable elements inside a container.\n */\nfunction getTabbableEdges(container: HTMLElement) {\n const candidates = getTabbableCandidates(container);\n const first = findVisible(candidates, container);\n const last = findVisible(candidates.reverse(), container);\n return [first, last] as const;\n}\n\n/**\n * Returns a list of potential tabbable candidates.\n *\n * NOTE: This is only a close approximation. For example it doesn't take into account cases like when\n * elements are not visible. This cannot be worked out easily by just reading a property, but rather\n * necessitate runtime knowledge (computed styles, etc). We deal with these cases separately.\n *\n * See: https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker\n * Credit: https://github.com/discord/focus-layers/blob/master/src/util/wrapFocus.tsx#L1\n */\nfunction getTabbableCandidates(container: HTMLElement) {\n const nodes: HTMLElement[] = [];\n const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {\n acceptNode: (node: any) => {\n const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden';\n if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP;\n // `.tabIndex` is not the same as the `tabindex` attribute. It works on the\n // runtime's understanding of tabbability, so this automatically accounts\n // for any kind of element that could be tabbed to.\n return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;\n },\n });\n while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement);\n // we do not take into account the order of nodes with positive `tabIndex` as it\n // hinders accessibility to have tab order different from visual order.\n return nodes;\n}\n\n/**\n * Returns the first visible element in a list.\n * NOTE: Only checks visibility up to the `container`.\n */\nfunction findVisible(elements: HTMLElement[], container: HTMLElement) {\n // `checkVisibility` performs a single native check for `visibility: hidden`\n // and `display: none` across the ancestor chain. This avoids the repeated\n // `getComputedStyle` calls per ancestor that the fallback path requires,\n // which reduces the cost of style resolution triggered by earlier DOM writes\n // (e.g. react-remove-scroll, DismissableLayer setting body styles).\n const canUseCheckVisibility =\n typeof container.checkVisibility === 'function' &&\n container.checkVisibility({ checkVisibilityCSS: true });\n\n for (const element of elements) {\n const hidden = canUseCheckVisibility\n ? !element.checkVisibility({ checkVisibilityCSS: true })\n : isHidden(element, { upTo: container });\n // we stop checking if it's hidden at the `container` level (excluding)\n if (!hidden) {\n return element;\n }\n }\n}\n\nfunction isHidden(node: HTMLElement, { upTo }: { upTo?: HTMLElement }) {\n if (getComputedStyle(node).visibility === 'hidden') return true;\n while (node) {\n // we stop at `upTo` (excluding it)\n if (upTo !== undefined && node === upTo) return false;\n if (getComputedStyle(node).display === 'none') return true;\n node = node.parentElement as HTMLElement;\n }\n return false;\n}\n\nfunction isSelectableInput(element: any): element is FocusableTarget & { select: () => void } {\n return element instanceof HTMLInputElement && 'select' in element;\n}\n\nfunction focus(element?: FocusableTarget | null, { select = false } = {}) {\n // only focus if that element is focusable\n if (element && element.focus) {\n const previouslyFocusedElement = document.activeElement;\n // NOTE: we prevent scrolling on focus, to minimize jarring transitions for users\n element.focus({ preventScroll: true });\n // only select if its not the same element, it supports selection and we need to select\n if (element !== previouslyFocusedElement && isSelectableInput(element) && select)\n element.select();\n }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope stack\n * -----------------------------------------------------------------------------------------------*/\n\ntype FocusScopeAPI = { paused: boolean; pause(): void; resume(): void };\nconst focusScopesStack = createFocusScopesStack();\n\nfunction createFocusScopesStack() {\n /** A stack of focus scopes, with the active one at the top */\n let stack: FocusScopeAPI[] = [];\n\n return {\n add(focusScope: FocusScopeAPI) {\n // pause the currently active focus scope (at the top of the stack)\n const activeFocusScope = stack[0];\n if (focusScope !== activeFocusScope) {\n activeFocusScope?.pause();\n }\n // remove in case it already exists (because we'll re-add it at the top of the stack)\n stack = arrayRemove(stack, focusScope);\n stack.unshift(focusScope);\n },\n\n remove(focusScope: FocusScopeAPI) {\n stack = arrayRemove(stack, focusScope);\n stack[0]?.resume();\n },\n };\n}\n\nfunction arrayRemove<T>(array: T[], item: T) {\n const updatedArray = [...array];\n const index = updatedArray.indexOf(item);\n if (index !== -1) {\n updatedArray.splice(index, 1);\n }\n return updatedArray;\n}\n\nfunction removeLinks(items: HTMLElement[]) {\n return items.filter((item) => item.tagName !== 'A');\n}\n\nexport {\n FocusScope,\n //\n FocusScope as Root,\n};\nexport type { FocusScopeProps };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,gCAAgC;AAChC,6BAA0B;AAC1B,oCAA+B;AAuMzB;AArMN,IAAM,qBAAqB;AAC3B,IAAM,uBAAuB;AAC7B,IAAM,gBAAgB,EAAE,SAAS,OAAO,YAAY,KAAK;AAsCzD,IAAM,aAA6B,gBAAM;AAAA,EACvC,gCAASA,YAAW,OAAO,cAAc;AACvC,UAAM;AAAA,MACJ,OAAO;AAAA,MACP,UAAU;AAAA,MACV,kBAAkB;AAAA,MAClB,oBAAoB;AAAA,MACpB,GAAG;AAAA,IACL,IAAI;AACJ,UAAM,CAAC,WAAW,YAAY,IAAU,eAA6B,IAAI;AACzE,UAAM,uBAAmB,8CAAe,oBAAoB;AAC5D,UAAM,yBAAqB,8CAAe,sBAAsB;AAChE,UAAM,wBAA8B,aAA2B,IAAI;AACnE,UAAM,mBAAe,2CAAgB,cAAc,YAAY;AAE/D,UAAM,aAAmB,aAAO;AAAA,MAC9B,QAAQ;AAAA,MACR,QAAQ;AACN,aAAK,SAAS;AAAA,MAChB;AAAA,MACA,SAAS;AACP,aAAK,SAAS;AAAA,MAChB;AAAA,IACF,CAAC,EAAE;AAGH,IAAM,gBAAU,MAAM;AACpB,UAAI,SAAS;AACX,YAASC,iBAAT,SAAuB,OAAmB;AACxC,cAAI,WAAW,UAAU,CAAC,UAAW;AACrC,gBAAM,SAAS,MAAM;AACrB,cAAI,UAAU,SAAS,MAAM,GAAG;AAC9B,kCAAsB,UAAU;AAAA,UAClC,OAAO;AACL,kBAAM,sBAAsB,SAAS,EAAE,QAAQ,KAAK,CAAC;AAAA,U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eAAe,CAAC,GAAG,KAAK;AAC9B,QAAM,QAAQ,aAAa,QAAQ,IAAI;AACvC,MAAI,UAAU,IAAI;AAChB,iBAAa,OAAO,OAAO,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;AAPS;AAST,SAAS,YAAY,OAAsB;AACzC,SAAO,MAAM,OAAO,CAAC,SAAS,KAAK,YAAY,GAAG;AACpD;AAFS;",
4
+ "sourcesContent": ["'use client';\nexport {\n FocusScope,\n FocusScopeBranchProvider,\n useFocusScopeBranchRegistry,\n useFocusScopeBranch,\n //\n Root,\n} from './focus-scope';\nexport type { FocusScopeProps, FocusScopeBranchRegistry } from './focus-scope';\n", "import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { Primitive } from '@radix-ui/react-primitive';\nimport { useCallbackRef } from '@radix-ui/react-use-callback-ref';\n\nconst AUTOFOCUS_ON_MOUNT = 'focusScope.autoFocusOnMount';\nconst AUTOFOCUS_ON_UNMOUNT = 'focusScope.autoFocusOnUnmount';\nconst EVENT_OPTIONS = { bubbles: false, cancelable: true };\n\ntype FocusableTarget = HTMLElement | { focus(): void };\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype FocusScopeElement = React.ComponentRef<typeof Primitive.div>;\ntype PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;\ninterface FocusScopeProps extends PrimitiveDivProps {\n /**\n * When `true`, tabbing from last item will focus first tabbable\n * and shift+tab from first item will focus last tababble.\n * @defaultValue false\n */\n loop?: boolean;\n\n /**\n * When `true`, focus cannot escape the focus scope via keyboard,\n * pointer, or a programmatic focus.\n * @defaultValue false\n */\n trapped?: boolean;\n\n /**\n * A list of nodes that should be treated as part of the focus scope even\n * though they don't live within the scope's DOM subtree (eg. portalled\n * content of a nested, non-modal layer). When the scope is `trapped`, focus\n * is allowed to move into these branches without being reclaimed.\n *\n * See: https://github.com/radix-ui/primitives/issues/3423\n */\n branches?: HTMLElement[];\n\n /**\n * Event handler called when auto-focusing on mount.\n * Can be prevented.\n */\n onMountAutoFocus?: (event: Event) => void;\n\n /**\n * Event handler called when auto-focusing on unmount.\n * Can be prevented.\n */\n onUnmountAutoFocus?: (event: Event) => void;\n}\n\nconst FocusScope = /* @__PURE__ */ React.forwardRef<FocusScopeElement, FocusScopeProps>(\n function FocusScope(props, forwardedRef) {\n const {\n loop = false,\n trapped = false,\n branches,\n onMountAutoFocus: onMountAutoFocusProp,\n onUnmountAutoFocus: onUnmountAutoFocusProp,\n ...scopeProps\n } = props;\n const [container, setContainer] = React.useState<HTMLElement | null>(null);\n const onMountAutoFocus = useCallbackRef(onMountAutoFocusProp);\n const onUnmountAutoFocus = useCallbackRef(onUnmountAutoFocusProp);\n const lastFocusedElementRef = React.useRef<HTMLElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, setContainer);\n\n // Keep the latest branches in a ref so the trap effect below doesn't need to resubscribe its\n // listeners whenever the branch list updates. We sync it in the commit phase (not during render)\n // to stay safe under concurrent rendering, where a render can be discarded or replayed. The trap's\n // focus event handlers only run in response to user interaction (well after commit), so reading\n // the ref from them always sees the committed branch list.\n const branchesRef = React.useRef(branches);\n React.useEffect(() => {\n branchesRef.current = branches;\n });\n const isTargetInScope = React.useCallback(\n (target: HTMLElement | null) => {\n if (!target) return false;\n if (container?.contains(target)) return true;\n return Boolean(branchesRef.current?.some((branch) => branch.contains(target)));\n },\n [container],\n );\n\n const focusScope = React.useRef({\n paused: false,\n pause() {\n this.paused = true;\n },\n resume() {\n this.paused = false;\n },\n }).current;\n\n // Takes care of trapping focus if focus is moved outside programmatically for example\n React.useEffect(() => {\n if (trapped) {\n function handleFocusIn(event: FocusEvent) {\n if (focusScope.paused || !container) return;\n const target = event.target as HTMLElement | null;\n if (isTargetInScope(target)) {\n lastFocusedElementRef.current = target;\n } else {\n focus(lastFocusedElementRef.current, { select: true });\n }\n }\n\n function handleFocusOut(event: FocusEvent) {\n if (focusScope.paused || !container) return;\n const relatedTarget = event.relatedTarget as HTMLElement | null;\n\n // A `focusout` event with a `null` `relatedTarget` will happen in at\n // least two cases:\n // 1. When the user switches app/tabs/windows/the browser itself loses\n // focus.\n // 2. In Google Chrome, when the focused element is removed from the\n // DOM.\n //\n // We let the browser do its thing here because:\n // 1. The browser already keeps a memory of what's focused for when\n // the page gets refocused.\n // 2. In Google Chrome, if we try to focus the deleted focused element\n // (as per below), it throws the CPU to 100%, so we avoid doing\n // anything for this reason here too.\n if (relatedTarget === null) return;\n\n // If the focus has moved to an actual legitimate element\n // (`relatedTarget !== null`) that is outside the container (and any\n // registered branches), we move focus to the last valid focused\n // element inside.\n if (!isTargetInScope(relatedTarget)) {\n focus(lastFocusedElementRef.current, { select: true });\n }\n }\n\n // When the focused element gets removed from the DOM, browsers move focus\n // back to the document.body. In this case, we move focus to the container\n // to keep focus trapped correctly.\n function handleMutations(mutations: MutationRecord[]) {\n const focusedElement = document.activeElement as HTMLElement | null;\n if (focusedElement !== document.body) return;\n for (const mutation of mutations) {\n if (mutation.removedNodes.length > 0) focus(container);\n }\n }\n\n document.addEventListener('focusin', handleFocusIn);\n document.addEventListener('focusout', handleFocusOut);\n const mutationObserver = new MutationObserver(handleMutations);\n if (container) mutationObserver.observe(container, { childList: true, subtree: true });\n\n return () => {\n document.removeEventListener('focusin', handleFocusIn);\n document.removeEventListener('focusout', handleFocusOut);\n mutationObserver.disconnect();\n };\n }\n }, [trapped, container, focusScope.paused, isTargetInScope]);\n\n React.useEffect(() => {\n if (container) {\n focusScopesStack.add(focusScope);\n const previouslyFocusedElement = document.activeElement as HTMLElement | null;\n const hasFocusedCandidate = container.contains(previouslyFocusedElement);\n\n if (!hasFocusedCandidate) {\n const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);\n container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);\n container.dispatchEvent(mountEvent);\n if (!mountEvent.defaultPrevented) {\n focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });\n if (document.activeElement === previouslyFocusedElement) {\n focus(container);\n }\n }\n }\n\n return () => {\n container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);\n\n // We hit a react bug (fixed in v17) with focusing in unmount.\n // We need to delay the focus a little to get around it for now.\n // See: https://github.com/facebook/react/issues/17894\n setTimeout(() => {\n const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);\n container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);\n container.dispatchEvent(unmountEvent);\n if (!unmountEvent.defaultPrevented) {\n focus(previouslyFocusedElement ?? document.body, { select: true });\n }\n // we need to remove the listener after we `dispatchEvent`\n container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);\n\n focusScopesStack.remove(focusScope);\n }, 0);\n };\n }\n }, [container, onMountAutoFocus, onUnmountAutoFocus, focusScope]);\n\n // Takes care of looping focus (when tabbing whilst at the edges)\n const handleKeyDown = React.useCallback(\n (event: React.KeyboardEvent) => {\n if (!loop && !trapped) return;\n if (focusScope.paused) return;\n\n const isTabKey = event.key === 'Tab' && !event.altKey && !event.ctrlKey && !event.metaKey;\n const focusedElement = document.activeElement as HTMLElement | null;\n\n if (isTabKey && focusedElement) {\n const container = event.currentTarget as HTMLElement;\n const [first, last] = getTabbableEdges(container);\n const hasTabbableElementsInside = first && last;\n\n // we can only wrap focus if we have tabbable edges\n if (!hasTabbableElementsInside) {\n if (focusedElement === container) event.preventDefault();\n } else {\n if (!event.shiftKey && focusedElement === last) {\n event.preventDefault();\n if (loop) focus(first, { select: true });\n } else if (event.shiftKey && focusedElement === first) {\n event.preventDefault();\n if (loop) focus(last, { select: true });\n }\n }\n }\n },\n [loop, trapped, focusScope.paused],\n );\n\n return (\n <Primitive.div tabIndex={-1} {...scopeProps} ref={composedRefs} onKeyDown={handleKeyDown} />\n );\n },\n);\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope branch registry\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * Lets portalled content that is a React descendant of a (modal) layer \u2014 but rendered outside of\n * that layer's DOM subtree \u2014 register itself as a \"branch\" of the layer's focus scope.\n *\n * A modal container (eg. `Dialog`) creates a registry via `useFocusScopeBranchRegistry`, renders a\n * `FocusScopeBranchProvider` around its children, and passes the collected `nodes` to its trapped\n * `FocusScope` (so focus isn't reclaimed from the branch) and, if applicable, to its `RemoveScroll`\n * `shards` (so the branch remains scrollable). Nested layers (eg. a non-modal `Popover`) register\n * their content node with `useFocusScopeBranch`.\n *\n * See: https://github.com/radix-ui/primitives/issues/3423\n */\ninterface FocusScopeBranchRegistry {\n add: (node: HTMLElement) => void;\n remove: (node: HTMLElement) => void;\n}\n\nconst FocusScopeBranchContext = React.createContext<FocusScopeBranchRegistry | null>(null);\n\nconst FocusScopeBranchProvider = FocusScopeBranchContext.Provider;\n\nfunction useFocusScopeBranchRegistry() {\n const [nodes, setNodes] = React.useState<HTMLElement[]>([]);\n const registry = React.useMemo<FocusScopeBranchRegistry>(\n () => ({\n add: (node) => setNodes((prev) => (prev.includes(node) ? prev : [...prev, node])),\n remove: (node) => setNodes((prev) => prev.filter((current) => current !== node)),\n }),\n [],\n );\n return { nodes, registry } as const;\n}\n\n/**\n * Registers `node` as a branch of the nearest ancestor `FocusScopeBranchProvider`. No-ops when\n * there is no ancestor registry (eg. the layer isn't nested inside a modal layer).\n */\nfunction useFocusScopeBranch(node: HTMLElement | null) {\n const registry = React.useContext(FocusScopeBranchContext);\n React.useEffect(() => {\n if (!node || !registry) return;\n registry.add(node);\n return () => registry.remove(node);\n }, [node, registry]);\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Utils\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * Attempts focusing the first element in a list of candidates.\n * Stops when focus has actually moved.\n */\nfunction focusFirst(candidates: HTMLElement[], { select = false } = {}) {\n const previouslyFocusedElement = document.activeElement;\n for (const candidate of candidates) {\n focus(candidate, { select });\n if (document.activeElement !== previouslyFocusedElement) return;\n }\n}\n\n/**\n * Returns the first and last tabbable elements inside a container.\n */\nfunction getTabbableEdges(container: HTMLElement) {\n const candidates = getTabbableCandidates(container);\n const first = findVisible(candidates, container);\n const last = findVisible(candidates.reverse(), container);\n return [first, last] as const;\n}\n\n/**\n * Returns a list of potential tabbable candidates.\n *\n * NOTE: This is only a close approximation. For example it doesn't take into account cases like when\n * elements are not visible. This cannot be worked out easily by just reading a property, but rather\n * necessitate runtime knowledge (computed styles, etc). We deal with these cases separately.\n *\n * See: https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker\n * Credit: https://github.com/discord/focus-layers/blob/master/src/util/wrapFocus.tsx#L1\n */\nfunction getTabbableCandidates(container: HTMLElement) {\n const nodes: HTMLElement[] = [];\n const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {\n acceptNode: (node: any) => {\n const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden';\n if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP;\n // `.tabIndex` is not the same as the `tabindex` attribute. It works on the\n // runtime's understanding of tabbability, so this automatically accounts\n // for any kind of element that could be tabbed to.\n return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;\n },\n });\n while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement);\n // we do not take into account the order of nodes with positive `tabIndex` as it\n // hinders accessibility to have tab order different from visual order.\n return nodes;\n}\n\n/**\n * Returns the first visible element in a list.\n * NOTE: Only checks visibility up to the `container`.\n */\nfunction findVisible(elements: HTMLElement[], container: HTMLElement) {\n // `checkVisibility` performs a single native check for `visibility: hidden`\n // and `display: none` across the ancestor chain. This avoids the repeated\n // `getComputedStyle` calls per ancestor that the fallback path requires,\n // which reduces the cost of style resolution triggered by earlier DOM writes\n // (e.g. react-remove-scroll, DismissableLayer setting body styles).\n const canUseCheckVisibility =\n typeof container.checkVisibility === 'function' &&\n container.checkVisibility({ checkVisibilityCSS: true });\n\n for (const element of elements) {\n const hidden = canUseCheckVisibility\n ? !element.checkVisibility({ checkVisibilityCSS: true })\n : isHidden(element, { upTo: container });\n // we stop checking if it's hidden at the `container` level (excluding)\n if (!hidden) {\n return element;\n }\n }\n}\n\nfunction isHidden(node: HTMLElement, { upTo }: { upTo?: HTMLElement }) {\n if (getComputedStyle(node).visibility === 'hidden') return true;\n while (node) {\n // we stop at `upTo` (excluding it)\n if (upTo !== undefined && node === upTo) return false;\n if (getComputedStyle(node).display === 'none') return true;\n node = node.parentElement as HTMLElement;\n }\n return false;\n}\n\nfunction isSelectableInput(element: any): element is FocusableTarget & { select: () => void } {\n return element instanceof HTMLInputElement && 'select' in element;\n}\n\nfunction focus(element?: FocusableTarget | null, { select = false } = {}) {\n // only focus if that element is focusable\n if (element && element.focus) {\n const previouslyFocusedElement = document.activeElement;\n // NOTE: we prevent scrolling on focus, to minimize jarring transitions for users\n element.focus({ preventScroll: true });\n // only select if its not the same element, it supports selection and we need to select\n if (element !== previouslyFocusedElement && isSelectableInput(element) && select)\n element.select();\n }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope stack\n * -----------------------------------------------------------------------------------------------*/\n\ntype FocusScopeAPI = { paused: boolean; pause(): void; resume(): void };\nconst focusScopesStack = createFocusScopesStack();\n\nfunction createFocusScopesStack() {\n /** A stack of focus scopes, with the active one at the top */\n let stack: FocusScopeAPI[] = [];\n\n return {\n add(focusScope: FocusScopeAPI) {\n // pause the currently active focus scope (at the top of the stack)\n const activeFocusScope = stack[0];\n if (focusScope !== activeFocusScope) {\n activeFocusScope?.pause();\n }\n // remove in case it already exists (because we'll re-add it at the top of the stack)\n stack = arrayRemove(stack, focusScope);\n stack.unshift(focusScope);\n },\n\n remove(focusScope: FocusScopeAPI) {\n stack = arrayRemove(stack, focusScope);\n stack[0]?.resume();\n },\n };\n}\n\nfunction arrayRemove<T>(array: T[], item: T) {\n const updatedArray = [...array];\n const index = updatedArray.indexOf(item);\n if (index !== -1) {\n updatedArray.splice(index, 1);\n }\n return updatedArray;\n}\n\nfunction removeLinks(items: HTMLElement[]) {\n return items.filter((item) => item.tagName !== 'A');\n}\n\nexport {\n FocusScope,\n FocusScopeBranchProvider,\n useFocusScopeBranchRegistry,\n useFocusScopeBranch,\n //\n FocusScope as Root,\n};\nexport type { FocusScopeProps, FocusScopeBranchRegistry };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,gCAAgC;AAChC,6BAA0B;AAC1B,oCAA+B;AAyOzB;AAvON,IAAM,qBAAqB;AAC3B,IAAM,uBAAuB;AAC7B,IAAM,gBAAgB,EAAE,SAAS,OAAO,YAAY,KAAK;AAgDzD,IAAM,aAA6B,gBAAM;AAAA,EACvC,gCAASA,YAAW,OAAO,cAAc;AACvC,UAAM;AAAA,MACJ,OAAO;AAAA,MACP,UAAU;AAAA,MACV;AAAA,MACA,kBAAkB;AAAA,MAClB,oBAAoB;AAAA,MACpB,GAAG;AAAA,IACL,IAAI;AACJ,UAAM,CAAC,WAAW,YAAY,IAAU,eAA6B,IAAI;AACzE,UAAM,uBAAmB,8CAAe,oBAAoB;AAC5D,UAAM,yBAAqB,8CAAe,sBAAsB;AAChE,UAAM,wBAA8B,aAA2B,IAAI;AACnE,UAAM,mBAAe,2CAAgB,cAAc,YAAY;AAO/D,UAAM,cAAoB,aAAO,QAAQ;AACzC,IAAM,gBAAU,MAAM;AACpB,kBAAY,UAAU;AAAA,IACxB,CAAC;AACD,UAAM,kBAAwB;AAAA,MAC5B,CAAC,WAA+B;AAC9B,YAAI,CAAC,OAAQ,QAAO;AACpB,YAAI,WAAW,SAAS,MAAM,EAAG,QAAO;AACxC,eAAO,QAAQ,YAAY,SAAS,KAAK,CAAC,WAAW,OAAO,SAAS,MAAM,CAAC,CAAC;AAAA,MAC/E;AAAA,MACA,CAAC,SAAS;AAAA,IACZ;A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AU,IAAI;AAChB,iBAAa,OAAO,OAAO,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;AAPS;AAST,SAAS,YAAY,OAAsB;AACzC,SAAO,MAAM,OAAO,CAAC,SAAS,KAAK,YAAY,GAAG;AACpD;AAFS;",
6
6
  "names": ["FocusScope", "handleFocusIn", "handleFocusOut", "handleMutations", "container"]
7
7
  }
package/dist/index.mjs CHANGED
@@ -16,6 +16,7 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
16
16
  const {
17
17
  loop = false,
18
18
  trapped = false,
19
+ branches,
19
20
  onMountAutoFocus: onMountAutoFocusProp,
20
21
  onUnmountAutoFocus: onUnmountAutoFocusProp,
21
22
  ...scopeProps
@@ -25,6 +26,18 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
25
26
  const onUnmountAutoFocus = useCallbackRef(onUnmountAutoFocusProp);
26
27
  const lastFocusedElementRef = React.useRef(null);
27
28
  const composedRefs = useComposedRefs(forwardedRef, setContainer);
29
+ const branchesRef = React.useRef(branches);
30
+ React.useEffect(() => {
31
+ branchesRef.current = branches;
32
+ });
33
+ const isTargetInScope = React.useCallback(
34
+ (target) => {
35
+ if (!target) return false;
36
+ if (container?.contains(target)) return true;
37
+ return Boolean(branchesRef.current?.some((branch) => branch.contains(target)));
38
+ },
39
+ [container]
40
+ );
28
41
  const focusScope = React.useRef({
29
42
  paused: false,
30
43
  pause() {
@@ -39,7 +52,7 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
39
52
  let handleFocusIn2 = function(event) {
40
53
  if (focusScope.paused || !container) return;
41
54
  const target = event.target;
42
- if (container.contains(target)) {
55
+ if (isTargetInScope(target)) {
43
56
  lastFocusedElementRef.current = target;
44
57
  } else {
45
58
  focus(lastFocusedElementRef.current, { select: true });
@@ -48,7 +61,7 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
48
61
  if (focusScope.paused || !container) return;
49
62
  const relatedTarget = event.relatedTarget;
50
63
  if (relatedTarget === null) return;
51
- if (!container.contains(relatedTarget)) {
64
+ if (!isTargetInScope(relatedTarget)) {
52
65
  focus(lastFocusedElementRef.current, { select: true });
53
66
  }
54
67
  }, handleMutations2 = function(mutations) {
@@ -72,7 +85,7 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
72
85
  mutationObserver.disconnect();
73
86
  };
74
87
  }
75
- }, [trapped, container, focusScope.paused]);
88
+ }, [trapped, container, focusScope.paused, isTargetInScope]);
76
89
  React.useEffect(() => {
77
90
  if (container) {
78
91
  focusScopesStack.add(focusScope);
@@ -132,6 +145,29 @@ var FocusScope = /* @__PURE__ */ React.forwardRef(
132
145
  return /* @__PURE__ */ jsx(Primitive.div, { tabIndex: -1, ...scopeProps, ref: composedRefs, onKeyDown: handleKeyDown });
133
146
  }, "FocusScope")
134
147
  );
148
+ var FocusScopeBranchContext = React.createContext(null);
149
+ var FocusScopeBranchProvider = FocusScopeBranchContext.Provider;
150
+ function useFocusScopeBranchRegistry() {
151
+ const [nodes, setNodes] = React.useState([]);
152
+ const registry = React.useMemo(
153
+ () => ({
154
+ add: /* @__PURE__ */ __name((node) => setNodes((prev) => prev.includes(node) ? prev : [...prev, node]), "add"),
155
+ remove: /* @__PURE__ */ __name((node) => setNodes((prev) => prev.filter((current) => current !== node)), "remove")
156
+ }),
157
+ []
158
+ );
159
+ return { nodes, registry };
160
+ }
161
+ __name(useFocusScopeBranchRegistry, "useFocusScopeBranchRegistry");
162
+ function useFocusScopeBranch(node) {
163
+ const registry = React.useContext(FocusScopeBranchContext);
164
+ React.useEffect(() => {
165
+ if (!node || !registry) return;
166
+ registry.add(node);
167
+ return () => registry.remove(node);
168
+ }, [node, registry]);
169
+ }
170
+ __name(useFocusScopeBranch, "useFocusScopeBranch");
135
171
  function focusFirst(candidates, { select = false } = {}) {
136
172
  const previouslyFocusedElement = document.activeElement;
137
173
  for (const candidate of candidates) {
@@ -227,6 +263,9 @@ function removeLinks(items) {
227
263
  __name(removeLinks, "removeLinks");
228
264
  export {
229
265
  FocusScope,
230
- FocusScope as Root
266
+ FocusScopeBranchProvider,
267
+ FocusScope as Root,
268
+ useFocusScopeBranch,
269
+ useFocusScopeBranchRegistry
231
270
  };
232
271
  //# sourceMappingURL=index.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/focus-scope.tsx"],
4
- "sourcesContent": ["import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { Primitive } from '@radix-ui/react-primitive';\nimport { useCallbackRef } from '@radix-ui/react-use-callback-ref';\n\nconst AUTOFOCUS_ON_MOUNT = 'focusScope.autoFocusOnMount';\nconst AUTOFOCUS_ON_UNMOUNT = 'focusScope.autoFocusOnUnmount';\nconst EVENT_OPTIONS = { bubbles: false, cancelable: true };\n\ntype FocusableTarget = HTMLElement | { focus(): void };\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype FocusScopeElement = React.ComponentRef<typeof Primitive.div>;\ntype PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;\ninterface FocusScopeProps extends PrimitiveDivProps {\n /**\n * When `true`, tabbing from last item will focus first tabbable\n * and shift+tab from first item will focus last tababble.\n * @defaultValue false\n */\n loop?: boolean;\n\n /**\n * When `true`, focus cannot escape the focus scope via keyboard,\n * pointer, or a programmatic focus.\n * @defaultValue false\n */\n trapped?: boolean;\n\n /**\n * Event handler called when auto-focusing on mount.\n * Can be prevented.\n */\n onMountAutoFocus?: (event: Event) => void;\n\n /**\n * Event handler called when auto-focusing on unmount.\n * Can be prevented.\n */\n onUnmountAutoFocus?: (event: Event) => void;\n}\n\nconst FocusScope = /* @__PURE__ */ React.forwardRef<FocusScopeElement, FocusScopeProps>(\n function FocusScope(props, forwardedRef) {\n const {\n loop = false,\n trapped = false,\n onMountAutoFocus: onMountAutoFocusProp,\n onUnmountAutoFocus: onUnmountAutoFocusProp,\n ...scopeProps\n } = props;\n const [container, setContainer] = React.useState<HTMLElement | null>(null);\n const onMountAutoFocus = useCallbackRef(onMountAutoFocusProp);\n const onUnmountAutoFocus = useCallbackRef(onUnmountAutoFocusProp);\n const lastFocusedElementRef = React.useRef<HTMLElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, setContainer);\n\n const focusScope = React.useRef({\n paused: false,\n pause() {\n this.paused = true;\n },\n resume() {\n this.paused = false;\n },\n }).current;\n\n // Takes care of trapping focus if focus is moved outside programmatically for example\n React.useEffect(() => {\n if (trapped) {\n function handleFocusIn(event: FocusEvent) {\n if (focusScope.paused || !container) return;\n const target = event.target as HTMLElement | null;\n if (container.contains(target)) {\n lastFocusedElementRef.current = target;\n } else {\n focus(lastFocusedElementRef.current, { select: true });\n }\n }\n\n function handleFocusOut(event: FocusEvent) {\n if (focusScope.paused || !container) return;\n const relatedTarget = event.relatedTarget as HTMLElement | null;\n\n // A `focusout` event with a `null` `relatedTarget` will happen in at least two cases:\n //\n // 1. When the user switches app/tabs/windows/the browser itself loses focus.\n // 2. In Google Chrome, when the focused element is removed from the DOM.\n //\n // We let the browser do its thing here because:\n //\n // 1. The browser already keeps a memory of what's focused for when the page gets refocused.\n // 2. In Google Chrome, if we try to focus the deleted focused element (as per below), it\n // throws the CPU to 100%, so we avoid doing anything for this reason here too.\n if (relatedTarget === null) return;\n\n // If the focus has moved to an actual legitimate element (`relatedTarget !== null`)\n // that is outside the container, we move focus to the last valid focused element inside.\n if (!container.contains(relatedTarget)) {\n focus(lastFocusedElementRef.current, { select: true });\n }\n }\n\n // When the focused element gets removed from the DOM, browsers move focus\n // back to the document.body. In this case, we move focus to the container\n // to keep focus trapped correctly.\n function handleMutations(mutations: MutationRecord[]) {\n const focusedElement = document.activeElement as HTMLElement | null;\n if (focusedElement !== document.body) return;\n for (const mutation of mutations) {\n if (mutation.removedNodes.length > 0) focus(container);\n }\n }\n\n document.addEventListener('focusin', handleFocusIn);\n document.addEventListener('focusout', handleFocusOut);\n const mutationObserver = new MutationObserver(handleMutations);\n if (container) mutationObserver.observe(container, { childList: true, subtree: true });\n\n return () => {\n document.removeEventListener('focusin', handleFocusIn);\n document.removeEventListener('focusout', handleFocusOut);\n mutationObserver.disconnect();\n };\n }\n }, [trapped, container, focusScope.paused]);\n\n React.useEffect(() => {\n if (container) {\n focusScopesStack.add(focusScope);\n const previouslyFocusedElement = document.activeElement as HTMLElement | null;\n const hasFocusedCandidate = container.contains(previouslyFocusedElement);\n\n if (!hasFocusedCandidate) {\n const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);\n container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);\n container.dispatchEvent(mountEvent);\n if (!mountEvent.defaultPrevented) {\n focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });\n if (document.activeElement === previouslyFocusedElement) {\n focus(container);\n }\n }\n }\n\n return () => {\n container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);\n\n // We hit a react bug (fixed in v17) with focusing in unmount.\n // We need to delay the focus a little to get around it for now.\n // See: https://github.com/facebook/react/issues/17894\n setTimeout(() => {\n const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);\n container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);\n container.dispatchEvent(unmountEvent);\n if (!unmountEvent.defaultPrevented) {\n focus(previouslyFocusedElement ?? document.body, { select: true });\n }\n // we need to remove the listener after we `dispatchEvent`\n container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);\n\n focusScopesStack.remove(focusScope);\n }, 0);\n };\n }\n }, [container, onMountAutoFocus, onUnmountAutoFocus, focusScope]);\n\n // Takes care of looping focus (when tabbing whilst at the edges)\n const handleKeyDown = React.useCallback(\n (event: React.KeyboardEvent) => {\n if (!loop && !trapped) return;\n if (focusScope.paused) return;\n\n const isTabKey = event.key === 'Tab' && !event.altKey && !event.ctrlKey && !event.metaKey;\n const focusedElement = document.activeElement as HTMLElement | null;\n\n if (isTabKey && focusedElement) {\n const container = event.currentTarget as HTMLElement;\n const [first, last] = getTabbableEdges(container);\n const hasTabbableElementsInside = first && last;\n\n // we can only wrap focus if we have tabbable edges\n if (!hasTabbableElementsInside) {\n if (focusedElement === container) event.preventDefault();\n } else {\n if (!event.shiftKey && focusedElement === last) {\n event.preventDefault();\n if (loop) focus(first, { select: true });\n } else if (event.shiftKey && focusedElement === first) {\n event.preventDefault();\n if (loop) focus(last, { select: true });\n }\n }\n }\n },\n [loop, trapped, focusScope.paused],\n );\n\n return (\n <Primitive.div tabIndex={-1} {...scopeProps} ref={composedRefs} onKeyDown={handleKeyDown} />\n );\n },\n);\n\n/* -------------------------------------------------------------------------------------------------\n * Utils\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * Attempts focusing the first element in a list of candidates.\n * Stops when focus has actually moved.\n */\nfunction focusFirst(candidates: HTMLElement[], { select = false } = {}) {\n const previouslyFocusedElement = document.activeElement;\n for (const candidate of candidates) {\n focus(candidate, { select });\n if (document.activeElement !== previouslyFocusedElement) return;\n }\n}\n\n/**\n * Returns the first and last tabbable elements inside a container.\n */\nfunction getTabbableEdges(container: HTMLElement) {\n const candidates = getTabbableCandidates(container);\n const first = findVisible(candidates, container);\n const last = findVisible(candidates.reverse(), container);\n return [first, last] as const;\n}\n\n/**\n * Returns a list of potential tabbable candidates.\n *\n * NOTE: This is only a close approximation. For example it doesn't take into account cases like when\n * elements are not visible. This cannot be worked out easily by just reading a property, but rather\n * necessitate runtime knowledge (computed styles, etc). We deal with these cases separately.\n *\n * See: https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker\n * Credit: https://github.com/discord/focus-layers/blob/master/src/util/wrapFocus.tsx#L1\n */\nfunction getTabbableCandidates(container: HTMLElement) {\n const nodes: HTMLElement[] = [];\n const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {\n acceptNode: (node: any) => {\n const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden';\n if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP;\n // `.tabIndex` is not the same as the `tabindex` attribute. It works on the\n // runtime's understanding of tabbability, so this automatically accounts\n // for any kind of element that could be tabbed to.\n return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;\n },\n });\n while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement);\n // we do not take into account the order of nodes with positive `tabIndex` as it\n // hinders accessibility to have tab order different from visual order.\n return nodes;\n}\n\n/**\n * Returns the first visible element in a list.\n * NOTE: Only checks visibility up to the `container`.\n */\nfunction findVisible(elements: HTMLElement[], container: HTMLElement) {\n // `checkVisibility` performs a single native check for `visibility: hidden`\n // and `display: none` across the ancestor chain. This avoids the repeated\n // `getComputedStyle` calls per ancestor that the fallback path requires,\n // which reduces the cost of style resolution triggered by earlier DOM writes\n // (e.g. react-remove-scroll, DismissableLayer setting body styles).\n const canUseCheckVisibility =\n typeof container.checkVisibility === 'function' &&\n container.checkVisibility({ checkVisibilityCSS: true });\n\n for (const element of elements) {\n const hidden = canUseCheckVisibility\n ? !element.checkVisibility({ checkVisibilityCSS: true })\n : isHidden(element, { upTo: container });\n // we stop checking if it's hidden at the `container` level (excluding)\n if (!hidden) {\n return element;\n }\n }\n}\n\nfunction isHidden(node: HTMLElement, { upTo }: { upTo?: HTMLElement }) {\n if (getComputedStyle(node).visibility === 'hidden') return true;\n while (node) {\n // we stop at `upTo` (excluding it)\n if (upTo !== undefined && node === upTo) return false;\n if (getComputedStyle(node).display === 'none') return true;\n node = node.parentElement as HTMLElement;\n }\n return false;\n}\n\nfunction isSelectableInput(element: any): element is FocusableTarget & { select: () => void } {\n return element instanceof HTMLInputElement && 'select' in element;\n}\n\nfunction focus(element?: FocusableTarget | null, { select = false } = {}) {\n // only focus if that element is focusable\n if (element && element.focus) {\n const previouslyFocusedElement = document.activeElement;\n // NOTE: we prevent scrolling on focus, to minimize jarring transitions for users\n element.focus({ preventScroll: true });\n // only select if its not the same element, it supports selection and we need to select\n if (element !== previouslyFocusedElement && isSelectableInput(element) && select)\n element.select();\n }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope stack\n * -----------------------------------------------------------------------------------------------*/\n\ntype FocusScopeAPI = { paused: boolean; pause(): void; resume(): void };\nconst focusScopesStack = createFocusScopesStack();\n\nfunction createFocusScopesStack() {\n /** A stack of focus scopes, with the active one at the top */\n let stack: FocusScopeAPI[] = [];\n\n return {\n add(focusScope: FocusScopeAPI) {\n // pause the currently active focus scope (at the top of the stack)\n const activeFocusScope = stack[0];\n if (focusScope !== activeFocusScope) {\n activeFocusScope?.pause();\n }\n // remove in case it already exists (because we'll re-add it at the top of the stack)\n stack = arrayRemove(stack, focusScope);\n stack.unshift(focusScope);\n },\n\n remove(focusScope: FocusScopeAPI) {\n stack = arrayRemove(stack, focusScope);\n stack[0]?.resume();\n },\n };\n}\n\nfunction arrayRemove<T>(array: T[], item: T) {\n const updatedArray = [...array];\n const index = updatedArray.indexOf(item);\n if (index !== -1) {\n updatedArray.splice(index, 1);\n }\n return updatedArray;\n}\n\nfunction removeLinks(items: HTMLElement[]) {\n return items.filter((item) => item.tagName !== 'A');\n}\n\nexport {\n FocusScope,\n //\n FocusScope as Root,\n};\nexport type { FocusScopeProps };\n"],
5
- "mappings": ";;;;;AAAA,YAAY,WAAW;AACvB,SAAS,uBAAuB;AAChC,SAAS,iBAAiB;AAC1B,SAAS,sBAAsB;AAuMzB;AArMN,IAAM,qBAAqB;AAC3B,IAAM,uBAAuB;AAC7B,IAAM,gBAAgB,EAAE,SAAS,OAAO,YAAY,KAAK;AAsCzD,IAAM,aAA6B,gBAAM;AAAA,EACvC,gCAASA,YAAW,OAAO,cAAc;AACvC,UAAM;AAAA,MACJ,OAAO;AAAA,MACP,UAAU;AAAA,MACV,kBAAkB;AAAA,MAClB,oBAAoB;AAAA,MACpB,GAAG;AAAA,IACL,IAAI;AACJ,UAAM,CAAC,WAAW,YAAY,IAAU,eAA6B,IAAI;AACzE,U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AAQ,MAAM,EAAE,eAAe,KAAK,CAAC;AAErC,QAAI,YAAY,4BAA4B,kBAAkB,OAAO,KAAK;AACxE,cAAQ,OAAO;AAAA,EACnB;AACF;AAVS;AAiBT,IAAM,mBAAmB,uBAAuB;AAEhD,SAAS,yBAAyB;AAEhC,MAAI,QAAyB,CAAC;AAE9B,SAAO;AAAA,IACL,IAAI,YAA2B;AAE7B,YAAM,mBAAmB,MAAM,CAAC;AAChC,UAAI,eAAe,kBAAkB;AACnC,0BAAkB,MAAM;AAAA,MAC1B;AAEA,cAAQ,YAAY,OAAO,UAAU;AACrC,YAAM,QAAQ,UAAU;AAAA,IAC1B;AAAA,IAEA,OAAO,YAA2B;AAChC,cAAQ,YAAY,OAAO,UAAU;AACrC,YAAM,CAAC,GAAG,OAAO;AAAA,IACnB;AAAA,EACF;AACF;AArBS;AAuBT,SAAS,YAAe,OAAY,MAAS;AAC3C,QAAM,eAAe,CAAC,GAAG,KAAK;AAC9B,QAAM,QAAQ,aAAa,QAAQ,IAAI;AACvC,MAAI,UAAU,IAAI;AAChB,iBAAa,OAAO,OAAO,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;AAPS;AAST,SAAS,YAAY,OAAsB;AACzC,SAAO,MAAM,OAAO,CAAC,SAAS,KAAK,YAAY,GAAG;AACpD;AAFS;",
4
+ "sourcesContent": ["import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { Primitive } from '@radix-ui/react-primitive';\nimport { useCallbackRef } from '@radix-ui/react-use-callback-ref';\n\nconst AUTOFOCUS_ON_MOUNT = 'focusScope.autoFocusOnMount';\nconst AUTOFOCUS_ON_UNMOUNT = 'focusScope.autoFocusOnUnmount';\nconst EVENT_OPTIONS = { bubbles: false, cancelable: true };\n\ntype FocusableTarget = HTMLElement | { focus(): void };\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype FocusScopeElement = React.ComponentRef<typeof Primitive.div>;\ntype PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;\ninterface FocusScopeProps extends PrimitiveDivProps {\n /**\n * When `true`, tabbing from last item will focus first tabbable\n * and shift+tab from first item will focus last tababble.\n * @defaultValue false\n */\n loop?: boolean;\n\n /**\n * When `true`, focus cannot escape the focus scope via keyboard,\n * pointer, or a programmatic focus.\n * @defaultValue false\n */\n trapped?: boolean;\n\n /**\n * A list of nodes that should be treated as part of the focus scope even\n * though they don't live within the scope's DOM subtree (eg. portalled\n * content of a nested, non-modal layer). When the scope is `trapped`, focus\n * is allowed to move into these branches without being reclaimed.\n *\n * See: https://github.com/radix-ui/primitives/issues/3423\n */\n branches?: HTMLElement[];\n\n /**\n * Event handler called when auto-focusing on mount.\n * Can be prevented.\n */\n onMountAutoFocus?: (event: Event) => void;\n\n /**\n * Event handler called when auto-focusing on unmount.\n * Can be prevented.\n */\n onUnmountAutoFocus?: (event: Event) => void;\n}\n\nconst FocusScope = /* @__PURE__ */ React.forwardRef<FocusScopeElement, FocusScopeProps>(\n function FocusScope(props, forwardedRef) {\n const {\n loop = false,\n trapped = false,\n branches,\n onMountAutoFocus: onMountAutoFocusProp,\n onUnmountAutoFocus: onUnmountAutoFocusProp,\n ...scopeProps\n } = props;\n const [container, setContainer] = React.useState<HTMLElement | null>(null);\n const onMountAutoFocus = useCallbackRef(onMountAutoFocusProp);\n const onUnmountAutoFocus = useCallbackRef(onUnmountAutoFocusProp);\n const lastFocusedElementRef = React.useRef<HTMLElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, setContainer);\n\n // Keep the latest branches in a ref so the trap effect below doesn't need to resubscribe its\n // listeners whenever the branch list updates. We sync it in the commit phase (not during render)\n // to stay safe under concurrent rendering, where a render can be discarded or replayed. The trap's\n // focus event handlers only run in response to user interaction (well after commit), so reading\n // the ref from them always sees the committed branch list.\n const branchesRef = React.useRef(branches);\n React.useEffect(() => {\n branchesRef.current = branches;\n });\n const isTargetInScope = React.useCallback(\n (target: HTMLElement | null) => {\n if (!target) return false;\n if (container?.contains(target)) return true;\n return Boolean(branchesRef.current?.some((branch) => branch.contains(target)));\n },\n [container],\n );\n\n const focusScope = React.useRef({\n paused: false,\n pause() {\n this.paused = true;\n },\n resume() {\n this.paused = false;\n },\n }).current;\n\n // Takes care of trapping focus if focus is moved outside programmatically for example\n React.useEffect(() => {\n if (trapped) {\n function handleFocusIn(event: FocusEvent) {\n if (focusScope.paused || !container) return;\n const target = event.target as HTMLElement | null;\n if (isTargetInScope(target)) {\n lastFocusedElementRef.current = target;\n } else {\n focus(lastFocusedElementRef.current, { select: true });\n }\n }\n\n function handleFocusOut(event: FocusEvent) {\n if (focusScope.paused || !container) return;\n const relatedTarget = event.relatedTarget as HTMLElement | null;\n\n // A `focusout` event with a `null` `relatedTarget` will happen in at\n // least two cases:\n // 1. When the user switches app/tabs/windows/the browser itself loses\n // focus.\n // 2. In Google Chrome, when the focused element is removed from the\n // DOM.\n //\n // We let the browser do its thing here because:\n // 1. The browser already keeps a memory of what's focused for when\n // the page gets refocused.\n // 2. In Google Chrome, if we try to focus the deleted focused element\n // (as per below), it throws the CPU to 100%, so we avoid doing\n // anything for this reason here too.\n if (relatedTarget === null) return;\n\n // If the focus has moved to an actual legitimate element\n // (`relatedTarget !== null`) that is outside the container (and any\n // registered branches), we move focus to the last valid focused\n // element inside.\n if (!isTargetInScope(relatedTarget)) {\n focus(lastFocusedElementRef.current, { select: true });\n }\n }\n\n // When the focused element gets removed from the DOM, browsers move focus\n // back to the document.body. In this case, we move focus to the container\n // to keep focus trapped correctly.\n function handleMutations(mutations: MutationRecord[]) {\n const focusedElement = document.activeElement as HTMLElement | null;\n if (focusedElement !== document.body) return;\n for (const mutation of mutations) {\n if (mutation.removedNodes.length > 0) focus(container);\n }\n }\n\n document.addEventListener('focusin', handleFocusIn);\n document.addEventListener('focusout', handleFocusOut);\n const mutationObserver = new MutationObserver(handleMutations);\n if (container) mutationObserver.observe(container, { childList: true, subtree: true });\n\n return () => {\n document.removeEventListener('focusin', handleFocusIn);\n document.removeEventListener('focusout', handleFocusOut);\n mutationObserver.disconnect();\n };\n }\n }, [trapped, container, focusScope.paused, isTargetInScope]);\n\n React.useEffect(() => {\n if (container) {\n focusScopesStack.add(focusScope);\n const previouslyFocusedElement = document.activeElement as HTMLElement | null;\n const hasFocusedCandidate = container.contains(previouslyFocusedElement);\n\n if (!hasFocusedCandidate) {\n const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);\n container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);\n container.dispatchEvent(mountEvent);\n if (!mountEvent.defaultPrevented) {\n focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });\n if (document.activeElement === previouslyFocusedElement) {\n focus(container);\n }\n }\n }\n\n return () => {\n container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);\n\n // We hit a react bug (fixed in v17) with focusing in unmount.\n // We need to delay the focus a little to get around it for now.\n // See: https://github.com/facebook/react/issues/17894\n setTimeout(() => {\n const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);\n container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);\n container.dispatchEvent(unmountEvent);\n if (!unmountEvent.defaultPrevented) {\n focus(previouslyFocusedElement ?? document.body, { select: true });\n }\n // we need to remove the listener after we `dispatchEvent`\n container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);\n\n focusScopesStack.remove(focusScope);\n }, 0);\n };\n }\n }, [container, onMountAutoFocus, onUnmountAutoFocus, focusScope]);\n\n // Takes care of looping focus (when tabbing whilst at the edges)\n const handleKeyDown = React.useCallback(\n (event: React.KeyboardEvent) => {\n if (!loop && !trapped) return;\n if (focusScope.paused) return;\n\n const isTabKey = event.key === 'Tab' && !event.altKey && !event.ctrlKey && !event.metaKey;\n const focusedElement = document.activeElement as HTMLElement | null;\n\n if (isTabKey && focusedElement) {\n const container = event.currentTarget as HTMLElement;\n const [first, last] = getTabbableEdges(container);\n const hasTabbableElementsInside = first && last;\n\n // we can only wrap focus if we have tabbable edges\n if (!hasTabbableElementsInside) {\n if (focusedElement === container) event.preventDefault();\n } else {\n if (!event.shiftKey && focusedElement === last) {\n event.preventDefault();\n if (loop) focus(first, { select: true });\n } else if (event.shiftKey && focusedElement === first) {\n event.preventDefault();\n if (loop) focus(last, { select: true });\n }\n }\n }\n },\n [loop, trapped, focusScope.paused],\n );\n\n return (\n <Primitive.div tabIndex={-1} {...scopeProps} ref={composedRefs} onKeyDown={handleKeyDown} />\n );\n },\n);\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope branch registry\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * Lets portalled content that is a React descendant of a (modal) layer \u2014 but rendered outside of\n * that layer's DOM subtree \u2014 register itself as a \"branch\" of the layer's focus scope.\n *\n * A modal container (eg. `Dialog`) creates a registry via `useFocusScopeBranchRegistry`, renders a\n * `FocusScopeBranchProvider` around its children, and passes the collected `nodes` to its trapped\n * `FocusScope` (so focus isn't reclaimed from the branch) and, if applicable, to its `RemoveScroll`\n * `shards` (so the branch remains scrollable). Nested layers (eg. a non-modal `Popover`) register\n * their content node with `useFocusScopeBranch`.\n *\n * See: https://github.com/radix-ui/primitives/issues/3423\n */\ninterface FocusScopeBranchRegistry {\n add: (node: HTMLElement) => void;\n remove: (node: HTMLElement) => void;\n}\n\nconst FocusScopeBranchContext = React.createContext<FocusScopeBranchRegistry | null>(null);\n\nconst FocusScopeBranchProvider = FocusScopeBranchContext.Provider;\n\nfunction useFocusScopeBranchRegistry() {\n const [nodes, setNodes] = React.useState<HTMLElement[]>([]);\n const registry = React.useMemo<FocusScopeBranchRegistry>(\n () => ({\n add: (node) => setNodes((prev) => (prev.includes(node) ? prev : [...prev, node])),\n remove: (node) => setNodes((prev) => prev.filter((current) => current !== node)),\n }),\n [],\n );\n return { nodes, registry } as const;\n}\n\n/**\n * Registers `node` as a branch of the nearest ancestor `FocusScopeBranchProvider`. No-ops when\n * there is no ancestor registry (eg. the layer isn't nested inside a modal layer).\n */\nfunction useFocusScopeBranch(node: HTMLElement | null) {\n const registry = React.useContext(FocusScopeBranchContext);\n React.useEffect(() => {\n if (!node || !registry) return;\n registry.add(node);\n return () => registry.remove(node);\n }, [node, registry]);\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Utils\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * Attempts focusing the first element in a list of candidates.\n * Stops when focus has actually moved.\n */\nfunction focusFirst(candidates: HTMLElement[], { select = false } = {}) {\n const previouslyFocusedElement = document.activeElement;\n for (const candidate of candidates) {\n focus(candidate, { select });\n if (document.activeElement !== previouslyFocusedElement) return;\n }\n}\n\n/**\n * Returns the first and last tabbable elements inside a container.\n */\nfunction getTabbableEdges(container: HTMLElement) {\n const candidates = getTabbableCandidates(container);\n const first = findVisible(candidates, container);\n const last = findVisible(candidates.reverse(), container);\n return [first, last] as const;\n}\n\n/**\n * Returns a list of potential tabbable candidates.\n *\n * NOTE: This is only a close approximation. For example it doesn't take into account cases like when\n * elements are not visible. This cannot be worked out easily by just reading a property, but rather\n * necessitate runtime knowledge (computed styles, etc). We deal with these cases separately.\n *\n * See: https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker\n * Credit: https://github.com/discord/focus-layers/blob/master/src/util/wrapFocus.tsx#L1\n */\nfunction getTabbableCandidates(container: HTMLElement) {\n const nodes: HTMLElement[] = [];\n const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {\n acceptNode: (node: any) => {\n const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden';\n if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP;\n // `.tabIndex` is not the same as the `tabindex` attribute. It works on the\n // runtime's understanding of tabbability, so this automatically accounts\n // for any kind of element that could be tabbed to.\n return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;\n },\n });\n while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement);\n // we do not take into account the order of nodes with positive `tabIndex` as it\n // hinders accessibility to have tab order different from visual order.\n return nodes;\n}\n\n/**\n * Returns the first visible element in a list.\n * NOTE: Only checks visibility up to the `container`.\n */\nfunction findVisible(elements: HTMLElement[], container: HTMLElement) {\n // `checkVisibility` performs a single native check for `visibility: hidden`\n // and `display: none` across the ancestor chain. This avoids the repeated\n // `getComputedStyle` calls per ancestor that the fallback path requires,\n // which reduces the cost of style resolution triggered by earlier DOM writes\n // (e.g. react-remove-scroll, DismissableLayer setting body styles).\n const canUseCheckVisibility =\n typeof container.checkVisibility === 'function' &&\n container.checkVisibility({ checkVisibilityCSS: true });\n\n for (const element of elements) {\n const hidden = canUseCheckVisibility\n ? !element.checkVisibility({ checkVisibilityCSS: true })\n : isHidden(element, { upTo: container });\n // we stop checking if it's hidden at the `container` level (excluding)\n if (!hidden) {\n return element;\n }\n }\n}\n\nfunction isHidden(node: HTMLElement, { upTo }: { upTo?: HTMLElement }) {\n if (getComputedStyle(node).visibility === 'hidden') return true;\n while (node) {\n // we stop at `upTo` (excluding it)\n if (upTo !== undefined && node === upTo) return false;\n if (getComputedStyle(node).display === 'none') return true;\n node = node.parentElement as HTMLElement;\n }\n return false;\n}\n\nfunction isSelectableInput(element: any): element is FocusableTarget & { select: () => void } {\n return element instanceof HTMLInputElement && 'select' in element;\n}\n\nfunction focus(element?: FocusableTarget | null, { select = false } = {}) {\n // only focus if that element is focusable\n if (element && element.focus) {\n const previouslyFocusedElement = document.activeElement;\n // NOTE: we prevent scrolling on focus, to minimize jarring transitions for users\n element.focus({ preventScroll: true });\n // only select if its not the same element, it supports selection and we need to select\n if (element !== previouslyFocusedElement && isSelectableInput(element) && select)\n element.select();\n }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope stack\n * -----------------------------------------------------------------------------------------------*/\n\ntype FocusScopeAPI = { paused: boolean; pause(): void; resume(): void };\nconst focusScopesStack = createFocusScopesStack();\n\nfunction createFocusScopesStack() {\n /** A stack of focus scopes, with the active one at the top */\n let stack: FocusScopeAPI[] = [];\n\n return {\n add(focusScope: FocusScopeAPI) {\n // pause the currently active focus scope (at the top of the stack)\n const activeFocusScope = stack[0];\n if (focusScope !== activeFocusScope) {\n activeFocusScope?.pause();\n }\n // remove in case it already exists (because we'll re-add it at the top of the stack)\n stack = arrayRemove(stack, focusScope);\n stack.unshift(focusScope);\n },\n\n remove(focusScope: FocusScopeAPI) {\n stack = arrayRemove(stack, focusScope);\n stack[0]?.resume();\n },\n };\n}\n\nfunction arrayRemove<T>(array: T[], item: T) {\n const updatedArray = [...array];\n const index = updatedArray.indexOf(item);\n if (index !== -1) {\n updatedArray.splice(index, 1);\n }\n return updatedArray;\n}\n\nfunction removeLinks(items: HTMLElement[]) {\n return items.filter((item) => item.tagName !== 'A');\n}\n\nexport {\n FocusScope,\n FocusScopeBranchProvider,\n useFocusScopeBranchRegistry,\n useFocusScopeBranch,\n //\n FocusScope as Root,\n};\nexport type { FocusScopeProps, FocusScopeBranchRegistry };\n"],
5
+ "mappings": ";;;;;AAAA,YAAY,WAAW;AACvB,SAAS,uBAAuB;AAChC,SAAS,iBAAiB;AAC1B,SAAS,sBAAsB;AAyOzB;AAvON,IAAM,qBAAqB;AAC3B,IAAM,uBAAuB;AAC7B,IAAM,gBAAgB,EAAE,SAAS,OAAO,YAAY,KAAK;AAgDzD,IAAM,aAA6B,gBAAM;AAAA,EACvC,gCAASA,YAAW,OAAO,cAAc;AACvC,UAAM;AAAA,MACJ,OAAO;AAAA,MACP,UAAU;AAAA,MACV;AAAA,MACA,kBAAkB;AAAA,MAClB,oBAAoB;AAAA,MACpB,GAAG;AAAA,IACL,IAAI;AACJ,UAAM,CAAC,WAAW,YAAY,IAAU,eAA6B,IAAI;AACzE,UAAM,mBAAmB,eAAe,oBAAoB;AAC5D,UAAM,qBAAqB,eAAe,sBAAsB;AAChE,UAAM,wBAA8B,aAA2B,IAAI;AACnE,UAAM,eAAe,gBAAgB,cAAc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AkB;AACnC,0BAAkB,MAAM;AAAA,MAC1B;AAEA,cAAQ,YAAY,OAAO,UAAU;AACrC,YAAM,QAAQ,UAAU;AAAA,IAC1B;AAAA,IAEA,OAAO,YAA2B;AAChC,cAAQ,YAAY,OAAO,UAAU;AACrC,YAAM,CAAC,GAAG,OAAO;AAAA,IACnB;AAAA,EACF;AACF;AArBS;AAuBT,SAAS,YAAe,OAAY,MAAS;AAC3C,QAAM,eAAe,CAAC,GAAG,KAAK;AAC9B,QAAM,QAAQ,aAAa,QAAQ,IAAI;AACvC,MAAI,UAAU,IAAI;AAChB,iBAAa,OAAO,OAAO,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;AAPS;AAST,SAAS,YAAY,OAAsB;AACzC,SAAO,MAAM,OAAO,CAAC,SAAS,KAAK,YAAY,GAAG;AACpD;AAFS;",
6
6
  "names": ["FocusScope", "handleFocusIn", "handleFocusOut", "handleMutations", "container"]
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radix-ui/react-focus-scope",
3
- "version": "1.1.14",
3
+ "version": "1.1.15",
4
4
  "license": "MIT",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./dist/index.js",
@@ -12,8 +12,8 @@
12
12
  "sideEffects": false,
13
13
  "dependencies": {
14
14
  "@radix-ui/react-compose-refs": "1.1.4",
15
- "@radix-ui/react-use-callback-ref": "1.1.3",
16
- "@radix-ui/react-primitive": "2.1.8"
15
+ "@radix-ui/react-primitive": "2.1.9",
16
+ "@radix-ui/react-use-callback-ref": "1.1.3"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@types/react": "^19.2.17",
@@ -21,9 +21,9 @@
21
21
  "react": "^19.2.7",
22
22
  "react-dom": "^19.2.7",
23
23
  "typescript": "^5.9.3",
24
+ "@repo/builder": "0.0.0",
24
25
  "@repo/test-utils": "0.0.0",
25
- "@repo/typescript-config": "0.0.0",
26
- "@repo/builder": "0.0.0"
26
+ "@repo/typescript-config": "0.0.0"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "@types/react": "*",