@primer/behaviors 0.0.0-20260108040435 → 0.0.0-20260109180928
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/dist/cjs/focus-zone.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export type FocusZoneSettings = IterateFocusableElements & {
|
|
|
28
28
|
focusInStrategy?: 'first' | 'closest' | 'previous' | 'initial' | ((previousFocusedElement: Element) => HTMLElement | undefined);
|
|
29
29
|
preventScroll?: boolean;
|
|
30
30
|
ignoreHoverEvents?: boolean;
|
|
31
|
+
focusPrependedElements?: boolean;
|
|
31
32
|
};
|
|
32
33
|
export declare const isActiveDescendantAttribute = "data-is-active-descendant";
|
|
33
34
|
export declare const activeDescendantActivatedDirectly = "activated-directly";
|
package/dist/cjs/focus-zone.js
CHANGED
|
@@ -131,7 +131,7 @@ const activeDescendantActivatedDirectly = 'activated-directly';
|
|
|
131
131
|
const activeDescendantActivatedIndirectly = 'activated-indirectly';
|
|
132
132
|
const hasActiveDescendantAttribute = 'data-has-active-descendant';
|
|
133
133
|
function focusZone(container, settings) {
|
|
134
|
-
var _a, _b, _c, _d, _e, _f;
|
|
134
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
135
135
|
const focusableElements = new indexedSet.IndexedSet();
|
|
136
136
|
const savedTabIndex = new WeakMap();
|
|
137
137
|
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) ? exports.FocusKeys.ArrowAll : exports.FocusKeys.ArrowVertical) | exports.FocusKeys.HomeAndEnd;
|
|
@@ -140,8 +140,10 @@ function focusZone(container, settings) {
|
|
|
140
140
|
const activeDescendantControl = settings === null || settings === void 0 ? void 0 : settings.activeDescendantControl;
|
|
141
141
|
const activeDescendantCallback = settings === null || settings === void 0 ? void 0 : settings.onActiveDescendantChanged;
|
|
142
142
|
const ignoreHoverEvents = (_d = settings === null || settings === void 0 ? void 0 : settings.ignoreHoverEvents) !== null && _d !== void 0 ? _d : false;
|
|
143
|
+
const focusPrependedElements = (_e = settings === null || settings === void 0 ? void 0 : settings.focusPrependedElements) !== null && _e !== void 0 ? _e : false;
|
|
143
144
|
let currentFocusedElement;
|
|
144
|
-
|
|
145
|
+
let wasDirectlyActivated = false;
|
|
146
|
+
const preventScroll = (_f = settings === null || settings === void 0 ? void 0 : settings.preventScroll) !== null && _f !== void 0 ? _f : false;
|
|
145
147
|
const preventInitialFocus = focusInStrategy === 'initial' && (settings === null || settings === void 0 ? void 0 : settings.activeDescendantControl);
|
|
146
148
|
function getFirstFocusableElement() {
|
|
147
149
|
return focusableElements.get(0);
|
|
@@ -152,6 +154,7 @@ function focusZone(container, settings) {
|
|
|
152
154
|
function updateFocusedElement(to, directlyActivated = false) {
|
|
153
155
|
const from = currentFocusedElement;
|
|
154
156
|
currentFocusedElement = to;
|
|
157
|
+
wasDirectlyActivated = directlyActivated;
|
|
155
158
|
if (activeDescendantControl) {
|
|
156
159
|
if (to && isActiveDescendantInputFocused()) {
|
|
157
160
|
setActiveDescendant(from, to, directlyActivated);
|
|
@@ -202,17 +205,34 @@ function focusZone(container, settings) {
|
|
|
202
205
|
if (filteredElements.length === 0) {
|
|
203
206
|
return;
|
|
204
207
|
}
|
|
205
|
-
|
|
208
|
+
const insertionIndex = findInsertionIndex(filteredElements);
|
|
209
|
+
focusableElements.insertAt(insertionIndex, ...filteredElements);
|
|
206
210
|
for (const element of filteredElements) {
|
|
207
211
|
if (!savedTabIndex.has(element)) {
|
|
208
212
|
savedTabIndex.set(element, element.getAttribute('tabindex'));
|
|
209
213
|
}
|
|
210
214
|
element.setAttribute('tabindex', '-1');
|
|
211
215
|
}
|
|
212
|
-
|
|
216
|
+
const shouldFocusPrepended = focusPrependedElements && insertionIndex === 0 && !wasDirectlyActivated;
|
|
217
|
+
if (!preventInitialFocus && shouldFocusPrepended) {
|
|
218
|
+
reinitializeWithFreshElements();
|
|
219
|
+
}
|
|
220
|
+
else if (!preventInitialFocus && !currentFocusedElement) {
|
|
213
221
|
updateFocusedElement(getFirstFocusableElement());
|
|
214
222
|
}
|
|
215
223
|
}
|
|
224
|
+
function reinitializeWithFreshElements() {
|
|
225
|
+
const freshElements = [...iterateFocusableElements.iterateFocusableElements(container, iterateFocusableElementsOptions)];
|
|
226
|
+
focusableElements.clear();
|
|
227
|
+
focusableElements.insertAt(0, ...freshElements);
|
|
228
|
+
for (const element of freshElements) {
|
|
229
|
+
if (!savedTabIndex.has(element)) {
|
|
230
|
+
savedTabIndex.set(element, element.getAttribute('tabindex'));
|
|
231
|
+
}
|
|
232
|
+
element.setAttribute('tabindex', '-1');
|
|
233
|
+
}
|
|
234
|
+
updateFocusedElement(getFirstFocusableElement());
|
|
235
|
+
}
|
|
216
236
|
function findInsertionIndex(elementsToInsert) {
|
|
217
237
|
const firstElementToInsert = elementsToInsert[0];
|
|
218
238
|
if (focusableElements.size === 0)
|
|
@@ -313,7 +333,12 @@ function focusZone(container, settings) {
|
|
|
313
333
|
}
|
|
314
334
|
}
|
|
315
335
|
if (toAdd.length > 0) {
|
|
316
|
-
|
|
336
|
+
if (focusPrependedElements) {
|
|
337
|
+
reinitializeWithFreshElements();
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
beginFocusManagement(...toAdd);
|
|
341
|
+
}
|
|
317
342
|
}
|
|
318
343
|
}
|
|
319
344
|
if (attributeAdditions.size > 0) {
|
|
@@ -327,7 +352,7 @@ function focusZone(container, settings) {
|
|
|
327
352
|
attributeOldValue: true,
|
|
328
353
|
});
|
|
329
354
|
const controller = new AbortController();
|
|
330
|
-
const signal = (
|
|
355
|
+
const signal = (_g = settings === null || settings === void 0 ? void 0 : settings.abortSignal) !== null && _g !== void 0 ? _g : controller.signal;
|
|
331
356
|
signal.addEventListener('abort', () => {
|
|
332
357
|
observer.disconnect();
|
|
333
358
|
endFocusManagement(...focusableElements);
|
package/dist/esm/focus-zone.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export type FocusZoneSettings = IterateFocusableElements & {
|
|
|
28
28
|
focusInStrategy?: 'first' | 'closest' | 'previous' | 'initial' | ((previousFocusedElement: Element) => HTMLElement | undefined);
|
|
29
29
|
preventScroll?: boolean;
|
|
30
30
|
ignoreHoverEvents?: boolean;
|
|
31
|
+
focusPrependedElements?: boolean;
|
|
31
32
|
};
|
|
32
33
|
export declare const isActiveDescendantAttribute = "data-is-active-descendant";
|
|
33
34
|
export declare const activeDescendantActivatedDirectly = "activated-directly";
|
package/dist/esm/focus-zone.mjs
CHANGED
|
@@ -129,7 +129,7 @@ const activeDescendantActivatedDirectly = 'activated-directly';
|
|
|
129
129
|
const activeDescendantActivatedIndirectly = 'activated-indirectly';
|
|
130
130
|
const hasActiveDescendantAttribute = 'data-has-active-descendant';
|
|
131
131
|
function focusZone(container, settings) {
|
|
132
|
-
var _a, _b, _c, _d, _e, _f;
|
|
132
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
133
133
|
const focusableElements = new IndexedSet();
|
|
134
134
|
const savedTabIndex = new WeakMap();
|
|
135
135
|
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;
|
|
@@ -138,8 +138,10 @@ function focusZone(container, settings) {
|
|
|
138
138
|
const activeDescendantControl = settings === null || settings === void 0 ? void 0 : settings.activeDescendantControl;
|
|
139
139
|
const activeDescendantCallback = settings === null || settings === void 0 ? void 0 : settings.onActiveDescendantChanged;
|
|
140
140
|
const ignoreHoverEvents = (_d = settings === null || settings === void 0 ? void 0 : settings.ignoreHoverEvents) !== null && _d !== void 0 ? _d : false;
|
|
141
|
+
const focusPrependedElements = (_e = settings === null || settings === void 0 ? void 0 : settings.focusPrependedElements) !== null && _e !== void 0 ? _e : false;
|
|
141
142
|
let currentFocusedElement;
|
|
142
|
-
|
|
143
|
+
let wasDirectlyActivated = false;
|
|
144
|
+
const preventScroll = (_f = settings === null || settings === void 0 ? void 0 : settings.preventScroll) !== null && _f !== void 0 ? _f : false;
|
|
143
145
|
const preventInitialFocus = focusInStrategy === 'initial' && (settings === null || settings === void 0 ? void 0 : settings.activeDescendantControl);
|
|
144
146
|
function getFirstFocusableElement() {
|
|
145
147
|
return focusableElements.get(0);
|
|
@@ -150,6 +152,7 @@ function focusZone(container, settings) {
|
|
|
150
152
|
function updateFocusedElement(to, directlyActivated = false) {
|
|
151
153
|
const from = currentFocusedElement;
|
|
152
154
|
currentFocusedElement = to;
|
|
155
|
+
wasDirectlyActivated = directlyActivated;
|
|
153
156
|
if (activeDescendantControl) {
|
|
154
157
|
if (to && isActiveDescendantInputFocused()) {
|
|
155
158
|
setActiveDescendant(from, to, directlyActivated);
|
|
@@ -200,17 +203,34 @@ function focusZone(container, settings) {
|
|
|
200
203
|
if (filteredElements.length === 0) {
|
|
201
204
|
return;
|
|
202
205
|
}
|
|
203
|
-
|
|
206
|
+
const insertionIndex = findInsertionIndex(filteredElements);
|
|
207
|
+
focusableElements.insertAt(insertionIndex, ...filteredElements);
|
|
204
208
|
for (const element of filteredElements) {
|
|
205
209
|
if (!savedTabIndex.has(element)) {
|
|
206
210
|
savedTabIndex.set(element, element.getAttribute('tabindex'));
|
|
207
211
|
}
|
|
208
212
|
element.setAttribute('tabindex', '-1');
|
|
209
213
|
}
|
|
210
|
-
|
|
214
|
+
const shouldFocusPrepended = focusPrependedElements && insertionIndex === 0 && !wasDirectlyActivated;
|
|
215
|
+
if (!preventInitialFocus && shouldFocusPrepended) {
|
|
216
|
+
reinitializeWithFreshElements();
|
|
217
|
+
}
|
|
218
|
+
else if (!preventInitialFocus && !currentFocusedElement) {
|
|
211
219
|
updateFocusedElement(getFirstFocusableElement());
|
|
212
220
|
}
|
|
213
221
|
}
|
|
222
|
+
function reinitializeWithFreshElements() {
|
|
223
|
+
const freshElements = [...iterateFocusableElements(container, iterateFocusableElementsOptions)];
|
|
224
|
+
focusableElements.clear();
|
|
225
|
+
focusableElements.insertAt(0, ...freshElements);
|
|
226
|
+
for (const element of freshElements) {
|
|
227
|
+
if (!savedTabIndex.has(element)) {
|
|
228
|
+
savedTabIndex.set(element, element.getAttribute('tabindex'));
|
|
229
|
+
}
|
|
230
|
+
element.setAttribute('tabindex', '-1');
|
|
231
|
+
}
|
|
232
|
+
updateFocusedElement(getFirstFocusableElement());
|
|
233
|
+
}
|
|
214
234
|
function findInsertionIndex(elementsToInsert) {
|
|
215
235
|
const firstElementToInsert = elementsToInsert[0];
|
|
216
236
|
if (focusableElements.size === 0)
|
|
@@ -311,7 +331,12 @@ function focusZone(container, settings) {
|
|
|
311
331
|
}
|
|
312
332
|
}
|
|
313
333
|
if (toAdd.length > 0) {
|
|
314
|
-
|
|
334
|
+
if (focusPrependedElements) {
|
|
335
|
+
reinitializeWithFreshElements();
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
beginFocusManagement(...toAdd);
|
|
339
|
+
}
|
|
315
340
|
}
|
|
316
341
|
}
|
|
317
342
|
if (attributeAdditions.size > 0) {
|
|
@@ -325,7 +350,7 @@ function focusZone(container, settings) {
|
|
|
325
350
|
attributeOldValue: true,
|
|
326
351
|
});
|
|
327
352
|
const controller = new AbortController();
|
|
328
|
-
const signal = (
|
|
353
|
+
const signal = (_g = settings === null || settings === void 0 ? void 0 : settings.abortSignal) !== null && _g !== void 0 ? _g : controller.signal;
|
|
329
354
|
signal.addEventListener('abort', () => {
|
|
330
355
|
observer.disconnect();
|
|
331
356
|
endFocusManagement(...focusableElements);
|