Home / Function/ saveFocus() — astro Function Reference

saveFocus() — astro Function Reference

Architecture documentation for the saveFocus() function in swap-functions.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  71513b03_bc12_8c61_def0_12ef28b2be6a["saveFocus()"]
  12084e8e_15f3_27ac_8f8b_d442375ff33d["swap-functions.ts"]
  71513b03_bc12_8c61_def0_12ef28b2be6a -->|defined in| 12084e8e_15f3_27ac_8f8b_d442375ff33d
  af9df8a2_d80a_7f14_9281_908d8b85f14f["swap()"]
  af9df8a2_d80a_7f14_9281_908d8b85f14f -->|calls| 71513b03_bc12_8c61_def0_12ef28b2be6a
  437a59db_c7c3_d406_781d_8c6ceae5787f["restoreFocus()"]
  71513b03_bc12_8c61_def0_12ef28b2be6a -->|calls| 437a59db_c7c3_d406_781d_8c6ceae5787f
  style 71513b03_bc12_8c61_def0_12ef28b2be6a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/transitions/swap-functions.ts lines 124–139

export const saveFocus = (): (() => void) => {
	const activeElement = document.activeElement as HTMLElement;
	// The element that currently has the focus is part of a DOM tree
	// that will survive the transition to the new document.
	// Save the element and the cursor position
	if (activeElement?.closest(`[${PERSIST_ATTR}]`)) {
		if (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement) {
			const start = activeElement.selectionStart;
			const end = activeElement.selectionEnd;
			return () => restoreFocus({ activeElement, start, end });
		}
		return () => restoreFocus({ activeElement });
	} else {
		return () => restoreFocus({ activeElement: null });
	}
};

Domain

Subdomains

Called By

Frequently Asked Questions

What does saveFocus() do?
saveFocus() is a function in the astro codebase, defined in packages/astro/src/transitions/swap-functions.ts.
Where is saveFocus() defined?
saveFocus() is defined in packages/astro/src/transitions/swap-functions.ts at line 124.
What does saveFocus() call?
saveFocus() calls 1 function(s): restoreFocus.
What calls saveFocus()?
saveFocus() is called by 1 function(s): swap.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free