@sharadox/scroll-web-library 0.3.1 → 0.4.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 +7 -6
- package/distribution/section-link/index.d.ts +11 -0
- package/distribution/section-link/index.d.ts.map +1 -0
- package/distribution/section-link/index.js +13 -0
- package/distribution/section-link/index.js.map +1 -0
- package/distribution/smooth-scroll/index.d.ts +4 -3
- package/distribution/smooth-scroll/index.d.ts.map +1 -1
- package/distribution/smooth-scroll/index.js +22 -9
- package/distribution/smooth-scroll/index.js.map +1 -1
- package/llms.txt +2 -0
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -15,12 +15,13 @@ pnpm add @sharadox/scroll-web-library
|
|
|
15
15
|
|
|
16
16
|
## API
|
|
17
17
|
|
|
18
|
-
| Import | Export
|
|
19
|
-
| ------------------ |
|
|
20
|
-
| `…/smooth-scroll` | `<SmoothScroll duration? anchorOffset?>` (renders nothing; default 1.15 s and -60 px)
|
|
21
|
-
| `…/
|
|
22
|
-
| `…/
|
|
23
|
-
| `…/
|
|
18
|
+
| Import | Export |
|
|
19
|
+
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------- |
|
|
20
|
+
| `…/smooth-scroll` | `<SmoothScroll duration? anchorOffset?>` (renders nothing; default 1.15 s and -60 px; `#top` glides to the top of any page) |
|
|
21
|
+
| `…/section-link` | `<SectionLink href>`: Next.js `Link` for a section (`#id`, `/en#id`) that leaves scrolling to `SmoothScroll` (`scroll={false}`) |
|
|
22
|
+
| `…/active-section` | `useActiveSection(ids)` → `{ active, within, page }` (keep `ids` stable, for example a module constant) |
|
|
23
|
+
| `…/glide` | `glideTo(top, { duration? })` |
|
|
24
|
+
| `…/scroll-steps` | `useScrollSteps(sectionRef, count, { duration? })` (0.9 s by default) |
|
|
24
25
|
|
|
25
26
|
## Develop
|
|
26
27
|
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Link from "next/link";
|
|
2
|
+
import type { ComponentProps } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* Next.js `Link` for a section of the page (`#contact`, or `/en#contact` from another page). Next.js updates the address
|
|
5
|
+
* and `SmoothScroll` does the scrolling (`scroll={false}`), so the page glides there instead of two scrolls fighting.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* <SectionLink href="#contact">Contact</SectionLink>
|
|
9
|
+
*/
|
|
10
|
+
export declare function SectionLink(properties: ComponentProps<typeof Link>): import("react").JSX.Element;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/section-link/index.tsx"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAE5C;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,UAAU,EAAE,cAAc,CAAC,OAAO,IAAI,CAAC,+BAElE"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import Link from "next/link";
|
|
3
|
+
/**
|
|
4
|
+
* Next.js `Link` for a section of the page (`#contact`, or `/en#contact` from another page). Next.js updates the address
|
|
5
|
+
* and `SmoothScroll` does the scrolling (`scroll={false}`), so the page glides there instead of two scrolls fighting.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* <SectionLink href="#contact">Contact</SectionLink>
|
|
9
|
+
*/
|
|
10
|
+
export function SectionLink(properties) {
|
|
11
|
+
return _jsx(Link, { scroll: false, ...properties });
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/section-link/index.tsx"],"names":[],"mappings":";AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,UAAuC;IACjE,OAAO,KAAC,IAAI,IAAC,MAAM,EAAE,KAAK,KAAM,UAAU,GAAI,CAAC;AACjD,CAAC"}
|
|
@@ -1,8 +1,9 @@
|
|
|
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
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Smooth, inertial page scrolling, including in-page anchor links (plain ones and `SectionLink`s) and `#top`, which
|
|
4
|
+
* glides to the top of whatever page is open. A link to the page already open (the logo on the home page, for one)
|
|
5
|
+
* glides back to the top instead of reloading. Smoothing is off when the visitor prefers reduced motion: anchors then
|
|
6
|
+
* jump, and the same-page link still goes to the top without reloading.
|
|
6
7
|
*/
|
|
7
8
|
export declare function SmoothScroll({ duration, anchorOffset }: SmoothScrollProperties): null;
|
|
8
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/smooth-scroll/index.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,iDAAiD,CAAC;AAK9F
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/smooth-scroll/index.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,iDAAiD,CAAC;AAK9F;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,EAAE,QAAe,EAAE,YAAkB,EAAE,EAAE,sBAAsB,QAqD3F"}
|
|
@@ -5,9 +5,10 @@ import { registerGlider } from "@sharadox/scroll-web-library/glide";
|
|
|
5
5
|
/** A path without its trailing slash, so "/en/" and "/en" count as the same page. */
|
|
6
6
|
const page = (path) => path.replace(/(.)\/$/, "$1");
|
|
7
7
|
/**
|
|
8
|
-
* Smooth, inertial page scrolling, including in-page anchor links
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* Smooth, inertial page scrolling, including in-page anchor links (plain ones and `SectionLink`s) and `#top`, which
|
|
9
|
+
* glides to the top of whatever page is open. A link to the page already open (the logo on the home page, for one)
|
|
10
|
+
* glides back to the top instead of reloading. Smoothing is off when the visitor prefers reduced motion: anchors then
|
|
11
|
+
* jump, and the same-page link still goes to the top without reloading.
|
|
11
12
|
*/
|
|
12
13
|
export function SmoothScroll({ duration = 1.15, anchorOffset = -60 }) {
|
|
13
14
|
useEffect(() => {
|
|
@@ -27,14 +28,26 @@ export function SmoothScroll({ duration = 1.15, anchorOffset = -60 }) {
|
|
|
27
28
|
const link = event.target.closest("a[href]");
|
|
28
29
|
if (!link)
|
|
29
30
|
return;
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (!
|
|
31
|
+
const href = link.getAttribute("href");
|
|
32
|
+
if (href.startsWith("#")) {
|
|
33
|
+
// "#top" is the top of any page, as in browsers; other anchors are the element with that id.
|
|
34
|
+
const anchor = href === "#top" ? document.documentElement : document.querySelector(href);
|
|
35
|
+
if (!anchor)
|
|
36
|
+
return;
|
|
37
|
+
// A `SectionLink` (Next.js Link that leaves scrolling to us) has already taken the click: scroll even then.
|
|
38
|
+
if (!lenis && !event.defaultPrevented)
|
|
35
39
|
return;
|
|
36
40
|
event.preventDefault();
|
|
37
|
-
|
|
41
|
+
if (anchor === document.documentElement) {
|
|
42
|
+
if (lenis)
|
|
43
|
+
lenis.scrollTo(0);
|
|
44
|
+
else
|
|
45
|
+
window.scrollTo({ top: 0, behavior: "auto" });
|
|
46
|
+
}
|
|
47
|
+
else if (lenis)
|
|
48
|
+
lenis.scrollTo(anchor, { offset: anchorOffset });
|
|
49
|
+
else
|
|
50
|
+
anchor.scrollIntoView();
|
|
38
51
|
return;
|
|
39
52
|
}
|
|
40
53
|
// A plain click on a link to this very page: back to the top, no reload.
|
|
@@ -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;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAGpE,qFAAqF;AACrF,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;AAE5D
|
|
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;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAGpE,qFAAqF;AACrF,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;AAE5D;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,EAAE,QAAQ,GAAG,IAAI,EAAE,YAAY,GAAG,CAAC,EAAE,EAA0B;IAC1F,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC,kCAAkC,CAAC,CAAC,OAAO;YACzE,CAAC,CAAC,IAAI;YACN,CAAC,CAAC,IAAI,KAAK,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/C,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,MAAM,OAAO,GAAG,CAAC,IAAY,EAAE,EAAE;YAC/B,KAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACjB,KAAK,GAAG,qBAAqB,CAAC,OAAO,CA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
|
- /scroll-steps: useScrollSteps(sectionRef, count, { duration? = 0.9 }) — while the section is pinned, each wheel gesture (a pause of 180 ms starts a new one) or finger swipe (30 px+) glides exactly one step; step i sits at top + (i + 0.5) / count × (height − innerHeight); past the ends the page scrolls on
|
|
11
11
|
- 0.3.0: glide, scroll-steps
|
|
12
12
|
- 0.3.1: a finger swipe's step glide starts after the touch event, so smooth scrolling (Lenis) no longer stops it on phones
|
|
13
|
+
- /section-link: <SectionLink href …LinkProps> — next/link with scroll={false} for in-page sections; SmoothScroll glides (or, with reduced motion, jumps) to the anchor even though Link took the click
|
|
14
|
+
- 0.4.0: SectionLink; SmoothScroll handles "#top" (top of the open page) and anchors whose click a Next.js Link already took; next is a peer dependency
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sharadox/scroll-web-library",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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",
|
|
@@ -70,14 +70,16 @@
|
|
|
70
70
|
"turbo": "^2.11.10",
|
|
71
71
|
"typedoc": "^0.28.20",
|
|
72
72
|
"typescript": "6.0.3",
|
|
73
|
-
"vitest": "^5.1.0"
|
|
73
|
+
"vitest": "^5.1.0",
|
|
74
|
+
"next": "16.4.5"
|
|
74
75
|
},
|
|
75
76
|
"dependencies": {
|
|
76
77
|
"@sharadox/web-library": "^0.5.0",
|
|
77
78
|
"lenis": "^1.3.27"
|
|
78
79
|
},
|
|
79
80
|
"peerDependencies": {
|
|
80
|
-
"react": ">=19"
|
|
81
|
+
"react": ">=19",
|
|
82
|
+
"next": ">=16"
|
|
81
83
|
},
|
|
82
84
|
"scripts": {
|
|
83
85
|
"build": "rm -rf distribution && tsc -p tsconfig.build.json",
|