@phreshos/react 0.1.33 → 0.1.34
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.
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { type ReactNode } from "react";
|
|
2
|
-
import type { Connection, Desktop, DesktopPreferences,
|
|
2
|
+
import type { Connection, Desktop, DesktopPreferences, DesktopViewportState } from "@phreshos/core";
|
|
3
3
|
import LiveSnapshot from "./live-snapshot.js";
|
|
4
4
|
/** Provides one complete Client Desktop environment to a React tree. */
|
|
5
5
|
export default function DesktopProvider({ children, desktop, fallback }: DesktopProviderProperties): string | number | bigint | boolean | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | import("react").FunctionComponentElement<import("react").ProviderProps<Readonly<{
|
|
6
6
|
desktop: Desktop;
|
|
7
|
-
viewport: LiveSnapshot<
|
|
7
|
+
viewport: LiveSnapshot<DesktopViewportState>;
|
|
8
8
|
preferences: LiveSnapshot<DesktopPreferences>;
|
|
9
9
|
}> | null>> | null;
|
|
10
10
|
/** Returns the complete Desktop supplied by the nearest provider. */
|
|
11
11
|
export declare function useDesktop(): Desktop;
|
|
12
|
-
/** Resolves and follows the current Desktop viewport. */
|
|
13
|
-
export declare function useDesktopViewport():
|
|
12
|
+
/** Resolves and follows the current Desktop viewport: its size and its offset together. */
|
|
13
|
+
export declare function useDesktopViewport(): DesktopViewportState;
|
|
14
14
|
/** Resolves and follows the effective Desktop preferences. */
|
|
15
15
|
export declare function useDesktopPreferences(): DesktopPreferences;
|
|
16
16
|
/** Resolves the browser Connection carrying this Desktop when explicitly used. */
|
package/dist/desktop-provider.js
CHANGED
|
@@ -7,7 +7,7 @@ const DesktopContext = createContext(null);
|
|
|
7
7
|
export default function DesktopProvider({ children, desktop, fallback = null }) {
|
|
8
8
|
const value = useMemo(() => ({
|
|
9
9
|
desktop,
|
|
10
|
-
viewport:
|
|
10
|
+
viewport: viewportState(desktop),
|
|
11
11
|
preferences: new LiveSnapshot(() => desktop.preferences.snapshot(), subscriber => desktop.preferences.subscribe("change", subscriber))
|
|
12
12
|
}), [desktop]);
|
|
13
13
|
const stores = useMemo(() => [value.viewport, value.preferences], [value]);
|
|
@@ -18,7 +18,7 @@ export default function DesktopProvider({ children, desktop, fallback = null })
|
|
|
18
18
|
export function useDesktop() {
|
|
19
19
|
return useValue().desktop;
|
|
20
20
|
}
|
|
21
|
-
/** Resolves and follows the current Desktop viewport. */
|
|
21
|
+
/** Resolves and follows the current Desktop viewport: its size and its offset together. */
|
|
22
22
|
export function useDesktopViewport() {
|
|
23
23
|
const store = useValue().viewport;
|
|
24
24
|
return useSyncExternalStore(store.subscribe, store.snapshot, store.snapshot);
|
|
@@ -40,3 +40,19 @@ function useValue() {
|
|
|
40
40
|
throw new Error("useDesktop must be used inside DesktopProvider");
|
|
41
41
|
return value;
|
|
42
42
|
}
|
|
43
|
+
/** One state from the viewport's two values: each event replaces its own value and keeps the other. */
|
|
44
|
+
function viewportState(desktop) {
|
|
45
|
+
let latest = {};
|
|
46
|
+
const complete = () => latest.size && latest.offset ? Object.freeze({ size: latest.size, offset: latest.offset }) : null;
|
|
47
|
+
return new LiveSnapshot(async () => {
|
|
48
|
+
const [size, offset] = await Promise.all([desktop.viewport.size(), desktop.viewport.offset()]);
|
|
49
|
+
latest = { size, offset };
|
|
50
|
+
return complete();
|
|
51
|
+
}, subscriber => {
|
|
52
|
+
const publish = () => { const state = complete(); if (state)
|
|
53
|
+
subscriber(state); };
|
|
54
|
+
const stopResize = desktop.viewport.subscribe("resize", size => { latest = { ...latest, size }; publish(); });
|
|
55
|
+
const stopMove = desktop.viewport.subscribe("move", offset => { latest = { ...latest, offset }; publish(); });
|
|
56
|
+
return () => { stopResize(); stopMove(); };
|
|
57
|
+
});
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phreshos/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.34",
|
|
4
4
|
"description": "Runtime-neutral React adapters for PhreshOS contracts.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -46,11 +46,11 @@
|
|
|
46
46
|
"prepack": "node --run build"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@phreshos/core": "^0.1.
|
|
49
|
+
"@phreshos/core": "^0.1.64",
|
|
50
50
|
"react": "^19.2.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@phreshos/core": "^0.1.
|
|
53
|
+
"@phreshos/core": "^0.1.64",
|
|
54
54
|
"@testing-library/dom": "^10.4.1",
|
|
55
55
|
"@testing-library/react": "^16.3.0",
|
|
56
56
|
"@types/react": "^19.2.18",
|