@real-router/preact 0.14.0 → 0.15.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 +21 -2
- package/dist/cjs/index.d.ts +61 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.mts +61 -0
- package/dist/esm/index.d.mts.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/RouterProvider.tsx +28 -1
package/src/RouterProvider.tsx
CHANGED
|
@@ -6,11 +6,12 @@ import { NavigatorContext, RouteContext, RouterContext } from "./context";
|
|
|
6
6
|
import {
|
|
7
7
|
createRouteAnnouncer,
|
|
8
8
|
createScrollRestoration,
|
|
9
|
+
createScrollSpy,
|
|
9
10
|
createViewTransitions,
|
|
10
11
|
} from "./dom-utils";
|
|
11
12
|
import { useSyncExternalStore } from "./useSyncExternalStore";
|
|
12
13
|
|
|
13
|
-
import type { ScrollRestorationOptions } from "./dom-utils";
|
|
14
|
+
import type { ScrollRestorationOptions, ScrollSpyOptions } from "./dom-utils";
|
|
14
15
|
import type { Router } from "@real-router/core";
|
|
15
16
|
import type { FunctionComponent, ComponentChildren } from "preact";
|
|
16
17
|
|
|
@@ -19,6 +20,7 @@ export interface RouteProviderProps {
|
|
|
19
20
|
children: ComponentChildren;
|
|
20
21
|
announceNavigation?: boolean;
|
|
21
22
|
scrollRestoration?: ScrollRestorationOptions;
|
|
23
|
+
scrollSpy?: ScrollSpyOptions;
|
|
22
24
|
viewTransitions?: boolean;
|
|
23
25
|
}
|
|
24
26
|
|
|
@@ -27,6 +29,7 @@ export const RouterProvider: FunctionComponent<RouteProviderProps> = ({
|
|
|
27
29
|
children,
|
|
28
30
|
announceNavigation,
|
|
29
31
|
scrollRestoration,
|
|
32
|
+
scrollSpy,
|
|
30
33
|
viewTransitions,
|
|
31
34
|
}) => {
|
|
32
35
|
useEffect(() => {
|
|
@@ -72,6 +75,30 @@ export const RouterProvider: FunctionComponent<RouteProviderProps> = ({
|
|
|
72
75
|
// eslint-disable-next-line @eslint-react/exhaustive-deps
|
|
73
76
|
}, [router, srEnabled, srMode, srAnchor, srBehavior, srStorageKey]);
|
|
74
77
|
|
|
78
|
+
const spySelector = scrollSpy?.selector;
|
|
79
|
+
const spyRootMargin = scrollSpy?.rootMargin;
|
|
80
|
+
const spyEnabled =
|
|
81
|
+
scrollSpy !== undefined && spySelector !== undefined && spySelector !== "";
|
|
82
|
+
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (!spyEnabled) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const spy = createScrollSpy(router, {
|
|
89
|
+
selector: spySelector,
|
|
90
|
+
rootMargin: spyRootMargin,
|
|
91
|
+
scrollContainer: scrollSpy.scrollContainer,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return () => {
|
|
95
|
+
spy.destroy();
|
|
96
|
+
};
|
|
97
|
+
// scrollSpy (for scrollContainer) omitted — same rationale as
|
|
98
|
+
// scrollRestoration above: getter is invoked lazily inside the utility.
|
|
99
|
+
// eslint-disable-next-line @eslint-react/exhaustive-deps
|
|
100
|
+
}, [router, spyEnabled, spySelector, spyRootMargin]);
|
|
101
|
+
|
|
75
102
|
useEffect(() => {
|
|
76
103
|
if (!viewTransitions) {
|
|
77
104
|
return;
|