@primer/behaviors 0.0.0-202201892424 → 0.0.0-2022027213841

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.
Files changed (78) hide show
  1. package/dist/cjs/anchored-position.d.ts +15 -0
  2. package/dist/cjs/anchored-position.js +210 -0
  3. package/dist/cjs/focus-trap.d.ts +1 -0
  4. package/dist/cjs/focus-trap.js +86 -0
  5. package/dist/cjs/focus-zone.d.ts +32 -0
  6. package/dist/cjs/focus-zone.js +410 -0
  7. package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  8. package/dist/cjs/index.js +16 -0
  9. package/dist/{polyfills → cjs/polyfills}/event-listener-signal.d.ts +0 -0
  10. package/dist/cjs/polyfills/event-listener-signal.js +44 -0
  11. package/dist/{scroll-into-view.d.ts → cjs/scroll-into-view.d.ts} +0 -0
  12. package/dist/cjs/scroll-into-view.js +21 -0
  13. package/dist/{utils → cjs/utils}/index.d.ts +0 -0
  14. package/dist/cjs/utils/index.js +15 -0
  15. package/dist/cjs/utils/iterate-focusable-elements.d.ts +9 -0
  16. package/dist/cjs/utils/iterate-focusable-elements.js +68 -0
  17. package/dist/{utils → cjs/utils}/unique-id.d.ts +0 -0
  18. package/dist/cjs/utils/unique-id.js +8 -0
  19. package/dist/{utils → cjs/utils}/user-agent.d.ts +0 -0
  20. package/dist/cjs/utils/user-agent.js +11 -0
  21. package/dist/esm/anchored-position.d.ts +15 -0
  22. package/dist/esm/anchored-position.js +206 -0
  23. package/dist/esm/focus-trap.d.ts +1 -0
  24. package/dist/esm/focus-trap.js +82 -0
  25. package/dist/esm/focus-zone.d.ts +32 -0
  26. package/dist/esm/focus-zone.js +406 -0
  27. package/{lib-esm → dist/esm}/index.d.ts +0 -0
  28. package/{lib/index.d.ts → dist/esm/index.js} +0 -0
  29. package/{lib-esm → dist/esm}/polyfills/event-listener-signal.d.ts +0 -0
  30. package/dist/esm/polyfills/event-listener-signal.js +40 -0
  31. package/{lib-esm → dist/esm}/scroll-into-view.d.ts +0 -0
  32. package/dist/esm/scroll-into-view.js +17 -0
  33. package/{lib-esm → dist/esm}/utils/index.d.ts +0 -0
  34. package/{lib/utils/index.d.ts → dist/esm/utils/index.js} +0 -0
  35. package/dist/esm/utils/iterate-focusable-elements.d.ts +9 -0
  36. package/dist/esm/utils/iterate-focusable-elements.js +61 -0
  37. package/{lib-esm → dist/esm}/utils/unique-id.d.ts +0 -0
  38. package/dist/esm/utils/unique-id.js +4 -0
  39. package/{lib-esm → dist/esm}/utils/user-agent.d.ts +0 -0
  40. package/dist/esm/utils/user-agent.js +7 -0
  41. package/package.json +18 -30
  42. package/utils/package.json +6 -6
  43. package/dist/anchored-position.d.ts +0 -89
  44. package/dist/focus-trap.d.ts +0 -12
  45. package/dist/focus-zone.d.ts +0 -137
  46. package/dist/utils/iterate-focusable-elements.d.ts +0 -42
  47. package/lib/anchored-position.d.ts +0 -89
  48. package/lib/anchored-position.js +0 -316
  49. package/lib/focus-trap.d.ts +0 -12
  50. package/lib/focus-trap.js +0 -179
  51. package/lib/focus-zone.d.ts +0 -137
  52. package/lib/focus-zone.js +0 -578
  53. package/lib/index.js +0 -57
  54. package/lib/polyfills/event-listener-signal.d.ts +0 -6
  55. package/lib/polyfills/event-listener-signal.js +0 -64
  56. package/lib/scroll-into-view.d.ts +0 -7
  57. package/lib/scroll-into-view.js +0 -42
  58. package/lib/utils/index.js +0 -44
  59. package/lib/utils/iterate-focusable-elements.d.ts +0 -42
  60. package/lib/utils/iterate-focusable-elements.js +0 -113
  61. package/lib/utils/unique-id.d.ts +0 -1
  62. package/lib/utils/unique-id.js +0 -12
  63. package/lib/utils/user-agent.d.ts +0 -1
  64. package/lib/utils/user-agent.js +0 -15
  65. package/lib-esm/anchored-position.d.ts +0 -89
  66. package/lib-esm/anchored-position.js +0 -309
  67. package/lib-esm/focus-trap.d.ts +0 -12
  68. package/lib-esm/focus-trap.js +0 -170
  69. package/lib-esm/focus-zone.d.ts +0 -137
  70. package/lib-esm/focus-zone.js +0 -559
  71. package/lib-esm/index.js +0 -4
  72. package/lib-esm/polyfills/event-listener-signal.js +0 -57
  73. package/lib-esm/scroll-into-view.js +0 -35
  74. package/lib-esm/utils/index.js +0 -3
  75. package/lib-esm/utils/iterate-focusable-elements.d.ts +0 -42
  76. package/lib-esm/utils/iterate-focusable-elements.js +0 -102
  77. package/lib-esm/utils/unique-id.js +0 -5
  78. package/lib-esm/utils/user-agent.js +0 -8
