@sharadox/scroll-web-library 0.1.1 → 0.3.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,9 @@
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. It turns off for visitors who prefer reduced motion.
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
+ - **Glide:** `glideTo(top)` moves the page through the smooth scrolling, so code-driven moves ease like the visitor's own.
7
+ - **Scroll steps:** `useScrollSteps(section, count)` gives a tall pinned section that shows steps one after another (a timelapse, a story) one step per scroll gesture: one wheel turn, trackpad swipe or finger swipe. It doesn't take many to get from one step to the next.
6
8
  - **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
9
 
8
10
  ## Install
@@ -17,6 +19,8 @@ pnpm add @sharadox/scroll-web-library
17
19
  | ------------------ | ------------------------------------------------------------------------------------------------------- |
18
20
  | `…/smooth-scroll` | `<SmoothScroll duration? anchorOffset?>` (renders nothing; default 1.15 s and -60 px) |
19
21
  | `…/active-section` | `useActiveSection(ids)` → `{ active, within, page }` (keep `ids` stable, for example a module constant) |
22
+ | `…/glide` | `glideTo(top, { duration? })` |
23
+ | `…/scroll-steps` | `useScrollSteps(sectionRef, count, { duration? })` (0.9 s by default) |
20
24
 
21
25
  ## Develop
22
26
 
