@react-aria/utils 3.33.0 → 3.33.1
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/DOMFunctions.main.js +16 -3
- package/dist/DOMFunctions.main.js.map +1 -1
- package/dist/DOMFunctions.mjs +17 -5
- package/dist/DOMFunctions.module.js +17 -5
- package/dist/DOMFunctions.module.js.map +1 -1
- package/dist/getScrollParents.main.js +6 -5
- package/dist/getScrollParents.main.js.map +1 -1
- package/dist/getScrollParents.mjs +6 -5
- package/dist/getScrollParents.module.js +6 -5
- package/dist/getScrollParents.module.js.map +1 -1
- package/dist/import.mjs +2 -2
- package/dist/isScrollable.main.js +3 -0
- package/dist/isScrollable.main.js.map +1 -1
- package/dist/isScrollable.mjs +3 -0
- package/dist/isScrollable.module.js +3 -0
- package/dist/isScrollable.module.js.map +1 -1
- package/dist/main.js +1 -0
- package/dist/main.js.map +1 -1
- package/dist/module.js +2 -2
- package/dist/module.js.map +1 -1
- package/dist/runAfterTransition.main.js +13 -8
- package/dist/runAfterTransition.main.js.map +1 -1
- package/dist/runAfterTransition.mjs +13 -8
- package/dist/runAfterTransition.module.js +13 -8
- package/dist/runAfterTransition.module.js.map +1 -1
- package/dist/scrollIntoView.main.js +86 -53
- package/dist/scrollIntoView.main.js.map +1 -1
- package/dist/scrollIntoView.mjs +86 -53
- package/dist/scrollIntoView.module.js +86 -53
- package/dist/scrollIntoView.module.js.map +1 -1
- package/dist/types.d.ts +17 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/useDrag1D.main.js +2 -2
- package/dist/useDrag1D.main.js.map +1 -1
- package/dist/useDrag1D.mjs +2 -2
- package/dist/useDrag1D.module.js +2 -2
- package/dist/useDrag1D.module.js.map +1 -1
- package/dist/useViewportSize.main.js +9 -4
- package/dist/useViewportSize.main.js.map +1 -1
- package/dist/useViewportSize.mjs +9 -4
- package/dist/useViewportSize.module.js +9 -4
- package/dist/useViewportSize.module.js.map +1 -1
- package/package.json +3 -3
- package/src/getScrollParents.ts +6 -5
- package/src/index.ts +1 -1
- package/src/isScrollable.ts +6 -0
- package/src/runAfterTransition.ts +11 -8
- package/src/scrollIntoView.ts +110 -71
- package/src/shadowdom/DOMFunctions.ts +38 -9
- package/src/useDrag1D.ts +2 -2
- package/src/useViewportSize.ts +11 -4
package/dist/useViewportSize.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {getActiveElement as $d4ee10de306f2510$export$cd4e5573fbe2b576, getEventTarget as $d4ee10de306f2510$export$e58f029f0fbfdb29} from "./DOMFunctions.mjs";
|
|
2
|
+
import {isIOS as $c87311424ea30a05$export$fedb369cb70207f1} from "./platform.mjs";
|
|
1
3
|
import {willOpenKeyboard as $21f1aa98acb08317$export$c57958e35f31ed73} from "./keyboard.mjs";
|
|
2
4
|
import {useState as $fuDHA$useState, useEffect as $fuDHA$useEffect} from "react";
|
|
3
5
|
import {useIsSSR as $fuDHA$useIsSSR} from "@react-aria/ssr";
|
|
@@ -15,6 +17,8 @@ import {useIsSSR as $fuDHA$useIsSSR} from "@react-aria/ssr";
|
|
|
15
17
|
*/
|
|
16
18
|
|
|
17
19
|
|
|
20
|
+
|
|
21
|
+
|
|
18
22
|
let $5df64b3807dc15ee$var$visualViewport = typeof document !== 'undefined' && window.visualViewport;
|
|
19
23
|
function $5df64b3807dc15ee$export$d699905dd57c73ca() {
|
|
20
24
|
let isSSR = (0, $fuDHA$useIsSSR)();
|
|
@@ -40,21 +44,22 @@ function $5df64b3807dc15ee$export$d699905dd57c73ca() {
|
|
|
40
44
|
let frame;
|
|
41
45
|
let onBlur = (e)=>{
|
|
42
46
|
if ($5df64b3807dc15ee$var$visualViewport && $5df64b3807dc15ee$var$visualViewport.scale > 1) return;
|
|
43
|
-
if ((0, $21f1aa98acb08317$export$c57958e35f31ed73)(e
|
|
47
|
+
if ((0, $21f1aa98acb08317$export$c57958e35f31ed73)((0, $d4ee10de306f2510$export$e58f029f0fbfdb29)(e))) // Wait one frame to see if a new element gets focused.
|
|
44
48
|
frame = requestAnimationFrame(()=>{
|
|
45
|
-
|
|
49
|
+
let activeElement = (0, $d4ee10de306f2510$export$cd4e5573fbe2b576)();
|
|
50
|
+
if (!activeElement || !(0, $21f1aa98acb08317$export$c57958e35f31ed73)(activeElement)) updateSize({
|
|
46
51
|
width: document.documentElement.clientWidth,
|
|
47
52
|
height: document.documentElement.clientHeight
|
|
48
53
|
});
|
|
49
54
|
});
|
|
50
55
|
};
|
|
51
56
|
updateSize($5df64b3807dc15ee$var$getViewportSize());
|
|
52
|
-
window.addEventListener('blur', onBlur, true);
|
|
57
|
+
if ((0, $c87311424ea30a05$export$fedb369cb70207f1)()) window.addEventListener('blur', onBlur, true);
|
|
53
58
|
if (!$5df64b3807dc15ee$var$visualViewport) window.addEventListener('resize', onResize);
|
|
54
59
|
else $5df64b3807dc15ee$var$visualViewport.addEventListener('resize', onResize);
|
|
55
60
|
return ()=>{
|
|
56
61
|
cancelAnimationFrame(frame);
|
|
57
|
-
window.removeEventListener('blur', onBlur, true);
|
|
62
|
+
if ((0, $c87311424ea30a05$export$fedb369cb70207f1)()) window.removeEventListener('blur', onBlur, true);
|
|
58
63
|
if (!$5df64b3807dc15ee$var$visualViewport) window.removeEventListener('resize', onResize);
|
|
59
64
|
else $5df64b3807dc15ee$var$visualViewport.removeEventListener('resize', onResize);
|
|
60
65
|
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {getActiveElement as $d4ee10de306f2510$export$cd4e5573fbe2b576, getEventTarget as $d4ee10de306f2510$export$e58f029f0fbfdb29} from "./DOMFunctions.module.js";
|
|
2
|
+
import {isIOS as $c87311424ea30a05$export$fedb369cb70207f1} from "./platform.module.js";
|
|
1
3
|
import {willOpenKeyboard as $21f1aa98acb08317$export$c57958e35f31ed73} from "./keyboard.module.js";
|
|
2
4
|
import {useState as $fuDHA$useState, useEffect as $fuDHA$useEffect} from "react";
|
|
3
5
|
import {useIsSSR as $fuDHA$useIsSSR} from "@react-aria/ssr";
|
|
@@ -15,6 +17,8 @@ import {useIsSSR as $fuDHA$useIsSSR} from "@react-aria/ssr";
|
|
|
15
17
|
*/
|
|
16
18
|
|
|
17
19
|
|
|
20
|
+
|
|
21
|
+
|
|
18
22
|
let $5df64b3807dc15ee$var$visualViewport = typeof document !== 'undefined' && window.visualViewport;
|
|
19
23
|
function $5df64b3807dc15ee$export$d699905dd57c73ca() {
|
|
20
24
|
let isSSR = (0, $fuDHA$useIsSSR)();
|
|
@@ -40,21 +44,22 @@ function $5df64b3807dc15ee$export$d699905dd57c73ca() {
|
|
|
40
44
|
let frame;
|
|
41
45
|
let onBlur = (e)=>{
|
|
42
46
|
if ($5df64b3807dc15ee$var$visualViewport && $5df64b3807dc15ee$var$visualViewport.scale > 1) return;
|
|
43
|
-
if ((0, $21f1aa98acb08317$export$c57958e35f31ed73)(e
|
|
47
|
+
if ((0, $21f1aa98acb08317$export$c57958e35f31ed73)((0, $d4ee10de306f2510$export$e58f029f0fbfdb29)(e))) // Wait one frame to see if a new element gets focused.
|
|
44
48
|
frame = requestAnimationFrame(()=>{
|
|
45
|
-
|
|
49
|
+
let activeElement = (0, $d4ee10de306f2510$export$cd4e5573fbe2b576)();
|
|
50
|
+
if (!activeElement || !(0, $21f1aa98acb08317$export$c57958e35f31ed73)(activeElement)) updateSize({
|
|
46
51
|
width: document.documentElement.clientWidth,
|
|
47
52
|
height: document.documentElement.clientHeight
|
|
48
53
|
});
|
|
49
54
|
});
|
|
50
55
|
};
|
|
51
56
|
updateSize($5df64b3807dc15ee$var$getViewportSize());
|
|
52
|
-
window.addEventListener('blur', onBlur, true);
|
|
57
|
+
if ((0, $c87311424ea30a05$export$fedb369cb70207f1)()) window.addEventListener('blur', onBlur, true);
|
|
53
58
|
if (!$5df64b3807dc15ee$var$visualViewport) window.addEventListener('resize', onResize);
|
|
54
59
|
else $5df64b3807dc15ee$var$visualViewport.addEventListener('resize', onResize);
|
|
55
60
|
return ()=>{
|
|
56
61
|
cancelAnimationFrame(frame);
|
|
57
|
-
window.removeEventListener('blur', onBlur, true);
|
|
62
|
+
if ((0, $c87311424ea30a05$export$fedb369cb70207f1)()) window.removeEventListener('blur', onBlur, true);
|
|
58
63
|
if (!$5df64b3807dc15ee$var$visualViewport) window.removeEventListener('resize', onResize);
|
|
59
64
|
else $5df64b3807dc15ee$var$visualViewport.removeEventListener('resize', onResize);
|
|
60
65
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;AAAA;;;;;;;;;;CAUC;;;;;AAaD,IAAI,uCAAiB,OAAO,aAAa,eAAe,OAAO,cAAc;AAEtE,SAAS;IACd,IAAI,QAAQ,CAAA,GAAA,eAAO;IACnB,IAAI,CAAC,MAAM,QAAQ,GAAG,CAAA,GAAA,eAAO,EAAE,IAAM,QAAQ;YAAC,OAAO;YAAG,QAAQ;QAAC,IAAI;IAErE,CAAA,GAAA,gBAAQ,EAAE;QACR,IAAI,aAAa,CAAC;YAChB,QAAQ,CAAA;gBACN,IAAI,QAAQ,KAAK,KAAK,KAAK,KAAK,IAAI,QAAQ,MAAM,KAAK,KAAK,MAAM,EAChE,OAAO;gBAET,OAAO;YACT;QACF;QAEA,wFAAwF;QACxF,IAAI,WAAW;YACb,8BAA8B;YAC9B,IAAI,wCAAkB,qCAAe,KAAK,GAAG,GAC3C;YAGF,WAAW;QACb;QAEA,iHAAiH;QACjH,gGAAgG;QAChG,IAAI;QACJ,IAAI,SAAS,CAAC;YACZ,IAAI,wCAAkB,qCAAe,KAAK,GAAG,GAC3C;YAGF,IAAI,CAAA,GAAA,yCAAe,EAAE,CAAA,GAAA,yCAAa,EAAE,KAClC,uDAAuD;YACvD,QAAQ,sBAAsB;gBAC5B,IAAI,gBAAgB,CAAA,GAAA,yCAAe;gBACnC,IAAI,CAAC,iBAAiB,CAAC,CAAA,GAAA,yCAAe,EAAE,gBACtC,WAAW;oBAAC,OAAO,SAAS,eAAe,CAAC,WAAW;oBAAE,QAAQ,SAAS,eAAe,CAAC,YAAY;gBAAA;YAE1G;QAEJ;QAEA,WAAW;QAEX,IAAI,CAAA,GAAA,yCAAI,KACN,OAAO,gBAAgB,CAAC,QAAQ,QAAQ;QAG1C,IAAI,CAAC,sCACH,OAAO,gBAAgB,CAAC,UAAU;aAElC,qCAAe,gBAAgB,CAAC,UAAU;QAG5C,OAAO;YACL,qBAAqB;YACrB,IAAI,CAAA,GAAA,yCAAI,KACN,OAAO,mBAAmB,CAAC,QAAQ,QAAQ;YAE7C,IAAI,CAAC,sCACH,OAAO,mBAAmB,CAAC,UAAU;iBAErC,qCAAe,mBAAmB,CAAC,UAAU;QAEjD;IACF,GAAG,EAAE;IAEL,OAAO;AACT;AAEA,SAAS;IACP,OAAO;QACL,+FAA+F;QAC/F,OAAO,uCAAiB,qCAAe,KAAK,GAAG,qCAAe,KAAK,GAAG,SAAS,eAAe,CAAC,WAAW;QAC1G,QAAQ,uCAAiB,qCAAe,MAAM,GAAG,qCAAe,KAAK,GAAG,SAAS,eAAe,CAAC,YAAY;IAC/G;AACF","sources":["packages/@react-aria/utils/src/useViewportSize.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {getActiveElement, getEventTarget} from './shadowdom/DOMFunctions';\nimport {isIOS} from './platform';\nimport {useEffect, useState} from 'react';\nimport {useIsSSR} from '@react-aria/ssr';\nimport {willOpenKeyboard} from './keyboard';\n\ninterface ViewportSize {\n width: number,\n height: number\n}\n\nlet visualViewport = typeof document !== 'undefined' && window.visualViewport;\n\nexport function useViewportSize(): ViewportSize {\n let isSSR = useIsSSR();\n let [size, setSize] = useState(() => isSSR ? {width: 0, height: 0} : getViewportSize());\n\n useEffect(() => {\n let updateSize = (newSize: ViewportSize) => {\n setSize(size => {\n if (newSize.width === size.width && newSize.height === size.height) {\n return size;\n }\n return newSize;\n });\n };\n\n // Use visualViewport api to track available height even on iOS virtual keyboard opening\n let onResize = () => {\n // Ignore updates when zoomed.\n if (visualViewport && visualViewport.scale > 1) {\n return;\n }\n\n updateSize(getViewportSize());\n };\n\n // When closing the keyboard, iOS does not fire the visual viewport resize event until the animation is complete.\n // We can anticipate this and resize early by handling the blur event and using the layout size.\n let frame: number;\n let onBlur = (e: FocusEvent) => {\n if (visualViewport && visualViewport.scale > 1) {\n return;\n }\n\n if (willOpenKeyboard(getEventTarget(e) as Element)) {\n // Wait one frame to see if a new element gets focused.\n frame = requestAnimationFrame(() => {\n let activeElement = getActiveElement();\n if (!activeElement || !willOpenKeyboard(activeElement)) {\n updateSize({width: document.documentElement.clientWidth, height: document.documentElement.clientHeight});\n }\n });\n }\n };\n\n updateSize(getViewportSize());\n\n if (isIOS()) {\n window.addEventListener('blur', onBlur, true);\n }\n\n if (!visualViewport) {\n window.addEventListener('resize', onResize);\n } else {\n visualViewport.addEventListener('resize', onResize);\n }\n\n return () => {\n cancelAnimationFrame(frame);\n if (isIOS()) {\n window.removeEventListener('blur', onBlur, true);\n }\n if (!visualViewport) {\n window.removeEventListener('resize', onResize);\n } else {\n visualViewport.removeEventListener('resize', onResize);\n }\n };\n }, []);\n\n return size;\n}\n\nfunction getViewportSize(): ViewportSize {\n return {\n // Multiply by the visualViewport scale to get the \"natural\" size, unaffected by pinch zooming.\n width: visualViewport ? visualViewport.width * visualViewport.scale : document.documentElement.clientWidth,\n height: visualViewport ? visualViewport.height * visualViewport.scale : document.documentElement.clientHeight\n };\n}\n"],"names":[],"version":3,"file":"useViewportSize.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/utils",
|
|
3
|
-
"version": "3.33.
|
|
3
|
+
"version": "3.33.1",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"@react-aria/ssr": "^3.9.10",
|
|
30
30
|
"@react-stately/flags": "^3.1.2",
|
|
31
31
|
"@react-stately/utils": "^3.11.0",
|
|
32
|
-
"@react-types/shared": "^3.33.
|
|
32
|
+
"@react-types/shared": "^3.33.1",
|
|
33
33
|
"@swc/helpers": "^0.5.0",
|
|
34
34
|
"clsx": "^2.0.0"
|
|
35
35
|
},
|
|
@@ -40,5 +40,5 @@
|
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|
|
42
42
|
},
|
|
43
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "8df187370053aa35f553cb388ad670f65e1ab371"
|
|
44
44
|
}
|
package/src/getScrollParents.ts
CHANGED
|
@@ -13,14 +13,15 @@
|
|
|
13
13
|
import {isScrollable} from './isScrollable';
|
|
14
14
|
|
|
15
15
|
export function getScrollParents(node: Element, checkForOverflow?: boolean): Element[] {
|
|
16
|
-
|
|
16
|
+
let parentElements: Element[] = [];
|
|
17
|
+
let root = document.scrollingElement || document.documentElement;
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
do {
|
|
19
20
|
if (isScrollable(node, checkForOverflow)) {
|
|
20
|
-
|
|
21
|
+
parentElements.push(node);
|
|
21
22
|
}
|
|
22
23
|
node = node.parentElement as Element;
|
|
23
|
-
}
|
|
24
|
+
} while (node && node !== root);
|
|
24
25
|
|
|
25
|
-
return
|
|
26
|
+
return parentElements;
|
|
26
27
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
export {useId, mergeIds, useSlotId} from './useId';
|
|
13
13
|
export {chain} from './chain';
|
|
14
14
|
export {createShadowTreeWalker, ShadowTreeWalker} from './shadowdom/ShadowTreeWalker';
|
|
15
|
-
export {getActiveElement, getEventTarget, nodeContains} from './shadowdom/DOMFunctions';
|
|
15
|
+
export {getActiveElement, getEventTarget, nodeContains, isFocusWithin} from './shadowdom/DOMFunctions';
|
|
16
16
|
export {getOwnerDocument, getOwnerWindow, isShadowRoot} from './domHelpers';
|
|
17
17
|
export {mergeProps} from './mergeProps';
|
|
18
18
|
export {mergeRefs} from './mergeRefs';
|
package/src/isScrollable.ts
CHANGED
|
@@ -15,8 +15,14 @@ export function isScrollable(node: Element | null, checkForOverflow?: boolean):
|
|
|
15
15
|
return false;
|
|
16
16
|
}
|
|
17
17
|
let style = window.getComputedStyle(node);
|
|
18
|
+
let root = document.scrollingElement || document.documentElement;
|
|
18
19
|
let isScrollable = /(auto|scroll)/.test(style.overflow + style.overflowX + style.overflowY);
|
|
19
20
|
|
|
21
|
+
// Root element has `visible` overflow by default, but is scrollable nonetheless.
|
|
22
|
+
if (node === root && style.overflow !== 'hidden') {
|
|
23
|
+
isScrollable = true;
|
|
24
|
+
}
|
|
25
|
+
|
|
20
26
|
if (isScrollable && checkForOverflow) {
|
|
21
27
|
isScrollable = node.scrollHeight !== node.clientHeight || node.scrollWidth !== node.clientWidth;
|
|
22
28
|
}
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
// bugs, e.g. Chrome sometimes fires both transitionend and transitioncancel rather
|
|
17
17
|
// than one or the other. So we need to track what's actually transitioning so that
|
|
18
18
|
// we can ignore these duplicate events.
|
|
19
|
+
import {getEventTarget} from './shadowdom/DOMFunctions';
|
|
19
20
|
let transitionsByElement = new Map<EventTarget, Set<string>>();
|
|
20
21
|
|
|
21
22
|
// A list of callbacks to call once there are no transitioning elements.
|
|
@@ -31,19 +32,20 @@ function setupGlobalEvents() {
|
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
let onTransitionStart = (e: Event) => {
|
|
34
|
-
|
|
35
|
+
let eventTarget = getEventTarget(e);
|
|
36
|
+
if (!isTransitionEvent(e) || !eventTarget) {
|
|
35
37
|
return;
|
|
36
38
|
}
|
|
37
39
|
// Add the transitioning property to the list for this element.
|
|
38
|
-
let transitions = transitionsByElement.get(
|
|
40
|
+
let transitions = transitionsByElement.get(eventTarget);
|
|
39
41
|
if (!transitions) {
|
|
40
42
|
transitions = new Set();
|
|
41
|
-
transitionsByElement.set(
|
|
43
|
+
transitionsByElement.set(eventTarget, transitions);
|
|
42
44
|
|
|
43
45
|
// The transitioncancel event must be registered on the element itself, rather than as a global
|
|
44
46
|
// event. This enables us to handle when the node is deleted from the document while it is transitioning.
|
|
45
47
|
// In that case, the cancel event would have nowhere to bubble to so we need to handle it directly.
|
|
46
|
-
|
|
48
|
+
eventTarget.addEventListener('transitioncancel', onTransitionEnd, {
|
|
47
49
|
once: true
|
|
48
50
|
});
|
|
49
51
|
}
|
|
@@ -52,11 +54,12 @@ function setupGlobalEvents() {
|
|
|
52
54
|
};
|
|
53
55
|
|
|
54
56
|
let onTransitionEnd = (e: Event) => {
|
|
55
|
-
|
|
57
|
+
let eventTarget = getEventTarget(e);
|
|
58
|
+
if (!isTransitionEvent(e) || !eventTarget) {
|
|
56
59
|
return;
|
|
57
60
|
}
|
|
58
61
|
// Remove property from list of transitioning properties.
|
|
59
|
-
let properties = transitionsByElement.get(
|
|
62
|
+
let properties = transitionsByElement.get(eventTarget);
|
|
60
63
|
if (!properties) {
|
|
61
64
|
return;
|
|
62
65
|
}
|
|
@@ -65,8 +68,8 @@ function setupGlobalEvents() {
|
|
|
65
68
|
|
|
66
69
|
// If empty, remove transitioncancel event, and remove the element from the list of transitioning elements.
|
|
67
70
|
if (properties.size === 0) {
|
|
68
|
-
|
|
69
|
-
transitionsByElement.delete(
|
|
71
|
+
eventTarget.removeEventListener('transitioncancel', onTransitionEnd);
|
|
72
|
+
transitionsByElement.delete(eventTarget);
|
|
70
73
|
}
|
|
71
74
|
|
|
72
75
|
// If no transitioning elements, call all of the queued callbacks.
|
package/src/scrollIntoView.ts
CHANGED
|
@@ -11,7 +11,15 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {getScrollParents} from './getScrollParents';
|
|
14
|
-
import {
|
|
14
|
+
import {isChrome, isIOS} from './platform';
|
|
15
|
+
|
|
16
|
+
interface ScrollIntoViewOpts {
|
|
17
|
+
/** The position to align items along the block axis in. */
|
|
18
|
+
block?: ScrollLogicalPosition,
|
|
19
|
+
/** The position to align items along the inline axis in. */
|
|
20
|
+
inline?: ScrollLogicalPosition
|
|
21
|
+
}
|
|
22
|
+
|
|
15
23
|
|
|
16
24
|
interface ScrollIntoViewportOpts {
|
|
17
25
|
/** The optional containing element of the target to be centered in the viewport. */
|
|
@@ -23,74 +31,96 @@ interface ScrollIntoViewportOpts {
|
|
|
23
31
|
* Similar to `element.scrollIntoView({block: 'nearest'})` (not supported in Edge),
|
|
24
32
|
* but doesn't affect parents above `scrollView`.
|
|
25
33
|
*/
|
|
26
|
-
export function scrollIntoView(scrollView: HTMLElement, element: HTMLElement): void {
|
|
27
|
-
let
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
let x = scrollView.scrollLeft;
|
|
34
|
+
export function scrollIntoView(scrollView: HTMLElement, element: HTMLElement, opts: ScrollIntoViewOpts = {}): void {
|
|
35
|
+
let {block = 'nearest', inline = 'nearest'} = opts;
|
|
36
|
+
|
|
37
|
+
if (scrollView === element) { return; }
|
|
38
|
+
|
|
32
39
|
let y = scrollView.scrollTop;
|
|
40
|
+
let x = scrollView.scrollLeft;
|
|
41
|
+
|
|
42
|
+
let target = element.getBoundingClientRect();
|
|
43
|
+
let view = scrollView.getBoundingClientRect();
|
|
44
|
+
let itemStyle = window.getComputedStyle(element);
|
|
45
|
+
let viewStyle = window.getComputedStyle(scrollView);
|
|
46
|
+
let root = document.scrollingElement || document.documentElement;
|
|
47
|
+
|
|
48
|
+
let viewTop = scrollView === root ? 0 : view.top;
|
|
49
|
+
let viewBottom = scrollView === root ? scrollView.clientHeight : view.bottom;
|
|
50
|
+
let viewLeft = scrollView === root ? 0 : view.left;
|
|
51
|
+
let viewRight = scrollView === root ? scrollView.clientWidth : view.right;
|
|
52
|
+
|
|
53
|
+
let scrollMarginTop = parseInt(itemStyle.scrollMarginTop, 10) || 0;
|
|
54
|
+
let scrollMarginBottom = parseInt(itemStyle.scrollMarginBottom, 10) || 0;
|
|
55
|
+
let scrollMarginLeft = parseInt(itemStyle.scrollMarginLeft, 10) || 0;
|
|
56
|
+
let scrollMarginRight = parseInt(itemStyle.scrollMarginRight, 10) || 0;
|
|
57
|
+
|
|
58
|
+
let scrollPaddingTop = parseInt(viewStyle.scrollPaddingTop, 10) || 0;
|
|
59
|
+
let scrollPaddingBottom = parseInt(viewStyle.scrollPaddingBottom, 10) || 0;
|
|
60
|
+
let scrollPaddingLeft = parseInt(viewStyle.scrollPaddingLeft, 10) || 0;
|
|
61
|
+
let scrollPaddingRight = parseInt(viewStyle.scrollPaddingRight, 10) || 0;
|
|
62
|
+
|
|
63
|
+
let borderTopWidth = parseInt(viewStyle.borderTopWidth, 10) || 0;
|
|
64
|
+
let borderBottomWidth = parseInt(viewStyle.borderBottomWidth, 10) || 0;
|
|
65
|
+
let borderLeftWidth = parseInt(viewStyle.borderLeftWidth, 10) || 0;
|
|
66
|
+
let borderRightWidth = parseInt(viewStyle.borderRightWidth, 10) || 0;
|
|
67
|
+
|
|
68
|
+
let scrollAreaTop = target.top - scrollMarginTop;
|
|
69
|
+
let scrollAreaBottom = target.bottom + scrollMarginBottom;
|
|
70
|
+
let scrollAreaLeft = target.left - scrollMarginLeft;
|
|
71
|
+
let scrollAreaRight = target.right + scrollMarginRight;
|
|
72
|
+
|
|
73
|
+
let scrollBarOffsetX = scrollView === root ? 0 : borderLeftWidth + borderRightWidth;
|
|
74
|
+
let scrollBarOffsetY = scrollView === root ? 0 : borderTopWidth + borderBottomWidth;
|
|
75
|
+
let scrollBarWidth = scrollView.offsetWidth - scrollView.clientWidth - scrollBarOffsetX;
|
|
76
|
+
let scrollBarHeight = scrollView.offsetHeight - scrollView.clientHeight - scrollBarOffsetY;
|
|
77
|
+
|
|
78
|
+
let scrollPortTop = viewTop + borderTopWidth + scrollPaddingTop;
|
|
79
|
+
let scrollPortBottom = viewBottom - borderBottomWidth - scrollPaddingBottom - scrollBarHeight;
|
|
80
|
+
let scrollPortLeft = viewLeft + borderLeftWidth + scrollPaddingLeft;
|
|
81
|
+
let scrollPortRight = viewRight - borderRightWidth - scrollPaddingRight;
|
|
33
82
|
|
|
34
|
-
//
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
scrollPaddingRight,
|
|
40
|
-
scrollPaddingBottom,
|
|
41
|
-
scrollPaddingLeft
|
|
42
|
-
} = getComputedStyle(scrollView);
|
|
43
|
-
|
|
44
|
-
let borderAdjustedX = x + parseInt(borderLeftWidth, 10);
|
|
45
|
-
let borderAdjustedY = y + parseInt(borderTopWidth, 10);
|
|
46
|
-
// Ignore end/bottom border via clientHeight/Width instead of offsetHeight/Width
|
|
47
|
-
let maxX = borderAdjustedX + scrollView.clientWidth;
|
|
48
|
-
let maxY = borderAdjustedY + scrollView.clientHeight;
|
|
49
|
-
|
|
50
|
-
// Get scroll padding values as pixels - defaults to 0 if no scroll padding
|
|
51
|
-
// is used.
|
|
52
|
-
let scrollPaddingTopNumber = parseInt(scrollPaddingTop, 10) || 0;
|
|
53
|
-
let scrollPaddingBottomNumber = parseInt(scrollPaddingBottom, 10) || 0;
|
|
54
|
-
let scrollPaddingRightNumber = parseInt(scrollPaddingRight, 10) || 0;
|
|
55
|
-
let scrollPaddingLeftNumber = parseInt(scrollPaddingLeft, 10) || 0;
|
|
56
|
-
|
|
57
|
-
if (offsetX <= x + scrollPaddingLeftNumber) {
|
|
58
|
-
x = offsetX - parseInt(borderLeftWidth, 10) - scrollPaddingLeftNumber;
|
|
59
|
-
} else if (offsetX + width > maxX - scrollPaddingRightNumber) {
|
|
60
|
-
x += offsetX + width - maxX + scrollPaddingRightNumber;
|
|
83
|
+
// IOS always positions the scrollbar on the right ¯\_(ツ)_/¯
|
|
84
|
+
if (viewStyle.direction === 'rtl' && !isIOS()) {
|
|
85
|
+
scrollPortLeft += scrollBarWidth;
|
|
86
|
+
} else {
|
|
87
|
+
scrollPortRight -= scrollBarWidth;
|
|
61
88
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
89
|
+
|
|
90
|
+
let shouldScrollBlock = scrollAreaTop < scrollPortTop || scrollAreaBottom > scrollPortBottom;
|
|
91
|
+
let shouldScrollInline = scrollAreaLeft < scrollPortLeft || scrollAreaRight > scrollPortRight;
|
|
92
|
+
|
|
93
|
+
if (shouldScrollBlock && block === 'start') {
|
|
94
|
+
y += scrollAreaTop - scrollPortTop;
|
|
95
|
+
} else if (shouldScrollBlock && block === 'center') {
|
|
96
|
+
y += (scrollAreaTop + scrollAreaBottom) / 2 - (scrollPortTop + scrollPortBottom) / 2;
|
|
97
|
+
} else if (shouldScrollBlock && block === 'end') {
|
|
98
|
+
y += scrollAreaBottom - scrollPortBottom;
|
|
99
|
+
} else if (shouldScrollBlock && block === 'nearest') {
|
|
100
|
+
let start = scrollAreaTop - scrollPortTop;
|
|
101
|
+
let end = scrollAreaBottom - scrollPortBottom;
|
|
102
|
+
y += Math.abs(start) <= Math.abs(end) ? start : end;
|
|
66
103
|
}
|
|
67
104
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
105
|
+
if (shouldScrollInline && inline === 'start') {
|
|
106
|
+
x += scrollAreaLeft - scrollPortLeft;
|
|
107
|
+
} else if (shouldScrollInline && inline === 'center') {
|
|
108
|
+
x += (scrollAreaLeft + scrollAreaRight) / 2 - (scrollPortLeft + scrollPortRight) / 2;
|
|
109
|
+
} else if (shouldScrollInline && inline === 'end') {
|
|
110
|
+
x += scrollAreaRight - scrollPortRight;
|
|
111
|
+
} else if (shouldScrollInline && inline === 'nearest') {
|
|
112
|
+
let start = scrollAreaLeft - scrollPortLeft;
|
|
113
|
+
let end = scrollAreaRight - scrollPortRight;
|
|
114
|
+
x += Math.abs(start) <= Math.abs(end) ? start : end;
|
|
115
|
+
}
|
|
71
116
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
function relativeOffset(ancestor: HTMLElement, child: HTMLElement, axis: 'left'|'top') {
|
|
77
|
-
const prop = axis === 'left' ? 'offsetLeft' : 'offsetTop';
|
|
78
|
-
let sum = 0;
|
|
79
|
-
while (child.offsetParent) {
|
|
80
|
-
sum += child[prop];
|
|
81
|
-
if (child.offsetParent === ancestor) {
|
|
82
|
-
// Stop once we have found the ancestor we are interested in.
|
|
83
|
-
break;
|
|
84
|
-
} else if (nodeContains(child.offsetParent, ancestor)) {
|
|
85
|
-
// If the ancestor is not `position:relative`, then we stop at
|
|
86
|
-
// _its_ offset parent, and we subtract off _its_ offset, so that
|
|
87
|
-
// we end up with the proper offset from child to ancestor.
|
|
88
|
-
sum -= ancestor[prop];
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
child = child.offsetParent as HTMLElement;
|
|
117
|
+
if (process.env.NODE_ENV === 'test') {
|
|
118
|
+
scrollView.scrollLeft = x;
|
|
119
|
+
scrollView.scrollTop = y;
|
|
120
|
+
return;
|
|
92
121
|
}
|
|
93
|
-
|
|
122
|
+
|
|
123
|
+
scrollView.scrollTo({left: x, top: y});
|
|
94
124
|
}
|
|
95
125
|
|
|
96
126
|
/**
|
|
@@ -98,12 +128,14 @@ function relativeOffset(ancestor: HTMLElement, child: HTMLElement, axis: 'left'|
|
|
|
98
128
|
* that will be centered in the viewport prior to scrolling the targetElement into view. If scrolling is prevented on
|
|
99
129
|
* the body (e.g. targetElement is in a popover), this will only scroll the scroll parents of the targetElement up to but not including the body itself.
|
|
100
130
|
*/
|
|
101
|
-
export function scrollIntoViewport(targetElement: Element | null, opts
|
|
102
|
-
|
|
131
|
+
export function scrollIntoViewport(targetElement: Element | null, opts: ScrollIntoViewportOpts = {}): void {
|
|
132
|
+
let {containingElement} = opts;
|
|
133
|
+
if (targetElement && targetElement.isConnected) {
|
|
103
134
|
let root = document.scrollingElement || document.documentElement;
|
|
104
135
|
let isScrollPrevented = window.getComputedStyle(root).overflow === 'hidden';
|
|
105
|
-
// If scrolling is not currently prevented then we aren
|
|
106
|
-
|
|
136
|
+
// If scrolling is not currently prevented then we aren't in a overlay nor is a overlay open, just use element.scrollIntoView to bring the element into view
|
|
137
|
+
// Also ignore in chrome because of this bug: https://issues.chromium.org/issues/40074749
|
|
138
|
+
if (!isScrollPrevented && !isChrome()) {
|
|
107
139
|
let {left: originalLeft, top: originalTop} = targetElement.getBoundingClientRect();
|
|
108
140
|
|
|
109
141
|
// use scrollIntoView({block: 'nearest'}) instead of .focus to check if the element is fully in view or not since .focus()
|
|
@@ -112,18 +144,25 @@ export function scrollIntoViewport(targetElement: Element | null, opts?: ScrollI
|
|
|
112
144
|
let {left: newLeft, top: newTop} = targetElement.getBoundingClientRect();
|
|
113
145
|
// Account for sub pixel differences from rounding
|
|
114
146
|
if ((Math.abs(originalLeft - newLeft) > 1) || (Math.abs(originalTop - newTop) > 1)) {
|
|
115
|
-
|
|
147
|
+
containingElement?.scrollIntoView?.({block: 'center', inline: 'center'});
|
|
116
148
|
targetElement.scrollIntoView?.({block: 'nearest'});
|
|
117
149
|
}
|
|
118
150
|
} else {
|
|
119
|
-
let
|
|
151
|
+
let {left: originalLeft, top: originalTop} = targetElement.getBoundingClientRect();
|
|
152
|
+
|
|
120
153
|
// If scrolling is prevented, we don't want to scroll the body since it might move the overlay partially offscreen and the user can't scroll it back into view.
|
|
121
|
-
|
|
122
|
-
scrollParents.push(root);
|
|
123
|
-
}
|
|
154
|
+
let scrollParents = getScrollParents(targetElement, true);
|
|
124
155
|
for (let scrollParent of scrollParents) {
|
|
125
156
|
scrollIntoView(scrollParent as HTMLElement, targetElement as HTMLElement);
|
|
126
157
|
}
|
|
158
|
+
let {left: newLeft, top: newTop} = targetElement.getBoundingClientRect();
|
|
159
|
+
// Account for sub pixel differences from rounding
|
|
160
|
+
if ((Math.abs(originalLeft - newLeft) > 1) || (Math.abs(originalTop - newTop) > 1)) {
|
|
161
|
+
scrollParents = containingElement ? getScrollParents(containingElement, true) : [];
|
|
162
|
+
for (let scrollParent of scrollParents) {
|
|
163
|
+
scrollIntoView(scrollParent as HTMLElement, containingElement as HTMLElement, {block: 'center', inline: 'center'});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
127
166
|
}
|
|
128
167
|
}
|
|
129
168
|
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
// Source: https://github.com/microsoft/tabster/blob/a89fc5d7e332d48f68d03b1ca6e344489d1c3898/src/Shadowdomize/DOMFunctions.ts#L16
|
|
2
|
-
/* eslint-disable rsp-rules/no-non-shadow-contains */
|
|
2
|
+
/* eslint-disable rsp-rules/no-non-shadow-contains, rsp-rules/safe-event-target */
|
|
3
3
|
|
|
4
|
-
import {isShadowRoot} from '../domHelpers';
|
|
4
|
+
import {getOwnerWindow, isShadowRoot} from '../domHelpers';
|
|
5
5
|
import {shadowDOM} from '@react-stately/flags';
|
|
6
|
+
import type {SyntheticEvent} from 'react';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* ShadowDOM safe version of Node.contains.
|
|
9
10
|
*/
|
|
10
11
|
export function nodeContains(
|
|
11
|
-
node: Node | null | undefined,
|
|
12
|
-
otherNode: Node | null | undefined
|
|
12
|
+
node: Node | Element | null | undefined,
|
|
13
|
+
otherNode: Node | Element | null | undefined
|
|
13
14
|
): boolean {
|
|
14
15
|
if (!shadowDOM()) {
|
|
15
16
|
return otherNode && node ? node.contains(otherNode) : false;
|
|
@@ -58,14 +59,42 @@ export const getActiveElement = (doc: Document = document): Element | null => {
|
|
|
58
59
|
return activeElement;
|
|
59
60
|
};
|
|
60
61
|
|
|
62
|
+
// Type helper to extract the target element type from an event
|
|
63
|
+
type EventTargetType<T> = T extends SyntheticEvent<infer E, any> ? E : EventTarget;
|
|
64
|
+
|
|
65
|
+
// Possibly we can improve the types for this using https://github.com/adobe/react-spectrum/pull/8991/changes#diff-2d491c0c91701d28d08e1cf9fcadbdb21a030b67ab681460c9934140f29127b8R68 but it was more changes than I
|
|
66
|
+
// wanted to make to fix the function.
|
|
61
67
|
/**
|
|
62
68
|
* ShadowDOM safe version of event.target.
|
|
63
69
|
*/
|
|
64
|
-
export function getEventTarget<T extends Event>(event: T):
|
|
65
|
-
if (shadowDOM() && (event.target
|
|
66
|
-
if (event
|
|
67
|
-
return event.composedPath()[0] as
|
|
70
|
+
export function getEventTarget<T extends Event | SyntheticEvent>(event: T): EventTargetType<T> {
|
|
71
|
+
if (shadowDOM() && (event.target instanceof Element) && event.target.shadowRoot) {
|
|
72
|
+
if ('composedPath' in event) {
|
|
73
|
+
return (event.composedPath()[0] ?? null) as EventTargetType<T>;
|
|
74
|
+
} else if ('composedPath' in event.nativeEvent) {
|
|
75
|
+
return (event.nativeEvent.composedPath()[0] ?? null) as EventTargetType<T>;
|
|
68
76
|
}
|
|
69
77
|
}
|
|
70
|
-
return event.target as
|
|
78
|
+
return event.target as EventTargetType<T>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* ShadowDOM safe fast version of node.contains(document.activeElement).
|
|
83
|
+
* @param node
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
export function isFocusWithin(node: Element | null | undefined): boolean {
|
|
87
|
+
if (!node) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
// Get the active element within the node's parent shadow root (or the document). Can return null.
|
|
91
|
+
let root = node.getRootNode();
|
|
92
|
+
let ownerWindow = getOwnerWindow(node);
|
|
93
|
+
if (!(root instanceof ownerWindow.Document || root instanceof ownerWindow.ShadowRoot)) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
let activeElement = root.activeElement;
|
|
97
|
+
|
|
98
|
+
// Check if the active element is within this node. These nodes are within the same shadow root.
|
|
99
|
+
return activeElement != null && node.contains(activeElement);
|
|
71
100
|
}
|
package/src/useDrag1D.ts
CHANGED
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
|
|
13
13
|
/* eslint-disable rulesdir/pure-render */
|
|
14
14
|
|
|
15
|
+
import {getEventTarget, nodeContains} from './shadowdom/DOMFunctions';
|
|
15
16
|
import {getOffset} from './getOffset';
|
|
16
|
-
import {nodeContains} from './shadowdom/DOMFunctions';
|
|
17
17
|
import {Orientation} from '@react-types/shared';
|
|
18
18
|
import React, {HTMLAttributes, MutableRefObject, useRef} from 'react';
|
|
19
19
|
|
|
@@ -81,7 +81,7 @@ export function useDrag1D(props: UseDrag1DProps): HTMLAttributes<HTMLElement> {
|
|
|
81
81
|
};
|
|
82
82
|
|
|
83
83
|
let onMouseUp = (e: MouseEvent) => {
|
|
84
|
-
|
|
84
|
+
let target = getEventTarget(e) as HTMLElement;
|
|
85
85
|
dragging.current = false;
|
|
86
86
|
let nextOffset = getNextOffset(e);
|
|
87
87
|
if (handlers.current.onDrag) {
|
package/src/useViewportSize.ts
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import {getActiveElement, getEventTarget} from './shadowdom/DOMFunctions';
|
|
14
|
+
import {isIOS} from './platform';
|
|
13
15
|
import {useEffect, useState} from 'react';
|
|
14
16
|
import {useIsSSR} from '@react-aria/ssr';
|
|
15
17
|
import {willOpenKeyboard} from './keyboard';
|
|
@@ -53,10 +55,11 @@ export function useViewportSize(): ViewportSize {
|
|
|
53
55
|
return;
|
|
54
56
|
}
|
|
55
57
|
|
|
56
|
-
if (willOpenKeyboard(e
|
|
58
|
+
if (willOpenKeyboard(getEventTarget(e) as Element)) {
|
|
57
59
|
// Wait one frame to see if a new element gets focused.
|
|
58
60
|
frame = requestAnimationFrame(() => {
|
|
59
|
-
|
|
61
|
+
let activeElement = getActiveElement();
|
|
62
|
+
if (!activeElement || !willOpenKeyboard(activeElement)) {
|
|
60
63
|
updateSize({width: document.documentElement.clientWidth, height: document.documentElement.clientHeight});
|
|
61
64
|
}
|
|
62
65
|
});
|
|
@@ -65,7 +68,9 @@ export function useViewportSize(): ViewportSize {
|
|
|
65
68
|
|
|
66
69
|
updateSize(getViewportSize());
|
|
67
70
|
|
|
68
|
-
|
|
71
|
+
if (isIOS()) {
|
|
72
|
+
window.addEventListener('blur', onBlur, true);
|
|
73
|
+
}
|
|
69
74
|
|
|
70
75
|
if (!visualViewport) {
|
|
71
76
|
window.addEventListener('resize', onResize);
|
|
@@ -75,7 +80,9 @@ export function useViewportSize(): ViewportSize {
|
|
|
75
80
|
|
|
76
81
|
return () => {
|
|
77
82
|
cancelAnimationFrame(frame);
|
|
78
|
-
|
|
83
|
+
if (isIOS()) {
|
|
84
|
+
window.removeEventListener('blur', onBlur, true);
|
|
85
|
+
}
|
|
79
86
|
if (!visualViewport) {
|
|
80
87
|
window.removeEventListener('resize', onResize);
|
|
81
88
|
} else {
|