@sharadox/scroll-web-library 0.1.0 → 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/LICENSE.md CHANGED
@@ -1,40 +1,40 @@
1
- # Sharadox Open Source Licence
1
+ # Sharadox Open Source License
2
2
 
3
3
  Version 0.1.0
4
4
 
5
- This is an informative English translation. The Portuguese text (`LICENCA.md`) is the only original and binding text of this licence; if the two differ, the Portuguese text prevails.
5
+ This is an informative English translation. The Portuguese text (`LICENCA.md`) is the only original and binding text of this license; if the two differ, the Portuguese text prevails.
6
6
 
7
7
  ## 1. Definitions
8
8
 
9
- For the purposes of this licence:
9
+ For the purposes of this license:
10
10
 
11
- 1. **"Work"** — the computer program, its documentation and any other content made available by the Licensor under this licence, in source code or in any other form.
11
+ 1. **"Work"** — the computer program, its documentation and any other content made available by the Licensor under this license, in source code or in any other form.
12
12
  2. **"Licensor"** — the copyright holder identified in the copyright notice that accompanies the Work.
13
- 3. **"Licensee"** — any natural or legal person exercising the rights granted by this licence.
13
+ 3. **"Licensee"** — any natural or legal person exercising the rights granted by this license.
14
14
  4. **"Derivative Work"** — any work based on or incorporating the Work, in whole or in part, with or without modifications, including translations, adaptations and programs that integrate it.
15
- 5. **"Notice"** — the Licensor's copyright notice and the statement that the Work is licensed under this licence, including its version.
15
+ 5. **"Notice"** — the Licensor's copyright notice and the statement that the Work is licensed under this license, including its version.
16
16
 
17
17
  ## 2. Grant of rights
18
18
 
19
- 1. Under this licence, the Licensor grants the Licensee a worldwide, royalty-free, non-exclusive licence, for the whole term of copyright protection, to:
19
+ 1. Under this license, the Licensor grants the Licensee a worldwide, royalty-free, non-exclusive license, for the whole term of copyright protection, to:
20
20
  1. use the Work for any purpose, including commercial purposes;
21
21
  2. reproduce the Work, in whole or in part, by any means and in any form;
22
22
  3. modify, translate, adapt and transform the Work and create Derivative Works;
23
23
  4. distribute, publish, communicate to the public, make available, sell and sublicense the Work and Derivative Works.
24
- 2. To the extent the Licensor holds patents necessarily infringed by the Work as made available by it, the Licensor also grants the Licensee, on the same terms, a licence to use those patents in exercising the rights in the previous paragraph.
25
- 3. This licence does not transfer ownership of any rights, which remain with the Licensor.
24
+ 2. To the extent the Licensor holds patents necessarily infringed by the Work as made available by it, the Licensor also grants the Licensee, on the same terms, a license to use those patents in exercising the rights in the previous paragraph.
25
+ 3. This license does not transfer ownership of any rights, which remain with the Licensor.
26
26
 
27
27
  ## 3. Conditions
28
28
 
29
29
  The exercise of the rights granted is subject to the following conditions:
30
30
 
31
- 1. Every copy of the Work, or of substantial parts of it, and every distributed Derivative Work must include the Notice and a copy of this licence or a stable link to its text.
31
+ 1. Every copy of the Work, or of substantial parts of it, and every distributed Derivative Work must include the Notice and a copy of this license or a stable link to its text.
32
32
  2. Anyone distributing a modified version of the Work must state visibly that it has been modified and may not present it as the Licensor's original version.
33
- 3. The Licensee may license their own modifications under other terms, provided the part of the Work included in them remains subject to the conditions of this licence.
33
+ 3. The Licensee may license their own modifications under other terms, provided the part of the Work included in them remains subject to the conditions of this license.
34
34
 
35
35
  ## 4. Names and trademarks
36
36
 
37
- This licence grants no rights to the Licensor's name, trademarks, logos or other distinctive signs, including the name "Sharadox", except as strictly necessary to comply with the duty to include the Notice. Using them to promote or suggest the Licensor's endorsement of Derivative Works or of any products or services without prior written permission is prohibited.
37
+ This license grants no rights to the Licensor's name, trademarks, logos or other distinctive signs, including the name "Sharadox", except as strictly necessary to comply with the duty to include the Notice. Using them to promote or suggest the Licensor's endorsement of Derivative Works or of any products or services without prior written permission is prohibited.
38
38
 
39
39
  ## 5. Disclaimer of warranties
40
40
 
@@ -43,29 +43,29 @@ The Work is provided "as is" and "as available", without any warranty, express o
43
43
  ## 6. Limitation of liability
44
44
 
45
45
  1. To the maximum extent permitted by applicable law, the Licensor is not liable for any damages, direct or indirect, including loss of profits, loss of data or business interruption, arising from the use of or inability to use the Work, even if advised of the possibility of such damages.
