pollyroll 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/dist/react.js ADDED
@@ -0,0 +1,47 @@
1
+ 'use client';
2
+ import { useState, useRef, useEffect, useImperativeHandle, createElement } from 'react';
3
+ import { createDiceTray } from 'pollyroll/render';
4
+
5
+ // src/react/index.ts
6
+
7
+ // src/react/skin.ts
8
+ var skinChanged = (next, last) => JSON.stringify(next) !== JSON.stringify(last);
9
+
10
+ // src/react/index.ts
11
+ function useDiceTray(ref, opts) {
12
+ const [tray, setTray] = useState(null);
13
+ const optsRef = useRef(opts);
14
+ optsRef.current = opts;
15
+ const skinRef = useRef(opts?.skin);
16
+ useEffect(() => {
17
+ const el = ref.current;
18
+ if (!el) return;
19
+ const created = createDiceTray(el, optsRef.current);
20
+ skinRef.current = optsRef.current?.skin;
21
+ setTray(created);
22
+ return () => {
23
+ created.dispose();
24
+ setTray(null);
25
+ };
26
+ }, [ref]);
27
+ const skin = opts?.skin;
28
+ useEffect(() => {
29
+ if (!tray || skin === void 0 || !skinChanged(skin, skinRef.current)) return;
30
+ skinRef.current = skin;
31
+ tray.setSkin(skin);
32
+ }, [tray, skin]);
33
+ return tray;
34
+ }
35
+ function DiceTray(props) {
36
+ const { trayRef, className, style, ...opts } = props;
37
+ const ref = useRef(null);
38
+ const tray = useDiceTray(ref, opts);
39
+ useImperativeHandle(trayRef, () => tray, [tray]);
40
+ return createElement("div", {
41
+ ref,
42
+ className,
43
+ style: { position: "absolute", inset: 0, pointerEvents: "none", ...style }
44
+ });
45
+ }
46
+
47
+ export { DiceTray, useDiceTray };