@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.
Files changed (74) hide show
  1. package/lib/__tests__/anchored-position.test.d.ts +1 -0
  2. package/lib/__tests__/anchored-position.test.js +388 -0
  3. package/lib/__tests__/focus-trap.test.d.ts +1 -0
  4. package/lib/__tests__/focus-trap.test.js +234 -0
  5. package/lib/__tests__/focus-zone.test.d.ts +1 -0
  6. package/lib/__tests__/focus-zone.test.js +570 -0
  7. package/lib/__tests__/iterate-focusable-elements.test.d.ts +1 -0
  8. package/lib/__tests__/iterate-focusable-elements.test.js +55 -0
  9. package/lib/__tests__/scroll-into-view.test.d.ts +1 -0
  10. package/lib/__tests__/scroll-into-view.test.js +245 -0
  11. package/lib/anchored-position.d.ts +89 -0
  12. package/lib/anchored-position.js +316 -0
  13. package/lib/focus-trap.d.ts +12 -0
  14. package/lib/focus-trap.js +179 -0
  15. package/lib/focus-zone.d.ts +137 -0
  16. package/lib/focus-zone.js +578 -0
  17. package/{dist/index.js → lib/index.d.ts} +0 -0
  18. package/lib/index.js +57 -0
  19. package/lib/polyfills/event-listener-signal.d.ts +6 -0
  20. package/lib/polyfills/event-listener-signal.js +64 -0
  21. package/lib/scroll-into-view.d.ts +7 -0
  22. package/lib/scroll-into-view.js +42 -0
  23. package/{dist/utils/index.js → lib/utils/index.d.ts} +0 -0
  24. package/lib/utils/index.js +44 -0
  25. package/lib/utils/iterate-focusable-elements.d.ts +42 -0
  26. package/lib/utils/iterate-focusable-elements.js +113 -0
  27. package/lib/utils/unique-id.d.ts +1 -0
  28. package/lib/utils/unique-id.js +12 -0
  29. package/lib/utils/user-agent.d.ts +1 -0
  30. package/lib/utils/user-agent.js +15 -0
  31. package/lib-esm/__tests__/anchored-position.test.d.ts +1 -0
  32. package/lib-esm/__tests__/anchored-position.test.js +386 -0
  33. package/lib-esm/__tests__/focus-trap.test.d.ts +1 -0
  34. package/lib-esm/__tests__/focus-trap.test.js +227 -0
  35. package/lib-esm/__tests__/focus-zone.test.d.ts +1 -0
  36. package/lib-esm/__tests__/focus-zone.test.js +487 -0
  37. package/lib-esm/__tests__/iterate-focusable-elements.test.d.ts +1 -0
  38. package/lib-esm/__tests__/iterate-focusable-elements.test.js +48 -0
  39. package/lib-esm/__tests__/scroll-into-view.test.d.ts +1 -0
  40. package/lib-esm/__tests__/scroll-into-view.test.js +243 -0
  41. package/lib-esm/anchored-position.d.ts +89 -0
  42. package/lib-esm/anchored-position.js +309 -0
  43. package/lib-esm/focus-trap.d.ts +12 -0
  44. package/lib-esm/focus-trap.js +170 -0
  45. package/lib-esm/focus-zone.d.ts +137 -0
  46. package/lib-esm/focus-zone.js +559 -0
  47. package/{dist → lib-esm}/index.d.ts +0 -0
  48. package/lib-esm/index.js +4 -0
  49. package/{dist → lib-esm}/polyfills/event-listener-signal.d.ts +0 -0
  50. package/lib-esm/polyfills/event-listener-signal.js +57 -0
  51. package/{dist → lib-esm}/scroll-into-view.d.ts +0 -0
  52. package/lib-esm/scroll-into-view.js +35 -0
  53. package/{dist → lib-esm}/utils/index.d.ts +0 -0
  54. package/lib-esm/utils/index.js +3 -0
  55. package/lib-esm/utils/iterate-focusable-elements.d.ts +42 -0
  56. package/lib-esm/utils/iterate-focusable-elements.js +102 -0
  57. package/{dist → lib-esm}/utils/unique-id.d.ts +0 -0
  58. package/lib-esm/utils/unique-id.js +5 -0
  59. package/{dist → lib-esm}/utils/user-agent.d.ts +0 -0
  60. package/lib-esm/utils/user-agent.js +8 -0
  61. package/package.json +36 -5
  62. package/dist/anchored-position.d.ts +0 -15
  63. package/dist/anchored-position.js +0 -206
  64. package/dist/focus-trap.d.ts +0 -2
  65. package/dist/focus-trap.js +0 -107
  66. package/dist/focus-zone.d.ts +0 -32
  67. package/dist/focus-zone.js +0 -406
  68. package/dist/polyfills/event-listener-signal.js +0 -40
  69. package/dist/scroll-into-view.js +0 -17
  70. package/dist/utils/iterate-focusable-elements.d.ts +0 -8
  71. package/dist/utils/iterate-focusable-elements.js +0 -57
  72. package/dist/utils/unique-id.js +0 -4
  73. package/dist/utils/user-agent.js +0 -7
  74. package/utils/package.json +0 -7
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.polyfill = polyfill;
7
+
8
+ /*
9
+
10
+ This file polyfills the following: https://github.com/whatwg/dom/issues/911
11
+ Once all targeted browsers support this DOM feature, this polyfill can be deleted.
12
+
13
+ This allows users to pass an AbortSignal to a call to addEventListener as part of the
14
+ AddEventListenerOptions object. When the signal is aborted, the event listener is
15
+ removed.
16
+
17
+ */
18
+ let signalSupported = false; // eslint-disable-next-line @typescript-eslint/no-empty-function
19
+
20
+ function noop() {}
21
+
22
+ try {
23
+ const options = Object.create({}, {
24
+ signal: {
25
+ get() {
26
+ signalSupported = true;
27
+ }
28
+
29
+ }
30
+ });
31
+ window.addEventListener('test', noop, options);
32
+ window.removeEventListener('test', noop, options);
33
+ } catch (e) {
34
+ /* */
35
+ }
36
+
37
+ function featureSupported() {
38
+ return signalSupported;
39
+ }
40
+
41
+ function monkeyPatch() {
42
+ if (typeof window === 'undefined') {
43
+ return;
44
+ }
45
+
46
+ const originalAddEventListener = EventTarget.prototype.addEventListener;
47
+
48
+ EventTarget.prototype.addEventListener = function (name, originalCallback, optionsOrCapture) {
49
+ if (typeof optionsOrCapture === 'object' && 'signal' in optionsOrCapture && optionsOrCapture.signal instanceof AbortSignal) {
50
+ originalAddEventListener.call(optionsOrCapture.signal, 'abort', () => {
51
+ this.removeEventListener(name, originalCallback, optionsOrCapture);
52
+ });
53
+ }
54
+
55
+ return originalAddEventListener.call(this, name, originalCallback, optionsOrCapture);
56
+ };
57
+ }
58
+
59
+ function polyfill() {
60
+ if (!featureSupported()) {
61
+ monkeyPatch();
62
+ signalSupported = true;
63
+ }
64
+ }
@@ -0,0 +1,7 @@
1
+ export interface ScrollIntoViewOptions {
2
+ direction?: 'horizontal' | 'vertical';
3
+ startMargin?: number;
4
+ endMargin?: number;
5
+ behavior?: ScrollBehavior;
6
+ }
7
+ export declare function scrollIntoView(child: HTMLElement, viewingArea: HTMLElement, { direction, startMargin, endMargin, behavior }?: ScrollIntoViewOptions): void;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.scrollIntoView = scrollIntoView;
7
+
8
+ function scrollIntoView(child, viewingArea, {
9
+ direction = 'vertical',
10
+ startMargin = 0,
11
+ endMargin = 0,
12
+ behavior = 'smooth'
13
+ } = {}) {
14
+ const startSide = direction === 'vertical' ? 'top' : 'left';
15
+ const endSide = direction === 'vertical' ? 'bottom' : 'right';
16
+ const scrollSide = direction === 'vertical' ? 'scrollTop' : 'scrollLeft';
17
+ const {
18
+ [startSide]: childStart,
19
+ [endSide]: childEnd
20
+ } = child.getBoundingClientRect();
21
+ const {
22
+ [startSide]: viewingAreaStart,
23
+ [endSide]: viewingAreaEnd
24
+ } = viewingArea.getBoundingClientRect();
25
+ const isChildStartAboveViewingArea = childStart < viewingAreaStart + startMargin;
26
+ const isChildBottomBelowViewingArea = childEnd > viewingAreaEnd - endMargin;
27
+
28
+ if (isChildStartAboveViewingArea) {
29
+ const scrollHeightToChildStart = childStart - viewingAreaStart + viewingArea[scrollSide];
30
+ viewingArea.scrollTo({
31
+ behavior,
32
+ [startSide]: scrollHeightToChildStart - startMargin
33
+ });
34
+ } else if (isChildBottomBelowViewingArea) {
35
+ const scrollHeightToChildBottom = childEnd - viewingAreaEnd + viewingArea[scrollSide];
36
+ viewingArea.scrollTo({
37
+ behavior,
38
+ [startSide]: scrollHeightToChildBottom + endMargin
39
+ });
40
+ } // either completely in view or outside viewing area on both ends, don't scroll
41
+
42
+ }
File without changes
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _iterateFocusableElements = require("./iterate-focusable-elements.js");
8
+
9
+ Object.keys(_iterateFocusableElements).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _iterateFocusableElements[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _iterateFocusableElements[key];
16
+ }
17
+ });
18
+ });
19
+
20
+ var _uniqueId = require("./unique-id.js");
21
+
22
+ Object.keys(_uniqueId).forEach(function (key) {
23
+ if (key === "default" || key === "__esModule") return;
24
+ if (key in exports && exports[key] === _uniqueId[key]) return;
25
+ Object.defineProperty(exports, key, {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _uniqueId[key];
29
+ }
30
+ });
31
+ });
32
+
33
+ var _userAgent = require("./user-agent.js");
34
+
35
+ Object.keys(_userAgent).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _userAgent[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function () {
41
+ return _userAgent[key];
42
+ }
43
+ });
44
+ });
@@ -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,113 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isFocusable = isFocusable;
7
+ exports.isTabbable = isTabbable;
8
+ exports.iterateFocusableElements = iterateFocusableElements;
9
+
10
+ /**
11
+ * Options to the focusable elements iterator
12
+ */
13
+
14
+ /**
15
+ * Returns an iterator over all of the focusable elements within `container`.
16
+ * Note: If `container` is itself focusable it will be included in the results.
17
+ * @param container The container over which to find focusable elements.
18
+ * @param reverse If true, iterate backwards through focusable elements.
19
+ */
20
+ function* iterateFocusableElements(container, options = {}) {
21
+ var _options$strict, _options$onlyTabbable;
22
+
23
+ const strict = (_options$strict = options.strict) !== null && _options$strict !== void 0 ? _options$strict : false;
24
+ const acceptFn = ((_options$onlyTabbable = options.onlyTabbable) !== null && _options$onlyTabbable !== void 0 ? _options$onlyTabbable : false) ? isTabbable : isFocusable;
25
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
26
+ acceptNode: node => node instanceof HTMLElement && acceptFn(node, strict) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
27
+ });
28
+ let nextNode = null; // Allow the container to participate
29
+
30
+ if (!options.reverse && acceptFn(container, strict)) {
31
+ yield container;
32
+ } // If iterating in reverse, continue traversing down into the last child until we reach
33
+ // a leaf DOM node
34
+
35
+
36
+ if (options.reverse) {
37
+ let lastChild = walker.lastChild();
38
+
39
+ while (lastChild) {
40
+ nextNode = lastChild;
41
+ lastChild = walker.lastChild();
42
+ }
43
+ } else {
44
+ nextNode = walker.firstChild();
45
+ }
46
+
47
+ while (nextNode instanceof HTMLElement) {
48
+ yield nextNode;
49
+ nextNode = options.reverse ? walker.previousNode() : walker.nextNode();
50
+ } // Allow the container to participate (in reverse)
51
+
52
+
53
+ if (options.reverse && acceptFn(container, strict)) {
54
+ yield container;
55
+ }
56
+
57
+ return undefined;
58
+ }
59
+ /**
60
+ * Determines whether the given element is focusable. If `strict` is true, we may
61
+ * perform additional checks that require a reflow (less performant).
62
+ * @param elem
63
+ * @param strict
64
+ */
65
+
66
+
67
+ function isFocusable(elem, strict = false) {
68
+ // Certain conditions cause an element to never be focusable, even if they have tabindex="0"
69
+ const disabledAttrInert = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'OPTGROUP', 'OPTION', 'FIELDSET'].includes(elem.tagName) && elem.disabled;
70
+ const hiddenInert = elem.hidden;
71
+ const hiddenInputInert = elem instanceof HTMLInputElement && elem.type === 'hidden';
72
+
73
+ if (disabledAttrInert || hiddenInert || hiddenInputInert) {
74
+ return false;
75
+ } // Each of the conditions checked below require a reflow, thus are gated by the `strict`
76
+ // argument. If any are true, the element is not focusable, even if tabindex is set.
77
+
78
+
79
+ if (strict) {
80
+ const sizeInert = elem.offsetWidth === 0 || elem.offsetHeight === 0;
81
+ const visibilityInert = ['hidden', 'collapse'].includes(getComputedStyle(elem).visibility);
82
+ const clientRectsInert = elem.getClientRects().length === 0;
83
+
84
+ if (sizeInert || visibilityInert || clientRectsInert) {
85
+ return false;
86
+ }
87
+ } // Any element with `tabindex` explicitly set can be focusable, even if it's set to "-1"
88
+
89
+
90
+ if (elem.getAttribute('tabindex') != null) {
91
+ return true;
92
+ } // One last way `elem.tabIndex` can be wrong.
93
+
94
+
95
+ if (elem instanceof HTMLAnchorElement && elem.getAttribute('href') == null) {
96
+ return false;
97
+ }
98
+
99
+ return elem.tabIndex !== -1;
100
+ }
101
+ /**
102
+ * Determines whether the given element is tabbable. If `strict` is true, we may
103
+ * perform additional checks that require a reflow (less performant). This check
104
+ * ensures that the element is focusable and that its tabindex is not explicitly
105
+ * set to "-1" (which makes it focusable, but removes it from the tab order).
106
+ * @param elem
107
+ * @param strict
108
+ */
109
+
110
+
111
+ function isTabbable(elem, strict = false) {
112
+ return isFocusable(elem, strict) && elem.getAttribute('tabindex') !== '-1';
113
+ }
@@ -0,0 +1 @@
1
+ export declare function uniqueId(): string;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.uniqueId = uniqueId;
7
+ // Note: uniqueId may be unsafe in SSR contexts if it is used create DOM IDs or otherwise cause a hydration warning. Use useSSRSafeId instead.
8
+ let idSeed = 10000;
9
+
10
+ function uniqueId() {
11
+ return `__primer_id_${idSeed++}`;
12
+ }
@@ -0,0 +1 @@
1
+ export declare function isMacOS(): boolean;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isMacOS = isMacOS;
7
+ let isMac = undefined;
8
+
9
+ function isMacOS() {
10
+ if (isMac === undefined) {
11
+ isMac = /^mac/i.test(window.navigator.platform);
12
+ }
13
+
14
+ return isMac;
15
+ }
@@ -0,0 +1 @@
1
+ export {};