46
- 2. Nothing in this licence excludes or limits the Licensor's liability where such exclusion or limitation is not permitted by law, in particular for acts committed with intent or gross negligence, under article 809 and article 800(2) of the Portuguese Civil Code, nor does it affect rights that the law mandatorily grants to consumers.
46
+ 2. Nothing in this license excludes or limits the Licensor's liability where such exclusion or limitation is not permitted by law, in particular for acts committed with intent or gross negligence, under article 809 and article 800(2) of the Portuguese Civil Code, nor does it affect rights that the law mandatorily grants to consumers.
47
47
 
48
48
  ## 7. Termination
49
49
 
50
- 1. The rights granted by this licence terminate automatically if the Licensee breaches any of its conditions.
50
+ 1. The rights granted by this license terminate automatically if the Licensee breaches any of its conditions.
51
51
  2. The rights are reinstated if the breach is remedied within 30 days of the Licensee becoming aware of it, unless the Licensor has meanwhile objected in writing.
52
- 3. Termination does not affect licences validly granted by the Licensee to third parties who comply with this licence.
52
+ 3. Termination does not affect licenses validly granted by the Licensee to third parties who comply with this license.
53
53
 
54
54
  ## 8. Governing law and jurisdiction
55
55
 
56
- 1. This licence is governed by Portuguese law, in particular the Code of Copyright and Related Rights, approved by Decree-Law no. 63/85 of 14 March, and Decree-Law no. 252/94 of 20 October on the legal protection of computer programs.
57
- 2. The Judicial Court of the District of Faro has jurisdiction over any dispute arising from this licence, without prejudice to applicable mandatory rules of jurisdiction, in particular those protecting consumers.
56
+ 1. This license is governed by Portuguese law, in particular the Code of Copyright and Related Rights, approved by Decree-Law no. 63/85 of 14 March, and Decree-Law no. 252/94 of 20 October on the legal protection of computer programs.
57
+ 2. The Judicial Court of the District of Faro has jurisdiction over any dispute arising from this license, without prejudice to applicable mandatory rules of jurisdiction, in particular those protecting consumers.
58
58
 
59
- ## 9. Licence versions
59
+ ## 9. License versions
60
60
 
61
- The Licensor may publish new versions of this licence, identified by a version number. The Work is subject to the version stated in the Notice. If the Notice allows it, the Licensee may choose any later version.
61
+ The Licensor may publish new versions of this license, identified by a version number. The Work is subject to the version stated in the Notice. If the Notice allows it, the Licensee may choose any later version.
62
62
 
63
63
  ## 10. Final provisions
64
64
 
65
- 1. If any provision of this licence is held invalid or unenforceable, the remaining provisions stay in force, and the affected provision shall be interpreted as closely as possible to its original intent.
66
- 2. Failure to enforce any provision of this licence at any time does not waive the right to enforce it later.
65
+ 1. If any provision of this license is held invalid or unenforceable, the remaining provisions stay in force, and the affected provision shall be interpreted as closely as possible to its original intent.
66
+ 2. Failure to enforce any provision of this license at any time does not waive the right to enforce it later.
67
67
 
68
- ## How to apply this licence
68
+ ## How to apply this license
69
69
 
70
70
  Include in the Work a `LICENCA.md` file with the Portuguese text, this translation as `LICENSE.md`, and the following notice, filling in the year and holder:
71
71
 
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. 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
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
@@ -22,6 +22,6 @@ pnpm add @sharadox/scroll-web-library
22
22
 
23
23
  `pnpm verify` runs formatting, lint, type-check, tests with 100% coverage and the build. Branches follow `<feature>.development` → `development` → `index`, and releases are `v<version>` tags on `index`.
24
24
 
25
- ## Licence
25
+ ## License
26
26
 
27
27
  Licença Sharadox de Código Aberto, versão 0.1.0. See [português](LICENCA.md) (the binding text) and the [English translation](LICENSE.md).
@@ -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":"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
- /** Smooth, inertial page scrolling, including in-page anchor links. Off when the visitor prefers reduced motion. */
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
- if (window.matchMedia("(prefers-reduced-motion: reduce)").matches)
8
- return;
9
- const lenis = new Lenis({ duration, smoothWheel: true });
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
- frame = requestAnimationFrame(animate);
21
+ if (lenis)
22
+ frame = requestAnimationFrame(animate);
16
23
  const onClick = (event) => {
17
- const link = event.target.closest('a[href^="#"]');
18
- const target = link && document.querySelector(link.getAttribute("href"));
19
- if (target) {
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.scrollTo(target, { offset: anchorOffset });
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.destroy();
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,oHAAoH;AACpH,MAAM,UAAU,YAAY,CAAC,EAAE,QAAQ,GAAG,IAAI,EAAE,YAAY,GAAG,CAAC,EAAE,EAA0B;IAC1F,SAAS,CAAC,GAAG,EAAE;QAC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CAAC,CAAC,CAAC;IAC7B,OAAO,IAAI,CAAC;AACd,CAAC"}
package/llms.txt CHANGED
@@ -4,4 +4,5 @@
4
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
- - Peer: react. Licence: Sharadox Open Source Licence 0.1.0
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.1.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",
@@ -31,7 +31,7 @@
31
31
  "**/*.css"
32
32
  ],
33
33
  "engines": {
34
- "node": ">=24"
34
+ "node": ">=26"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"