@sharadox/scroll-web-library 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Scrolling for React websites:
|
|
4
4
|
|
|
5
|
-
- **Smooth scrolling:** inertial scrolling with Lenis, including in-page anchor links that stop above a fixed header.
|
|
5
|
+
- **Smooth scrolling:** inertial scrolling with Lenis, including in-page anchor links that stop above a fixed header. A link to the page already open (the logo on the home page) glides back to the top instead of reloading. Smoothing turns off for visitors who prefer reduced motion; same-page links still skip the reload.
|
|
6
6
|
- **Active section:** which section holds the middle of the screen, how far through it you are, and how far down the page. That's what section indicators and progress bars need.
|
|
7
7
|
|
|
8
8
|
## Install
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import type { SmoothScrollProperties } from "@sharadox/scroll-web-library/smooth-scroll/type";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Smooth, inertial page scrolling, including in-page anchor links. A link to the page already open (the logo on the
|
|
4
|
+
* home page, for one) glides back to the top instead of reloading. Smoothing is off when the visitor prefers reduced
|
|
5
|
+
* motion: anchors then jump, and the same-page link still goes to the top without reloading.
|
|
6
|
+
*/
|
|
3
7
|
export declare function SmoothScroll({ duration, anchorOffset }: SmoothScrollProperties): null;
|
|
4
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/smooth-scroll/index.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,iDAAiD,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/smooth-scroll/index.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,iDAAiD,CAAC;AAK9F;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,EAAE,QAAe,EAAE,YAAkB,EAAE,EAAE,sBAAsB,QA4C3F"}
|
|
@@ -1,31 +1,58 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import Lenis from "lenis";
|
|
3
3
|
import { useEffect } from "react";
|
|
4
|
-
/**
|
|
4
|
+
/** A path without its trailing slash, so "/en/" and "/en" count as the same page. */
|
|
5
|
+
const page = (path) => path.replace(/(.)\/$/, "$1");
|
|
6
|
+
/**
|
|
7
|
+
* Smooth, inertial page scrolling, including in-page anchor links. A link to the page already open (the logo on the
|
|
8
|
+
* home page, for one) glides back to the top instead of reloading. Smoothing is off when the visitor prefers reduced
|
|
9
|
+
* motion: anchors then jump, and the same-page link still goes to the top without reloading.
|
|
10
|
+
*/
|
|
5
11
|
export function SmoothScroll({ duration = 1.15, anchorOffset = -60 }) {
|
|
6
12
|
useEffect(() => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
13
|
+
const lenis = window.matchMedia("(prefers-reduced-motion: reduce)").matches
|
|
14
|
+
? null
|
|
15
|
+
: new Lenis({ duration, smoothWheel: true });
|
|
10
16
|
let frame = 0;
|
|
11
17
|
const animate = (time) => {
|
|
12
18
|
lenis.raf(time);
|
|
13
19
|
frame = requestAnimationFrame(animate);
|
|
14
20
|
};
|
|
15
|
-
|
|
21
|
+
if (lenis)
|
|
22
|
+
frame = requestAnimationFrame(animate);
|
|
16
23
|
const onClick = (event) => {
|
|
17
|
-
const link = event.target.closest(
|
|
18
|
-
|
|
19
|
-
|
|
24
|
+
const link = event.target.closest("a[href]");
|
|
25
|
+
if (!link)
|
|
26
|
+
return;
|
|
27
|
+
const anchor = link.getAttribute("href").startsWith("#")
|
|
28
|
+
? document.querySelector(link.getAttribute("href"))
|
|
29
|
+
: null;
|
|
30
|
+
if (anchor) {
|
|
31
|
+
if (!lenis)
|
|
32
|
+
return;
|
|
33
|
+
event.preventDefault();
|
|
34
|
+
lenis.scrollTo(anchor, { offset: anchorOffset });
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
// A plain click on a link to this very page: back to the top, no reload.
|
|
38
|
+
const samePage = link.origin === location.origin &&
|
|
39
|
+
page(link.pathname) === page(location.pathname) &&
|
|
40
|
+
!link.hash &&
|
|
41
|
+
!link.search;
|
|
42
|
+
const plainClick = event.button === 0 && !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey;
|
|
43
|
+
if (samePage && plainClick && !link.target) {
|
|
20
44
|
event.preventDefault();
|
|
21
|
-
lenis
|
|
45
|
+
if (lenis)
|
|
46
|
+
lenis.scrollTo(0);
|
|
47
|
+
else
|
|
48
|
+
window.scrollTo({ top: 0, behavior: "auto" });
|
|
22
49
|
}
|
|
23
50
|
};
|
|
24
51
|
document.addEventListener("click", onClick);
|
|
25
52
|
return () => {
|
|
26
53
|
cancelAnimationFrame(frame);
|
|
27
54
|
document.removeEventListener("click", onClick);
|
|
28
|
-
lenis
|
|
55
|
+
lenis?.destroy();
|
|
29
56
|
};
|
|
30
57
|
}, [duration, anchorOffset]);
|
|
31
58
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/smooth-scroll/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGlC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/smooth-scroll/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGlC,qFAAqF;AACrF,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;AAE5D;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAA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,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC;gBAC/C,CAAC,IAAI,CAAC,IAAI;gBACV,CAAC,IAAI,CAAC,MAAM,CAAC;YACf,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;YAC9G,IAAI,QAAQ,IAAI,UAAU,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;gBAC3C,KAAK,CAAC,cAAc,EAAE,CAAC;gBACvB,IAAI,KAAK;oBAAE,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;;oBACxB,MAAM,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;YACrD,CAAC;QACH,CAAC,CAAC;QACF,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC5C,OAAO,GAAG,EAAE;YACV,oBAAoB,CAAC,KAAK,CAAC,CAAC;YAC5B,QAAQ,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC/C,KAAK,EAAE,OAAO,EAAE,CAAC;QACnB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC;IAC7B,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/llms.txt
CHANGED
|
@@ -5,3 +5,4 @@
|
|
|
5
5
|
- /smooth-scroll: <SmoothScroll duration=1.15 anchorOffset=-60 /> — off with prefers-reduced-motion; handles a[href^="#"] clicks
|
|
6
6
|
- /active-section: useActiveSection(ids: readonly string[]) → { active: id at viewport middle, within: 0–1 through it, page: 0–1 page progress }
|
|
7
7
|
- Peer: react. License: Sharadox Open Source License 0.1.0
|
|
8
|
+
- SmoothScroll also turns plain clicks on links to the current page (same path, no hash or query, no target, no modifier keys) into a scroll to the top, without reloading — with or without reduced motion.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sharadox/scroll-web-library",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Scrolling for React websites: smooth inertial scrolling with anchor jumps (Lenis) and the active section with page progress.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "SEE LICENSE IN LICENCA.md",
|