* Restore focus to previously focused element
()
| 189 | * Restore focus to previously focused element |
| 190 | */ |
| 191 | function restoreFocusState(): void { |
| 192 | if (!preserveFocus || !containerRef.value) return; |
| 193 | |
| 194 | const state = getPreservedState(surfaceId); |
| 195 | |
| 196 | if (state.focusedElementId) { |
| 197 | const el = containerRef.value.querySelector(`#${state.focusedElementId}`) |
| 198 | || containerRef.value.querySelector(`[data-component-id="${state.focusedElementId}"]`) |
| 199 | || containerRef.value.querySelector(`[name="${state.focusedElementId}"]`); |
| 200 | |
| 201 | if (el && el instanceof HTMLElement) { |
| 202 | // Use requestAnimationFrame to ensure DOM is ready |
| 203 | requestAnimationFrame(() => { |
| 204 | el.focus(); |
| 205 | |
| 206 | // Restore cursor position for input elements |
| 207 | if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) { |
| 208 | const pendingValue = state.pendingInputValues.get(state.focusedElementId!); |
| 209 | if (pendingValue !== undefined) { |
| 210 | // Set selection to end of input |
| 211 | const len = el.value.length; |
| 212 | el.setSelectionRange(len, len); |
| 213 | } |
| 214 | } |
| 215 | }); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Save all state before an update |
no test coverage detected