fix: Sometimes losing focus when closing modals

This commit is contained in:
Aleksi Lassila
2024-06-08 01:36:37 +03:00
parent 05719d8c04
commit 88f1be68d5

View File

@@ -609,7 +609,7 @@ export class Selectable {
destroy: () => { destroy: () => {
let elementToFocus: Selectable | undefined = undefined; let elementToFocus: Selectable | undefined = undefined;
for (const child of Selectable._childrenToRemove) { for (const child of Selectable._childrenToRemove) {
console.warn('Removing child', child, 'from parent', child.parent); // console.warn('Removing child', child, 'from parent', child.parent);
const parent = child.parent; const parent = child.parent;
if (parent) { if (parent) {
@@ -620,7 +620,7 @@ export class Selectable {
const index = parent.children.indexOf(childToFocus); const index = parent.children.indexOf(childToFocus);
if (index !== -1) { if (index !== -1) {
parent.focusIndex.update((prev) => index); parent.focusIndex.set(index);
} else { } else {
console.error( console.error(
"Couldn't find index of child to focus", "Couldn't find index of child to focus",
@@ -632,6 +632,15 @@ export class Selectable {
elementToFocus = parent; elementToFocus = parent;
console.warn('Focusing parent after last child unmount', parent); console.warn('Focusing parent after last child unmount', parent);
} }
} else {
const topRootIndex = Selectable.rootObjectsStack.indexOf(child);
if (topRootIndex === Selectable.rootObjectsStack.length - 1) {
Selectable.rootObjectsStack.pop();
Selectable.rootObjectsStack[Selectable.rootObjectsStack.length - 1]?.focus();
} else if (topRootIndex !== -1) {
Selectable.rootObjectsStack.splice(topRootIndex, 1);
}
} }
if (child.htmlElement) { if (child.htmlElement) {
@@ -639,18 +648,6 @@ export class Selectable {
} }
} }
let topRoot = Selectable.rootObjectsStack[this.rootObjectsStack.length - 1];
while (
topRoot &&
Selectable._childrenToRemove.indexOf(topRoot) !== -1 &&
Selectable.rootObjectsStack.length > 0
) {
console.warn('REMOVED TOP ROOT');
Selectable.rootObjectsStack.pop();
topRoot = Selectable.rootObjectsStack[this.rootObjectsStack.length - 1];
topRoot?.focus();
}
Selectable._childrenToRemove = []; Selectable._childrenToRemove = [];
if (elementToFocus) { if (elementToFocus) {