Debian Codegraph @

src/modules/scrollToContent.ts

raw
1 import type Swup from '../Swup.js';
2 import type { Visit } from './Visit.js';
3
4 /**
5 * Update the scroll position after page render.
6 * @returns Promise<boolean>
7 */
8 export const scrollToContent = function (this: Swup, visit: Visit): boolean {
9 const options: ScrollIntoViewOptions = { behavior: 'auto' };
10 const { target, reset } = visit.scroll;
11 const scrollTarget = target ?? visit.to.hash;
12
13 let scrolled = false;
14
15 if (scrollTarget) {
16 scrolled = this.hooks.callSync(
17 'scroll:anchor',
18 visit,
19 { hash: scrollTarget, options },
20 (visit, { hash, options }) => {
21 const anchor = this.getAnchorElement(hash);
22 if (anchor) {
23 anchor.scrollIntoView(options);
24 }
25 return !!anchor;
26 }
27 );
28 }
29
30 if (reset && !scrolled) {
31 scrolled = this.hooks.callSync('scroll:top', visit, { options }, (visit, { options }) => {
32 window.scrollTo({ top: 0, left: 0, ...options });
33 return true;
34 });
35 }
36
37 return scrolled;
38 };