@reshaped/utilities 3.9.1-canary.2 → 3.10.0-canary.4

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 (100) hide show
  1. package/dist/a11y/Chain.d.ts +20 -0
  2. package/dist/a11y/Chain.js +60 -0
  3. package/dist/a11y/TrapFocus.d.ts +28 -0
  4. package/dist/a11y/TrapFocus.js +162 -0
  5. package/dist/a11y/TrapScreenReader.d.ts +15 -0
  6. package/dist/a11y/TrapScreenReader.js +42 -0
  7. package/dist/a11y/focus.d.ts +38 -0
  8. package/dist/a11y/focus.js +101 -0
  9. package/dist/a11y/index.d.ts +4 -0
  10. package/dist/a11y/index.js +3 -0
  11. package/dist/a11y/keyboardMode.d.ts +4 -0
  12. package/dist/a11y/keyboardMode.js +10 -0
  13. package/dist/a11y/tests/Chain.test.js +88 -0
  14. package/dist/a11y/tests/TrapFocus.test.d.ts +1 -0
  15. package/dist/a11y/tests/TrapFocus.test.js +313 -0
  16. package/dist/a11y/tests/TrapScreenReader.test.d.ts +1 -0
  17. package/dist/a11y/tests/TrapScreenReader.test.js +126 -0
  18. package/dist/a11y/tests/focus.test.d.ts +1 -0
  19. package/dist/a11y/tests/focus.test.js +278 -0
  20. package/dist/a11y/tests/keyboardMode.test.d.ts +1 -0
  21. package/dist/a11y/tests/keyboardMode.test.js +27 -0
  22. package/dist/a11y/types.d.ts +24 -0
  23. package/dist/a11y/types.js +1 -0
  24. package/dist/constants/keys.d.ts +11 -0
  25. package/dist/constants/keys.js +11 -0
  26. package/dist/css/StyleCache.d.ts +7 -0
  27. package/dist/css/StyleCache.js +19 -0
  28. package/dist/css/classNames.d.ts +7 -0
  29. package/dist/css/classNames.js +19 -0
  30. package/dist/css/index.d.ts +2 -0
  31. package/dist/css/index.js +4 -0
  32. package/dist/css/tests/StyleCache.test.d.ts +1 -0
  33. package/dist/css/tests/StyleCache.test.js +45 -0
  34. package/dist/css/tests/classNames.test.d.ts +1 -0
  35. package/dist/css/tests/classNames.test.js +63 -0
  36. package/dist/dom/findClosestScrollableContainer.d.ts +5 -0
  37. package/dist/dom/findClosestScrollableContainer.js +12 -0
  38. package/dist/dom/findParent.d.ts +2 -0
  39. package/dist/dom/findParent.js +10 -0
  40. package/dist/dom/index.d.ts +3 -0
  41. package/dist/dom/index.js +4 -0
  42. package/dist/dom/tests/findClosestScrollableContainer.test.d.ts +1 -0
  43. package/dist/dom/tests/findClosestScrollableContainer.test.js +61 -0
  44. package/dist/dom/tests/findParent.test.d.ts +1 -0
  45. package/dist/dom/tests/findParent.test.js +45 -0
  46. package/dist/flyout/Flyout.js +11 -9
  47. package/dist/flyout/constants.d.ts +1 -1
  48. package/dist/flyout/constants.js +1 -1
  49. package/dist/flyout/index.d.ts +1 -1
  50. package/dist/flyout/index.js +1 -1
  51. package/dist/flyout/tests/Flyout.test.js +1 -1
  52. package/dist/flyout/types.d.ts +1 -1
  53. package/dist/flyout/utilities/applyPosition.js +46 -26
  54. package/dist/flyout/utilities/calculateLayoutAdjustment.d.ts +19 -0
  55. package/dist/flyout/utilities/calculateLayoutAdjustment.js +73 -0
  56. package/dist/flyout/utilities/calculatePosition.d.ts +7 -20
  57. package/dist/flyout/utilities/calculatePosition.js +11 -87
  58. package/dist/flyout/utilities/isFullyVisible.d.ts +2 -4
  59. package/dist/flyout/utilities/isFullyVisible.js +11 -14
  60. package/dist/flyout/utilities/tests/applyPosition.test.js +1 -1
  61. package/dist/flyout/utilities/tests/calculateLayoutAdjustment.test.d.ts +1 -0
  62. package/dist/flyout/utilities/tests/calculateLayoutAdjustment.test.js +384 -0
  63. package/dist/flyout/utilities/tests/calculatePosition.test.js +244 -293
  64. package/dist/flyout/utilities/tests/centerBySize.test.js +1 -1
  65. package/dist/flyout/utilities/tests/getPositionFallbacks.test.js +1 -1
  66. package/dist/flyout/utilities/tests/getRTLPosition.test.js +1 -1
  67. package/dist/flyout/utilities/tests/isFullyVisible.test.js +28 -52
  68. package/dist/helpers/classNames.d.ts +7 -0
  69. package/dist/helpers/classNames.js +19 -0
  70. package/dist/helpers/index.d.ts +1 -0
  71. package/dist/helpers/index.js +2 -0
  72. package/dist/helpers/tests/classNames.test.d.ts +1 -0
  73. package/dist/helpers/tests/classNames.test.js +63 -0
  74. package/dist/i18n/index.d.ts +1 -0
  75. package/dist/i18n/index.js +2 -0
  76. package/dist/index.d.ts +5 -1
  77. package/dist/index.js +5 -1
  78. package/dist/internal.d.ts +11 -0
  79. package/dist/internal.js +10 -0
  80. package/dist/platform/index.d.ts +1 -0
  81. package/dist/platform/index.js +16 -0
  82. package/dist/scroll/disable.d.ts +7 -0
  83. package/dist/scroll/disable.js +15 -0
  84. package/dist/scroll/helpers.d.ts +1 -0
  85. package/dist/scroll/helpers.js +17 -0
  86. package/dist/scroll/index.d.ts +2 -0
  87. package/dist/scroll/index.js +4 -0
  88. package/dist/scroll/lock.d.ts +7 -0
  89. package/dist/scroll/lock.js +26 -0
  90. package/dist/scroll/lockSafari.d.ts +2 -0
  91. package/dist/scroll/lockSafari.js +20 -0
  92. package/dist/scroll/lockStandard.d.ts +4 -0
  93. package/dist/scroll/lockStandard.js +15 -0
  94. package/dist/scroll/tests/lock.test.d.ts +1 -0
  95. package/dist/scroll/tests/lock.test.js +81 -0
  96. package/package.json +6 -1
  97. package/dist/flyout/utilities/findClosestFixedContainer.d.ts +0 -5
  98. package/dist/flyout/utilities/findClosestFixedContainer.js +0 -18
  99. package/dist/flyout/utilities/tests/findClosestFixedContainer.test.js +0 -46
  100. /package/dist/{flyout/utilities/tests/findClosestFixedContainer.test.d.ts → a11y/tests/Chain.test.d.ts} +0 -0
