@primer/behaviors 0.0.0-202111322495 → 0.0.0-2022017101021
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/lib/__tests__/anchored-position.test.d.ts +1 -0
- package/lib/__tests__/anchored-position.test.js +388 -0
- package/lib/__tests__/focus-trap.test.d.ts +1 -0
- package/lib/__tests__/focus-trap.test.js +234 -0
- package/lib/__tests__/focus-zone.test.d.ts +1 -0
- package/lib/__tests__/focus-zone.test.js +570 -0
- package/lib/__tests__/iterate-focusable-elements.test.d.ts +1 -0
- package/lib/__tests__/iterate-focusable-elements.test.js +55 -0
- package/lib/__tests__/scroll-into-view.test.d.ts +1 -0
- package/lib/__tests__/scroll-into-view.test.js +245 -0
- package/lib/anchored-position.d.ts +89 -0
- package/lib/anchored-position.js +316 -0
- package/lib/focus-trap.d.ts +12 -0
- package/lib/focus-trap.js +179 -0
- package/lib/focus-zone.d.ts +137 -0
- package/lib/focus-zone.js +578 -0
- package/{dist/index.js → lib/index.d.ts} +0 -0
- package/lib/index.js +57 -0
- package/lib/polyfills/event-listener-signal.d.ts +6 -0
- package/lib/polyfills/event-listener-signal.js +64 -0
- package/lib/scroll-into-view.d.ts +7 -0
- package/lib/scroll-into-view.js +42 -0
- package/{dist/utils/index.js → lib/utils/index.d.ts} +0 -0
- package/lib/utils/index.js +44 -0
- package/lib/utils/iterate-focusable-elements.d.ts +42 -0
- package/lib/utils/iterate-focusable-elements.js +113 -0
- package/lib/utils/unique-id.d.ts +1 -0
- package/lib/utils/unique-id.js +12 -0
- package/lib/utils/user-agent.d.ts +1 -0
- package/lib/utils/user-agent.js +15 -0
- package/lib-esm/__tests__/anchored-position.test.d.ts +1 -0
- package/lib-esm/__tests__/anchored-position.test.js +386 -0
- package/lib-esm/__tests__/focus-trap.test.d.ts +1 -0
- package/lib-esm/__tests__/focus-trap.test.js +227 -0
- package/lib-esm/__tests__/focus-zone.test.d.ts +1 -0
- package/lib-esm/__tests__/focus-zone.test.js +487 -0
- package/lib-esm/__tests__/iterate-focusable-elements.test.d.ts +1 -0
- package/lib-esm/__tests__/iterate-focusable-elements.test.js +48 -0
- package/lib-esm/__tests__/scroll-into-view.test.d.ts +1 -0
- package/lib-esm/__tests__/scroll-into-view.test.js +243 -0
- package/lib-esm/anchored-position.d.ts +89 -0
- package/lib-esm/anchored-position.js +309 -0
- package/lib-esm/focus-trap.d.ts +12 -0
- package/lib-esm/focus-trap.js +170 -0
- package/lib-esm/focus-zone.d.ts +137 -0
- package/lib-esm/focus-zone.js +559 -0
- package/{dist → lib-esm}/index.d.ts +0 -0
- package/lib-esm/index.js +4 -0
- package/{dist → lib-esm}/polyfills/event-listener-signal.d.ts +0 -0
- package/lib-esm/polyfills/event-listener-signal.js +57 -0
- package/{dist → lib-esm}/scroll-into-view.d.ts +0 -0
- package/lib-esm/scroll-into-view.js +35 -0
- package/{dist → lib-esm}/utils/index.d.ts +0 -0
- package/lib-esm/utils/index.js +3 -0
- package/lib-esm/utils/iterate-focusable-elements.d.ts +42 -0
- package/lib-esm/utils/iterate-focusable-elements.js +102 -0
- package/{dist → lib-esm}/utils/unique-id.d.ts +0 -0
- package/lib-esm/utils/unique-id.js +5 -0
- package/{dist → lib-esm}/utils/user-agent.d.ts +0 -0
- package/lib-esm/utils/user-agent.js +8 -0
- package/package.json +36 -5
- package/dist/anchored-position.d.ts +0 -15
- package/dist/anchored-position.js +0 -206
- package/dist/focus-trap.d.ts +0 -2
- package/dist/focus-trap.js +0 -107
- package/dist/focus-zone.d.ts +0 -32
- package/dist/focus-zone.js +0 -406
- package/dist/polyfills/event-listener-signal.js +0 -40
- package/dist/scroll-into-view.js +0 -17
- package/dist/utils/iterate-focusable-elements.d.ts +0 -8
- package/dist/utils/iterate-focusable-elements.js +0 -57
- package/dist/utils/unique-id.js +0 -4
- package/dist/utils/user-agent.js +0 -7
- package/utils/package.json +0 -7
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options to the focusable elements iterator
|
|
3
|
+
*/
|
|
4
|
+
export interface IterateFocusableElements {
|
|
5
|
+
/**
|
|
6
|
+
* (Default: false) Iterate through focusable elements in reverse-order
|
|
7
|
+
*/
|
|
8
|
+
reverse?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* (Default: false) Perform additional checks to determine tabbability
|
|
11
|
+
* which may adversely affect app performance.
|
|
12
|
+
*/
|
|
13
|
+
strict?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* (Default: false) Only iterate tabbable elements, which is the subset
|
|
16
|
+
* of focusable elements that are part of the page's tab sequence.
|
|
17
|
+
*/
|
|
18
|
+
onlyTabbable?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns an iterator over all of the focusable elements within `container`.
|
|
22
|
+
* Note: If `container` is itself focusable it will be included in the results.
|
|
23
|
+
* @param container The container over which to find focusable elements.
|
|
24
|
+
* @param reverse If true, iterate backwards through focusable elements.
|
|
25
|
+
*/
|
|
26
|
+
export declare function iterateFocusableElements(container: HTMLElement, options?: IterateFocusableElements): Generator<HTMLElement, undefined, undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* Determines whether the given element is focusable. If `strict` is true, we may
|
|
29
|
+
* perform additional checks that require a reflow (less performant).
|
|
30
|
+
* @param elem
|
|
31
|
+
* @param strict
|
|
32
|
+
*/
|
|
33
|
+
export declare function isFocusable(elem: HTMLElement, strict?: boolean): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Determines whether the given element is tabbable. If `strict` is true, we may
|
|
36
|
+
* perform additional checks that require a reflow (less performant). This check
|
|
37
|
+
* ensures that the element is focusable and that its tabindex is not explicitly
|
|
38
|
+
* set to "-1" (which makes it focusable, but removes it from the tab order).
|
|
39
|
+
* @param elem
|
|
40
|
+
* @param strict
|
|
41
|
+
*/
|
|
42
|
+
export declare function isTabbable(elem: HTMLElement, strict?: boolean): boolean;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options to the focusable elements iterator
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns an iterator over all of the focusable elements within `container`.
|
|
7
|
+
* Note: If `container` is itself focusable it will be included in the results.
|
|
8
|
+
* @param container The container over which to find focusable elements.
|
|
9
|
+
* @param reverse If true, iterate backwards through focusable elements.
|
|
10
|
+
*/
|
|
11
|
+
export function* iterateFocusableElements(container, options = {}) {
|
|
12
|
+
var _options$strict, _options$onlyTabbable;
|
|
13
|
+
|
|
14
|
+
const strict = (_options$strict = options.strict) !== null && _options$strict !== void 0 ? _options$strict : false;
|
|
15
|
+
const acceptFn = ((_options$onlyTabbable = options.onlyTabbable) !== null && _options$onlyTabbable !== void 0 ? _options$onlyTabbable : false) ? isTabbable : isFocusable;
|
|
16
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
|
|
17
|
+
acceptNode: node => node instanceof HTMLElement && acceptFn(node, strict) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
|
|
18
|
+
});
|
|
19
|
+
let nextNode = null; // Allow the container to participate
|
|
20
|
+
|
|
21
|
+
if (!options.reverse && acceptFn(container, strict)) {
|
|
22
|
+
yield container;
|
|
23
|
+
} // If iterating in reverse, continue traversing down into the last child until we reach
|
|
24
|
+
// a leaf DOM node
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
if (options.reverse) {
|
|
28
|
+
let lastChild = walker.lastChild();
|
|
29
|
+
|
|
30
|
+
while (lastChild) {
|
|
31
|
+
nextNode = lastChild;
|
|
32
|
+
lastChild = walker.lastChild();
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
nextNode = walker.firstChild();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
while (nextNode instanceof HTMLElement) {
|
|
39
|
+
yield nextNode;
|
|
40
|
+
nextNode = options.reverse ? walker.previousNode() : walker.nextNode();
|
|
41
|
+
} // Allow the container to participate (in reverse)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if (options.reverse && acceptFn(container, strict)) {
|
|
45
|
+
yield container;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Determines whether the given element is focusable. If `strict` is true, we may
|
|
52
|
+
* perform additional checks that require a reflow (less performant).
|
|
53
|
+
* @param elem
|
|
54
|
+
* @param strict
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
export function isFocusable(elem, strict = false) {
|
|
58
|
+
// Certain conditions cause an element to never be focusable, even if they have tabindex="0"
|
|
59
|
+
const disabledAttrInert = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'OPTGROUP', 'OPTION', 'FIELDSET'].includes(elem.tagName) && elem.disabled;
|
|
60
|
+
const hiddenInert = elem.hidden;
|
|
61
|
+
const hiddenInputInert = elem instanceof HTMLInputElement && elem.type === 'hidden';
|
|
62
|
+
|
|
63
|
+
if (disabledAttrInert || hiddenInert || hiddenInputInert) {
|
|
64
|
+
return false;
|
|
65
|
+
} // Each of the conditions checked below require a reflow, thus are gated by the `strict`
|
|
66
|
+
// argument. If any are true, the element is not focusable, even if tabindex is set.
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
if (strict) {
|
|
70
|
+
const sizeInert = elem.offsetWidth === 0 || elem.offsetHeight === 0;
|
|
71
|
+
const visibilityInert = ['hidden', 'collapse'].includes(getComputedStyle(elem).visibility);
|
|
72
|
+
const clientRectsInert = elem.getClientRects().length === 0;
|
|
73
|
+
|
|
74
|
+
if (sizeInert || visibilityInert || clientRectsInert) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
} // Any element with `tabindex` explicitly set can be focusable, even if it's set to "-1"
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
if (elem.getAttribute('tabindex') != null) {
|
|
81
|
+
return true;
|
|
82
|
+
} // One last way `elem.tabIndex` can be wrong.
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if (elem instanceof HTMLAnchorElement && elem.getAttribute('href') == null) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return elem.tabIndex !== -1;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Determines whether the given element is tabbable. If `strict` is true, we may
|
|
93
|
+
* perform additional checks that require a reflow (less performant). This check
|
|
94
|
+
* ensures that the element is focusable and that its tabindex is not explicitly
|
|
95
|
+
* set to "-1" (which makes it focusable, but removes it from the tab order).
|
|
96
|
+
* @param elem
|
|
97
|
+
* @param strict
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
export function isTabbable(elem, strict = false) {
|
|
101
|
+
return isFocusable(elem, strict) && elem.getAttribute('tabindex') !== '-1';
|
|
102
|
+
}
|
|
File without changes
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,26 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@primer/behaviors",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-2022017101021",
|
|
4
4
|
"description": "Shared behaviors for JavaScript components",
|
|
5
|
-
"main": "
|
|
6
|
-
"
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"module": "lib-esm/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"node": "./lib/index.js",
|
|
10
|
+
"default": "./lib-esm/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./utils": {
|
|
13
|
+
"node": "./lib/utils.js",
|
|
14
|
+
"default": "./lib-esm/utils.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
7
17
|
"types": "dist/index.d.ts",
|
|
8
18
|
"files": [
|
|
9
19
|
"dist",
|
|
10
|
-
"utils"
|
|
20
|
+
"utils",
|
|
21
|
+
"lib",
|
|
22
|
+
"lib-esm"
|
|
11
23
|
],
|
|
12
24
|
"sideEffects": [
|
|
13
25
|
"dist/focus-zone.js",
|
|
14
26
|
"dist/focus-trap.js"
|
|
15
27
|
],
|
|
16
28
|
"scripts": {
|
|
29
|
+
"build:esm": "cross-env BABEL_ENV=esmUnbundled babel src --extensions '.ts' --out-dir 'lib/esm' --source-maps",
|
|
30
|
+
"build:cjs": "cross-env BABEL_ENV=cjs babel src --extensions '.ts' --out-dir 'lib/cjs' --source-maps",
|
|
17
31
|
"lint": "eslint src/",
|
|
18
32
|
"test": "npm run jest && npm run lint",
|
|
19
33
|
"test:watch": "jest --watch",
|
|
20
34
|
"jest": "jest",
|
|
21
35
|
"clean": "rm -rf dist",
|
|
22
36
|
"prebuild": "npm run clean",
|
|
23
|
-
"build": "
|
|
37
|
+
"build": "./scripts/build",
|
|
24
38
|
"size-limit": "npm run build && size-limit",
|
|
25
39
|
"release": "npm run build && changeset publish"
|
|
26
40
|
},
|
|
@@ -48,6 +62,22 @@
|
|
|
48
62
|
}
|
|
49
63
|
],
|
|
50
64
|
"devDependencies": {
|
|
65
|
+
"@babel/cli": "7.14.5",
|
|
66
|
+
"@babel/core": "7.14.8",
|
|
67
|
+
"@babel/eslint-parser": "7.15.7",
|
|
68
|
+
"@babel/plugin-proposal-nullish-coalescing-operator": "7.16.0",
|
|
69
|
+
"@babel/plugin-proposal-optional-chaining": "7.14.5",
|
|
70
|
+
"@babel/plugin-transform-modules-commonjs": "7.14.5",
|
|
71
|
+
"@babel/preset-react": "7.14.5",
|
|
72
|
+
"@babel/preset-typescript": "7.15.0",
|
|
73
|
+
"babel-core": "7.0.0-bridge.0",
|
|
74
|
+
"babel-loader": "^8.2.2",
|
|
75
|
+
"babel-plugin-add-react-displayname": "0.0.5",
|
|
76
|
+
"babel-plugin-macros": "3.1.0",
|
|
77
|
+
"babel-plugin-preval": "5.0.0",
|
|
78
|
+
"babel-plugin-styled-components": "2.0.2",
|
|
79
|
+
"babel-plugin-transform-replace-expressions": "0.2.0",
|
|
80
|
+
"babel-polyfill": "6.26.0",
|
|
51
81
|
"@changesets/changelog-github": "^0.4.2",
|
|
52
82
|
"@changesets/cli": "^2.18.1",
|
|
53
83
|
"@github/prettier-config": "0.0.4",
|
|
@@ -56,6 +86,7 @@
|
|
|
56
86
|
"@testing-library/user-event": "^13.5.0",
|
|
57
87
|
"@types/jest": "^27.0.3",
|
|
58
88
|
"@types/react": "^17.0.37",
|
|
89
|
+
"cross-env": "^7.0.3",
|
|
59
90
|
"esbuild": "^0.14.1",
|
|
60
91
|
"esbuild-jest": "^0.5.0",
|
|
61
92
|
"eslint": "^8.3.0",
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export declare type AnchorAlignment = 'start' | 'center' | 'end';
|
|
2
|
-
export declare type AnchorSide = 'inside-top' | 'inside-bottom' | 'inside-left' | 'inside-right' | 'inside-center' | 'outside-top' | 'outside-bottom' | 'outside-left' | 'outside-right';
|
|
3
|
-
export interface PositionSettings {
|
|
4
|
-
side: AnchorSide;
|
|
5
|
-
align: AnchorAlignment;
|
|
6
|
-
anchorOffset: number;
|
|
7
|
-
alignmentOffset: number;
|
|
8
|
-
allowOutOfBounds: boolean;
|
|
9
|
-
}
|
|
10
|
-
export interface AnchorPosition {
|
|
11
|
-
top: number;
|
|
12
|
-
left: number;
|
|
13
|
-
anchorSide: AnchorSide;
|
|
14
|
-
}
|
|
15
|
-
export declare function getAnchoredPosition(floatingElement: Element, anchorElement: Element | DOMRect, settings?: Partial<PositionSettings>): AnchorPosition;
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
const alternateOrders = {
|
|
2
|
-
'outside-top': ['outside-bottom', 'outside-right', 'outside-left', 'outside-bottom'],
|
|
3
|
-
'outside-bottom': ['outside-top', 'outside-right', 'outside-left', 'outside-bottom'],
|
|
4
|
-
'outside-left': ['outside-right', 'outside-bottom', 'outside-top', 'outside-bottom'],
|
|
5
|
-
'outside-right': ['outside-left', 'outside-bottom', 'outside-top', 'outside-bottom']
|
|
6
|
-
};
|
|
7
|
-
export function getAnchoredPosition(floatingElement, anchorElement, settings = {}) {
|
|
8
|
-
const parentElement = getPositionedParent(floatingElement);
|
|
9
|
-
const clippingRect = getClippingRect(parentElement);
|
|
10
|
-
const parentElementStyle = getComputedStyle(parentElement);
|
|
11
|
-
const parentElementRect = parentElement.getBoundingClientRect();
|
|
12
|
-
const [borderTop, borderLeft] = [parentElementStyle.borderTopWidth, parentElementStyle.borderLeftWidth].map(v => parseInt(v, 10) || 0);
|
|
13
|
-
const relativeRect = {
|
|
14
|
-
top: parentElementRect.top + borderTop,
|
|
15
|
-
left: parentElementRect.left + borderLeft
|
|
16
|
-
};
|
|
17
|
-
return pureCalculateAnchoredPosition(clippingRect, relativeRect, floatingElement.getBoundingClientRect(), anchorElement instanceof Element ? anchorElement.getBoundingClientRect() : anchorElement, getDefaultSettings(settings));
|
|
18
|
-
}
|
|
19
|
-
function getPositionedParent(element) {
|
|
20
|
-
let parentNode = element.parentNode;
|
|
21
|
-
while (parentNode !== null) {
|
|
22
|
-
if (parentNode instanceof HTMLElement && getComputedStyle(parentNode).position !== 'static') {
|
|
23
|
-
return parentNode;
|
|
24
|
-
}
|
|
25
|
-
parentNode = parentNode.parentNode;
|
|
26
|
-
}
|
|
27
|
-
return document.body;
|
|
28
|
-
}
|
|
29
|
-
function getClippingRect(element) {
|
|
30
|
-
let parentNode = element;
|
|
31
|
-
while (parentNode !== null) {
|
|
32
|
-
if (parentNode === document.body) {
|
|
33
|
-
break;
|
|
34
|
-
}
|
|
35
|
-
const parentNodeStyle = getComputedStyle(parentNode);
|
|
36
|
-
if (parentNodeStyle.overflow !== 'visible') {
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
parentNode = parentNode.parentNode;
|
|
40
|
-
}
|
|
41
|
-
const clippingNode = parentNode === document.body || !(parentNode instanceof HTMLElement) ? document.body : parentNode;
|
|
42
|
-
const elemRect = clippingNode.getBoundingClientRect();
|
|
43
|
-
const elemStyle = getComputedStyle(clippingNode);
|
|
44
|
-
const [borderTop, borderLeft, borderRight, borderBottom] = [
|
|
45
|
-
elemStyle.borderTopWidth,
|
|
46
|
-
elemStyle.borderLeftWidth,
|
|
47
|
-
elemStyle.borderRightWidth,
|
|
48
|
-
elemStyle.borderBottomWidth
|
|
49
|
-
].map(v => parseInt(v, 10) || 0);
|
|
50
|
-
return {
|
|
51
|
-
top: elemRect.top + borderTop,
|
|
52
|
-
left: elemRect.left + borderLeft,
|
|
53
|
-
width: elemRect.width - borderRight - borderLeft,
|
|
54
|
-
height: Math.max(elemRect.height - borderTop - borderBottom, clippingNode === document.body ? window.innerHeight : -Infinity)
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
const positionDefaults = {
|
|
58
|
-
side: 'outside-bottom',
|
|
59
|
-
align: 'start',
|
|
60
|
-
anchorOffset: 4,
|
|
61
|
-
alignmentOffset: 4,
|
|
62
|
-
allowOutOfBounds: false
|
|
63
|
-
};
|
|
64
|
-
function getDefaultSettings(settings = {}) {
|
|
65
|
-
var _a, _b, _c, _d, _e;
|
|
66
|
-
const side = (_a = settings.side) !== null && _a !== void 0 ? _a : positionDefaults.side;
|
|
67
|
-
const align = (_b = settings.align) !== null && _b !== void 0 ? _b : positionDefaults.align;
|
|
68
|
-
return {
|
|
69
|
-
side,
|
|
70
|
-
align,
|
|
71
|
-
anchorOffset: (_c = settings.anchorOffset) !== null && _c !== void 0 ? _c : (side === 'inside-center' ? 0 : positionDefaults.anchorOffset),
|
|
72
|
-
alignmentOffset: (_d = settings.alignmentOffset) !== null && _d !== void 0 ? _d : (align !== 'center' && side.startsWith('inside') ? positionDefaults.alignmentOffset : 0),
|
|
73
|
-
allowOutOfBounds: (_e = settings.allowOutOfBounds) !== null && _e !== void 0 ? _e : positionDefaults.allowOutOfBounds
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
function pureCalculateAnchoredPosition(viewportRect, relativePosition, floatingRect, anchorRect, { side, align, allowOutOfBounds, anchorOffset, alignmentOffset }) {
|
|
77
|
-
const relativeViewportRect = {
|
|
78
|
-
top: viewportRect.top - relativePosition.top,
|
|
79
|
-
left: viewportRect.left - relativePosition.left,
|
|
80
|
-
width: viewportRect.width,
|
|
81
|
-
height: viewportRect.height
|
|
82
|
-
};
|
|
83
|
-
let pos = calculatePosition(floatingRect, anchorRect, side, align, anchorOffset, alignmentOffset);
|
|
84
|
-
let anchorSide = side;
|
|
85
|
-
pos.top -= relativePosition.top;
|
|
86
|
-
pos.left -= relativePosition.left;
|
|
87
|
-
if (!allowOutOfBounds) {
|
|
88
|
-
const alternateOrder = alternateOrders[side];
|
|
89
|
-
let positionAttempt = 0;
|
|
90
|
-
if (alternateOrder) {
|
|
91
|
-
let prevSide = side;
|
|
92
|
-
while (positionAttempt < alternateOrder.length &&
|
|
93
|
-
shouldRecalculatePosition(prevSide, pos, relativeViewportRect, floatingRect)) {
|
|
94
|
-
const nextSide = alternateOrder[positionAttempt++];
|
|
95
|
-
prevSide = nextSide;
|
|
96
|
-
pos = calculatePosition(floatingRect, anchorRect, nextSide, align, anchorOffset, alignmentOffset);
|
|
97
|
-
pos.top -= relativePosition.top;
|
|
98
|
-
pos.left -= relativePosition.left;
|
|
99
|
-
anchorSide = nextSide;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
if (pos.top < relativeViewportRect.top) {
|
|
103
|
-
pos.top = relativeViewportRect.top;
|
|
104
|
-
}
|
|
105
|
-
if (pos.left < relativeViewportRect.left) {
|
|
106
|
-
pos.left = relativeViewportRect.left;
|
|
107
|
-
}
|
|
108
|
-
if (pos.left + floatingRect.width > viewportRect.width + relativeViewportRect.left) {
|
|
109
|
-
pos.left = viewportRect.width + relativeViewportRect.left - floatingRect.width;
|
|
110
|
-
}
|
|
111
|
-
if (alternateOrder && positionAttempt < alternateOrder.length) {
|
|
112
|
-
if (pos.top + floatingRect.height > viewportRect.height + relativeViewportRect.top) {
|
|
113
|
-
pos.top = viewportRect.height + relativeViewportRect.top - floatingRect.height;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return Object.assign(Object.assign({}, pos), { anchorSide });
|
|
118
|
-
}
|
|
119
|
-
function calculatePosition(elementDimensions, anchorPosition, side, align, anchorOffset, alignmentOffset) {
|
|
120
|
-
const anchorRight = anchorPosition.left + anchorPosition.width;
|
|
121
|
-
const anchorBottom = anchorPosition.top + anchorPosition.height;
|
|
122
|
-
let top = -1;
|
|
123
|
-
let left = -1;
|
|
124
|
-
if (side === 'outside-top') {
|
|
125
|
-
top = anchorPosition.top - anchorOffset - elementDimensions.height;
|
|
126
|
-
}
|
|
127
|
-
else if (side === 'outside-bottom') {
|
|
128
|
-
top = anchorBottom + anchorOffset;
|
|
129
|
-
}
|
|
130
|
-
else if (side === 'outside-left') {
|
|
131
|
-
left = anchorPosition.left - anchorOffset - elementDimensions.width;
|
|
132
|
-
}
|
|
133
|
-
else if (side === 'outside-right') {
|
|
134
|
-
left = anchorRight + anchorOffset;
|
|
135
|
-
}
|
|
136
|
-
if (side === 'outside-top' || side === 'outside-bottom') {
|
|
137
|
-
if (align === 'start') {
|
|
138
|
-
left = anchorPosition.left + alignmentOffset;
|
|
139
|
-
}
|
|
140
|
-
else if (align === 'center') {
|
|
141
|
-
left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
left = anchorRight - elementDimensions.width - alignmentOffset;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
if (side === 'outside-left' || side === 'outside-right') {
|
|
148
|
-
if (align === 'start') {
|
|
149
|
-
top = anchorPosition.top + alignmentOffset;
|
|
150
|
-
}
|
|
151
|
-
else if (align === 'center') {
|
|
152
|
-
top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
top = anchorBottom - elementDimensions.height - alignmentOffset;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
if (side === 'inside-top') {
|
|
159
|
-
top = anchorPosition.top + anchorOffset;
|
|
160
|
-
}
|
|
161
|
-
else if (side === 'inside-bottom') {
|
|
162
|
-
top = anchorBottom - anchorOffset - elementDimensions.height;
|
|
163
|
-
}
|
|
164
|
-
else if (side === 'inside-left') {
|
|
165
|
-
left = anchorPosition.left + anchorOffset;
|
|
166
|
-
}
|
|
167
|
-
else if (side === 'inside-right') {
|
|
168
|
-
left = anchorRight - anchorOffset - elementDimensions.width;
|
|
169
|
-
}
|
|
170
|
-
else if (side === 'inside-center') {
|
|
171
|
-
left = (anchorRight + anchorPosition.left) / 2 - elementDimensions.width / 2 + anchorOffset;
|
|
172
|
-
}
|
|
173
|
-
if (side === 'inside-top' || side === 'inside-bottom') {
|
|
174
|
-
if (align === 'start') {
|
|
175
|
-
left = anchorPosition.left + alignmentOffset;
|
|
176
|
-
}
|
|
177
|
-
else if (align === 'center') {
|
|
178
|
-
left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
left = anchorRight - elementDimensions.width - alignmentOffset;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
else if (side === 'inside-left' || side === 'inside-right' || side === 'inside-center') {
|
|
185
|
-
if (align === 'start') {
|
|
186
|
-
top = anchorPosition.top + alignmentOffset;
|
|
187
|
-
}
|
|
188
|
-
else if (align === 'center') {
|
|
189
|
-
top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
top = anchorBottom - elementDimensions.height - alignmentOffset;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
return { top, left };
|
|
196
|
-
}
|
|
197
|
-
function shouldRecalculatePosition(side, currentPos, containerDimensions, elementDimensions) {
|
|
198
|
-
if (side === 'outside-top' || side === 'outside-bottom') {
|
|
199
|
-
return (currentPos.top < containerDimensions.top ||
|
|
200
|
-
currentPos.top + elementDimensions.height > containerDimensions.height + containerDimensions.top);
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
return (currentPos.left < containerDimensions.left ||
|
|
204
|
-
currentPos.left + elementDimensions.width > containerDimensions.width + containerDimensions.left);
|
|
205
|
-
}
|
|
206
|
-
}
|
package/dist/focus-trap.d.ts
DELETED
package/dist/focus-trap.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { isTabbable, iterateFocusableElements } from './utils/iterate-focusable-elements.js';
|
|
2
|
-
import { polyfill as eventListenerSignalPolyfill } from './polyfills/event-listener-signal.js';
|
|
3
|
-
eventListenerSignalPolyfill();
|
|
4
|
-
const suspendedTrapStack = [];
|
|
5
|
-
let activeTrap = undefined;
|
|
6
|
-
function tryReactivate() {
|
|
7
|
-
const trapToReactivate = suspendedTrapStack.pop();
|
|
8
|
-
if (trapToReactivate) {
|
|
9
|
-
focusTrap(trapToReactivate.container, trapToReactivate.initialFocus, trapToReactivate.originalSignal);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function followSignal(signal) {
|
|
13
|
-
const controller = new AbortController();
|
|
14
|
-
signal.addEventListener('abort', () => {
|
|
15
|
-
controller.abort();
|
|
16
|
-
});
|
|
17
|
-
return controller;
|
|
18
|
-
}
|
|
19
|
-
function getFocusableChild(container, lastChild = false) {
|
|
20
|
-
return iterateFocusableElements(container, { reverse: lastChild, strict: true, onlyTabbable: true }).next().value;
|
|
21
|
-
}
|
|
22
|
-
export function focusTrap(container, initialFocus, abortSignal) {
|
|
23
|
-
const controller = new AbortController();
|
|
24
|
-
const signal = abortSignal !== null && abortSignal !== void 0 ? abortSignal : controller.signal;
|
|
25
|
-
container.setAttribute('data-focus-trap', 'active');
|
|
26
|
-
let lastFocusedChild = undefined;
|
|
27
|
-
function ensureTrapZoneHasFocus(focusedElement) {
|
|
28
|
-
if (focusedElement instanceof HTMLElement && document.contains(container)) {
|
|
29
|
-
if (container.contains(focusedElement)) {
|
|
30
|
-
lastFocusedChild = focusedElement;
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
if (lastFocusedChild && isTabbable(lastFocusedChild) && container.contains(lastFocusedChild)) {
|
|
35
|
-
lastFocusedChild.focus();
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
else if (initialFocus && container.contains(initialFocus)) {
|
|
39
|
-
initialFocus.focus();
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
const containerNeedsTemporaryTabIndex = container.getAttribute('tabindex') === null;
|
|
44
|
-
if (containerNeedsTemporaryTabIndex) {
|
|
45
|
-
container.setAttribute('tabindex', '-1');
|
|
46
|
-
}
|
|
47
|
-
container.focus();
|
|
48
|
-
if (containerNeedsTemporaryTabIndex) {
|
|
49
|
-
container.addEventListener('blur', () => container.removeAttribute('tabindex'), { once: true });
|
|
50
|
-
}
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
const wrappingController = followSignal(signal);
|
|
57
|
-
container.addEventListener('keydown', event => {
|
|
58
|
-
if (event.key !== 'Tab' || event.defaultPrevented) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const { target } = event;
|
|
62
|
-
const firstFocusableChild = getFocusableChild(container);
|
|
63
|
-
const lastFocusableChild = getFocusableChild(container, true);
|
|
64
|
-
if (target === firstFocusableChild && event.shiftKey) {
|
|
65
|
-
event.preventDefault();
|
|
66
|
-
lastFocusableChild === null || lastFocusableChild === void 0 ? void 0 : lastFocusableChild.focus();
|
|
67
|
-
}
|
|
68
|
-
else if (target === lastFocusableChild && !event.shiftKey) {
|
|
69
|
-
event.preventDefault();
|
|
70
|
-
firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
|
|
71
|
-
}
|
|
72
|
-
}, { signal: wrappingController.signal });
|
|
73
|
-
if (activeTrap) {
|
|
74
|
-
const suspendedTrap = activeTrap;
|
|
75
|
-
activeTrap.container.setAttribute('data-focus-trap', 'suspended');
|
|
76
|
-
activeTrap.controller.abort();
|
|
77
|
-
suspendedTrapStack.push(suspendedTrap);
|
|
78
|
-
}
|
|
79
|
-
wrappingController.signal.addEventListener('abort', () => {
|
|
80
|
-
activeTrap = undefined;
|
|
81
|
-
});
|
|
82
|
-
signal.addEventListener('abort', () => {
|
|
83
|
-
container.removeAttribute('data-focus-trap');
|
|
84
|
-
const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
|
|
85
|
-
if (suspendedTrapIndex >= 0) {
|
|
86
|
-
suspendedTrapStack.splice(suspendedTrapIndex, 1);
|
|
87
|
-
}
|
|
88
|
-
tryReactivate();
|
|
89
|
-
});
|
|
90
|
-
document.addEventListener('focus', event => {
|
|
91
|
-
ensureTrapZoneHasFocus(event.target);
|
|
92
|
-
}, { signal: wrappingController.signal, capture: true });
|
|
93
|
-
ensureTrapZoneHasFocus(document.activeElement);
|
|
94
|
-
activeTrap = {
|
|
95
|
-
container,
|
|
96
|
-
controller: wrappingController,
|
|
97
|
-
initialFocus,
|
|
98
|
-
originalSignal: signal
|
|
99
|
-
};
|
|
100
|
-
const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
|
|
101
|
-
if (suspendedTrapIndex >= 0) {
|
|
102
|
-
suspendedTrapStack.splice(suspendedTrapIndex, 1);
|
|
103
|
-
}
|
|
104
|
-
if (!abortSignal) {
|
|
105
|
-
return controller;
|
|
106
|
-
}
|
|
107
|
-
}
|
package/dist/focus-zone.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export declare type Direction = 'previous' | 'next' | 'start' | 'end';
|
|
2
|
-
export declare type FocusMovementKeys = 'ArrowLeft' | 'ArrowDown' | 'ArrowUp' | 'ArrowRight' | 'h' | 'j' | 'k' | 'l' | 'a' | 's' | 'w' | 'd' | 'Tab' | 'Home' | 'End' | 'PageUp' | 'PageDown';
|
|
3
|
-
export declare enum FocusKeys {
|
|
4
|
-
ArrowHorizontal = 1,
|
|
5
|
-
ArrowVertical = 2,
|
|
6
|
-
JK = 4,
|
|
7
|
-
HL = 8,
|
|
8
|
-
HomeAndEnd = 16,
|
|
9
|
-
PageUpDown = 256,
|
|
10
|
-
WS = 32,
|
|
11
|
-
AD = 64,
|
|
12
|
-
Tab = 128,
|
|
13
|
-
ArrowAll = 3,
|
|
14
|
-
HJKL = 12,
|
|
15
|
-
WASD = 96,
|
|
16
|
-
All = 511
|
|
17
|
-
}
|
|
18
|
-
export interface FocusZoneSettings {
|
|
19
|
-
focusOutBehavior?: 'stop' | 'wrap';
|
|
20
|
-
getNextFocusable?: (direction: Direction, from: Element | undefined, event: KeyboardEvent) => HTMLElement | undefined;
|
|
21
|
-
focusableElementFilter?: (element: HTMLElement) => boolean;
|
|
22
|
-
bindKeys?: FocusKeys;
|
|
23
|
-
abortSignal?: AbortSignal;
|
|
24
|
-
activeDescendantControl?: HTMLElement;
|
|
25
|
-
onActiveDescendantChanged?: (newActiveDescendant: HTMLElement | undefined, previousActiveDescendant: HTMLElement | undefined, directlyActivated: boolean) => void;
|
|
26
|
-
focusInStrategy?: 'first' | 'closest' | 'previous' | ((previousFocusedElement: Element) => HTMLElement | undefined);
|
|
27
|
-
}
|
|
28
|
-
export declare const isActiveDescendantAttribute = "data-is-active-descendant";
|
|
29
|
-
export declare const activeDescendantActivatedDirectly = "activated-directly";
|
|
30
|
-
export declare const activeDescendantActivatedIndirectly = "activated-indirectly";
|
|
31
|
-
export declare const hasActiveDescendantAttribute = "data-has-active-descendant";
|
|
32
|
-
export declare function focusZone(container: HTMLElement, settings?: FocusZoneSettings): AbortController;
|