* Called by the reconciler when a node is removed from the tree. * Handles both the exact node and any focused descendant within * the removed subtree. Dispatches blur and restores focus from stack.
(node: DOMElement, root: DOMElement)
| 55 | * the removed subtree. Dispatches blur and restores focus from stack. |
| 56 | */ |
| 57 | handleNodeRemoved(node: DOMElement, root: DOMElement): void { |
| 58 | // Remove the node and any descendants from the stack |
| 59 | this.focusStack = this.focusStack.filter( |
| 60 | n => n !== node && isInTree(n, root), |
| 61 | ) |
| 62 | |
| 63 | // Check if activeElement is the removed node OR a descendant |
| 64 | if (!this.activeElement) return |
| 65 | if (this.activeElement !== node && isInTree(this.activeElement, root)) { |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | const removed = this.activeElement |
| 70 | this.activeElement = null |
| 71 | this.dispatchFocusEvent(removed, new FocusEvent('blur', null)) |
| 72 | |
| 73 | // Restore focus to the most recent still-mounted element |
| 74 | while (this.focusStack.length > 0) { |
| 75 | const candidate = this.focusStack.pop()! |
| 76 | if (isInTree(candidate, root)) { |
| 77 | this.activeElement = candidate |
| 78 | this.dispatchFocusEvent(candidate, new FocusEvent('focus', removed)) |
| 79 | return |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | handleAutoFocus(node: DOMElement): void { |
| 85 | this.focus(node) |
no test coverage detected