UNPKG

svelte-scrolling

Version:

Scroll to given elements with smooth animation

25 lines (24 loc) 633 B
import { get } from 'svelte/store'; import { sanitize } from '../shared/utils'; import { elements } from '../store'; /** * Adds a reference to the elements that `scrollTo` should scroll * * @param reference - The reference element */ const scrollRef = (node, reference) => { if (!reference) { throw new Error('scrollRef require a reference'); } const elementsList = get(elements); elementsList.push({ node, reference: sanitize(reference) }); return { destroy() { elementsList.length = 0; // empty the elements list } }; }; export default scrollRef;