@@ -0,0 +1,12 @@
1
+ import type { GlideOptions, Glider } from "@sharadox/scroll-web-library/glide/type";
2
+ /** Called by `SmoothScroll` with its smooth scrolling while it runs, and with null when it stops. */
3
+ export declare function registerGlider(next: Glider | null): void;
4
+ /**
5
+ * Moves the page to `top` (pixels from the top of the page): through the smooth scrolling when it runs, so the
6
+ * move eases like every other; otherwise the browser glides there, or jumps when the visitor prefers reduced motion.
7
+ *
8
+ * @example
9
+ * glideTo(section.offsetTop, { duration: 0.9 });
10
+ */
11
+ export declare function glideTo(top: number, { duration }?: GlideOptions): void;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/glide/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,yCAAyC,CAAC;AAIpF,qGAAqG;AACrG,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,QAEjD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,GAAE,YAAiB,QAInE"}
@@ -0,0 +1,19 @@
1
+ let glider = null;
2
+ /** Called by `SmoothScroll` with its smooth scrolling while it runs, and with null when it stops. */
3
+ export function registerGlider(next) {
4
+ glider = next;
5
+ }
6
+ /**
7
+ * Moves the page to `top` (pixels from the top of the page): through the smooth scrolling when it runs, so the
8
+ * move eases like every other; otherwise the browser glides there, or jumps when the visitor prefers reduced motion.
9
+ *
10
+ * @example
11
+ * glideTo(section.offsetTop, { duration: 0.9 });
12
+ */
13
+ export function glideTo(top, { duration } = {}) {
14
+ if (glider)
15
+ return glider.scrollTo(top, duration === undefined ? {} : { duration });
16
+ const smooth = !matchMedia("(prefers-reduced-motion: reduce)").matches;
17
+ scrollTo({ top, behavior: smooth ? "smooth" : "auto" });
18
+ }
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/glide/index.ts"],"names":[],"mappings":"AAEA,IAAI,MAAM,GAAkB,IAAI,CAAC;AAEjC,qGAAqG;AACrG,MAAM,UAAU,cAAc,CAAC,IAAmB;IAChD,MAAM,GAAG,IAAI,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,OAAO,CAAC,GAAW,EAAE,EAAE,QAAQ,KAAmB,EAAE;IAClE,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;IACpF,MAAM,MAAM,GAAG,CAAC,UAAU,CAAC,kCAAkC,CAAC,CAAC,OAAO,CAAC;IACvE,QAAQ,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,11 @@
1
+ /** What moves the page smoothly: the smooth scrolling `SmoothScroll` starts, while it runs. */
2
+ export interface Glider {
3
+ scrollTo: (top: number, options?: {
4
+ duration?: number;
5
+ }) => void;
6
+ }
7
+ export interface GlideOptions {
8
+ /** How long the glide takes, in seconds (with smooth scrolling). */
9
+ duration?: number;
10
+ }
11
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/glide/type.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,MAAM,WAAW,MAAM;IACrB,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;CAClE;AAED,MAAM,WAAW,YAAY;IAC3B,oEAAoE;IACpE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/glide/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,14 @@
1
+ import { type RefObject } from "react";
2
+ import type { ScrollStepsOptions } from "@sharadox/scroll-web-library/scroll-steps/type";
3
+ /**
4
+ * For a tall pinned section that shows `count` steps one after another as the page scrolls through it: while it is
5
+ * pinned, each scroll gesture (a wheel turn, a trackpad swipe, a finger swipe) moves exactly one step, gliding to
6
+ * that step's place, instead of taking many to get there. Past the first or last step the page scrolls on as usual.
7
+ * Keyboard and scrollbar scrolling are left alone. Step `index` sits in the middle of its share of the section.
8
+ *
9
+ * @example
10
+ * const section = useRef<HTMLElement>(null);
11
+ * useScrollSteps(section, 7);
12
+ */
13
+ export declare function useScrollSteps<Element extends HTMLElement>(section: RefObject<Element | null>, count: number, { duration }?: ScrollStepsOptions): void;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/scroll-steps/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAElD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gDAAgD,CAAC;AAOzF;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,OAAO,SAAS,WAAW,EACxD,OAAO,EAAE,SAAS,CAAC,OAAO,GAAG,IAAI,CAAC,EAClC,KAAK,EAAE,MAAM,EACb,EAAE,QAAc,EAAE,GAAE,kBAAuB,QA0E5C"}
@@ -0,0 +1,104 @@
1
+ "use client";
2
+ import { useEffect } from "react";
3
+ import { glideTo } from "@sharadox/scroll-web-library/glide";
4
+ /** A pause this long between wheel events starts a new gesture. */
5
+ const GESTURE_GAP = 180;
6
+ /** How far a finger must travel to count as a swipe. */
7
+ const SWIPE = 30;
8
+ /**
9
+ * For a tall pinned section that shows `count` steps one after another as the page scrolls through it: while it is
10
+ * pinned, each scroll gesture (a wheel turn, a trackpad swipe, a finger swipe) moves exactly one step, gliding to
11
+ * that step's place, instead of taking many to get there. Past the first or last step the page scrolls on as usual.
12
+ * Keyboard and scrollbar scrolling are left alone. Step `index` sits in the middle of its share of the section.
13
+ *
14
+ * @example
15
+ * const section = useRef<HTMLElement>(null);
16
+ * useScrollSteps(section, 7);
17
+ */
18
+ export function useScrollSteps(section, count, { duration = 0.9 } = {}) {
19
+ useEffect(() => {
20
+ let lastWheel = 0;
21
+ let lockedUntil = 0;
22
+ let touchY = null;
23
+ // Where the section's step `index` is, and which step the page shows now, while the section is pinned.
24
+ const place = () => {
25
+ const element = section.current;
26
+ if (!element)
27
+ return null;
28
+ const bounds = element.getBoundingClientRect();
29
+ const travel = element.offsetHeight - innerHeight;
30
+ if (travel <= 0 || bounds.top > 1 || bounds.bottom < innerHeight - 1)
31
+ return null;
32
+ const top = bounds.top + scrollY;
33
+ const progress = (scrollY - top) / travel;
34
+ return {
35
+ current: Math.min(count - 1, Math.max(0, Math.floor(progress * count))),
36
+ at: (index) => top + ((index + 0.5) / count) * travel,
37
+ };
38
+ };
39
+ // Moves one step in `direction` if there is one; true when the gesture was taken.
40
+ const step = (direction, now) => {
41
+ const where = place();
42
+ if (!where)
43
+ return false;
44
+ const next = where.current + direction;
45
+ if (next < 0 || next >= count)
46
+ return false;
47
+ if (now >= lockedUntil) {
48
+ lockedUntil = now + duration * 1000;
49
+ glideTo(where.at(next), { duration });
50
+ }
51
+ return true;
52
+ };
53
+ const wheel = (event) => {
54
+ if (Math.abs(event.deltaY) < Math.abs(event.deltaX))
55
+ return;
56
+ const now = event.timeStamp;
57
+ const fresh = now - lastWheel > GESTURE_GAP;
58
+ lastWheel = now;
59
+ const where = place();
60
+ if (!where)
61
+ return;
62
+ const direction = event.deltaY > 0 ? 1 : -1;
63
+ const next = where.current + direction;
64
+ if (next < 0 || next >= count)
65
+ return;
66
+ // Inside the steps the gesture is ours: the page doesn't scroll by it, and only a new gesture moves again.
67
+ event.preventDefault();
68
+ event.stopPropagation();
69
+ if (fresh)
70
+ step(direction, now);
71
+ };
72
+ const touchStart = (event) => {
73
+ touchY = event.touches[0].clientY;
74
+ };
75
+ const touchMove = (event) => {
76
+ if (touchY === null)
77
+ return;
78
+ const where = place();
79
+ const direction = touchY - event.touches[0].clientY > 0 ? 1 : -1;
80
+ if (where && where.current + direction >= 0 && where.current + direction < count)
81
+ event.preventDefault();
82
+ };
83
+ const touchEnd = (event) => {
84
+ if (touchY === null)
85
+ return;
86
+ const travel = touchY - event.changedTouches[0].clientY;
87
+ touchY = null;
88
+ if (Math.abs(travel) >= SWIPE)
89
+ step(travel > 0 ? 1 : -1, event.timeStamp);
90
+ };
91
+ const options = { capture: true, passive: false };
92
+ addEventListener("wheel", wheel, options);
93
+ addEventListener("touchstart", touchStart, options);
94
+ addEventListener("touchmove", touchMove, options);
95
+ addEventListener("touchend", touchEnd, options);
96
+ return () => {
97
+ removeEventListener("wheel", wheel, options);
98
+ removeEventListener("touchstart", touchStart, options);
99
+ removeEventListener("touchmove", touchMove, options);
100
+ removeEventListener("touchend", touchEnd, options);
101
+ };
102
+ }, [section, count, duration]);
103
+ }
104
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/scroll-steps/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,OAAO,EAAE,SAAS,EAAkB,MAAM,OAAO,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,oCAAoC,CAAC;AAG7D,mEAAmE;AACnE,MAAM,WAAW,GAAG,GAAG,CAAC;AACxB,wDAAwD;AACxD,MAAM,KAAK,GAAG,EAAE,CAAC;AAEjB;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAC5B,OAAkC,EAClC,KAAa,EACb,EAAE,QAAQ,GAAG,GAAG,KAAyB,EAAE;IAE3C,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,SAAS,GAAG,CAAC,CAAC;QAClB,IAAI,WAAW,GAAG,CAAC,CAAC;QACpB,IAAI,MAAM,GAAkB,IAAI,CAAC;QACjC,uGAAuG;QACvG,MAAM,KAAK,GAAG,GAAG,EAAE;YACjB,MAAM,OAAO,GAAG,OAAO,CAAC,OA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AAE,OAAO,CAAC,CAAC;YACrD,mBAAmB,CAAC,UAAU,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;QACrD,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;AACjC,CAAC"}
@@ -0,0 +1,5 @@
1
+ export interface ScrollStepsOptions {
2
+ /** How long each step's glide takes, in seconds. 0.9 by default. */
3
+ duration?: number;
4
+ }
5
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/scroll-steps/type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC,oEAAoE;IACpE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/scroll-steps/type.ts"],"names":[],"mappings":""}
@@ -1,4 +1,8 @@
1
1
  import type { SmoothScrollProperties } from "@sharadox/scroll-web-library/smooth-scroll/type";
