@pothooks/react 0.0.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 +21 -0
- package/README.md +55 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +100 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pothooks
|
|
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,55 @@
|
|
|
1
|
+
# @pothooks/react
|
|
2
|
+
|
|
3
|
+
React component for replaying handwriting recorded by
|
|
4
|
+
[Pothooks](https://pothooks.com). Wraps
|
|
5
|
+
[`@pothooks/core`](https://github.com/danbillson/pothooks/tree/main/packages/core).
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @pothooks/react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { Handwriting } from '@pothooks/react';
|
|
13
|
+
|
|
14
|
+
<Handwriting payload={payload} trigger="visible" speed={1} loop color="#111" />
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Renders the finished SVG on the server, then animates after hydration — never a
|
|
18
|
+
blank first paint, and never a blank box under `prefers-reduced-motion`.
|
|
19
|
+
|
|
20
|
+
## Props
|
|
21
|
+
|
|
22
|
+
| Prop | Default | Meaning |
|
|
23
|
+
| --- | --- | --- |
|
|
24
|
+
| `payload` | — | The exported Pothooks payload. |
|
|
25
|
+
| `trigger` | `"visible"` | `"mount"`, `"visible"`, `"hover"` or `"manual"`. |
|
|
26
|
+
| `speed` | `1` | `1` is the recorded cadence. |
|
|
27
|
+
| `loop` | `false` | `true`, or ms to pause between loops. |
|
|
28
|
+
| `startDelay` | `0` | Ms before the first stroke. |
|
|
29
|
+
| `color` | inherits | Ink colour. The ink is `currentColor`, so this just sets `color`. |
|
|
30
|
+
| `title` | `payload.meta.text` | Accessible name. Without either, the SVG is decorative. |
|
|
31
|
+
| `reducedMotion` | `"respect"` | `"ignore"` only if the motion *is* the content. |
|
|
32
|
+
| `onDone` | — | Fires each time the piece finishes, including on every loop. |
|
|
33
|
+
|
|
34
|
+
Any other SVG prop (`className`, `style`, `width`, …) passes through to the
|
|
35
|
+
root `<svg>`.
|
|
36
|
+
|
|
37
|
+
`trigger="visible"` uses an `IntersectionObserver`; it starts playback the first
|
|
38
|
+
time the piece is on screen and then keeps feeding the pause gate, so scrolling
|
|
39
|
+
away stops the loop rather than leaving it burning frames.
|
|
40
|
+
|
|
41
|
+
## Manual control
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
const playback = useRef<Playback>(null);
|
|
45
|
+
|
|
46
|
+
<Handwriting ref={playback} payload={payload} trigger="manual" />
|
|
47
|
+
<button onClick={() => playback.current?.restart()}>Replay</button>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The ref is a stable [`Playback`][playback] facade — it keeps working when a
|
|
51
|
+
prop change rebuilds the underlying loop.
|
|
52
|
+
|
|
53
|
+
[playback]: https://github.com/danbillson/pothooks/tree/main/packages/core#playsvg-opts-playback
|
|
54
|
+
|
|
55
|
+
`react` is a peer dependency (>=18).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CSSProperties, SVGProps } from "react";
|
|
2
|
+
import { Payload, Payload as Payload$1, Playback, Playback as Playback$1, Stroke } from "@pothooks/core";
|
|
3
|
+
//#region src/Handwriting.d.ts
|
|
4
|
+
/** How playback starts. `"manual"` waits for a call on the forwarded ref. */
|
|
5
|
+
type Trigger = 'mount' | 'visible' | 'hover' | 'manual';
|
|
6
|
+
interface HandwritingProps extends Omit<SVGProps<SVGSVGElement>, 'ref' | 'children' | 'color' | 'dangerouslySetInnerHTML'> {
|
|
7
|
+
payload: Payload$1;
|
|
8
|
+
/** 1 = the recorded cadence. */
|
|
9
|
+
speed?: number;
|
|
10
|
+
/** `true` loops immediately; a number loops after that many ms. */
|
|
11
|
+
loop?: boolean | number;
|
|
12
|
+
startDelay?: number;
|
|
13
|
+
trigger?: Trigger;
|
|
14
|
+
/** CSS colour for the ink. Defaults to inheriting `currentColor`. */
|
|
15
|
+
color?: string;
|
|
16
|
+
/** Accessible name. Defaults to `payload.meta.text`. */
|
|
17
|
+
title?: string;
|
|
18
|
+
reducedMotion?: 'respect' | 'ignore';
|
|
19
|
+
/** Fires each time the piece finishes drawing, including on every loop. */
|
|
20
|
+
onDone?: () => void;
|
|
21
|
+
style?: CSSProperties;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Renders the finished SVG — on the server too — then animates after
|
|
25
|
+
* hydration. There is never a blank first paint.
|
|
26
|
+
*/
|
|
27
|
+
export declare const Handwriting: import("react").ForwardRefExoticComponent<HandwritingProps & import("react").RefAttributes<Playback$1 | null>>;
|
|
28
|
+
//#endregion
|
|
29
|
+
export type { HandwritingProps, Payload, Playback, Stroke, Trigger };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { forwardRef, useCallback, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useRef } from "react";
|
|
2
|
+
import { play, renderMarkup } from "@pothooks/core";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
//#region src/Handwriting.tsx
|
|
5
|
+
const useIsomorphicLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
6
|
+
/**
|
|
7
|
+
* Renders the finished SVG — on the server too — then animates after
|
|
8
|
+
* hydration. There is never a blank first paint.
|
|
9
|
+
*/
|
|
10
|
+
const Handwriting = forwardRef(function Handwriting({ payload, speed = 1, loop = false, startDelay, trigger = "visible", color, title, reducedMotion, onDone, style, onMouseEnter, onFocus, ...rest }, ref) {
|
|
11
|
+
const svgRef = useRef(null);
|
|
12
|
+
const playbackRef = useRef(null);
|
|
13
|
+
const idPrefix = useId();
|
|
14
|
+
const { attrs, inner } = useMemo(() => renderMarkup(payload, {
|
|
15
|
+
idPrefix,
|
|
16
|
+
title
|
|
17
|
+
}), [
|
|
18
|
+
payload,
|
|
19
|
+
idPrefix,
|
|
20
|
+
title
|
|
21
|
+
]);
|
|
22
|
+
const onDoneRef = useRef(onDone);
|
|
23
|
+
onDoneRef.current = onDone;
|
|
24
|
+
useIsomorphicLayoutEffect(() => {
|
|
25
|
+
const svg = svgRef.current;
|
|
26
|
+
if (!svg) return;
|
|
27
|
+
const playback = play(svg, {
|
|
28
|
+
speed,
|
|
29
|
+
loop,
|
|
30
|
+
startDelay,
|
|
31
|
+
reducedMotion,
|
|
32
|
+
autoplay: trigger === "mount",
|
|
33
|
+
onDone: () => onDoneRef.current?.()
|
|
34
|
+
});
|
|
35
|
+
playbackRef.current = playback;
|
|
36
|
+
let started = trigger !== "visible";
|
|
37
|
+
let observer;
|
|
38
|
+
if (typeof IntersectionObserver !== "undefined") {
|
|
39
|
+
observer = new IntersectionObserver((entries) => {
|
|
40
|
+
const visible = entries.some((entry) => entry.isIntersecting);
|
|
41
|
+
if (visible && !started) {
|
|
42
|
+
started = true;
|
|
43
|
+
playback.play();
|
|
44
|
+
}
|
|
45
|
+
playback.setInView(visible);
|
|
46
|
+
}, { threshold: .01 });
|
|
47
|
+
observer.observe(svg);
|
|
48
|
+
} else if (trigger === "visible") playback.play();
|
|
49
|
+
return () => {
|
|
50
|
+
observer?.disconnect();
|
|
51
|
+
playback.destroy();
|
|
52
|
+
playbackRef.current = null;
|
|
53
|
+
};
|
|
54
|
+
}, [
|
|
55
|
+
speed,
|
|
56
|
+
loop,
|
|
57
|
+
startDelay,
|
|
58
|
+
trigger,
|
|
59
|
+
reducedMotion,
|
|
60
|
+
inner
|
|
61
|
+
]);
|
|
62
|
+
const facade = useMemo(() => ({
|
|
63
|
+
play: () => playbackRef.current?.play(),
|
|
64
|
+
pause: () => playbackRef.current?.pause(),
|
|
65
|
+
restart: () => playbackRef.current?.restart(),
|
|
66
|
+
seek: (t) => playbackRef.current?.seek(t),
|
|
67
|
+
setInView: (v) => playbackRef.current?.setInView(v),
|
|
68
|
+
destroy: () => playbackRef.current?.destroy(),
|
|
69
|
+
get duration() {
|
|
70
|
+
return playbackRef.current?.duration ?? 0;
|
|
71
|
+
},
|
|
72
|
+
get playing() {
|
|
73
|
+
return playbackRef.current?.playing ?? false;
|
|
74
|
+
}
|
|
75
|
+
}), []);
|
|
76
|
+
useImperativeHandle(ref, () => facade, [facade]);
|
|
77
|
+
const startOnHover = useCallback(() => {
|
|
78
|
+
if (trigger === "hover") playbackRef.current?.restart();
|
|
79
|
+
}, [trigger]);
|
|
80
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
81
|
+
...attrs,
|
|
82
|
+
...rest,
|
|
83
|
+
ref: svgRef,
|
|
84
|
+
style: color ? {
|
|
85
|
+
...style,
|
|
86
|
+
color
|
|
87
|
+
} : style,
|
|
88
|
+
onMouseEnter: (event) => {
|
|
89
|
+
startOnHover();
|
|
90
|
+
onMouseEnter?.(event);
|
|
91
|
+
},
|
|
92
|
+
onFocus: (event) => {
|
|
93
|
+
startOnHover();
|
|
94
|
+
onFocus?.(event);
|
|
95
|
+
},
|
|
96
|
+
dangerouslySetInnerHTML: { __html: inner }
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
//#endregion
|
|
100
|
+
export { Handwriting };
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pothooks/react",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Animate handwriting on the web — React component",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/danbillson/pothooks.git",
|
|
25
|
+
"directory": "packages/react"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/danbillson/pothooks/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://pothooks.com",
|
|
31
|
+
"keywords": [
|
|
32
|
+
"handwriting",
|
|
33
|
+
"signature",
|
|
34
|
+
"animation",
|
|
35
|
+
"svg",
|
|
36
|
+
"react",
|
|
37
|
+
"pothooks"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@pothooks/core": "^0.0.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"react": ">=18"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@testing-library/react": "^16.3.3",
|
|
47
|
+
"@types/react": "^19.2.18",
|
|
48
|
+
"@types/react-dom": "^19.2.7",
|
|
49
|
+
"happy-dom": "^20.14.0",
|
|
50
|
+
"react": "^19.2.8",
|
|
51
|
+
"react-dom": "^19.2.8",
|
|
52
|
+
"tsdown": "^0.23.0",
|
|
53
|
+
"typescript": "^7.0.2",
|
|
54
|
+
"vitest": "^5.0.0"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsdown",
|
|
58
|
+
"test": "vitest run",
|
|
59
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
60
|
+
"lint": "oxlint src test"
|
|
61
|
+
}
|
|
62
|
+
}
|