@indietabletop/appkit 0.1.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 ADDED
@@ -0,0 +1,6 @@
1
+ # Indie Tabletop Club's AppKit
2
+
3
+ A collection of modules used in Indie Tabletop Club's apps.
4
+
5
+ - [Hobgoblin App](https://hobgoblin.indietabletop.club)
6
+ - [Eternøl App](https://eternol.indietabletop.club)
@@ -0,0 +1 @@
1
+ export declare function useMediaQuery(query: string): boolean;
@@ -0,0 +1,15 @@
1
+ import { useState, useEffect } from "react";
2
+ export function useMediaQuery(query) {
3
+ const [isMatch, setMatch] = useState(window.matchMedia(query).matches);
4
+ useEffect(() => {
5
+ const mql = window.matchMedia(query);
6
+ const handleChange = ({ matches }) => {
7
+ setMatch(matches);
8
+ };
9
+ mql.addEventListener("change", handleChange);
10
+ return () => {
11
+ mql.removeEventListener("change", handleChange);
12
+ };
13
+ }, [query]);
14
+ return isMatch;
15
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Handles scroll restoration on window.
3
+ *
4
+ * This hook behaves a little differently than the default browser scroll
5
+ * restoration. This is due to limitations of Wouter (our router of choice)
6
+ * as well the need to make the app feel more app-like.
7
+ *
8
+ * Every scroll position is remembered however the user has got to it (we
9
+ * don't differentiate between new entries in browser history and back
10
+ * navigation), but they are only restored if the user last visited the
11
+ * location less than 60 minutes ago.
12
+ */
13
+ export declare function useScrollRestoration(
14
+ /**
15
+ * The current path, provided by your router of choice.
16
+ */
17
+ pathname: string, options?: {
18
+ /**
19
+ * A list of paths where scroll restoration should never be performed.
20
+ *
21
+ * This list should have stable identity for optimal performance. Make
22
+ * sure to use `useMemo` or define the list in module scope.
23
+ */
24
+ neverRestore?: string[];
25
+ }): void;
@@ -0,0 +1,67 @@
1
+ import { useEffect, useLayoutEffect, useMemo } from "react";
2
+ const scrollPositions = new Map();
3
+ /**
4
+ * Handles scroll restoration on window.
5
+ *
6
+ * This hook behaves a little differently than the default browser scroll
7
+ * restoration. This is due to limitations of Wouter (our router of choice)
8
+ * as well the need to make the app feel more app-like.
9
+ *
10
+ * Every scroll position is remembered however the user has got to it (we
11
+ * don't differentiate between new entries in browser history and back
12
+ * navigation), but they are only restored if the user last visited the
13
+ * location less than 60 minutes ago.
14
+ */
15
+ export function useScrollRestoration(
16
+ /**
17
+ * The current path, provided by your router of choice.
18
+ */
19
+ pathname, options) {
20
+ // Standardise pathname, making sure that paths ending with and without
21
+ // a slash are treated equally
22
+ const normalizedPathname = pathname.replace(/\/$/, "") || "/";
23
+ const neverRestore = useMemo(() => new Set(options?.neverRestore), [options?.neverRestore]);
24
+ // Record scroll position for given pathname on pushState/replaceState.
25
+ useEffect(() => {
26
+ const handleEvent = () => {
27
+ const x = window.scrollX;
28
+ const y = window.scrollY;
29
+ scrollPositions.set(normalizedPathname, [Date.now(), x, y]);
30
+ console.info(`Set scroll position for '${normalizedPathname}' (x: ${x}, y: ${y}).`);
31
+ };
32
+ // These events are provided by Wouter. They are not native browser events!
33
+ window.addEventListener("pushState", handleEvent);
34
+ window.addEventListener("replaceState", handleEvent);
35
+ return () => {
36
+ window.removeEventListener("pushState", handleEvent);
37
+ window.removeEventListener("replaceState", handleEvent);
38
+ };
39
+ }, [normalizedPathname]);
40
+ // Restore scroll position if last visit was less than 60 minutes ago.
41
+ useLayoutEffect(() => {
42
+ const maxDiff = 60000 * 60; // 60 minutes
43
+ const coordinates = scrollPositions.get(normalizedPathname);
44
+ if (coordinates) {
45
+ if (!neverRestore.has(normalizedPathname)) {
46
+ const now = Date.now();
47
+ const [ts, x, y] = coordinates;
48
+ if (now - ts < maxDiff) {
49
+ window.scrollTo(x, y);
50
+ console.info(`Restoring scroll position for '${normalizedPathname}' (x: ${x}, y: ${y}).`);
51
+ }
52
+ else {
53
+ window.scrollTo(0, 0);
54
+ console.info(`Not restoring scroll position for '${normalizedPathname}'. Last visit >60 min ago.`);
55
+ }
56
+ }
57
+ else {
58
+ window.scrollTo(0, 0);
59
+ console.info(`Not restoring scroll position for '${normalizedPathname}'. Page is set never to restore.`);
60
+ }
61
+ }
62
+ else {
63
+ window.scrollTo(0, 0);
64
+ console.info(`Not restoring scroll position for '${normalizedPathname}'. Page not visited yet.`);
65
+ }
66
+ }, [normalizedPathname]);
67
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@indietabletop/appkit",
3
+ "version": "0.1.0",
4
+ "description": "A collection of modules used in apps built by Indie Tabletop Club",
5
+ "private": false,
6
+ "type": "module",
7
+ "scripts": {
8
+ "release": "np",
9
+ "build": "tsc",
10
+ "test": "echo 'No tests'"
11
+ },
12
+ "exports": {
13
+ "./*": "./dist/*.js"
14
+ },
15
+ "files": [
16
+ "/dist"
17
+ ],
18
+ "author": "Artur Müller",
19
+ "repository": "github:indietabletop/appkit",
20
+ "license": "MIT",
21
+ "peerDependencies": {
22
+ "react": "^18.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "@types/react": "^18.3.12",
26
+ "np": "^10.1.0",
27
+ "typescript": "^5.7.2"
28
+ }
29
+ }