Skip to content

Commit a8fd11c

Browse files
zombieJclaude
andcommitted
refactor: simplify map structure - use id mapping to element
Change from elementToIdMap to idToElementMap for clearer logic: stable ID as key maps to element. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent ba194a5 commit a8fd11c

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/Dom/focus.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ export function triggerFocus(
103103
// ======================================================
104104
let lastFocusElement: HTMLElement | null = null;
105105
let focusElements: HTMLElement[] = [];
106-
// Map lock element to its stable ID
107-
const elementToIdMap = new Map<HTMLElement, string>();
106+
// Map stable ID to lock element
107+
const idToElementMap = new Map<string, HTMLElement>();
108108
// Map stable ID to ignored element
109109
const ignoredElementMap = new Map<string, HTMLElement | null>();
110110

@@ -116,8 +116,18 @@ function isIgnoredElement(element: Element | null): boolean {
116116
if (!element) return false;
117117
const lastElement = getLastElement();
118118
if (!lastElement) return false;
119-
const lockId = elementToIdMap.get(lastElement);
119+
120+
// Find the ID that maps to the last element
121+
let lockId: string | undefined;
122+
for (const [id, ele] of idToElementMap.entries()) {
123+
if (ele === lastElement) {
124+
lockId = id;
125+
break;
126+
}
127+
}
128+
120129
if (!lockId) return false;
130+
121131
const ignoredEle = ignoredElementMap.get(lockId);
122132
return (
123133
!!ignoredEle && (ignoredEle === element || ignoredEle.contains(element))
@@ -175,8 +185,8 @@ function onWindowKeyDown(e: KeyboardEvent) {
175185
*/
176186
export function lockFocus(element: HTMLElement, id: string): VoidFunction {
177187
if (element) {
178-
// Store the mapping between element and its stable ID
179-
elementToIdMap.set(element, id);
188+
// Store the mapping between ID and element
189+
idToElementMap.set(id, element);
180190

181191
// Refresh focus elements
182192
focusElements = focusElements.filter(ele => ele !== element);
@@ -192,7 +202,7 @@ export function lockFocus(element: HTMLElement, id: string): VoidFunction {
192202
return () => {
193203
lastFocusElement = null;
194204
focusElements = focusElements.filter(ele => ele !== element);
195-
elementToIdMap.delete(element);
205+
idToElementMap.delete(id);
196206
ignoredElementMap.delete(id);
197207
if (focusElements.length === 0) {
198208
window.removeEventListener('focusin', syncFocus);
@@ -223,12 +233,9 @@ export function useLockFocus(
223233
}, [lock, id]);
224234

225235
const ignoreElement = (ele: HTMLElement) => {
226-
const element = getElement();
227-
if (element && ele) {
228-
// Set the ignored element for current lock using stable ID
229-
// Only one element can be ignored at a time for this lock
230-
const lockId = elementToIdMap.get(element) || id;
231-
ignoredElementMap.set(lockId, ele);
236+
if (ele) {
237+
// Set the ignored element using stable ID
238+
ignoredElementMap.set(id, ele);
232239
}
233240
};
234241

0 commit comments

Comments
 (0)