2
- /** Smooth, inertial page scrolling, including in-page anchor links. Off when the visitor prefers reduced motion. */
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;AAE9F,oHAAoH;AACpH,wBAAgB,YAAY,CAAC,EAAE,QAAe,EAAE,YAAkB,EAAE,EAAE,sBAAsB,QA0B3F"}
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;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,EAAE,QAAe,EAAE,YAAkB,EAAE,EAAE,sBAAsB,QA+C3F"}
@@ -1,31 +1,62 @@
1
1
  "use client";
2
2
  import Lenis from "lenis";
3
3
  import { useEffect } from "react";
4
- /** Smooth, inertial page scrolling, including in-page anchor links. Off when the visitor prefers reduced motion. */
4
+ import { registerGlider } from "@sharadox/scroll-web-library/glide";
5
+ /** A path without its trailing slash, so "/en/" and "/en" count as the same page. */
6
+ const page = (path) => path.replace(/(.)\/$/, "$1");
7
+ /**
8
+ * Smooth, inertial page scrolling, including in-page anchor links. A link to the page already open (the logo on the
9
+ * home page, for one) glides back to the top instead of reloading. Smoothing is off when the visitor prefers reduced
10
+ * motion: anchors then jump, and the same-page link still goes to the top without reloading.
11
+ */
5
12
  export function SmoothScroll({ duration = 1.15, anchorOffset = -60 }) {
6
13
  useEffect(() => {
7
- if (window.matchMedia("(prefers-reduced-motion: reduce)").matches)
8
- return;
9
- const lenis = new Lenis({ duration, smoothWheel: true });
14
+ const lenis = window.matchMedia("(prefers-reduced-motion: reduce)").matches
15
+ ? null
16
+ : new Lenis({ duration, smoothWheel: true });
10
17
  let frame = 0;
11
18
  const animate = (time) => {
12
19
  lenis.raf(time);
13
20
  frame = requestAnimationFrame(animate);
14
21
  };
15
- frame = requestAnimationFrame(animate);
22
+ if (lenis)
23
+ frame = requestAnimationFrame(animate);
24
+ // glideTo moves the page through it, so programmatic moves ease like the visitor's own.
25
+ registerGlider(lenis && { scrollTo: (top, options) => lenis.scrollTo(top, options) });
16
26
  const onClick = (event) => {
17
- const link = event.target.closest('a[href^="#"]');
18
- const target = link && document.querySelector(link.getAttribute("href"));
19
- if (target) {
27
+ const link = event.target.closest("a[href]");
28
+ if (!link)
29
+ return;
30
+ const anchor = link.getAttribute("href").startsWith("#")
31
+ ? document.querySelector(link.getAttribute("href"))
32
+ : null;
33
+ if (anchor) {
34
+ if (!lenis)
35
+ return;
36
+ event.preventDefault();
37
+ lenis.scrollTo(anchor, { offset: anchorOffset });
38
+ return;
39
+ }
40
+ // A plain click on a link to this very page: back to the top, no reload.
41
+ const samePage = link.origin === location.origin &&
42
+ page(link.pathname) === page(location.pathname) &&
43
+ !link.hash &&
44
+ !link.search;
45
+ const plainClick = event.button === 0 && !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey;
46
+ if (samePage && plainClick && !link.target) {
20
47
  event.preventDefault();
21
- lenis.scrollTo(target, { offset: anchorOffset });
48
+ if (lenis)
49
+ lenis.scrollTo(0);
50
+ else
51
+ window.scrollTo({ top: 0, behavior: "auto" });
22
52
  }
23
53
  };
24
54
  document.addEventListener("click", onClick);
25
55
  return () => {
26
56
  cancelAnimationFrame(frame);
27
57
  document.removeEventListener("click", onClick);
28
- lenis.destroy();
58
+ lenis?.destroy();
59
+ registerGlider(null);
29
60
  };
30
61
  }, [duration, anchorOffset]);
31
62
  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,oHAAoH;AACpH,MAAM,UAAU,YAAY,CAAC,EAAE,QAAQ,GAAG,IAAI,EAAE,YAAY,GAAG,CAAC,EAAE,EAA0B;IAC1F,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,MAAM,CAAC,UAAU,CAAC,kCAAkC,CAAC,CAAC,OAAO;YAAE,OAAO;QAC1E,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CA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B,cAAc,CAAC,IAAI,CAAC,CAAC;QACvB,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,7 @@
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.
9
+ - /glide: glideTo(top, { duration? }) — through the running SmoothScroll (Lenis) or window.scrollTo (smooth, or auto with reduced motion); registerGlider is internal to SmoothScroll
10
+ - /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
+ - 0.3.0: glide, scroll-steps
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sharadox/scroll-web-library",
3
- "version": "0.1.1",
3
+ "version": "0.3.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",