@prtcl/plonk-hooks 1.0.0-alpha.2

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,3 @@
1
+ # @prtcl/plonk-hooks
2
+
3
+ React hook wrappers for [plonk](https://github.com/prtcl/plonk)
@@ -0,0 +1,3 @@
1
+ import 'client-only';
2
+ export { default as useFrames } from './useFrames';
3
+ export { default as useMetro, type UseMetroOptions } from './useMetro';
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ 'use client';
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.useMetro = exports.useFrames = void 0;
8
+ require("client-only");
9
+ var useFrames_1 = require("./useFrames");
10
+ Object.defineProperty(exports, "useFrames", { enumerable: true, get: function () { return __importDefault(useFrames_1).default; } });
11
+ var useMetro_1 = require("./useMetro");
12
+ Object.defineProperty(exports, "useMetro", { enumerable: true, get: function () { return __importDefault(useMetro_1).default; } });
@@ -0,0 +1,9 @@
1
+ import { Frames, type FramesOptions, type TimerCallback } from '@prtcl/plonk-core';
2
+ export type UseFramesOptions = FramesOptions & {
3
+ autostart?: boolean;
4
+ };
5
+ /**
6
+ * Hook wrapper for Frames which provides an animation loop with variable frame rate
7
+ */
8
+ declare const useFrames: (callback: TimerCallback<Frames>, opts?: UseFramesOptions) => Frames;
9
+ export default useFrames;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = require("react");
7
+ const plonk_core_1 = require("@prtcl/plonk-core");
8
+ const usePrevious_1 = __importDefault(require("./usePrevious"));
9
+ /**
10
+ * Hook wrapper for Frames which provides an animation loop with variable frame rate
11
+ */
12
+ const useFrames = (callback, opts) => {
13
+ const { autostart = true } = opts || {};
14
+ const callbackRef = (0, react_1.useRef)(callback);
15
+ const optsRef = (0, react_1.useRef)(opts);
16
+ const prevOpts = (0, usePrevious_1.default)(opts);
17
+ callbackRef.current = callback;
18
+ const frames = (0, react_1.useMemo)(() => {
19
+ return new plonk_core_1.Frames((m) => {
20
+ callbackRef.current(m);
21
+ }, optsRef.current);
22
+ }, []);
23
+ (0, react_1.useEffect)(() => {
24
+ if (opts && prevOpts && opts.fps !== prevOpts.fps) {
25
+ frames.setFPS(opts.fps);
26
+ }
27
+ }, [opts, prevOpts, frames]);
28
+ (0, react_1.useEffect)(() => {
29
+ if (autostart) {
30
+ frames.run();
31
+ }
32
+ return () => {
33
+ frames.stop();
34
+ };
35
+ }, [frames]);
36
+ return frames;
37
+ };
38
+ exports.default = useFrames;
@@ -0,0 +1,9 @@
1
+ import { Metro, type MetroOptions, type TimerCallback } from '@prtcl/plonk-core';
2
+ export type UseMetroOptions = MetroOptions & {
3
+ autostart?: boolean;
4
+ };
5
+ /**
6
+ * Hook wrapper for Metro which provides a timer loop with variable interval.
7
+ */
8
+ declare const useMetro: (callback: TimerCallback<Metro>, opts?: UseMetroOptions) => Metro;
9
+ export default useMetro;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = require("react");
7
+ const plonk_core_1 = require("@prtcl/plonk-core");
8
+ const usePrevious_1 = __importDefault(require("./usePrevious"));
9
+ /**
10
+ * Hook wrapper for Metro which provides a timer loop with variable interval.
11
+ */
12
+ const useMetro = (callback, opts) => {
13
+ const { autostart = true } = opts || {};
14
+ const callbackRef = (0, react_1.useRef)(callback);
15
+ const optsRef = (0, react_1.useRef)(opts);
16
+ const prevOpts = (0, usePrevious_1.default)(opts);
17
+ callbackRef.current = callback;
18
+ const metro = (0, react_1.useMemo)(() => {
19
+ return new plonk_core_1.Metro((m) => {
20
+ callbackRef.current(m);
21
+ }, optsRef.current);
22
+ }, []);
23
+ (0, react_1.useEffect)(() => {
24
+ if (opts && prevOpts && opts.time !== prevOpts.time) {
25
+ metro.setTime(opts.time);
26
+ }
27
+ }, [opts, prevOpts, metro]);
28
+ (0, react_1.useEffect)(() => {
29
+ if (autostart) {
30
+ metro.run();
31
+ }
32
+ return () => {
33
+ metro.stop();
34
+ };
35
+ }, [metro]);
36
+ return metro;
37
+ };
38
+ exports.default = useMetro;
@@ -0,0 +1,3 @@
1
+ /** Returns the previous value without causing additional updates. */
2
+ declare const usePrevious: <Value>(value: Value) => Value | undefined;
3
+ export default usePrevious;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ /** Returns the previous value without causing additional updates. */
5
+ const usePrevious = (value) => {
6
+ const ref = (0, react_1.useRef)({
7
+ value,
8
+ prev: undefined,
9
+ });
10
+ const current = ref.current.value;
11
+ if (value !== current) {
12
+ ref.current = {
13
+ value,
14
+ prev: current,
15
+ };
16
+ }
17
+ return ref.current.prev;
18
+ };
19
+ exports.default = usePrevious;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@prtcl/plonk-hooks",
3
+ "version": "1.0.0-alpha.2",
4
+ "description": "React hook wrappers for plonk",
5
+ "author": "Cory O'Brien <cory@prtcl.cc>",
6
+ "license": "MIT",
7
+ "main": "dist/index.js",
8
+ "exports": {
9
+ ".": "./dist/index.js"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/prtcl/plonk.git"
14
+ },
15
+ "scripts": {
16
+ "build": "npx tsc",
17
+ "clean": "rm -rf dist | true",
18
+ "test": "npx jest --env=jsdom"
19
+ },
20
+ "dependencies": {
21
+ "@prtcl/plonk-core": "^1.0.0-alpha.2",
22
+ "client-only": "^0.0.1"
23
+ },
24
+ "peerDependencies": {
25
+ "react": "17 - 18"
26
+ },
27
+ "devDependencies": {
28
+ "@testing-library/react": "^14.2.2",
29
+ "@types/jest": "^29.5.12",
30
+ "@types/node": "^20.12.3",
31
+ "@types/react": "^18.2.74",
32
+ "jest": "^29.7.0",
33
+ "jest-environment-jsdom": "^29.7.0",
34
+ "react": "^18.2.0",
35
+ "ts-jest": "^29.1.2",
36
+ "typescript": "^5.4.3"
37
+ }
38
+ }