@primer/behaviors 1.0.2-rc.b89e9b4 → 1.1.0-rc.e605114

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 (44) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/{anchored-position.d.ts → cjs/anchored-position.d.ts} +0 -0
  3. package/dist/cjs/anchored-position.js +210 -0
  4. package/dist/cjs/focus-trap.d.ts +1 -0
  5. package/dist/cjs/focus-trap.js +107 -0
  6. package/dist/{focus-zone.d.ts → cjs/focus-zone.d.ts} +0 -0
  7. package/dist/cjs/focus-zone.js +410 -0
  8. package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  9. package/dist/cjs/index.js +16 -0
  10. package/dist/{polyfills → cjs/polyfills}/event-listener-signal.d.ts +0 -0
  11. package/dist/cjs/polyfills/event-listener-signal.js +44 -0
  12. package/dist/{scroll-into-view.d.ts → cjs/scroll-into-view.d.ts} +0 -0
  13. package/dist/cjs/scroll-into-view.js +21 -0
  14. package/dist/{utils → cjs/utils}/index.d.ts +0 -0
  15. package/dist/cjs/utils/index.js +15 -0
  16. package/dist/{utils → cjs/utils}/iterate-focusable-elements.d.ts +1 -0
  17. package/dist/cjs/utils/iterate-focusable-elements.js +68 -0
  18. package/dist/{utils → cjs/utils}/unique-id.d.ts +0 -0
  19. package/dist/cjs/utils/unique-id.js +8 -0
  20. package/dist/{utils → cjs/utils}/user-agent.d.ts +0 -0
  21. package/dist/cjs/utils/user-agent.js +11 -0
  22. package/dist/esm/anchored-position.d.ts +15 -0
  23. package/dist/{anchored-position.js → esm/anchored-position.js} +0 -0
  24. package/dist/esm/focus-trap.d.ts +1 -0
  25. package/dist/{focus-trap.js → esm/focus-trap.js} +24 -28
  26. package/dist/esm/focus-zone.d.ts +32 -0
  27. package/dist/{focus-zone.js → esm/focus-zone.js} +0 -0
  28. package/dist/{index.js → esm/index.d.ts} +0 -0
  29. package/dist/esm/index.js +4 -0
  30. package/dist/esm/polyfills/event-listener-signal.d.ts +6 -0
  31. package/dist/{polyfills → esm/polyfills}/event-listener-signal.js +0 -0
  32. package/dist/esm/scroll-into-view.d.ts +7 -0
  33. package/dist/{scroll-into-view.js → esm/scroll-into-view.js} +0 -0
  34. package/dist/{utils/index.js → esm/utils/index.d.ts} +0 -0
  35. package/dist/esm/utils/index.js +3 -0
  36. package/dist/esm/utils/iterate-focusable-elements.d.ts +9 -0
  37. package/dist/{utils → esm/utils}/iterate-focusable-elements.js +5 -1
  38. package/dist/esm/utils/unique-id.d.ts +1 -0
  39. package/dist/{utils → esm/utils}/unique-id.js +0 -0
  40. package/dist/esm/utils/user-agent.d.ts +1 -0
  41. package/dist/{utils → esm/utils}/user-agent.js +0 -0
  42. package/package.json +24 -8
  43. package/utils/package.json +2 -2
  44. package/dist/focus-trap.d.ts +0 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @primer/behaviors
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#52](https://github.com/primer/behaviors/pull/52) [`1aa3027`](https://github.com/primer/behaviors/commit/1aa302782e3c833f9d9c27f602a046e81f05c3e5) Thanks [@owenniblock](https://github.com/owenniblock)! - Update focusTrap to use new methodology after accessibility discussions
8
+
9
+ ## 1.0.3
10
+
11
+ ### Patch Changes
12
+
13
+ - [#42](https://github.com/primer/behaviors/pull/42) [`41945a3`](https://github.com/primer/behaviors/commit/41945a37ef07da82ce5a29feb03d7a7d96ec76ea) Thanks [@dgreif](https://github.com/dgreif)! - Build both esm and cjs output for the package
14
+
3
15
  ## 1.0.2
4
16
 
5
17
  ### Patch Changes
@@ -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,107 @@
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
+ let lastFocusedChild = undefined;
45
+ function ensureTrapZoneHasFocus(focusedElement) {
46
+ if (focusedElement instanceof HTMLElement && document.contains(container)) {
47
+ if (container.contains(focusedElement)) {
48
+ lastFocusedChild = focusedElement;
49
+ return;
50
+ }
51
+ else {
52
+ if (lastFocusedChild && (0, iterate_focusable_elements_js_1.isTabbable)(lastFocusedChild) && container.contains(lastFocusedChild)) {
53
+ lastFocusedChild.focus();
54
+ return;
55
+ }
56
+ else if (initialFocus && container.contains(initialFocus)) {
57
+ initialFocus.focus();
58
+ return;
59
+ }
60
+ else {
61
+ const firstFocusableChild = (0, iterate_focusable_elements_js_1.getFocusableChild)(container);
62
+ firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
63
+ return;
64
+ }
65
+ }
66
+ }
67
+ }
68
+ const wrappingController = followSignal(signal);
69
+ if (activeTrap) {
70
+ const suspendedTrap = activeTrap;
71
+ activeTrap.container.setAttribute('data-focus-trap', 'suspended');
72
+ activeTrap.controller.abort();
73
+ suspendedTrapStack.push(suspendedTrap);
74
+ }
75
+ wrappingController.signal.addEventListener('abort', () => {
76
+ activeTrap = undefined;
77
+ });
78
+ signal.addEventListener('abort', () => {
79
+ container.removeAttribute('data-focus-trap');
80
+ const sentinels = container.getElementsByClassName('sentinel');
81
+ while (sentinels.length > 0)
82
+ sentinels[0].remove();
83
+ const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
84
+ if (suspendedTrapIndex >= 0) {
85
+ suspendedTrapStack.splice(suspendedTrapIndex, 1);
86
+ }
87
+ tryReactivate();
88
+ });
89
+ document.addEventListener('focus', event => {
90
+ ensureTrapZoneHasFocus(event.target);
91
+ }, { signal: wrappingController.signal, capture: true });
92
+ ensureTrapZoneHasFocus(document.activeElement);
93
+ activeTrap = {
94
+ container,
95
+ controller: wrappingController,
96
+ initialFocus,
97
+ originalSignal: signal
98
+ };
99
+ const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
100
+ if (suspendedTrapIndex >= 0) {
101
+ suspendedTrapStack.splice(suspendedTrapIndex, 1);
102
+ }
103
+ if (!abortSignal) {
104
+ return controller;
105
+ }
106
+ }
107
+ exports.focusTrap = focusTrap;
File without changes