@sharadox/scroll-web-library 0.3.1 → 0.4.1

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
@@ -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
- | `…/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) |
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: on this page (`#id`) scrolling is left to `SmoothScroll` (`scroll={false}`); on another page (`/en#id`) Next.js scrolls there |
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,13 @@
1
+ import Link from "next/link";
2
+ import type { ComponentProps } from "react";
3
+ /**
4
+ * Next.js `Link` for a section of a page. For one on this page (`#contact`), Next.js updates the address and
5
+ * `SmoothScroll` does the scrolling (`scroll={false}`), so the page glides there instead of two scrolls fighting. For
6
+ * one on another page (`/en#contact`), Next.js goes to that page and scrolls to the section itself, as `Link` does.
7
+ *
8
+ * @example
9
+ * <SectionLink href="#contact">Contact</SectionLink>
10
+ * <SectionLink href="/en#contact">Contact</SectionLink>
11
+ */
12
+ export declare function SectionLink(properties: ComponentProps<typeof Link>): import("react").JSX.Element;
13
+ //# 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;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,UAAU,EAAE,cAAc,CAAC,OAAO,IAAI,CAAC,+BAGlE"}
@@ -0,0 +1,16 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import Link from "next/link";
3
+ /**
4
+ * Next.js `Link` for a section of a page. For one on this page (`#contact`), Next.js updates the address and
5
+ * `SmoothScroll` does the scrolling (`scroll={false}`), so the page glides there instead of two scrolls fighting. For
6
+ * one on another page (`/en#contact`), Next.js goes to that page and scrolls to the section itself, as `Link` does.
7
+ *
8
+ * @example
9
+ * <SectionLink href="#contact">Contact</SectionLink>
10
+ * <SectionLink href="/en#contact">Contact</SectionLink>
11
+ */
12
+ export function SectionLink(properties) {
13
+ const here = typeof properties.href === "string" && properties.href.startsWith("#");
14
+ return _jsx(Link, { ...(here ? { scroll: false } : {}), ...properties });
15
+ }
16
+ //# 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;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAAC,UAAuC;IACjE,MAAM,IAAI,GAAG,OAAO,UAAU,CAAC,IAAI,KAAK,QAAQ,IAAI,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IACpF,OAAO,KAAC,IAAI,OAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,KAAM,UAAU,GAAI,CAAC;AACvE,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. 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.
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;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,EAAE,QAAe,EAAE,YAAkB,EAAE,EAAE,sBAAsB,QA+C3F"}
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. 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.
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 anchor = link.getAttribute("href").startsWith("#")
31
- ? document.querySelector(link.getAttribute("href"))
32
- : null;
33
- if (anchor) {
34
- if (!lenis)
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
- lenis.scrollTo(anchor, { offset: anchorOffset });
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,KAA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KAAK,QAAQ,CAAC,MAAM;gBAC/B,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;YACjB,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
@@ -10,3 +10,6 @@
10
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
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; for "#id" on this page scroll={false} (SmoothScroll glides), for "/path#id" Next.js navigates and scrolls as usual; 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
15
+ - 0.4.1: SectionLink leaves Next.js scrolling on for sections of other pages ("/en#contact"), so they land on the section
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sharadox/scroll-web-library",
3
- "version": "0.3.1",
3
+ "version": "0.4.1",
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",