@proto-labs-inc/rig 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Proto Labs Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @proto-labs-inc/rig
2
+
3
+ The rig for React prototypes: `<ProtoRig manifest={…}>` at the root, and
4
+ `usePreviewState` / `useVariant` wherever a view reads its state or variant.
5
+ All logic lives in `@proto-labs-inc/rig-core`; this package is the React idiom.
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @proto-labs-inc/rig — the React adapter over @proto-labs-inc/rig-core. All rig logic
3
+ * (URL-state sync, the bridge, comment mode) lives in the core; this file
4
+ * is only the React idiom: a provider component + hooks.
5
+ */
6
+ import { type ReactNode } from "react";
7
+ import { type Manifest } from "@proto-labs-inc/wire";
8
+ export declare function ProtoRig({ manifest, children, }: {
9
+ manifest: Manifest;
10
+ children: ReactNode;
11
+ }): import("react").JSX.Element;
12
+ /** The prototype's preview state, addressable as ?state=<id>. */
13
+ export declare function usePreviewState(defaultId: string, validIds?: readonly string[]): readonly [string, (v: string) => void];
14
+ /** A component's design variant, addressable as ?v.<component>=<id>. */
15
+ export declare function useVariant(component: string, defaultVariant: string, validIds?: readonly string[]): readonly [string, (v: string) => void];
package/dist/index.js ADDED
@@ -0,0 +1,132 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ /**
4
+ * @proto-labs-inc/rig — the React adapter over @proto-labs-inc/rig-core. All rig logic
5
+ * (URL-state sync, the bridge, comment mode) lives in the core; this file
6
+ * is only the React idiom: a provider component + hooks.
7
+ */
8
+ import { createContext, useCallback, useContext, useEffect, useRef, useState, useSyncExternalStore, } from "react";
9
+ import { postToFrame, RigStore, wireRig, STATE_PARAM, variantParam, } from "@proto-labs-inc/rig-core";
10
+ import { isFrameToRig } from "@proto-labs-inc/wire";
11
+ const RigContext = createContext(null);
12
+ const PROTO_BLUE = "#0088FF";
13
+ function TargetHighlight({ focus, onDismiss, }) {
14
+ const [rect, setRect] = useState(null);
15
+ useEffect(() => {
16
+ let didScroll = false;
17
+ const update = () => {
18
+ const target = document.querySelector(`[data-proto-id="${CSS.escape(focus.id)}"]`);
19
+ if (!target) {
20
+ setRect(null);
21
+ postToFrame({
22
+ type: "target-measured",
23
+ targetId: focus.id,
24
+ anchor: null,
25
+ });
26
+ return;
27
+ }
28
+ if (!didScroll) {
29
+ target.scrollIntoView({
30
+ behavior: "smooth",
31
+ block: "center",
32
+ inline: "nearest",
33
+ });
34
+ didScroll = true;
35
+ }
36
+ const nextRect = target.getBoundingClientRect();
37
+ setRect(nextRect);
38
+ postToFrame({
39
+ type: "target-measured",
40
+ targetId: focus.id,
41
+ anchor: {
42
+ x: nextRect.left,
43
+ y: nextRect.top,
44
+ width: nextRect.width,
45
+ height: nextRect.height,
46
+ },
47
+ });
48
+ };
49
+ update();
50
+ const retry = window.setInterval(update, 50);
51
+ const stopRetry = window.setTimeout(() => window.clearInterval(retry), 400);
52
+ window.addEventListener("scroll", update, true);
53
+ window.addEventListener("resize", update);
54
+ // Dismiss before the prototype handles the interaction. A pointer-down can
55
+ // replace the focused target before a click is dispatched, which removes
56
+ // the visual highlight without giving the Frame a chance to clear its
57
+ // associated selection.
58
+ document.addEventListener("pointerdown", onDismiss, true);
59
+ return () => {
60
+ window.clearInterval(retry);
61
+ window.clearTimeout(stopRetry);
62
+ window.removeEventListener("scroll", update, true);
63
+ window.removeEventListener("resize", update);
64
+ document.removeEventListener("pointerdown", onDismiss, true);
65
+ };
66
+ }, [focus, onDismiss]);
67
+ if (!rect)
68
+ return null;
69
+ return (_jsx("div", { "aria-hidden": "true", "data-proto-target-highlight": true, style: {
70
+ position: "fixed",
71
+ zIndex: 2147483647,
72
+ pointerEvents: "none",
73
+ top: rect.top,
74
+ left: rect.left,
75
+ width: rect.width,
76
+ height: rect.height,
77
+ border: `2px solid color-mix(in srgb, ${PROTO_BLUE} 55%, transparent)`,
78
+ borderRadius: 9,
79
+ background: `color-mix(in srgb, ${PROTO_BLUE} 6%, transparent)`,
80
+ opacity: 1,
81
+ } }));
82
+ }
83
+ export function ProtoRig({ manifest, children, }) {
84
+ // One store per mount; survives re-renders, resets on full reload (correct:
85
+ // a reload re-hydrates from the URL, which is the source of truth).
86
+ const storeRef = useRef(null);
87
+ if (storeRef.current === null)
88
+ storeRef.current = new RigStore(manifest);
89
+ const store = storeRef.current;
90
+ const [targetFocus, setTargetFocus] = useState(null);
91
+ useEffect(() => {
92
+ const onMessage = (e) => {
93
+ if (!isFrameToRig(e.data) || e.data.type !== "focus-target")
94
+ return;
95
+ if (e.data.id === null) {
96
+ setTargetFocus(null);
97
+ return;
98
+ }
99
+ setTargetFocus((current) => ({
100
+ id: e.data.id,
101
+ token: (current?.token ?? 0) + 1,
102
+ }));
103
+ };
104
+ window.addEventListener("message", onMessage);
105
+ const dispose = wireRig(store, manifest);
106
+ return () => {
107
+ window.removeEventListener("message", onMessage);
108
+ dispose();
109
+ };
110
+ }, [store, manifest]);
111
+ return (_jsxs(RigContext.Provider, { value: store, children: [children, targetFocus ? (_jsx(TargetHighlight, { focus: targetFocus, onDismiss: () => {
112
+ setTargetFocus((current) => current?.token === targetFocus.token ? null : current);
113
+ postToFrame({ type: "target-focus-dismissed" });
114
+ } })) : null] }));
115
+ }
116
+ function useRigValue(key, fallback, valid) {
117
+ const store = useContext(RigContext);
118
+ if (!store)
119
+ throw new Error("useRig* hooks require a <ProtoRig> ancestor");
120
+ const raw = useSyncExternalStore(store.subscribe, () => store.get(key) ?? fallback, () => fallback);
121
+ const value = valid && !valid.includes(raw) ? fallback : raw;
122
+ const set = useCallback((v) => store.set(key, v), [store, key]);
123
+ return [value, set];
124
+ }
125
+ /** The prototype's preview state, addressable as ?state=<id>. */
126
+ export function usePreviewState(defaultId, validIds) {
127
+ return useRigValue(STATE_PARAM, defaultId, validIds);
128
+ }
129
+ /** A component's design variant, addressable as ?v.<component>=<id>. */
130
+ export function useVariant(component, defaultVariant, validIds) {
131
+ return useRigValue(variantParam(component), defaultVariant, validIds);
132
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@proto-labs-inc/rig",
3
+ "version": "0.1.0",
4
+ "description": "Proto's rig for React prototypes: a provider and hooks over @proto-labs-inc/rig-core.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/proto-labs-inc/proto.git",
9
+ "directory": "packages/rig"
10
+ },
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ },
17
+ "./package.json": "./package.json"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "peerDependencies": {
23
+ "react": ">=18"
24
+ },
25
+ "dependencies": {
26
+ "@proto-labs-inc/wire": "^0.1.0",
27
+ "@proto-labs-inc/rig-core": "^0.1.0"
28
+ },
29
+ "devDependencies": {
30
+ "typescript": "~6.0.2",
31
+ "react": "19.3.0",
32
+ "@types/react": "^19.2.14"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc -p tsconfig.build.json",
39
+ "typecheck": "tsc --noEmit"
40
+ },
41
+ "main": "./dist/index.js",
42
+ "types": "./dist/index.d.ts"
43
+ }