@tamagui/focus-scope 1.0.1-rc.5 → 1.0.1-rc.6

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.
@@ -34,126 +34,128 @@ const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount";
34
34
  const AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount";
35
35
  const EVENT_OPTIONS = { bubbles: false, cancelable: true };
36
36
  const FOCUS_SCOPE_NAME = "FocusScope";
37
- const FocusScope = React.forwardRef((props, forwardedRef) => {
38
- const {
39
- loop = false,
40
- trapped = false,
41
- onMountAutoFocus: onMountAutoFocusProp,
42
- onUnmountAutoFocus: onUnmountAutoFocusProp,
43
- children,
44
- forceUnmount,
45
- ...scopeProps
46
- } = props;
47
- const [container, setContainer] = React.useState(null);
48
- const onMountAutoFocus = (0, import_use_event.useEvent)(onMountAutoFocusProp);
49
- const onUnmountAutoFocus = (0, import_use_event.useEvent)(onUnmountAutoFocusProp);
50
- const lastFocusedElementRef = React.useRef(null);
51
- const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, (node) => setContainer(node));
52
- const focusScope = React.useRef({
53
- paused: false,
54
- pause() {
55
- this.paused = true;
56
- },
57
- resume() {
58
- this.paused = false;
59
- }
60
- }).current;
61
- React.useEffect(() => {
62
- if (!trapped)
63
- return;
64
- function handleFocusIn(event) {
65
- if (focusScope.paused || !container)
66
- return;
67
- const target = event.target;
68
- if (container.contains(target)) {
69
- lastFocusedElementRef.current = target;
70
- } else {
71
- focus(lastFocusedElementRef.current, { select: true });
37
+ const FocusScope = React.forwardRef(
38
+ (props, forwardedRef) => {
39
+ const {
40
+ loop = false,
41
+ trapped = false,
42
+ onMountAutoFocus: onMountAutoFocusProp,
43
+ onUnmountAutoFocus: onUnmountAutoFocusProp,
44
+ children,
45
+ forceUnmount,
46
+ ...scopeProps
47
+ } = props;
48
+ const [container, setContainer] = React.useState(null);
49
+ const onMountAutoFocus = (0, import_use_event.useEvent)(onMountAutoFocusProp);
50
+ const onUnmountAutoFocus = (0, import_use_event.useEvent)(onUnmountAutoFocusProp);
51
+ const lastFocusedElementRef = React.useRef(null);
52
+ const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, (node) => setContainer(node));
53
+ const focusScope = React.useRef({
54
+ paused: false,
55
+ pause() {
56
+ this.paused = true;
57
+ },
58
+ resume() {
59
+ this.paused = false;
72
60
  }
73
- }
74
- function handleFocusOut(event) {
75
- if (focusScope.paused || !container)
61
+ }).current;
62
+ React.useEffect(() => {
63
+ if (!trapped)
76
64
  return;
77
- if (!container.contains(event.relatedTarget)) {
78
- focus(lastFocusedElementRef.current, { select: true });
79
- }
80
- }
81
- document.addEventListener("focusin", handleFocusIn);
82
- document.addEventListener("focusout", handleFocusOut);
83
- return () => {
84
- document.removeEventListener("focusin", handleFocusIn);
85
- document.removeEventListener("focusout", handleFocusOut);
86
- };
87
- }, [trapped, forceUnmount, container, focusScope.paused]);
88
- React.useEffect(() => {
89
- if (!container)
90
- return;
91
- if (forceUnmount)
92
- return;
93
- focusScopesStack.add(focusScope);
94
- const previouslyFocusedElement = document.activeElement;
95
- const hasFocusedCandidate = container.contains(previouslyFocusedElement);
96
- if (!hasFocusedCandidate) {
97
- const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
98
- container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
99
- container.dispatchEvent(mountEvent);
100
- if (!mountEvent.defaultPrevented) {
101
- focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });
102
- if (document.activeElement === previouslyFocusedElement) {
103
- focus(container);
65
+ function handleFocusIn(event) {
66
+ if (focusScope.paused || !container)
67
+ return;
68
+ const target = event.target;
69
+ if (container.contains(target)) {
70
+ lastFocusedElementRef.current = target;
71
+ } else {
72
+ focus(lastFocusedElementRef.current, { select: true });
104
73
  }
105
74
  }
106
- }
107
- return () => {
108
- container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
109
- const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
110
- container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
111
- container.dispatchEvent(unmountEvent);
112
- if (!unmountEvent.defaultPrevented) {
113
- focus(previouslyFocusedElement ?? document.body, { select: true });
75
+ function handleFocusOut(event) {
76
+ if (focusScope.paused || !container)
77
+ return;
78
+ if (!container.contains(event.relatedTarget)) {
79
+ focus(lastFocusedElementRef.current, { select: true });
80
+ }
114
81
  }
115
- container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
116
- focusScopesStack.remove(focusScope);
117
- };
118
- }, [container, forceUnmount, onMountAutoFocus, onUnmountAutoFocus, focusScope]);
119
- const handleKeyDown = React.useCallback(
120
- (event) => {
121
- if (!loop && !trapped)
82
+ document.addEventListener("focusin", handleFocusIn);
83
+ document.addEventListener("focusout", handleFocusOut);
84
+ return () => {
85
+ document.removeEventListener("focusin", handleFocusIn);
86
+ document.removeEventListener("focusout", handleFocusOut);
87
+ };
88
+ }, [trapped, forceUnmount, container, focusScope.paused]);
89
+ React.useEffect(() => {
90
+ if (!container)
122
91
  return;
123
- if (focusScope.paused)
92
+ if (forceUnmount)
124
93
  return;
125
- const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey;
126
- const focusedElement = document.activeElement;
127
- if (isTabKey && focusedElement) {
128
- const container2 = event.currentTarget;
129
- const [first, last] = getTabbableEdges(container2);
130
- const hasTabbableElementsInside = first && last;
131
- if (!hasTabbableElementsInside) {
132
- if (focusedElement === container2)
133
- event.preventDefault();
134
- } else {
135
- if (!event.shiftKey && focusedElement === last) {
136
- event.preventDefault();
137
- if (loop)
138
- focus(first, { select: true });
139
- } else if (event.shiftKey && focusedElement === first) {
140
- event.preventDefault();
141
- if (loop)
142
- focus(last, { select: true });
94
+ focusScopesStack.add(focusScope);
95
+ const previouslyFocusedElement = document.activeElement;
96
+ const hasFocusedCandidate = container.contains(previouslyFocusedElement);
97
+ if (!hasFocusedCandidate) {
98
+ const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
99
+ container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
100
+ container.dispatchEvent(mountEvent);
101
+ if (!mountEvent.defaultPrevented) {
102
+ focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });
103
+ if (document.activeElement === previouslyFocusedElement) {
104
+ focus(container);
143
105
  }
144
106
  }
145
107
  }
146
- },
147
- [loop, trapped, focusScope.paused]
148
- );
149
- const child = React.Children.only(children);
150
- return React.cloneElement(child, {
151
- tabIndex: -1,
152
- ...scopeProps,
153
- ref: composedRefs,
154
- onKeyDown: handleKeyDown
155
- });
156
- });
108
+ return () => {
109
+ container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
110
+ const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
111
+ container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
112
+ container.dispatchEvent(unmountEvent);
113
+ if (!unmountEvent.defaultPrevented) {
114
+ focus(previouslyFocusedElement ?? document.body, { select: true });
115
+ }
116
+ container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
117
+ focusScopesStack.remove(focusScope);
118
+ };
119
+ }, [container, forceUnmount, onMountAutoFocus, onUnmountAutoFocus, focusScope]);
120
+ const handleKeyDown = React.useCallback(
121
+ (event) => {
122
+ if (!(loop || trapped))
123
+ return;
124
+ if (focusScope.paused)
125
+ return;
126
+ const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey;
127
+ const focusedElement = document.activeElement;
128
+ if (isTabKey && focusedElement) {
129
+ const container2 = event.currentTarget;
130
+ const [first, last] = getTabbableEdges(container2);
131
+ const hasTabbableElementsInside = first && last;
132
+ if (!hasTabbableElementsInside) {
133
+ if (focusedElement === container2)
134
+ event.preventDefault();
135
+ } else {
136
+ if (!event.shiftKey && focusedElement === last) {
137
+ event.preventDefault();
138
+ if (loop)
139
+ focus(first, { select: true });
140
+ } else if (event.shiftKey && focusedElement === first) {
141
+ event.preventDefault();
142
+ if (loop)
143
+ focus(last, { select: true });
144
+ }
145
+ }
146
+ }
147
+ },
148
+ [loop, trapped, focusScope.paused]
149
+ );
150
+ const child = React.Children.only(children);
151
+ return React.cloneElement(child, {
152
+ tabIndex: -1,
153
+ ...scopeProps,
154
+ ref: composedRefs,
155
+ onKeyDown: handleKeyDown
156
+ });
157
+ }
158
+ );
157
159
  FocusScope.displayName = FOCUS_SCOPE_NAME;
158
160
  function focusFirst(candidates, { select = false } = {}) {
159
161
  const previouslyFocusedElement = document.activeElement;
@@ -205,7 +207,7 @@ function isSelectableInput(element) {
205
207
  return element instanceof HTMLInputElement && "select" in element;
206
208
  }
207
209
  function focus(element, { select = false } = {}) {
208
- if (element && element.focus) {
210
+ if (element == null ? void 0 : element.focus) {
209
211
  const previouslyFocusedElement = document.activeElement;
210
212
  element.focus({ preventScroll: true });
211
213
  if (element !== previouslyFocusedElement && isSelectableInput(element) && select)
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/FocusScope.tsx"],
4
- "sourcesContent": ["import { useComposedRefs } from '@tamagui/compose-refs'\nimport { useEvent } from '@tamagui/use-event'\nimport * as React from 'react'\n\nimport { FocusScopeProps } from './FocusScopeProps'\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\nconst FOCUS_SCOPE_NAME = 'FocusScope'\n\ntype FocusScopeElement = HTMLDivElement\n\nconst FocusScope = React.forwardRef<FocusScopeElement, FocusScopeProps>((props, forwardedRef) => {\n const {\n loop = false,\n trapped = false,\n onMountAutoFocus: onMountAutoFocusProp,\n onUnmountAutoFocus: onUnmountAutoFocusProp,\n children,\n forceUnmount,\n ...scopeProps\n } = props\n const [container, setContainer] = React.useState<HTMLElement | null>(null)\n const onMountAutoFocus = useEvent(onMountAutoFocusProp)\n const onUnmountAutoFocus = useEvent(onUnmountAutoFocusProp)\n const lastFocusedElementRef = React.useRef<HTMLElement | null>(null)\n const composedRefs = useComposedRefs(forwardedRef, (node) => setContainer(node))\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) return\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 if (!container.contains(event.relatedTarget as HTMLElement | null)) {\n focus(lastFocusedElementRef.current, { select: true })\n }\n }\n\n document.addEventListener('focusin', handleFocusIn)\n document.addEventListener('focusout', handleFocusOut)\n return () => {\n document.removeEventListener('focusin', handleFocusIn)\n document.removeEventListener('focusout', handleFocusOut)\n }\n }, [trapped, forceUnmount, container, focusScope.paused])\n\n React.useEffect(() => {\n if (!container) return\n if (forceUnmount) return\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 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 }\n }, [container, forceUnmount, 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 const child = React.Children.only(children)\n\n return React.cloneElement(child as any, {\n tabIndex: -1,\n ...scopeProps,\n ref: composedRefs,\n onKeyDown: handleKeyDown,\n })\n})\n\nFocusScope.displayName = FOCUS_SCOPE_NAME\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 for (const element of elements) {\n // we stop checking if it's hidden at the `container` level (excluding)\n if (!isHidden(element, { upTo: container })) return element\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 { FocusScope }\n\nexport type { FocusScopeProps }\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;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AAI,YAA2B;AAE7B,YAAM,mBAAmB,MAAM;AAC/B,UAAI,eAAe,kBAAkB;AACnC,6DAAkB;AAAA,MACpB;AAEA,cAAQ,YAAY,OAAO,UAAU;AACrC,YAAM,QAAQ,UAAU;AAAA,IAC1B;AAAA,IAEA,OAAO,YAA2B;AA3QtC;AA4QM,cAAQ,YAAY,OAAO,UAAU;AACrC,kBAAM,OAAN,mBAAU;AAAA,IACZ;AAAA,EACF;AACF;AAEA,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;AAEA,SAAS,YAAY,OAAsB;AACzC,SAAO,MAAM,OAAO,CAAC,SAAS,KAAK,YAAY,GAAG;AACpD;",
4
+ "sourcesContent": ["import { useComposedRefs } from '@tamagui/compose-refs'\nimport { useEvent } from '@tamagui/use-event'\nimport * as React from 'react'\n\nimport { FocusScopeProps } from './FocusScopeProps'\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\nconst FOCUS_SCOPE_NAME = 'FocusScope'\n\ntype FocusScopeElement = HTMLDivElement\n\nconst FocusScope = React.forwardRef<FocusScopeElement, FocusScopeProps>(\n (props, forwardedRef) => {\n const {\n loop = false,\n trapped = false,\n onMountAutoFocus: onMountAutoFocusProp,\n onUnmountAutoFocus: onUnmountAutoFocusProp,\n children,\n forceUnmount,\n ...scopeProps\n } = props\n const [container, setContainer] = React.useState<HTMLElement | null>(null)\n const onMountAutoFocus = useEvent(onMountAutoFocusProp)\n const onUnmountAutoFocus = useEvent(onUnmountAutoFocusProp)\n const lastFocusedElementRef = React.useRef<HTMLElement | null>(null)\n const composedRefs = useComposedRefs(forwardedRef, (node) => setContainer(node))\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) return\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 if (!container.contains(event.relatedTarget as HTMLElement | null)) {\n focus(lastFocusedElementRef.current, { select: true })\n }\n }\n\n document.addEventListener('focusin', handleFocusIn)\n document.addEventListener('focusout', handleFocusOut)\n return () => {\n document.removeEventListener('focusin', handleFocusIn)\n document.removeEventListener('focusout', handleFocusOut)\n }\n }, [trapped, forceUnmount, container, focusScope.paused])\n\n React.useEffect(() => {\n if (!container) return\n if (forceUnmount) return\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 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 }\n }, [container, forceUnmount, 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 =\n 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 const child = React.Children.only(children)\n\n return React.cloneElement(child as any, {\n tabIndex: -1,\n ...scopeProps,\n ref: composedRefs,\n onKeyDown: handleKeyDown,\n })\n },\n)\n\nFocusScope.displayName = FOCUS_SCOPE_NAME\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)\n 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 for (const element of elements) {\n // we stop checking if it's hidden at the `container` level (excluding)\n if (!isHidden(element, { upTo: container })) return element\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(\n element: any,\n): 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?.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 { FocusScope }\n\nexport type { FocusScopeProps }\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAAgC;AAChC,uBAAyB;AACzB,YAAuB;AAIvB,MAAM,qBAAqB;AAC3B,MAAM,uBAAuB;AAC7B,MAAM,gBAAgB,EAAE,SAAS,OAAO,YAAY,KAAK;AAQzD,MAAM,mBAAmB;AAIzB,MAAM,aAAa,MAAM;AAAA,EACvB,CAAC,OAAO,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,UAAU;AACrC,kBAAM,OAAN,mBAAU;AAAA,IACZ;AAAA,EACF;AACF;AAEA,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;AAEA,SAAS,YAAY,OAAsB;AACzC,SAAO,MAAM,OAAO,CAAC,SAAS,KAAK,YAAY,GAAG;AACpD;",
6
6
  "names": ["container"]
7
7
  }
@@ -5,126 +5,128 @@ const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount";
5
5
  const AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount";
6
6
  const EVENT_OPTIONS = { bubbles: false, cancelable: true };
7
7
  const FOCUS_SCOPE_NAME = "FocusScope";
8
- const FocusScope = React.forwardRef((props, forwardedRef) => {
9
- const {
10
- loop = false,
11
- trapped = false,
12
- onMountAutoFocus: onMountAutoFocusProp,
13
- onUnmountAutoFocus: onUnmountAutoFocusProp,
14
- children,
15
- forceUnmount,
16
- ...scopeProps
17
- } = props;
18
- const [container, setContainer] = React.useState(null);
19
- const onMountAutoFocus = useEvent(onMountAutoFocusProp);
20
- const onUnmountAutoFocus = useEvent(onUnmountAutoFocusProp);
21
- const lastFocusedElementRef = React.useRef(null);
22
- const composedRefs = useComposedRefs(forwardedRef, (node) => setContainer(node));
23
- const focusScope = React.useRef({
24
- paused: false,
25
- pause() {
26
- this.paused = true;
27
- },
28
- resume() {
29
- this.paused = false;
30
- }
31
- }).current;
32
- React.useEffect(() => {
33
- if (!trapped)
34
- return;
35
- function handleFocusIn(event) {
36
- if (focusScope.paused || !container)
37
- return;
38
- const target = event.target;
39
- if (container.contains(target)) {
40
- lastFocusedElementRef.current = target;
41
- } else {
42
- focus(lastFocusedElementRef.current, { select: true });
8
+ const FocusScope = React.forwardRef(
9
+ (props, forwardedRef) => {
10
+ const {
11
+ loop = false,
12
+ trapped = false,
13
+ onMountAutoFocus: onMountAutoFocusProp,
14
+ onUnmountAutoFocus: onUnmountAutoFocusProp,
15
+ children,
16
+ forceUnmount,
17
+ ...scopeProps
18
+ } = props;
19
+ const [container, setContainer] = React.useState(null);
20
+ const onMountAutoFocus = useEvent(onMountAutoFocusProp);
21
+ const onUnmountAutoFocus = useEvent(onUnmountAutoFocusProp);
22
+ const lastFocusedElementRef = React.useRef(null);
23
+ const composedRefs = useComposedRefs(forwardedRef, (node) => setContainer(node));
24
+ const focusScope = React.useRef({
25
+ paused: false,
26
+ pause() {
27
+ this.paused = true;
28
+ },
29
+ resume() {
30
+ this.paused = false;
43
31
  }
44
- }
45
- function handleFocusOut(event) {
46
- if (focusScope.paused || !container)
32
+ }).current;
33
+ React.useEffect(() => {
34
+ if (!trapped)
47
35
  return;
48
- if (!container.contains(event.relatedTarget)) {
49
- focus(lastFocusedElementRef.current, { select: true });
50
- }
51
- }
52
- document.addEventListener("focusin", handleFocusIn);
53
- document.addEventListener("focusout", handleFocusOut);
54
- return () => {
55
- document.removeEventListener("focusin", handleFocusIn);
56
- document.removeEventListener("focusout", handleFocusOut);
57
- };
58
- }, [trapped, forceUnmount, container, focusScope.paused]);
59
- React.useEffect(() => {
60
- if (!container)
61
- return;
62
- if (forceUnmount)
63
- return;
64
- focusScopesStack.add(focusScope);
65
- const previouslyFocusedElement = document.activeElement;
66
- const hasFocusedCandidate = container.contains(previouslyFocusedElement);
67
- if (!hasFocusedCandidate) {
68
- const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
69
- container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
70
- container.dispatchEvent(mountEvent);
71
- if (!mountEvent.defaultPrevented) {
72
- focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });
73
- if (document.activeElement === previouslyFocusedElement) {
74
- focus(container);
36
+ function handleFocusIn(event) {
37
+ if (focusScope.paused || !container)
38
+ return;
39
+ const target = event.target;
40
+ if (container.contains(target)) {
41
+ lastFocusedElementRef.current = target;
42
+ } else {
43
+ focus(lastFocusedElementRef.current, { select: true });
75
44
  }
76
45
  }
77
- }
78
- return () => {
79
- container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
80
- const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
81
- container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
82
- container.dispatchEvent(unmountEvent);
83
- if (!unmountEvent.defaultPrevented) {
84
- focus(previouslyFocusedElement ?? document.body, { select: true });
46
+ function handleFocusOut(event) {
47
+ if (focusScope.paused || !container)
48
+ return;
49
+ if (!container.contains(event.relatedTarget)) {
50
+ focus(lastFocusedElementRef.current, { select: true });
51
+ }
85
52
  }
86
- container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
87
- focusScopesStack.remove(focusScope);
88
- };
89
- }, [container, forceUnmount, onMountAutoFocus, onUnmountAutoFocus, focusScope]);
90
- const handleKeyDown = React.useCallback(
91
- (event) => {
92
- if (!loop && !trapped)
53
+ document.addEventListener("focusin", handleFocusIn);
54
+ document.addEventListener("focusout", handleFocusOut);
55
+ return () => {
56
+ document.removeEventListener("focusin", handleFocusIn);
57
+ document.removeEventListener("focusout", handleFocusOut);
58
+ };
59
+ }, [trapped, forceUnmount, container, focusScope.paused]);
60
+ React.useEffect(() => {
61
+ if (!container)
93
62
  return;
94
- if (focusScope.paused)
63
+ if (forceUnmount)
95
64
  return;
96
- const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey;
97
- const focusedElement = document.activeElement;
98
- if (isTabKey && focusedElement) {
99
- const container2 = event.currentTarget;
100
- const [first, last] = getTabbableEdges(container2);
101
- const hasTabbableElementsInside = first && last;
102
- if (!hasTabbableElementsInside) {
103
- if (focusedElement === container2)
104
- event.preventDefault();
105
- } else {
106
- if (!event.shiftKey && focusedElement === last) {
107
- event.preventDefault();
108
- if (loop)
109
- focus(first, { select: true });
110
- } else if (event.shiftKey && focusedElement === first) {
111
- event.preventDefault();
112
- if (loop)
113
- focus(last, { select: true });
65
+ focusScopesStack.add(focusScope);
66
+ const previouslyFocusedElement = document.activeElement;
67
+ const hasFocusedCandidate = container.contains(previouslyFocusedElement);
68
+ if (!hasFocusedCandidate) {
69
+ const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
70
+ container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
71
+ container.dispatchEvent(mountEvent);
72
+ if (!mountEvent.defaultPrevented) {
73
+ focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });
74
+ if (document.activeElement === previouslyFocusedElement) {
75
+ focus(container);
114
76
  }
115
77
  }
116
78
  }
117
- },
118
- [loop, trapped, focusScope.paused]
119
- );
120
- const child = React.Children.only(children);
121
- return React.cloneElement(child, {
122
- tabIndex: -1,
123
- ...scopeProps,
124
- ref: composedRefs,
125
- onKeyDown: handleKeyDown
126
- });
127
- });
79
+ return () => {
80
+ container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
81
+ const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
82
+ container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
83
+ container.dispatchEvent(unmountEvent);
84
+ if (!unmountEvent.defaultPrevented) {
85
+ focus(previouslyFocusedElement ?? document.body, { select: true });
86
+ }
87
+ container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
88
+ focusScopesStack.remove(focusScope);
89
+ };
90
+ }, [container, forceUnmount, onMountAutoFocus, onUnmountAutoFocus, focusScope]);
91
+ const handleKeyDown = React.useCallback(
92
+ (event) => {
93
+ if (!(loop || trapped))
94
+ return;
95
+ if (focusScope.paused)
96
+ return;
97
+ const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey;
98
+ const focusedElement = document.activeElement;
99
+ if (isTabKey && focusedElement) {
100
+ const container2 = event.currentTarget;
101
+ const [first, last] = getTabbableEdges(container2);
102
+ const hasTabbableElementsInside = first && last;
103
+ if (!hasTabbableElementsInside) {
104
+ if (focusedElement === container2)
105
+ event.preventDefault();
106
+ } else {
107
+ if (!event.shiftKey && focusedElement === last) {
108
+ event.preventDefault();
109
+ if (loop)
110
+ focus(first, { select: true });
111
+ } else if (event.shiftKey && focusedElement === first) {
112
+ event.preventDefault();
113
+ if (loop)
114
+ focus(last, { select: true });
115
+ }
116
+ }
117
+ }
118
+ },
119
+ [loop, trapped, focusScope.paused]
120
+ );
121
+ const child = React.Children.only(children);
122
+ return React.cloneElement(child, {
123
+ tabIndex: -1,
124
+ ...scopeProps,
125
+ ref: composedRefs,
126
+ onKeyDown: handleKeyDown
127
+ });
128
+ }
129
+ );
128
130
  FocusScope.displayName = FOCUS_SCOPE_NAME;
129
131
  function focusFirst(candidates, { select = false } = {}) {
130
132
  const previouslyFocusedElement = document.activeElement;
@@ -176,7 +178,7 @@ function isSelectableInput(element) {
176
178
  return element instanceof HTMLInputElement && "select" in element;
177
179
  }
178
180
  function focus(element, { select = false } = {}) {
179
- if (element && element.focus) {
181
+ if (element == null ? void 0 : element.focus) {
180
182
  const previouslyFocusedElement = document.activeElement;
181
183
  element.focus({ preventScroll: true });
182
184
  if (element !== previouslyFocusedElement && isSelectableInput(element) && select)