@@ -0,0 +1,20 @@
1
+ type ID = number;
2
+ type Item<T> = {
3
+ previousId?: ID | null;
4
+ nextId?: ID | null;
5
+ data: T;
6
+ };
7
+ declare class Chain<T = unknown> {
8
+ chain: Record<ID, Item<T>>;
9
+ tailId: ID | null;
10
+ idCounter: ID;
11
+ generateId(): number;
12
+ getAll(): Record<number, Item<T>>;
13
+ get(id: ID): Item<T>;
14
+ isLast(id: ID): boolean;
15
+ isEmpty(): boolean;
16
+ add(data: T): number;
17
+ remove(id: ID): T | undefined;
18
+ removePreviousTill(id: ID, condition: (item: Item<T>) => boolean): T | undefined;
19
+ }
20
+ export default Chain;
@@ -0,0 +1,60 @@
1
+ class Chain {
2
+ chain = {};
3
+ tailId = null;
4
+ idCounter = 0;
5
+ generateId() {
6
+ this.idCounter += 1;
7
+ return this.idCounter;
8
+ }
9
+ getAll() {
10
+ return this.chain;
11
+ }
12
+ get(id) {
13
+ return this.chain[id];
14
+ }
15
+ isLast(id) {
16
+ return this.tailId !== null && id === this.tailId;
17
+ }
18
+ isEmpty() {
19
+ return typeof this.tailId !== "number";
20
+ }
21
+ add(data) {
22
+ const previousId = this.tailId;
23
+ const previousItem = previousId && this.get(previousId);
24
+ const id = this.generateId();
25
+ this.chain[id] = { previousId, data };
26
+ if (previousItem)
27
+ previousItem.nextId = id;
28
+ this.tailId = id;
29
+ return id;
30
+ }
31
+ remove(id) {
32
+ const target = this.chain[id];
33
+ if (!target)
34
+ return;
35
+ const previousId = target.previousId;
36
+ const previousItem = previousId && this.get(previousId);
37
+ const nextId = target.nextId;
38
+ const nextItem = nextId && this.get(nextId);
39
+ if (previousItem)
40
+ previousItem.nextId = target.nextId ?? null;
41
+ if (nextItem)
42
+ nextItem.previousId = target.previousId ?? null;
43
+ if (!nextId)
44
+ this.tailId = previousId ?? null;
45
+ const data = this.get(id).data;
46
+ delete this.chain[id];
47
+ return data;
48
+ }
49
+ removePreviousTill(id, condition) {
50
+ const target = this.get(id);
51
+ const data = this.remove(id);
52
+ if (!target || !target.previousId)
53
+ return data;
54
+ const keepIterating = !condition(target);
55
+ if (keepIterating)
56
+ return this.removePreviousTill(target.previousId, condition);
57
+ return data;
58
+ }
59
+ }
60
+ export default Chain;
@@ -0,0 +1,28 @@
1
+ import Chain from "./Chain";
2
+ import type { FocusableElement, TrapMode } from "./types";
3
+ type ReleaseOptions = {
4
+ withoutFocusReturn?: boolean;
5
+ };
6
+ type TrapOptions = {
7
+ onRelease?: () => void;
8
+ includeTrigger?: boolean;
9
+ initialFocusEl?: FocusableElement | null;
10
+ mode?: TrapMode;
11
+ };
12
+ declare class TrapFocus {
13
+ #private;
14
+ static chain: Chain<TrapFocus>;
15
+ trapped?: boolean;
16
+ constructor();
17
+ /**
18
+ * Trap the focus, add observer and keyboard event listeners
19
+ * and create a chain item
20
+ */
21
+ trap: (root: HTMLElement, options?: TrapOptions) => void;
22
+ /**
23
+ * Disabled the trap focus for the element,
24
+ * cleanup all observers/handlers and trap for the previous element in the chain
25
+ */
26
+ release: (releaseOptions?: ReleaseOptions) => void;
27
+ }
28
+ export default TrapFocus;
@@ -0,0 +1,162 @@
1
+ var _a;
2
+ import * as keys from "../constants/keys.js";
3
+ import { getShadowRoot } from "../dom/index.js";
4
+ import Chain from "./Chain.js";
5
+ import { getActiveElement, getFocusableElements, focusElement, getFocusData } from "./focus.js";
6
+ import { checkKeyboardMode } from "./keyboardMode.js";
7
+ import TrapScreenReader from "./TrapScreenReader.js";
8
+ class TrapFocus {
9
+ static chain = new Chain();
10
+ #chainId;
11
+ #root = null;
12
+ #trigger = null;
13
+ #options = {};
14
+ trapped;
15
+ #screenReaderTrap = null;
16
+ #mutationObserver = null;
17
+ constructor() { }
18
+ /**
19
+ * Handle keyboard navigation while focus is trapped
20
+ */
21
+ #handleKeyDown = (event) => {
22
+ if (event.defaultPrevented)
23
+ return;
24
+ if (_a.chain.tailId !== this.#chainId)
25
+ return;
26
+ if (!this.#root)
27
+ return;
28
+ const { mode, onRelease, pseudoFocus, includeTrigger } = this.#options;
29
+ let navigationMode = "tabs";
30
+ if (mode === "action-menu" || mode === "selection-menu" || mode === "action-bar") {
31
+ navigationMode = "arrows";
32
+ }
33
+ const key = event.key;
34
+ const isTab = key === keys.TAB;
35
+ const isPrevTab = isTab && event.shiftKey;
36
+ const isNextTab = isTab && !event.shiftKey;
37
+ const isArrow = [keys.LEFT, keys.RIGHT, keys.UP, keys.DOWN].includes(key);
38
+ const isPrevArrow = navigationMode === "arrows" && key === (mode === "action-bar" ? keys.LEFT : keys.UP);
39
+ const isNextArrow = navigationMode === "arrows" && key === (mode === "action-bar" ? keys.RIGHT : keys.DOWN);
40
+ const isPrev = (isPrevTab && navigationMode === "tabs") || isPrevArrow;
41
+ const isNext = (isNextTab && navigationMode === "tabs") || isNextArrow;
42
+ const isFocusedOnTrigger = getActiveElement(this.#root) === this.#trigger;
43
+ const focusData = getFocusData({
44
+ root: this.#root,
45
+ target: isPrev ? "prev" : "next",
46
+ options: {
47
+ additionalElement: includeTrigger ? this.#trigger : undefined,
48
+ circular: true,
49
+ },
50
+ });
51
+ // Release the trap when tab is used in navigation modes that support arrows
52
+ const hasNavigatedOutside = (isTab && navigationMode === "arrows") ||
53
+ (mode === "content-menu" && isTab && focusData.overflow);
54
+ if (hasNavigatedOutside) {
55
+ // Prevent shift + tab event to avoid focus moving after the trap release
56
+ if (isPrevTab && !isFocusedOnTrigger)
57
+ event.preventDefault();
58
+ this.release();
59
+ onRelease?.();
60
+ return;
61
+ }
62
+ if (!isPrev && !isNext) {
63
+ // Avoid page from scrolling with arrow keys while focus it trapped
64
+ if (isArrow && (mode === "action-bar" || mode === "action-menu"))
65
+ event.preventDefault();
66
+ return;
67
+ }
68
+ event.preventDefault();
69
+ if (!focusData.el)
70
+ return;
71
+ focusElement(focusData.el, { pseudoFocus });
72
+ };
73
+ #addListeners = () => {
74
+ const shadowRoot = getShadowRoot(this.#root);
75
+ const el = shadowRoot ?? document;
76
+ el.addEventListener("keydown", this.#handleKeyDown);
77
+ };
78
+ #removeListeners = () => {
79
+ const shadowRoot = getShadowRoot(this.#root);
80
+ const el = shadowRoot ?? document;
81
+ el.removeEventListener("keydown", this.#handleKeyDown);
82
+ };
83
+ #isLast = () => {
84
+ const tailItem = _a.chain.tailId && _a.chain.get(_a.chain.tailId);
85
+ return tailItem && tailItem.data.#root === this.#root;
86
+ };
87
+ /**
88
+ * Trap the focus, add observer and keyboard event listeners
89
+ * and create a chain item
90
+ */
91
+ trap = (root, options = {}) => {
92
+ const { mode = "dialog", includeTrigger, initialFocusEl } = options;
93
+ this.#root = root;
94
+ this.#screenReaderTrap = new TrapScreenReader(root);
95
+ const trigger = getActiveElement(this.#root);
96
+ const focusable = getFocusableElements(this.#root, {
97
+ additionalElement: includeTrigger ? trigger : undefined,
98
+ });
99
+ const pseudoFocus = mode === "selection-menu";
100
+ this.#options = { ...options, pseudoFocus };
101
+ this.#trigger = trigger;
102
+ this.#mutationObserver = new MutationObserver(() => {
103
+ if (!this.#root)
104
+ return;
105
+ if (!this.#isLast())
106
+ return;
107
+ const currentActiveElement = getActiveElement(this.#root);
108
+ // Focus stayed inside the wrapper, no need to refocus
109
+ if (this.#root.contains(currentActiveElement))
110
+ return;
111
+ const focusable = getFocusableElements(this.#root, {
112
+ additionalElement: includeTrigger ? trigger : undefined,
113
+ });
114
+ if (!focusable.length)
115
+ return;
116
+ focusElement(focusable[0], { pseudoFocus });
117
+ });
118
+ this.#removeListeners();
119
+ this.#mutationObserver.observe(this.#root, { childList: true, subtree: true });
120
+ // Don't trap in case there is nothing to focus inside
121
+ if (!focusable.length && !initialFocusEl)
122
+ return;
123
+ this.#addListeners();
124
+ if (mode === "dialog")
125
+ this.#screenReaderTrap.trap();
126
+ const currentActiveElement = getActiveElement(this.#root);
127
+ const isLastInChain = this.#isLast();
128
+ // Don't add back to the chain if we're traversing back
129
+ if (!isLastInChain) {
130
+ this.#chainId = _a.chain.add(this);
131
+ // If the focus was moved manually (e.g. with autoFocus) - keep it there
132
+ if (!this.#root.contains(currentActiveElement)) {
133
+ focusElement(initialFocusEl || focusable[0], { pseudoFocus });
134
+ }
135
+ }
136
+ this.trapped = true;
137
+ };
138
+ /**
139
+ * Disabled the trap focus for the element,
140
+ * cleanup all observers/handlers and trap for the previous element in the chain
141
+ */
142
+ release = (releaseOptions = {}) => {
143
+ const { withoutFocusReturn } = releaseOptions;
144
+ if (!this.trapped || !this.#chainId || !this.#root)
145
+ return;
146
+ this.trapped = false;
147
+ if (this.#trigger && !withoutFocusReturn) {
148
+ this.#trigger.focus({ preventScroll: !checkKeyboardMode() });
149
+ }
150
+ _a.chain.removePreviousTill(this.#chainId, (item) => document.body.contains(item.data.#trigger));
151
+ this.#mutationObserver?.disconnect();
152
+ this.#removeListeners();
153
+ this.#screenReaderTrap?.release();
154
+ const previousItem = _a.chain.tailId && _a.chain.get(_a.chain.tailId);
155
+ if (previousItem && previousItem.data.#root) {
156
+ const trapInstance = new _a();
157
+ trapInstance.trap(previousItem.data.#root, previousItem.data.#options);
158
+ }
159
+ };
160
+ }
161
+ _a = TrapFocus;
162
+ export default TrapFocus;
@@ -0,0 +1,15 @@
1
+ declare class TrapScreenReader {
2
+ root: HTMLElement;
3
+ /**
4
+ * Elements ignored by screen reader when trap is active
5
+ */
6
+ private hiddenElements;
7
+ constructor(root: HTMLElement);
8
+ /**
9
+ * Apply aria-hidden to all elements except the passed
10
+ */
11
+ hideSiblingsFromScreenReader: (el: HTMLElement) => void;
12
+ release: () => void;
13
+ trap: () => void;
14
+ }
15
+ export default TrapScreenReader;
@@ -0,0 +1,42 @@
1
+ class TrapScreenReader {
2
+ root;
3
+ /**
4
+ * Elements ignored by screen reader when trap is active
5
+ */
6
+ hiddenElements = [];
7
+ constructor(root) {
8
+ this.root = root;
9
+ }
10
+ /**
11
+ * Apply aria-hidden to all elements except the passed
12
+ */
13
+ hideSiblingsFromScreenReader = (el) => {
14
+ let sibling = el.parentNode && el.parentNode.firstChild;
15
+ while (sibling) {
16
+ const notCurrent = sibling !== el;
17
+ const isValid = sibling.nodeType === 1 && !sibling.hasAttribute("aria-hidden");
18
+ if (notCurrent && isValid) {
19
+ sibling.setAttribute("aria-hidden", "true");
20
+ this.hiddenElements.push(sibling);
21
+ }
22
+ sibling = sibling.nextSibling;
23
+ }
24
+ };
25
+ release = () => {
26
+ this.hiddenElements.forEach((el) => {
27
+ el.removeAttribute("aria-hidden");
28
+ });
29
+ this.hiddenElements = [];
30
+ };
31
+ trap = () => {
32
+ let currentEl = this.root;
33
+ this.release();
34
+ // Stop at the body level for regular pages
35
+ // And stop at shadow root
36
+ while (currentEl !== document.body && currentEl.parentElement) {
37
+ this.hideSiblingsFromScreenReader(currentEl);
38
+ currentEl = currentEl.parentElement;
39
+ }
40
+ };
41
+ }
42
+ export default TrapScreenReader;
@@ -0,0 +1,38 @@
1
+ import type { FocusableElement, FocusableOptions } from "./types";
2
+ export declare const focusableSelector = "a,button,input:not([type=\"hidden\"]),textarea,select,details,[tabindex],[contenteditable]";
3
+ export declare const getActiveElement: (originEl?: HTMLElement | null) => HTMLButtonElement;
4
+ export declare const focusElement: (el: FocusableElement, options?: {
5
+ pseudoFocus?: boolean;
6
+ }) => void;
7
+ export declare const getFocusableElements: (rootEl: HTMLElement, options?: FocusableOptions) => FocusableElement[];
8
+ export declare const getFocusData: (args: {
9
+ root: HTMLElement;
10
+ target: "next" | "prev" | "first" | "last";
11
+ options?: FocusableOptions & {
12
+ circular?: boolean;
13
+ };
14
+ }) => {
15
+ overflow: boolean;
16
+ el: FocusableElement;
17
+ focusableElements: FocusableElement[];
18
+ };
19
+ export declare const focusNextElement: (root: HTMLElement, options?: {
20
+ circular?: boolean;
21
+ }) => {
22
+ el: FocusableElement;
23
+ focusableElements: FocusableElement[];
24
+ };
25
+ export declare const focusPreviousElement: (root: HTMLElement, options?: {
26
+ circular?: boolean;
27
+ }) => {
28
+ el: FocusableElement;
29
+ focusableElements: FocusableElement[];
30
+ };
31
+ export declare const focusFirstElement: (root: HTMLElement) => {
32
+ el: FocusableElement;
33
+ focusableElements: FocusableElement[];
34
+ };
35
+ export declare const focusLastElement: (root: HTMLElement) => {
36
+ el: FocusableElement;
37
+ focusableElements: FocusableElement[];
38
+ };
@@ -0,0 +1,101 @@
1
+ import { getShadowRoot } from "../dom/index.js";
2
+ const pseudoFocusAttribute = "data-rs-focus";
3
+ export const focusableSelector = 'a,button,input:not([type="hidden"]),textarea,select,details,[tabindex],[contenteditable]';
4
+ export const getActiveElement = (originEl) => {
5
+ const shadowRoot = originEl ? getShadowRoot(originEl) : null;
6
+ const rootEl = shadowRoot ?? document;
7
+ const pseudoFocusedEl = rootEl.querySelector(`[${pseudoFocusAttribute}]`);
8
+ return (pseudoFocusedEl || rootEl.activeElement);
9
+ };
10
+ export const focusElement = (el, options) => {
11
+ const shadowRoot = getShadowRoot(el);
12
+ const rootEl = shadowRoot ?? document;
13
+ rootEl.querySelector(`[${pseudoFocusAttribute}]`)?.removeAttribute(pseudoFocusAttribute);
14
+ if (options?.pseudoFocus) {
15
+ el.setAttribute(pseudoFocusAttribute, "true");
16
+ }
17
+ else {
18
+ el.focus();
19
+ }
20
+ };
21
+ export const getFocusableElements = (rootEl, options) => {
22
+ const focusableElements = Array.from(rootEl.querySelectorAll(focusableSelector));
23
+ const filteredElements = focusableElements.filter((el) => {
24
+ if (el.hasAttribute("disabled"))
25
+ return false;
26
+ if (el.clientHeight === 0)
27
+ return false;
28
+ // Using getAttribute here since browser sets el.tabIndex to -1 by default
29
+ if (!options?.includeNegativeTabIndex && el.getAttribute("tabindex") === "-1")
30
+ return false;
31
+ if (el.type === "radio") {
32
+ let sameNameRadioEls;
33
+ if (el.form) {
34
+ const formInputs = el.form.elements.namedItem(el.name);
35
+ // Synthetic error handling for narrowing down types
36
+ // Radio element can't find itself in the form, so we don't need to include it in the array
37
+ if (!formInputs)
38
+ return false;
39
+ const multipleElementsFound = "length" in formInputs;
40
+ if (!multipleElementsFound) {
41
+ // Single element found is always an input radio since we're inside the condition
42
+ sameNameRadioEls = [formInputs];
43
+ }
44
+ else {
45
+ sameNameRadioEls = Array.from(formInputs).filter((el) => "type" in el && el.type === "radio");
46
+ }
47
+ }
48
+ else {
49
+ sameNameRadioEls = Array.from(rootEl.querySelectorAll(`[type="radio"][name="${el.name}"]`));
50
+ }
51
+ if (sameNameRadioEls?.length) {
52
+ const checkedEl = Array.from(sameNameRadioEls).find((el) => el.checked);
53
+ if (checkedEl && el !== checkedEl)
54
+ return false;
55
+ if (!checkedEl && el !== sameNameRadioEls[0])
56
+ return false;
57
+ }
58
+ }
59
+ return true;
60
+ });
61
+ if (options?.additionalElement && filteredElements.length) {
62
+ filteredElements.unshift(options.additionalElement);
63
+ }
64
+ return filteredElements;
65
+ };
66
+ export const getFocusData = (args) => {
67
+ const { root, target, options } = args;
68
+ const focusable = getFocusableElements(root, {
69
+ additionalElement: options?.additionalElement,
70
+ includeNegativeTabIndex: options?.includeNegativeTabIndex,
71
+ });
72
+ const focusableLimit = focusable.length - 1;
73
+ const currentElement = getActiveElement(root);
74
+ const currentIndex = focusable.indexOf(currentElement);
75
+ const positions = {
76
+ next: currentIndex + 1,
77
+ prev: currentIndex - 1,
78
+ first: 0,
79
+ last: focusableLimit,
80
+ };
81
+ let nextIndex = positions[target];
82
+ const isOverflow = nextIndex > focusableLimit || nextIndex < 0;
83
+ if (isOverflow) {
84
+ if (options?.circular) {
85
+ nextIndex = target === "prev" ? positions.last : positions.first;
86
+ }
87
+ else {
88
+ nextIndex = target === "prev" ? positions.first : positions.last;
89
+ }
90
+ }
91
+ return { overflow: isOverflow, el: focusable[nextIndex], focusableElements: focusable };
92
+ };
93
+ const focusTargetElement = (root, target, options) => {
94
+ const data = getFocusData({ root, target, options });
95
+ focusElement(data.el);
96
+ return { el: data.el, focusableElements: data.focusableElements };
97
+ };
98
+ export const focusNextElement = (root, options) => focusTargetElement(root, "next", { ...options, includeNegativeTabIndex: true });
99
+ export const focusPreviousElement = (root, options) => focusTargetElement(root, "prev", { ...options, includeNegativeTabIndex: true });
100
+ export const focusFirstElement = (root) => focusTargetElement(root, "first", { includeNegativeTabIndex: true });
101
+ export const focusLastElement = (root) => focusTargetElement(root, "last", { includeNegativeTabIndex: true });
@@ -0,0 +1,4 @@
1
+ export { focusableSelector, getActiveElement, getFocusableElements, focusNextElement, focusPreviousElement, focusFirstElement, focusLastElement, } from "./focus";
2
+ export { activateKeyboardMode, deactivateKeyboardMode, checkKeyboardMode } from "./keyboardMode";
3
+ export { default as TrapFocus } from "./TrapFocus";
4
+ export type { TrapMode, FocusableElement } from "./types";
@@ -0,0 +1,3 @@
1
+ export { focusableSelector, getActiveElement, getFocusableElements, focusNextElement, focusPreviousElement, focusFirstElement, focusLastElement, } from "./focus.js";
2
+ export { activateKeyboardMode, deactivateKeyboardMode, checkKeyboardMode } from "./keyboardMode.js";
3
+ export { default as TrapFocus } from "./TrapFocus.js";
@@ -0,0 +1,4 @@
1
+ export declare const keyboardModeAttribute = "data-rs-keyboard";
2
+ export declare const activateKeyboardMode: () => void;
3
+ export declare const deactivateKeyboardMode: () => void;
4
+ export declare const checkKeyboardMode: () => boolean;
@@ -0,0 +1,10 @@
1
+ export const keyboardModeAttribute = "data-rs-keyboard";
2
+ export const activateKeyboardMode = () => {
3
+ document.documentElement.setAttribute(keyboardModeAttribute, "true");
4
+ };
5
+ export const deactivateKeyboardMode = () => {
6
+ document.documentElement.removeAttribute(keyboardModeAttribute);
7
+ };
8
+ export const checkKeyboardMode = () => {
9
+ return document.documentElement.hasAttribute(keyboardModeAttribute);
10
+ };
@@ -0,0 +1,88 @@
1
+ import { expect, test, describe, beforeEach } from "vitest";
2
+ import Chain from "../Chain.js";
3
+ describe("a11y/Chain", () => {
4
+ let chain;
5
+ beforeEach(() => {
6
+ chain = new Chain();
7
+ });
8
+ test("initializes as empty", () => {
9
+ expect(chain.isEmpty()).toBe(true);
10
+ expect(chain.getAll()).toEqual({});
11
+ expect(chain.tailId).toBe(null);
12
+ });
13
+ test("adds items and maintains chain order", () => {
14
+ const id1 = chain.add("first");
15
+ const id2 = chain.add("second");
16
+ const id3 = chain.add("third");
17
+ expect(chain.isEmpty()).toBe(false);
18
+ expect(chain.get(id1)).toEqual({ previousId: null, data: "first", nextId: id2 });
19
+ expect(chain.get(id2)).toEqual({ previousId: id1, data: "second", nextId: id3 });
20
+ expect(chain.get(id3)).toEqual({ previousId: id2, data: "third" });
21
+ expect(chain.tailId).toBe(id3);
22
+ });
23
+ test("identifies last item correctly", () => {
24
+ const id1 = chain.add("first");
25
+ const id2 = chain.add("second");
26
+ expect(chain.isLast(id1)).toBe(false);
27
+ expect(chain.isLast(id2)).toBe(true);
28
+ });
29
+ test("removes middle item and reconnects chain", () => {
30
+ const id1 = chain.add("first");
31
+ const id2 = chain.add("second");
32
+ const id3 = chain.add("third");
33
+ const removed = chain.remove(id2);
34
+ expect(removed).toBe("second");
35
+ expect(chain.get(id1).nextId).toBe(id3);
36
+ expect(chain.get(id3).previousId).toBe(id1);
37
+ expect(chain.get(id2)).toBeUndefined();
38
+ });
39
+ test("removes last item and updates tail", () => {
40
+ const id1 = chain.add("first");
41
+ const id2 = chain.add("second");
42
+ chain.remove(id2);
43
+ expect(chain.tailId).toBe(id1);
44
+ expect(chain.get(id1).nextId).toBe(null);
45
+ });
46
+ test("removes first item", () => {
47
+ const id1 = chain.add("first");
48
+ const id2 = chain.add("second");
49
+ chain.remove(id1);
50
+ expect(chain.get(id2).previousId).toBe(null);
51
+ expect(chain.get(id1)).toBeUndefined();
52
+ });
53
+ test("removes all items leaving chain empty", () => {
54
+ const id1 = chain.add("only");
55
+ chain.remove(id1);
56
+ expect(chain.isEmpty()).toBe(true);
57
+ expect(chain.tailId).toBe(null);
58
+ });
59
+ test("handles removing non-existent item", () => {
60
+ chain.add("first");
61
+ const result = chain.remove(999);
62
+ expect(result).toBeUndefined();
63
+ });
64
+ test("removes previous items until condition is met", () => {
65
+ const id1 = chain.add("first");
66
+ const id2 = chain.add("second");
67
+ const id3 = chain.add("third");
68
+ const id4 = chain.add("fourth");
69
+ const result = chain.removePreviousTill(id4, (item) => item.data === "second");
70
+ // Last deleted id
71
+ expect(result).toBe("second");
72
+ // Keeps only the first item
73
+ expect(chain.get(id1)).toEqual({ previousId: null, nextId: null, data: "first" });
74
+ expect(chain.get(id2)).toBeUndefined();
75
+ expect(chain.get(id3)).toBeUndefined();
76
+ expect(chain.get(id4)).toBeUndefined();
77
+ expect(chain.tailId).toBe(id1);
78
+ });
79
+ test("removePreviousTill stops at first item when condition never met", () => {
80
+ const id1 = chain.add("first");
81
+ const id2 = chain.add("second");
82
+ const result = chain.removePreviousTill(id2, () => false);
83
+ expect(result).toBe("first");
84
+ expect(chain.get(id1)).toBeUndefined();
85
+ expect(chain.get(id2)).toBeUndefined();
86
+ expect(chain.isEmpty()).toBe(true);
87
+ });
88
+ });
@@ -0,0 +1 @@
1
+ export {};