@primer/behaviors 0.0.0-202111318955

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 GitHub
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,38 @@
1
+ <h1 align="center">Primer Behaviors</h1>
2
+
3
+ <p align="center">Shared behaviors for JavaScript components</p>
4
+
5
+ <p align="center">
6
+ <a aria-label="npm package" href="https://www.npmjs.com/package/@primer/behaviors">
7
+ <img alt="" src="https://img.shields.io/npm/v/@primer/behaviors.svg">
8
+ </a>
9
+ <a aria-label="contributors graph" href="https://github.com/primer/behaviors/graphs/contributors">
10
+ <img src="https://img.shields.io/github/contributors/primer/behaviors.svg">
11
+ </a>
12
+ <a aria-label="last commit" href="https://github.com/primer/behaviors/commits/main">
13
+ <img alt="" src=
14
+ "https://img.shields.io/github/last-commit/primer/behaviors.svg">
15
+ </a>
16
+ <a aria-label="license" href="https://github.com/primer/behaviors/blob/main/LICENSE">
17
+ <img src="https://img.shields.io/github/license/primer/behaviors.svg" alt="">
18
+ </a>
19
+ </p>
20
+
21
+ ## Documentation
22
+
23
+ * [Anchored Position](https://primer.style/react/anchoredPosition)
24
+ * [Focus Trap](https://primer.style/react/focusTrap)
25
+ * [Focus Zone](https://primer.style/react/focusZone)
26
+ * Scroll Into Viewing Area - coming soon
27
+
28
+ ## Installation
29
+
30
+ ```bash
31
+ npm install @primer/behaviors
32
+ ```
33
+
34
+ or
35
+
36
+ ```bash
37
+ yarn add @primer/behaviors
38
+ ```
@@ -0,0 +1,15 @@
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;
@@ -0,0 +1,206 @@
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
+ }
@@ -0,0 +1,2 @@
1
+ export declare function focusTrap(container: HTMLElement, initialFocus?: HTMLElement): AbortController;
2
+ export declare function focusTrap(container: HTMLElement, initialFocus: HTMLElement | undefined, abortSignal: AbortSignal): void;
@@ -0,0 +1,107 @@
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
+ }
@@ -0,0 +1,32 @@
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;
@@ -0,0 +1,406 @@
1
+ import { polyfill as eventListenerSignalPolyfill } from './polyfills/event-listener-signal.js';
2
+ import { isMacOS } from './utils/user-agent.js';
3
+ import { iterateFocusableElements } from './utils/iterate-focusable-elements.js';
4
+ import { uniqueId } from './utils/unique-id.js';
5
+ eventListenerSignalPolyfill();
6
+ export var FocusKeys;
7
+ (function (FocusKeys) {
8
+ FocusKeys[FocusKeys["ArrowHorizontal"] = 1] = "ArrowHorizontal";
9
+ FocusKeys[FocusKeys["ArrowVertical"] = 2] = "ArrowVertical";
10
+ FocusKeys[FocusKeys["JK"] = 4] = "JK";
11
+ FocusKeys[FocusKeys["HL"] = 8] = "HL";
12
+ FocusKeys[FocusKeys["HomeAndEnd"] = 16] = "HomeAndEnd";
13
+ FocusKeys[FocusKeys["PageUpDown"] = 256] = "PageUpDown";
14
+ FocusKeys[FocusKeys["WS"] = 32] = "WS";
15
+ FocusKeys[FocusKeys["AD"] = 64] = "AD";
16
+ FocusKeys[FocusKeys["Tab"] = 128] = "Tab";
17
+ FocusKeys[FocusKeys["ArrowAll"] = 3] = "ArrowAll";
18
+ FocusKeys[FocusKeys["HJKL"] = 12] = "HJKL";
19
+ FocusKeys[FocusKeys["WASD"] = 96] = "WASD";
20
+ FocusKeys[FocusKeys["All"] = 511] = "All";
21
+ })(FocusKeys || (FocusKeys = {}));
22
+ const KEY_TO_BIT = {
23
+ ArrowLeft: FocusKeys.ArrowHorizontal,
24
+ ArrowDown: FocusKeys.ArrowVertical,
25
+ ArrowUp: FocusKeys.ArrowVertical,
26
+ ArrowRight: FocusKeys.ArrowHorizontal,
27
+ h: FocusKeys.HL,
28
+ j: FocusKeys.JK,
29
+ k: FocusKeys.JK,
30
+ l: FocusKeys.HL,
31
+ a: FocusKeys.AD,
32
+ s: FocusKeys.WS,
33
+ w: FocusKeys.WS,
34
+ d: FocusKeys.AD,
35
+ Tab: FocusKeys.Tab,
36
+ Home: FocusKeys.HomeAndEnd,
37
+ End: FocusKeys.HomeAndEnd,
38
+ PageUp: FocusKeys.PageUpDown,
39
+ PageDown: FocusKeys.PageUpDown
40
+ };
41
+ const KEY_TO_DIRECTION = {
42
+ ArrowLeft: 'previous',
43
+ ArrowDown: 'next',
44
+ ArrowUp: 'previous',
45
+ ArrowRight: 'next',
46
+ h: 'previous',
47
+ j: 'next',
48
+ k: 'previous',
49
+ l: 'next',
50
+ a: 'previous',
51
+ s: 'next',
52
+ w: 'previous',
53
+ d: 'next',
54
+ Tab: 'next',
55
+ Home: 'start',
56
+ End: 'end',
57
+ PageUp: 'start',
58
+ PageDown: 'end'
59
+ };
60
+ function getDirection(keyboardEvent) {
61
+ const direction = KEY_TO_DIRECTION[keyboardEvent.key];
62
+ if (keyboardEvent.key === 'Tab' && keyboardEvent.shiftKey) {
63
+ return 'previous';
64
+ }
65
+ const isMac = isMacOS();
66
+ if ((isMac && keyboardEvent.metaKey) || (!isMac && keyboardEvent.ctrlKey)) {
67
+ if (keyboardEvent.key === 'ArrowLeft' || keyboardEvent.key === 'ArrowUp') {
68
+ return 'start';
69
+ }
70
+ else if (keyboardEvent.key === 'ArrowRight' || keyboardEvent.key === 'ArrowDown') {
71
+ return 'end';
72
+ }
73
+ }
74
+ return direction;
75
+ }
76
+ function shouldIgnoreFocusHandling(keyboardEvent, activeElement) {
77
+ const key = keyboardEvent.key;
78
+ const keyLength = [...key].length;
79
+ const isTextInput = (activeElement instanceof HTMLInputElement && activeElement.type === 'text') ||
80
+ activeElement instanceof HTMLTextAreaElement;
81
+ if (isTextInput && (keyLength === 1 || key === 'Home' || key === 'End')) {
82
+ return true;
83
+ }
84
+ if (activeElement instanceof HTMLSelectElement) {
85
+ if (keyLength === 1) {
86
+ return true;
87
+ }
88
+ if (key === 'ArrowDown' && isMacOS() && !keyboardEvent.metaKey) {
89
+ return true;
90
+ }
91
+ if (key === 'ArrowDown' && !isMacOS() && keyboardEvent.altKey) {
92
+ return true;
93
+ }
94
+ }
95
+ if (activeElement instanceof HTMLTextAreaElement && (key === 'PageUp' || key === 'PageDown')) {
96
+ return true;
97
+ }
98
+ if (isTextInput) {
99
+ const textInput = activeElement;
100
+ const cursorAtStart = textInput.selectionStart === 0 && textInput.selectionEnd === 0;
101
+ const cursorAtEnd = textInput.selectionStart === textInput.value.length && textInput.selectionEnd === textInput.value.length;
102
+ if (key === 'ArrowLeft' && !cursorAtStart) {
103
+ return true;
104
+ }
105
+ if (key === 'ArrowRight' && !cursorAtEnd) {
106
+ return true;
107
+ }
108
+ if (textInput instanceof HTMLTextAreaElement) {
109
+ if (key === 'ArrowUp' && !cursorAtStart) {
110
+ return true;
111
+ }
112
+ if (key === 'ArrowDown' && !cursorAtEnd) {
113
+ return true;
114
+ }
115
+ }
116
+ }
117
+ return false;
118
+ }
119
+ export const isActiveDescendantAttribute = 'data-is-active-descendant';
120
+ export const activeDescendantActivatedDirectly = 'activated-directly';
121
+ export const activeDescendantActivatedIndirectly = 'activated-indirectly';
122
+ export const hasActiveDescendantAttribute = 'data-has-active-descendant';
123
+ export function focusZone(container, settings) {
124
+ var _a, _b, _c, _d;
125
+ const focusableElements = [];
126
+ const savedTabIndex = new WeakMap();
127
+ const bindKeys = (_a = settings === null || settings === void 0 ? void 0 : settings.bindKeys) !== null && _a !== void 0 ? _a : ((settings === null || settings === void 0 ? void 0 : settings.getNextFocusable) ? FocusKeys.ArrowAll : FocusKeys.ArrowVertical) | FocusKeys.HomeAndEnd;
128
+ const focusOutBehavior = (_b = settings === null || settings === void 0 ? void 0 : settings.focusOutBehavior) !== null && _b !== void 0 ? _b : 'stop';
129
+ const focusInStrategy = (_c = settings === null || settings === void 0 ? void 0 : settings.focusInStrategy) !== null && _c !== void 0 ? _c : 'previous';
130
+ const activeDescendantControl = settings === null || settings === void 0 ? void 0 : settings.activeDescendantControl;
131
+ const activeDescendantCallback = settings === null || settings === void 0 ? void 0 : settings.onActiveDescendantChanged;
132
+ let currentFocusedElement;
133
+ function getFirstFocusableElement() {
134
+ return focusableElements[0];
135
+ }
136
+ function isActiveDescendantInputFocused() {
137
+ return document.activeElement === activeDescendantControl;
138
+ }
139
+ function updateFocusedElement(to, directlyActivated = false) {
140
+ const from = currentFocusedElement;
141
+ currentFocusedElement = to;
142
+ if (activeDescendantControl) {
143
+ if (to && isActiveDescendantInputFocused()) {
144
+ setActiveDescendant(from, to, directlyActivated);
145
+ }
146
+ else {
147
+ clearActiveDescendant();
148
+ }
149
+ return;
150
+ }
151
+ if (from && from !== to && savedTabIndex.has(from)) {
152
+ from.setAttribute('tabindex', '-1');
153
+ }
154
+ to === null || to === void 0 ? void 0 : to.setAttribute('tabindex', '0');
155
+ }
156
+ function setActiveDescendant(from, to, directlyActivated = false) {
157
+ if (!to.id) {
158
+ to.setAttribute('id', uniqueId());
159
+ }
160
+ if (from && from !== to) {
161
+ from.removeAttribute(isActiveDescendantAttribute);
162
+ }
163
+ if (!activeDescendantControl ||
164
+ (!directlyActivated && activeDescendantControl.getAttribute('aria-activedescendant') === to.id)) {
165
+ return;
166
+ }
167
+ activeDescendantControl.setAttribute('aria-activedescendant', to.id);
168
+ container.setAttribute(hasActiveDescendantAttribute, to.id);
169
+ to.setAttribute(isActiveDescendantAttribute, directlyActivated ? activeDescendantActivatedDirectly : activeDescendantActivatedIndirectly);
170
+ activeDescendantCallback === null || activeDescendantCallback === void 0 ? void 0 : activeDescendantCallback(to, from, directlyActivated);
171
+ }
172
+ function clearActiveDescendant(previouslyActiveElement = currentFocusedElement) {
173
+ if (focusInStrategy === 'first') {
174
+ currentFocusedElement = undefined;
175
+ }
176
+ activeDescendantControl === null || activeDescendantControl === void 0 ? void 0 : activeDescendantControl.removeAttribute('aria-activedescendant');
177
+ container.removeAttribute(hasActiveDescendantAttribute);
178
+ previouslyActiveElement === null || previouslyActiveElement === void 0 ? void 0 : previouslyActiveElement.removeAttribute(isActiveDescendantAttribute);
179
+ activeDescendantCallback === null || activeDescendantCallback === void 0 ? void 0 : activeDescendantCallback(undefined, previouslyActiveElement, false);
180
+ }
181
+ function beginFocusManagement(...elements) {
182
+ const filteredElements = elements.filter(e => { var _a, _b; return (_b = (_a = settings === null || settings === void 0 ? void 0 : settings.focusableElementFilter) === null || _a === void 0 ? void 0 : _a.call(settings, e)) !== null && _b !== void 0 ? _b : true; });
183
+ if (filteredElements.length === 0) {
184
+ return;
185
+ }
186
+ const insertIndex = focusableElements.findIndex(e => (e.compareDocumentPosition(filteredElements[0]) & Node.DOCUMENT_POSITION_PRECEDING) > 0);
187
+ focusableElements.splice(insertIndex === -1 ? focusableElements.length : insertIndex, 0, ...filteredElements);
188
+ for (const element of filteredElements) {
189
+ if (!savedTabIndex.has(element)) {
190
+ savedTabIndex.set(element, element.getAttribute('tabindex'));
191
+ }
192
+ element.setAttribute('tabindex', '-1');
193
+ }
194
+ if (!currentFocusedElement) {
195
+ updateFocusedElement(getFirstFocusableElement());
196
+ }
197
+ }
198
+ function endFocusManagement(...elements) {
199
+ for (const element of elements) {
200
+ const focusableElementIndex = focusableElements.indexOf(element);
201
+ if (focusableElementIndex >= 0) {
202
+ focusableElements.splice(focusableElementIndex, 1);
203
+ }
204
+ const savedIndex = savedTabIndex.get(element);
205
+ if (savedIndex !== undefined) {
206
+ if (savedIndex === null) {
207
+ element.removeAttribute('tabindex');
208
+ }
209
+ else {
210
+ element.setAttribute('tabindex', savedIndex);
211
+ }
212
+ savedTabIndex.delete(element);
213
+ }
214
+ if (element === currentFocusedElement) {
215
+ const nextElementToFocus = getFirstFocusableElement();
216
+ updateFocusedElement(nextElementToFocus);
217
+ }
218
+ }
219
+ }
220
+ beginFocusManagement(...iterateFocusableElements(container));
221
+ updateFocusedElement(getFirstFocusableElement());
222
+ const observer = new MutationObserver(mutations => {
223
+ for (const mutation of mutations) {
224
+ for (const removedNode of mutation.removedNodes) {
225
+ if (removedNode instanceof HTMLElement) {
226
+ endFocusManagement(...iterateFocusableElements(removedNode));
227
+ }
228
+ }
229
+ }
230
+ for (const mutation of mutations) {
231
+ for (const addedNode of mutation.addedNodes) {
232
+ if (addedNode instanceof HTMLElement) {
233
+ beginFocusManagement(...iterateFocusableElements(addedNode));
234
+ }
235
+ }
236
+ }
237
+ });
238
+ observer.observe(container, {
239
+ subtree: true,
240
+ childList: true
241
+ });
242
+ const controller = new AbortController();
243
+ const signal = (_d = settings === null || settings === void 0 ? void 0 : settings.abortSignal) !== null && _d !== void 0 ? _d : controller.signal;
244
+ signal.addEventListener('abort', () => {
245
+ endFocusManagement(...focusableElements);
246
+ });
247
+ let elementIndexFocusedByClick = undefined;
248
+ container.addEventListener('mousedown', event => {
249
+ if (event.target instanceof HTMLElement && event.target !== document.activeElement) {
250
+ elementIndexFocusedByClick = focusableElements.indexOf(event.target);
251
+ }
252
+ }, { signal });
253
+ if (activeDescendantControl) {
254
+ container.addEventListener('focusin', event => {
255
+ if (event.target instanceof HTMLElement && focusableElements.includes(event.target)) {
256
+ activeDescendantControl.focus();
257
+ updateFocusedElement(event.target);
258
+ }
259
+ });
260
+ container.addEventListener('mousemove', ({ target }) => {
261
+ if (!(target instanceof Node)) {
262
+ return;
263
+ }
264
+ const focusableElement = focusableElements.find(element => element.contains(target));
265
+ if (focusableElement) {
266
+ updateFocusedElement(focusableElement);
267
+ }
268
+ }, { signal, capture: true });
269
+ activeDescendantControl.addEventListener('focusin', () => {
270
+ if (!currentFocusedElement) {
271
+ updateFocusedElement(getFirstFocusableElement());
272
+ }
273
+ else {
274
+ setActiveDescendant(undefined, currentFocusedElement);
275
+ }
276
+ });
277
+ activeDescendantControl.addEventListener('focusout', () => {
278
+ clearActiveDescendant();
279
+ });
280
+ }
281
+ else {
282
+ container.addEventListener('focusin', event => {
283
+ if (event.target instanceof HTMLElement) {
284
+ if (elementIndexFocusedByClick !== undefined) {
285
+ if (elementIndexFocusedByClick >= 0) {
286
+ if (focusableElements[elementIndexFocusedByClick] !== currentFocusedElement) {
287
+ updateFocusedElement(focusableElements[elementIndexFocusedByClick]);
288
+ }
289
+ }
290
+ elementIndexFocusedByClick = undefined;
291
+ }
292
+ else {
293
+ if (focusInStrategy === 'previous') {
294
+ updateFocusedElement(event.target);
295
+ }
296
+ else if (focusInStrategy === 'closest' || focusInStrategy === 'first') {
297
+ if (event.relatedTarget instanceof Element && !container.contains(event.relatedTarget)) {
298
+ const targetElementIndex = lastKeyboardFocusDirection === 'previous' ? focusableElements.length - 1 : 0;
299
+ const targetElement = focusableElements[targetElementIndex];
300
+ targetElement === null || targetElement === void 0 ? void 0 : targetElement.focus();
301
+ return;
302
+ }
303
+ else {
304
+ updateFocusedElement(event.target);
305
+ }
306
+ }
307
+ else if (typeof focusInStrategy === 'function') {
308
+ if (event.relatedTarget instanceof Element && !container.contains(event.relatedTarget)) {
309
+ const elementToFocus = focusInStrategy(event.relatedTarget);
310
+ const requestedFocusElementIndex = elementToFocus ? focusableElements.indexOf(elementToFocus) : -1;
311
+ if (requestedFocusElementIndex >= 0 && elementToFocus instanceof HTMLElement) {
312
+ elementToFocus.focus();
313
+ return;
314
+ }
315
+ else {
316
+ console.warn('Element requested is not a known focusable element.');
317
+ }
318
+ }
319
+ else {
320
+ updateFocusedElement(event.target);
321
+ }
322
+ }
323
+ }
324
+ }
325
+ lastKeyboardFocusDirection = undefined;
326
+ }, { signal });
327
+ }
328
+ const keyboardEventRecipient = activeDescendantControl !== null && activeDescendantControl !== void 0 ? activeDescendantControl : container;
329
+ let lastKeyboardFocusDirection = undefined;
330
+ if (focusInStrategy === 'closest') {
331
+ document.addEventListener('keydown', event => {
332
+ if (event.key === 'Tab') {
333
+ lastKeyboardFocusDirection = getDirection(event);
334
+ }
335
+ }, { signal, capture: true });
336
+ }
337
+ function getCurrentFocusedIndex() {
338
+ if (!currentFocusedElement) {
339
+ return 0;
340
+ }
341
+ const focusedIndex = focusableElements.indexOf(currentFocusedElement);
342
+ const fallbackIndex = currentFocusedElement === container ? -1 : 0;
343
+ return focusedIndex !== -1 ? focusedIndex : fallbackIndex;
344
+ }
345
+ keyboardEventRecipient.addEventListener('keydown', event => {
346
+ var _a;
347
+ if (event.key in KEY_TO_DIRECTION) {
348
+ const keyBit = KEY_TO_BIT[event.key];
349
+ if (!event.defaultPrevented &&
350
+ (keyBit & bindKeys) > 0 &&
351
+ !shouldIgnoreFocusHandling(event, document.activeElement)) {
352
+ const direction = getDirection(event);
353
+ let nextElementToFocus = undefined;
354
+ if (settings === null || settings === void 0 ? void 0 : settings.getNextFocusable) {
355
+ nextElementToFocus = settings.getNextFocusable(direction, (_a = document.activeElement) !== null && _a !== void 0 ? _a : undefined, event);
356
+ }
357
+ if (!nextElementToFocus) {
358
+ const lastFocusedIndex = getCurrentFocusedIndex();
359
+ let nextFocusedIndex = lastFocusedIndex;
360
+ if (direction === 'previous') {
361
+ nextFocusedIndex -= 1;
362
+ }
363
+ else if (direction === 'start') {
364
+ nextFocusedIndex = 0;
365
+ }
366
+ else if (direction === 'next') {
367
+ nextFocusedIndex += 1;
368
+ }
369
+ else {
370
+ nextFocusedIndex = focusableElements.length - 1;
371
+ }
372
+ if (nextFocusedIndex < 0) {
373
+ if (focusOutBehavior === 'wrap' && event.key !== 'Tab') {
374
+ nextFocusedIndex = focusableElements.length - 1;
375
+ }
376
+ else {
377
+ nextFocusedIndex = 0;
378
+ }
379
+ }
380
+ if (nextFocusedIndex >= focusableElements.length) {
381
+ if (focusOutBehavior === 'wrap' && event.key !== 'Tab') {
382
+ nextFocusedIndex = 0;
383
+ }
384
+ else {
385
+ nextFocusedIndex = focusableElements.length - 1;
386
+ }
387
+ }
388
+ if (lastFocusedIndex !== nextFocusedIndex) {
389
+ nextElementToFocus = focusableElements[nextFocusedIndex];
390
+ }
391
+ }
392
+ if (activeDescendantControl) {
393
+ updateFocusedElement(nextElementToFocus || currentFocusedElement, true);
394
+ }
395
+ else if (nextElementToFocus) {
396
+ lastKeyboardFocusDirection = direction;
397
+ nextElementToFocus.focus();
398
+ }
399
+ if (event.key !== 'Tab' || nextElementToFocus) {
400
+ event.preventDefault();
401
+ }
402
+ }
403
+ }
404
+ }, { signal });
405
+ return controller;
406
+ }
@@ -0,0 +1,4 @@
1
+ export * from './anchored-position.js';
2
+ export * from './focus-trap.js';
3
+ export * from './focus-zone.js';
4
+ export * from './scroll-into-viewing-area.js';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './anchored-position.js';
2
+ export * from './focus-trap.js';
3
+ export * from './focus-zone.js';
4
+ export * from './scroll-into-viewing-area.js';
@@ -0,0 +1,6 @@
1
+ export declare function polyfill(): void;
2
+ declare global {
3
+ interface AddEventListenerOptions {
4
+ signal?: AbortSignal;
5
+ }
6
+ }
@@ -0,0 +1,40 @@
1
+ let signalSupported = false;
2
+ function noop() { }
3
+ try {
4
+ const options = Object.create({}, {
5
+ signal: {
6
+ get() {
7
+ signalSupported = true;
8
+ }
9
+ }
10
+ });
11
+ window.addEventListener('test', noop, options);
12
+ window.removeEventListener('test', noop, options);
13
+ }
14
+ catch (e) {
15
+ }
16
+ function featureSupported() {
17
+ return signalSupported;
18
+ }
19
+ function monkeyPatch() {
20
+ if (typeof window === 'undefined') {
21
+ return;
22
+ }
23
+ const originalAddEventListener = EventTarget.prototype.addEventListener;
24
+ EventTarget.prototype.addEventListener = function (name, originalCallback, optionsOrCapture) {
25
+ if (typeof optionsOrCapture === 'object' &&
26
+ 'signal' in optionsOrCapture &&
27
+ optionsOrCapture.signal instanceof AbortSignal) {
28
+ originalAddEventListener.call(optionsOrCapture.signal, 'abort', () => {
29
+ this.removeEventListener(name, originalCallback, optionsOrCapture);
30
+ });
31
+ }
32
+ return originalAddEventListener.call(this, name, originalCallback, optionsOrCapture);
33
+ };
34
+ }
35
+ export function polyfill() {
36
+ if (!featureSupported()) {
37
+ monkeyPatch();
38
+ signalSupported = true;
39
+ }
40
+ }
@@ -0,0 +1 @@
1
+ export declare const scrollIntoViewingArea: (child: HTMLElement, viewingArea: HTMLElement, direction?: 'horizontal' | 'vertical', startMargin?: number, endMargin?: number, behavior?: ScrollBehavior) => void;
@@ -0,0 +1,17 @@
1
+ export const scrollIntoViewingArea = (child, viewingArea, direction = 'vertical', startMargin = 8, endMargin = 0, behavior = 'smooth') => {
2
+ const startSide = direction === 'vertical' ? 'top' : 'left';
3
+ const endSide = direction === 'vertical' ? 'bottom' : 'right';
4
+ const scrollSide = direction === 'vertical' ? 'scrollTop' : 'scrollLeft';
5
+ const { [startSide]: childStart, [endSide]: childEnd } = child.getBoundingClientRect();
6
+ const { [startSide]: viewingAreaStart, [endSide]: viewingAreaEnd } = viewingArea.getBoundingClientRect();
7
+ const isChildStartAboveViewingArea = childStart < viewingAreaStart + endMargin;
8
+ const isChildBottomBelowViewingArea = childEnd > viewingAreaEnd - startMargin;
9
+ if (isChildStartAboveViewingArea) {
10
+ const scrollHeightToChildStart = childStart - viewingAreaStart + viewingArea[scrollSide];
11
+ viewingArea.scrollTo({ behavior, [startSide]: scrollHeightToChildStart - endMargin });
12
+ }
13
+ else if (isChildBottomBelowViewingArea) {
14
+ const scrollHeightToChildBottom = childEnd - viewingAreaEnd + viewingArea[scrollSide];
15
+ viewingArea.scrollTo({ behavior, [startSide]: scrollHeightToChildBottom + startMargin });
16
+ }
17
+ };
@@ -0,0 +1,3 @@
1
+ export * from './iterate-focusable-elements.js';
2
+ export * from './unique-id.js';
3
+ export * from './user-agent.js';
@@ -0,0 +1,3 @@
1
+ export * from './iterate-focusable-elements.js';
2
+ export * from './unique-id.js';
3
+ export * from './user-agent.js';
@@ -0,0 +1,8 @@
1
+ export interface IterateFocusableElements {
2
+ reverse?: boolean;
3
+ strict?: boolean;
4
+ onlyTabbable?: boolean;
5
+ }
6
+ export declare function iterateFocusableElements(container: HTMLElement, options?: IterateFocusableElements): Generator<HTMLElement, undefined, undefined>;
7
+ export declare function isFocusable(elem: HTMLElement, strict?: boolean): boolean;
8
+ export declare function isTabbable(elem: HTMLElement, strict?: boolean): boolean;
@@ -0,0 +1,57 @@
1
+ export function* iterateFocusableElements(container, options = {}) {
2
+ var _a, _b;
3
+ const strict = (_a = options.strict) !== null && _a !== void 0 ? _a : false;
4
+ const acceptFn = ((_b = options.onlyTabbable) !== null && _b !== void 0 ? _b : false) ? isTabbable : isFocusable;
5
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
6
+ acceptNode: node => node instanceof HTMLElement && acceptFn(node, strict) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
7
+ });
8
+ let nextNode = null;
9
+ if (!options.reverse && acceptFn(container, strict)) {
10
+ yield container;
11
+ }
12
+ if (options.reverse) {
13
+ let lastChild = walker.lastChild();
14
+ while (lastChild) {
15
+ nextNode = lastChild;
16
+ lastChild = walker.lastChild();
17
+ }
18
+ }
19
+ else {
20
+ nextNode = walker.firstChild();
21
+ }
22
+ while (nextNode instanceof HTMLElement) {
23
+ yield nextNode;
24
+ nextNode = options.reverse ? walker.previousNode() : walker.nextNode();
25
+ }
26
+ if (options.reverse && acceptFn(container, strict)) {
27
+ yield container;
28
+ }
29
+ return undefined;
30
+ }
31
+ export function isFocusable(elem, strict = false) {
32
+ const disabledAttrInert = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'OPTGROUP', 'OPTION', 'FIELDSET'].includes(elem.tagName) &&
33
+ elem.disabled;
34
+ const hiddenInert = elem.hidden;
35
+ const hiddenInputInert = elem instanceof HTMLInputElement && elem.type === 'hidden';
36
+ if (disabledAttrInert || hiddenInert || hiddenInputInert) {
37
+ return false;
38
+ }
39
+ if (strict) {
40
+ const sizeInert = elem.offsetWidth === 0 || elem.offsetHeight === 0;
41
+ const visibilityInert = ['hidden', 'collapse'].includes(getComputedStyle(elem).visibility);
42
+ const clientRectsInert = elem.getClientRects().length === 0;
43
+ if (sizeInert || visibilityInert || clientRectsInert) {
44
+ return false;
45
+ }
46
+ }
47
+ if (elem.getAttribute('tabindex') != null) {
48
+ return true;
49
+ }
50
+ if (elem instanceof HTMLAnchorElement && elem.getAttribute('href') == null) {
51
+ return false;
52
+ }
53
+ return elem.tabIndex !== -1;
54
+ }
55
+ export function isTabbable(elem, strict = false) {
56
+ return isFocusable(elem, strict) && elem.getAttribute('tabindex') !== '-1';
57
+ }
@@ -0,0 +1 @@
1
+ export declare function uniqueId(): string;
@@ -0,0 +1,4 @@
1
+ let idSeed = 10000;
2
+ export function uniqueId() {
3
+ return `__primer_id_${idSeed++}`;
4
+ }
@@ -0,0 +1 @@
1
+ export declare function isMacOS(): boolean;
@@ -0,0 +1,7 @@
1
+ let isMac = undefined;
2
+ export function isMacOS() {
3
+ if (isMac === undefined) {
4
+ isMac = /^mac/i.test(window.navigator.platform);
5
+ }
6
+ return isMac;
7
+ }
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@primer/behaviors",
3
+ "version": "0.0.0-202111318955",
4
+ "description": "Shared behaviors for JavaScript components",
5
+ "main": "./dist/index.js",
6
+ "type": "module",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "sideEffects": [
12
+ "dist/focus-zone.js",
13
+ "dist/focus-trap.js"
14
+ ],
15
+ "scripts": {
16
+ "lint": "eslint src/",
17
+ "test": "npm run jest && npm run lint",
18
+ "test:watch": "jest --watch",
19
+ "jest": "jest",
20
+ "clean": "rm -rf dist",
21
+ "prebuild": "npm run clean",
22
+ "build": "tsc",
23
+ "size-limit": "npm run build && size-limit",
24
+ "release": "npm run build && changeset publish"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/primer/behaviors.git"
29
+ },
30
+ "keywords": [
31
+ "primer",
32
+ "behavior",
33
+ "behaviors",
34
+ "focus"
35
+ ],
36
+ "author": "",
37
+ "prettier": "@github/prettier-config",
38
+ "license": "MIT",
39
+ "bugs": {
40
+ "url": "https://github.com/primer/behaviors/issues"
41
+ },
42
+ "homepage": "https://github.com/primer/behaviors#readme",
43
+ "size-limit": [
44
+ {
45
+ "limit": "10kb",
46
+ "path": "dist/index.js"
47
+ }
48
+ ],
49
+ "devDependencies": {
50
+ "@changesets/changelog-github": "^0.4.2",
51
+ "@changesets/cli": "^2.18.1",
52
+ "@github/prettier-config": "0.0.4",
53
+ "@size-limit/preset-small-lib": "^7.0.3",
54
+ "@testing-library/react": "^12.1.2",
55
+ "@testing-library/user-event": "^13.5.0",
56
+ "@types/jest": "^27.0.3",
57
+ "@types/react": "^17.0.37",
58
+ "esbuild": "^0.14.1",
59
+ "esbuild-jest": "^0.5.0",
60
+ "eslint": "^8.2.0",
61
+ "eslint-plugin-github": "^4.3.5",
62
+ "jest": "^27.4.3",
63
+ "prettier": "^2.4.1",
64
+ "react": "^17.0.2",
65
+ "react-dom": "^17.0.2",
66
+ "size-limit": "^7.0.3",
67
+ "typescript": "^4.4.4"
68
+ }
69
+ }