Snippet

Svelte Attachment: Scroll to on Init

export function scroll_to_on_init(
	condition: boolean,
	block: ScrollLogicalPosition = 'center'
): Attachment {
	return (element) => {
		if (condition) {
			element.scrollIntoView({ behavior: 'smooth', block, container: 'nearest' });
		}
	};
}

Conditionally scrolls to an element on init or when a condition is met.

Usage

<div {@attach scroll_to_on_init(condition)}>
  content
</div>