Skip to content

Commit 589da1b

Browse files
committed
fix(focus): Fix removing elements not applying across focus layers
1 parent dcaa985 commit 589da1b

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

.changeset/four-eggs-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@plextv/react-lightning": patch
3+
---
4+
5+
fix(focus): Fix removing elements not applying across focus layers

packages/react-lightning/src/focus/FocusManager.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -237,33 +237,41 @@ export class FocusManager<
237237
this._recalculateFocusPath();
238238
}
239239

240-
public removeElement(element: T): void {
241-
const node = this.activeLayer.elements.get(element);
240+
private _forAllNodes(
241+
element: T,
242+
callback: (node: FocusNode<T>) => void,
243+
): void {
244+
for (let i = this._focusStack.length - 1; i >= 0; i--) {
245+
const layer = this._focusStack[i];
246+
// biome-ignore lint/style/noNonNullAssertion: Already asserted layer exists
247+
const node = layer!.elements.get(element);
242248

243-
if (!node) {
244-
return;
249+
if (node) {
250+
callback(node);
251+
}
245252
}
253+
}
246254

247-
this._removeNode(node, true);
255+
public removeElement(element: T): void {
256+
this._forAllNodes(element, (node) => {
257+
this._removeNode(node, true);
258+
});
248259
}
249260

250261
public setTraps(element: T, traps: Traps): void {
251-
const node = this.activeLayer.elements.get(element);
252-
253-
if (node) {
262+
this._forAllNodes(element, (node) => {
254263
node.traps = traps;
255-
}
264+
});
256265
}
257266

258267
public setAutoFocus(element: T, autoFocus?: boolean): void {
259-
const node = this.activeLayer.elements.get(element);
260-
261-
if (node) {
268+
this._forAllNodes(element, (node) => {
262269
node.autoFocus = !!autoFocus;
263-
}
270+
});
264271
}
265272

266273
public setFocusRedirect(element: T, focusRedirect?: boolean): void {
274+
// Only apply redirect to the active layer, as redirects are likely to be layer-specific
267275
const node = this.activeLayer.elements.get(element);
268276

269277
if (node) {
@@ -272,11 +280,9 @@ export class FocusManager<
272280
}
273281

274282
public setDestinations(element: T, destinations?: (T | null)[]): void {
275-
const node = this.activeLayer.elements.get(element);
276-
277-
if (node) {
283+
this._forAllNodes(element, (node) => {
278284
node.destinations = destinations ?? null;
279-
}
285+
});
280286
}
281287

282288
public pushLayer(): void {

0 commit comments

Comments
 (0)