summaryrefslogtreecommitdiff
path: root/node_modules/swup/src/modules/scrollToContent.ts
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2024-08-30 17:21:18 +0800
committerlittleblack111 <littleblack11111@gmail.com>2024-08-30 17:21:18 +0800
commitefbc2ffe6a1ee5ec38a4396305561d6515d0943e (patch)
tree2b4c94f18f2c792dfda813050755d5c0273fae22 /node_modules/swup/src/modules/scrollToContent.ts
parent43acceb75957c880a5ba5a31aae221394a36d5f6 (diff)
node fixes, updates
Diffstat (limited to 'node_modules/swup/src/modules/scrollToContent.ts')
-rwxr-xr-xnode_modules/swup/src/modules/scrollToContent.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/node_modules/swup/src/modules/scrollToContent.ts b/node_modules/swup/src/modules/scrollToContent.ts
new file mode 100755
index 00000000..0c081a28
--- /dev/null
+++ b/node_modules/swup/src/modules/scrollToContent.ts
@@ -0,0 +1,38 @@
+import type Swup from '../Swup.js';
+import type { Visit } from './Visit.js';
+
+/**
+ * Update the scroll position after page render.
+ * @returns Promise<boolean>
+ */
+export const scrollToContent = function (this: Swup, visit: Visit): boolean {
+ const options: ScrollIntoViewOptions = { behavior: 'auto' };
+ const { target, reset } = visit.scroll;
+ const scrollTarget = target ?? visit.to.hash;
+
+ let scrolled = false;
+
+ if (scrollTarget) {
+ scrolled = this.hooks.callSync(
+ 'scroll:anchor',
+ visit,
+ { hash: scrollTarget, options },
+ (visit, { hash, options }) => {
+ const anchor = this.getAnchorElement(hash);
+ if (anchor) {
+ anchor.scrollIntoView(options);
+ }
+ return !!anchor;
+ }
+ );
+ }
+
+ if (reset && !scrolled) {
+ scrolled = this.hooks.callSync('scroll:top', visit, { options }, (visit, { options }) => {
+ window.scrollTo({ top: 0, left: 0, ...options });
+ return true;
+ });
+ }
+
+ return scrolled;
+};