* Save currently focused element
()
| 161 | * Save currently focused element |
| 162 | */ |
| 163 | function saveFocusState(): void { |
| 164 | if (!preserveFocus || !containerRef.value) return; |
| 165 | |
| 166 | const state = getPreservedState(surfaceId); |
| 167 | const activeElement = document.activeElement; |
| 168 | |
| 169 | if (activeElement && containerRef.value.contains(activeElement)) { |
| 170 | // Try to get a unique identifier for the element |
| 171 | const id = activeElement.id |
| 172 | || activeElement.getAttribute('data-component-id') |
| 173 | || activeElement.getAttribute('name'); |
| 174 | |
| 175 | if (id) { |
| 176 | state.focusedElementId = id; |
| 177 | |
| 178 | // Save pending input value if it's an input element |
| 179 | if (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement) { |
| 180 | state.pendingInputValues.set(id, activeElement.value); |
| 181 | } |
| 182 | } |
| 183 | } else { |
| 184 | state.focusedElementId = null; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Restore focus to previously focused element |
no test coverage detected