@@ -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,210 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAnchoredPosition = void 0;
4
+ const alternateOrders = {
5
+ 'outside-top': ['outside-bottom', 'outside-right', 'outside-left', 'outside-bottom'],
6
+ 'outside-bottom': ['outside-top', 'outside-right', 'outside-left', 'outside-bottom'],
7
+ 'outside-left': ['outside-right', 'outside-bottom', 'outside-top', 'outside-bottom'],
8
+ 'outside-right': ['outside-left', 'outside-bottom', 'outside-top', 'outside-bottom']
9
+ };
10
+ function getAnchoredPosition(floatingElement, anchorElement, settings = {}) {
11
+ const parentElement = getPositionedParent(floatingElement);
12
+ const clippingRect = getClippingRect(parentElement);
13
+ const parentElementStyle = getComputedStyle(parentElement);
14
+ const parentElementRect = parentElement.getBoundingClientRect();
15
+ const [borderTop, borderLeft] = [parentElementStyle.borderTopWidth, parentElementStyle.borderLeftWidth].map(v => parseInt(v, 10) || 0);
16
+ const relativeRect = {
17
+ top: parentElementRect.top + borderTop,
18
+ left: parentElementRect.left + borderLeft
19
+ };
20
+ return pureCalculateAnchoredPosition(clippingRect, relativeRect, floatingElement.getBoundingClientRect(), anchorElement instanceof Element ? anchorElement.getBoundingClientRect() : anchorElement, getDefaultSettings(settings));
21
+ }
22
+ exports.getAnchoredPosition = getAnchoredPosition;
23
+ function getPositionedParent(element) {
24
+ let parentNode = element.parentNode;
25
+ while (parentNode !== null) {
26
+ if (parentNode instanceof HTMLElement && getComputedStyle(parentNode).position !== 'static') {
27
+ return parentNode;
28
+ }
29
+ parentNode = parentNode.parentNode;
30
+ }
31
+ return document.body;
32
+ }
33
+ function getClippingRect(element) {
34
+ let parentNode = element;
35
+ while (parentNode !== null) {
36
+ if (parentNode === document.body) {
37
+ break;
38
+ }
39
+ const parentNodeStyle = getComputedStyle(parentNode);
40
+ if (parentNodeStyle.overflow !== 'visible') {
41
+ break;
42
+ }
43
+ parentNode = parentNode.parentNode;
44
+ }
45
+ const clippingNode = parentNode === document.body || !(parentNode instanceof HTMLElement) ? document.body : parentNode;
46
+ const elemRect = clippingNode.getBoundingClientRect();
47
+ const elemStyle = getComputedStyle(clippingNode);
48
+ const [borderTop, borderLeft, borderRight, borderBottom] = [
49
+ elemStyle.borderTopWidth,
50
+ elemStyle.borderLeftWidth,
51
+ elemStyle.borderRightWidth,
52
+ elemStyle.borderBottomWidth
53
+ ].map(v => parseInt(v, 10) || 0);
54
+ return {
55
+ top: elemRect.top + borderTop,
56
+ left: elemRect.left + borderLeft,
57
+ width: elemRect.width - borderRight - borderLeft,
58
+ height: Math.max(elemRect.height - borderTop - borderBottom, clippingNode === document.body ? window.innerHeight : -Infinity)
59
+ };
60
+ }
61
+ const positionDefaults = {
62
+ side: 'outside-bottom',
63
+ align: 'start',
64
+ anchorOffset: 4,
65
+ alignmentOffset: 4,
66
+ allowOutOfBounds: false
67
+ };
68
+ function getDefaultSettings(settings = {}) {
69
+ var _a, _b, _c, _d, _e;
70
+ const side = (_a = settings.side) !== null && _a !== void 0 ? _a : positionDefaults.side;
71
+ const align = (_b = settings.align) !== null && _b !== void 0 ? _b : positionDefaults.align;
72
+ return {
73
+ side,
74
+ align,
75
+ anchorOffset: (_c = settings.anchorOffset) !== null && _c !== void 0 ? _c : (side === 'inside-center' ? 0 : positionDefaults.anchorOffset),
76
+ alignmentOffset: (_d = settings.alignmentOffset) !== null && _d !== void 0 ? _d : (align !== 'center' && side.startsWith('inside') ? positionDefaults.alignmentOffset : 0),
77
+ allowOutOfBounds: (_e = settings.allowOutOfBounds) !== null && _e !== void 0 ? _e : positionDefaults.allowOutOfBounds
78
+ };
79
+ }
80
+ function pureCalculateAnchoredPosition(viewportRect, relativePosition, floatingRect, anchorRect, { side, align, allowOutOfBounds, anchorOffset, alignmentOffset }) {
81
+ const relativeViewportRect = {
82
+ top: viewportRect.top - relativePosition.top,
83
+ left: viewportRect.left - relativePosition.left,
84
+ width: viewportRect.width,
85
+ height: viewportRect.height
86
+ };
87
+ let pos = calculatePosition(floatingRect, anchorRect, side, align, anchorOffset, alignmentOffset);
88
+ let anchorSide = side;
89
+ pos.top -= relativePosition.top;
90
+ pos.left -= relativePosition.left;
91
+ if (!allowOutOfBounds) {
92
+ const alternateOrder = alternateOrders[side];
93
+ let positionAttempt = 0;
94
+ if (alternateOrder) {
95
+ let prevSide = side;
96
+ while (positionAttempt < alternateOrder.length &&
97
+ shouldRecalculatePosition(prevSide, pos, relativeViewportRect, floatingRect)) {
98
+ const nextSide = alternateOrder[positionAttempt++];
99
+ prevSide = nextSide;
100
+ pos = calculatePosition(floatingRect, anchorRect, nextSide, align, anchorOffset, alignmentOffset);
101
+ pos.top -= relativePosition.top;
102
+ pos.left -= relativePosition.left;
103
+ anchorSide = nextSide;
104
+ }
105
+ }
106
+ if (pos.top < relativeViewportRect.top) {
107
+ pos.top = relativeViewportRect.top;
108
+ }
109
+ if (pos.left < relativeViewportRect.left) {
110
+ pos.left = relativeViewportRect.left;
111
+ }
112
+ if (pos.left + floatingRect.width > viewportRect.width + relativeViewportRect.left) {
113
+ pos.left = viewportRect.width + relativeViewportRect.left - floatingRect.width;
114
+ }
115
+ if (alternateOrder && positionAttempt < alternateOrder.length) {
116
+ if (pos.top + floatingRect.height > viewportRect.height + relativeViewportRect.top) {
117
+ pos.top = viewportRect.height + relativeViewportRect.top - floatingRect.height;
118
+ }
119
+ }
120
+ }
121
+ return Object.assign(Object.assign({}, pos), { anchorSide });
122
+ }
123
+ function calculatePosition(elementDimensions, anchorPosition, side, align, anchorOffset, alignmentOffset) {
124
+ const anchorRight = anchorPosition.left + anchorPosition.width;
125
+ const anchorBottom = anchorPosition.top + anchorPosition.height;
126
+ let top = -1;
127
+ let left = -1;
128
+ if (side === 'outside-top') {
129
+ top = anchorPosition.top - anchorOffset - elementDimensions.height;
130
+ }
131
+ else if (side === 'outside-bottom') {
132
+ top = anchorBottom + anchorOffset;
133
+ }
134
+ else if (side === 'outside-left') {
135
+ left = anchorPosition.left - anchorOffset - elementDimensions.width;
136
+ }
137
+ else if (side === 'outside-right') {
138
+ left = anchorRight + anchorOffset;
139
+ }
140
+ if (side === 'outside-top' || side === 'outside-bottom') {
141
+ if (align === 'start') {
142
+ left = anchorPosition.left + alignmentOffset;
143
+ }
144
+ else if (align === 'center') {
145
+ left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
146
+ }
147
+ else {
148
+ left = anchorRight - elementDimensions.width - alignmentOffset;
149
+ }
150
+ }
151
+ if (side === 'outside-left' || side === 'outside-right') {
152
+ if (align === 'start') {
153
+ top = anchorPosition.top + alignmentOffset;
154
+ }
155
+ else if (align === 'center') {
156
+ top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
157
+ }
158
+ else {
159
+ top = anchorBottom - elementDimensions.height - alignmentOffset;
160
+ }
161
+ }
162
+ if (side === 'inside-top') {
163
+ top = anchorPosition.top + anchorOffset;
164
+ }
165
+ else if (side === 'inside-bottom') {
166
+ top = anchorBottom - anchorOffset - elementDimensions.height;
167
+ }
168
+ else if (side === 'inside-left') {
169
+ left = anchorPosition.left + anchorOffset;
170
+ }
171
+ else if (side === 'inside-right') {
172
+ left = anchorRight - anchorOffset - elementDimensions.width;
173
+ }
174
+ else if (side === 'inside-center') {
175
+ left = (anchorRight + anchorPosition.left) / 2 - elementDimensions.width / 2 + anchorOffset;
176
+ }
177
+ if (side === 'inside-top' || side === 'inside-bottom') {
178
+ if (align === 'start') {
179
+ left = anchorPosition.left + alignmentOffset;
180
+ }
181
+ else if (align === 'center') {
182
+ left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
183
+ }
184
+ else {
185
+ left = anchorRight - elementDimensions.width - alignmentOffset;
186
+ }
187
+ }
188
+ else if (side === 'inside-left' || side === 'inside-right' || side === 'inside-center') {
189
+ if (align === 'start') {
190
+ top = anchorPosition.top + alignmentOffset;
191
+ }
192
+ else if (align === 'center') {
193
+ top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
194
+ }
195
+ else {
196
+ top = anchorBottom - elementDimensions.height - alignmentOffset;
197
+ }
198
+ }
199
+ return { top, left };
200
+ }
201
+ function shouldRecalculatePosition(side, currentPos, containerDimensions, elementDimensions) {
202
+ if (side === 'outside-top' || side === 'outside-bottom') {
203
+ return (currentPos.top < containerDimensions.top ||
204
+ currentPos.top + elementDimensions.height > containerDimensions.height + containerDimensions.top);
205
+ }
206
+ else {
207
+ return (currentPos.left < containerDimensions.left ||
208
+ currentPos.left + elementDimensions.width > containerDimensions.width + containerDimensions.left);
209
+ }
210
+ }
@@ -0,0 +1 @@
1
+ export declare function focusTrap(container: HTMLElement, initialFocus?: HTMLElement, abortSignal?: AbortSignal): AbortController | undefined;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.focusTrap = void 0;
4
+ const iterate_focusable_elements_js_1 = require("./utils/iterate-focusable-elements.js");
5
+ const event_listener_signal_js_1 = require("./polyfills/event-listener-signal.js");
6
+ (0, event_listener_signal_js_1.polyfill)();
7
+ const suspendedTrapStack = [];
8
+ let activeTrap = undefined;
9
+ function tryReactivate() {
10
+ const trapToReactivate = suspendedTrapStack.pop();
11
+ if (trapToReactivate) {
12
+ focusTrap(trapToReactivate.container, trapToReactivate.initialFocus, trapToReactivate.originalSignal);
13
+ }
14
+ }
15
+ function followSignal(signal) {
16
+ const controller = new AbortController();
17
+ signal.addEventListener('abort', () => {
18
+ controller.abort();
19
+ });
20
+ return controller;
21
+ }
22
+ 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
+ const sentinelStart = document.createElement('span');
27
+ sentinelStart.setAttribute('class', 'sentinel');
28
+ sentinelStart.setAttribute('tabindex', '0');
29
+ sentinelStart.setAttribute('aria-hidden', 'true');
30
+ sentinelStart.onfocus = () => {
31
+ const lastFocusableChild = (0, iterate_focusable_elements_js_1.getFocusableChild)(container, true);
32
+ lastFocusableChild === null || lastFocusableChild === void 0 ? void 0 : lastFocusableChild.focus();
33
+ };
34
+ const sentinelEnd = document.createElement('span');
35
+ sentinelEnd.setAttribute('class', 'sentinel');
36
+ sentinelEnd.setAttribute('tabindex', '0');
37
+ sentinelEnd.setAttribute('aria-hidden', 'true');
38
+ sentinelEnd.onfocus = () => {
39
+ const firstFocusableChild = (0, iterate_focusable_elements_js_1.getFocusableChild)(container);
40
+ firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
41
+ };
42
+ container.prepend(sentinelStart);
43
+ container.append(sentinelEnd);
44
+ if (initialFocus) {
45
+ initialFocus.focus();
46
+ }
47
+ else {
48
+ const firstFocusableChild = (0, iterate_focusable_elements_js_1.getFocusableChild)(container);
49
+ firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
50
+ }
51
+ const wrappingController = followSignal(signal);
52
+ if (activeTrap) {
53
+ const suspendedTrap = activeTrap;
54
+ activeTrap.container.setAttribute('data-focus-trap', 'suspended');
55
+ activeTrap.controller.abort();
56
+ suspendedTrapStack.push(suspendedTrap);
57
+ }
58
+ wrappingController.signal.addEventListener('abort', () => {
59
+ activeTrap = undefined;
60
+ });
61
+ signal.addEventListener('abort', () => {
62
+ container.removeAttribute('data-focus-trap');
63
+ const sentinels = container.getElementsByClassName('sentinel');
64
+ while (sentinels.length > 0)
65
+ sentinels[0].remove();
66
+ const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
67
+ if (suspendedTrapIndex >= 0) {
68
+ suspendedTrapStack.splice(suspendedTrapIndex, 1);
69
+ }
70
+ tryReactivate();
71
+ });
72
+ activeTrap = {
73
+ container,
74
+ controller: wrappingController,
75
+ initialFocus,
76
+ originalSignal: signal
77
+ };
78
+ const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
79
+ if (suspendedTrapIndex >= 0) {
80
+ suspendedTrapStack.splice(suspendedTrapIndex, 1);
81
+ }
82
+ if (!abortSignal) {
83
+ return controller;
84
+ }
85
+ }
86
+ exports.focusTrap = focusTrap;
@@ -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;