@prosopo/procaptcha-puzzle 2.10.8 → 2.10.9

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,5 +1,5 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
2
- import { useCallback, useEffect, useRef, useState } from "react";
1
+ import { jsxs, jsx } from "@emotion/react/jsx-runtime";
2
+ import { useState, useRef, useEffect, useCallback } from "react";
3
3
  const CONTAINER_WIDTH = 300;
4
4
  const CONTAINER_HEIGHT = 200;
5
5
  const TARGET_SIZE = 30;
@@ -11,196 +11,265 @@ const SHAKE_KEYFRAMES = `
11
11
  20%, 40%, 60%, 80% { transform: translateX(4px); }
12
12
  }
13
13
  `;
14
- export const PuzzleCanvas = ({ originX, originY, targetX, targetY, onComplete, showRetry, submitting, }) => {
15
- const [posX, setPosX] = useState(originX);
16
- const [posY, setPosY] = useState(originY);
17
- const isDragging = useRef(false);
18
- const puzzleEvents = useRef([]);
19
- const containerRef = useRef(null);
20
- const offsetRef = useRef({ x: 0, y: 0 });
21
- const [visible, setVisible] = useState(false);
22
- const [shaking, setShaking] = useState(false);
23
- useEffect(() => {
24
- setPosX(originX);
25
- setPosY(originY);
26
- }, [originX, originY]);
27
- useEffect(() => {
28
- const frame = requestAnimationFrame(() => setVisible(true));
29
- return () => cancelAnimationFrame(frame);
30
- }, []);
31
- useEffect(() => {
32
- if (showRetry) {
33
- setShaking(true);
34
- const timer = setTimeout(() => setShaking(false), 500);
35
- return () => clearTimeout(timer);
36
- }
37
- return () => { };
38
- }, [showRetry]);
39
- const clamp = useCallback((value, min, max) => {
40
- return Math.max(min, Math.min(max, value));
41
- }, []);
42
- const getContainerOffset = useCallback(() => {
43
- if (containerRef.current) {
44
- const rect = containerRef.current.getBoundingClientRect();
45
- return { x: rect.left, y: rect.top };
46
- }
47
- return { x: 0, y: 0 };
48
- }, []);
49
- const handleMoveEvent = useCallback((clientX, clientY) => {
50
- if (!isDragging.current) {
51
- return;
52
- }
53
- const containerOffset = getContainerOffset();
54
- const newX = clamp(clientX - containerOffset.x - offsetRef.current.x, 0, CONTAINER_WIDTH);
55
- const newY = clamp(clientY - containerOffset.y - offsetRef.current.y, 0, CONTAINER_HEIGHT);
56
- setPosX(newX);
57
- setPosY(newY);
58
- puzzleEvents.current.push({ x: newX, y: newY, t: Date.now() });
59
- }, [clamp, getContainerOffset]);
60
- const handleEndEvent = useCallback(() => {
61
- if (!isDragging.current) {
62
- return;
63
- }
64
- isDragging.current = false;
65
- const currentEvents = [...puzzleEvents.current];
66
- const lastEvent = currentEvents[currentEvents.length - 1];
67
- const finalX = lastEvent ? lastEvent.x : originX;
68
- const finalY = lastEvent ? lastEvent.y : originY;
69
- onComplete(finalX, finalY, currentEvents);
70
- }, [onComplete, originX, originY]);
71
- const handleMouseMove = useCallback((event) => {
72
- handleMoveEvent(event.clientX, event.clientY);
73
- }, [handleMoveEvent]);
74
- const handleTouchMove = useCallback((event) => {
75
- const touch = event.touches[0];
76
- if (touch) {
77
- handleMoveEvent(touch.clientX, touch.clientY);
78
- }
79
- }, [handleMoveEvent]);
80
- const handleMouseUp = useCallback(() => {
81
- handleEndEvent();
82
- }, [handleEndEvent]);
83
- const handleTouchEnd = useCallback(() => {
84
- handleEndEvent();
85
- }, [handleEndEvent]);
86
- useEffect(() => {
87
- document.addEventListener("mousemove", handleMouseMove);
88
- document.addEventListener("mouseup", handleMouseUp);
89
- document.addEventListener("touchmove", handleTouchMove);
90
- document.addEventListener("touchend", handleTouchEnd);
91
- return () => {
92
- document.removeEventListener("mousemove", handleMouseMove);
93
- document.removeEventListener("mouseup", handleMouseUp);
94
- document.removeEventListener("touchmove", handleTouchMove);
95
- document.removeEventListener("touchend", handleTouchEnd);
96
- };
97
- }, [handleMouseMove, handleMouseUp, handleTouchMove, handleTouchEnd]);
98
- const handlePieceMouseDown = useCallback((event) => {
99
- if (submitting)
100
- return;
14
+ const PuzzleCanvas = ({
15
+ originX,
16
+ originY,
17
+ targetX,
18
+ targetY,
19
+ onComplete,
20
+ showRetry,
21
+ submitting
22
+ }) => {
23
+ const [posX, setPosX] = useState(originX);
24
+ const [posY, setPosY] = useState(originY);
25
+ const isDragging = useRef(false);
26
+ const puzzleEvents = useRef([]);
27
+ const containerRef = useRef(null);
28
+ const offsetRef = useRef({ x: 0, y: 0 });
29
+ const [visible, setVisible] = useState(false);
30
+ const [shaking, setShaking] = useState(false);
31
+ useEffect(() => {
32
+ setPosX(originX);
33
+ setPosY(originY);
34
+ }, [originX, originY]);
35
+ useEffect(() => {
36
+ const frame = requestAnimationFrame(() => setVisible(true));
37
+ return () => cancelAnimationFrame(frame);
38
+ }, []);
39
+ useEffect(() => {
40
+ if (showRetry) {
41
+ setShaking(true);
42
+ const timer = setTimeout(() => setShaking(false), 500);
43
+ return () => clearTimeout(timer);
44
+ }
45
+ return () => {
46
+ };
47
+ }, [showRetry]);
48
+ const clamp = useCallback(
49
+ (value, min, max) => {
50
+ return Math.max(min, Math.min(max, value));
51
+ },
52
+ []
53
+ );
54
+ const getContainerOffset = useCallback(() => {
55
+ if (containerRef.current) {
56
+ const rect = containerRef.current.getBoundingClientRect();
57
+ return { x: rect.left, y: rect.top };
58
+ }
59
+ return { x: 0, y: 0 };
60
+ }, []);
61
+ const handleMoveEvent = useCallback(
62
+ (clientX, clientY) => {
63
+ if (!isDragging.current) {
64
+ return;
65
+ }
66
+ const containerOffset = getContainerOffset();
67
+ const newX = clamp(
68
+ clientX - containerOffset.x - offsetRef.current.x,
69
+ 0,
70
+ CONTAINER_WIDTH
71
+ );
72
+ const newY = clamp(
73
+ clientY - containerOffset.y - offsetRef.current.y,
74
+ 0,
75
+ CONTAINER_HEIGHT
76
+ );
77
+ setPosX(newX);
78
+ setPosY(newY);
79
+ puzzleEvents.current.push({ x: newX, y: newY, t: Date.now() });
80
+ },
81
+ [clamp, getContainerOffset]
82
+ );
83
+ const handleEndEvent = useCallback(() => {
84
+ if (!isDragging.current) {
85
+ return;
86
+ }
87
+ isDragging.current = false;
88
+ const currentEvents = [...puzzleEvents.current];
89
+ const lastEvent = currentEvents[currentEvents.length - 1];
90
+ const finalX = lastEvent ? lastEvent.x : originX;
91
+ const finalY = lastEvent ? lastEvent.y : originY;
92
+ onComplete(finalX, finalY, currentEvents);
93
+ }, [onComplete, originX, originY]);
94
+ const handleMouseMove = useCallback(
95
+ (event) => {
96
+ handleMoveEvent(event.clientX, event.clientY);
97
+ },
98
+ [handleMoveEvent]
99
+ );
100
+ const handleTouchMove = useCallback(
101
+ (event) => {
102
+ const touch = event.touches[0];
103
+ if (touch) {
104
+ handleMoveEvent(touch.clientX, touch.clientY);
105
+ }
106
+ },
107
+ [handleMoveEvent]
108
+ );
109
+ const handleMouseUp = useCallback(() => {
110
+ handleEndEvent();
111
+ }, [handleEndEvent]);
112
+ const handleTouchEnd = useCallback(() => {
113
+ handleEndEvent();
114
+ }, [handleEndEvent]);
115
+ useEffect(() => {
116
+ document.addEventListener("mousemove", handleMouseMove);
117
+ document.addEventListener("mouseup", handleMouseUp);
118
+ document.addEventListener("touchmove", handleTouchMove);
119
+ document.addEventListener("touchend", handleTouchEnd);
120
+ return () => {
121
+ document.removeEventListener("mousemove", handleMouseMove);
122
+ document.removeEventListener("mouseup", handleMouseUp);
123
+ document.removeEventListener("touchmove", handleTouchMove);
124
+ document.removeEventListener("touchend", handleTouchEnd);
125
+ };
126
+ }, [handleMouseMove, handleMouseUp, handleTouchMove, handleTouchEnd]);
127
+ const handlePieceMouseDown = useCallback(
128
+ (event) => {
129
+ if (submitting) return;
130
+ isDragging.current = true;
131
+ puzzleEvents.current = [];
132
+ const containerOffset = getContainerOffset();
133
+ offsetRef.current = {
134
+ x: event.clientX - containerOffset.x - posX,
135
+ y: event.clientY - containerOffset.y - posY
136
+ };
137
+ },
138
+ [getContainerOffset, posX, posY, submitting]
139
+ );
140
+ const handlePieceTouchStart = useCallback(
141
+ (event) => {
142
+ if (submitting) return;
143
+ const touch = event.touches[0];
144
+ if (touch) {
101
145
  isDragging.current = true;
102
146
  puzzleEvents.current = [];
103
147
  const containerOffset = getContainerOffset();
104
148
  offsetRef.current = {
105
- x: event.clientX - containerOffset.x - posX,
106
- y: event.clientY - containerOffset.y - posY,
149
+ x: touch.clientX - containerOffset.x - posX,
150
+ y: touch.clientY - containerOffset.y - posY
107
151
  };
108
- }, [getContainerOffset, posX, posY, submitting]);
109
- const handlePieceTouchStart = useCallback((event) => {
110
- if (submitting)
111
- return;
112
- const touch = event.touches[0];
113
- if (touch) {
114
- isDragging.current = true;
115
- puzzleEvents.current = [];
116
- const containerOffset = getContainerOffset();
117
- offsetRef.current = {
118
- x: touch.clientX - containerOffset.x - posX,
119
- y: touch.clientY - containerOffset.y - posY,
120
- };
121
- }
122
- }, [getContainerOffset, posX, posY, submitting]);
123
- const instructionText = showRetry
124
- ? "Not quite \u2014 try again"
125
- : "Drag the piece to the target";
126
- const headerBorderColor = showRetry
127
- ? "rgba(220, 53, 69, 0.4)"
128
- : "transparent";
129
- const headerTextColor = showRetry ? "#dc3545" : "#333";
130
- return (_jsxs("div", { style: {
131
- position: "fixed",
132
- inset: 0,
133
- zIndex: 2147483646,
134
- display: "flex",
135
- alignItems: "center",
136
- justifyContent: "center",
137
- backgroundColor: visible ? "rgba(0, 0, 0, 0.4)" : "rgba(0, 0, 0, 0)",
138
- transition: "background-color 0.3s ease",
139
- }, children: [_jsx("style", { children: SHAKE_KEYFRAMES }), _jsxs("div", { style: {
140
- display: "flex",
141
- flexDirection: "column",
142
- alignItems: "center",
143
- gap: "0",
144
- zIndex: 2147483647,
145
- opacity: visible ? 1 : 0,
146
- transform: visible ? "scale(1)" : "scale(0.9)",
147
- transition: "opacity 0.3s ease, transform 0.3s ease",
148
- animation: shaking ? "prosopo-puzzle-shake 0.5s ease" : "none",
149
- }, children: [_jsx("div", { style: {
150
- backgroundColor: "#fff",
151
- borderRadius: "8px 8px 0 0",
152
- padding: "12px 20px",
153
- width: `${CONTAINER_WIDTH}px`,
154
- boxSizing: "border-box",
155
- textAlign: "center",
156
- fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
157
- fontSize: "14px",
158
- fontWeight: 500,
159
- color: headerTextColor,
160
- borderBottom: `2px solid ${headerBorderColor}`,
161
- boxShadow: "0px 11px 15px -7px rgba(0,0,0,0.2), 0px 24px 38px 3px rgba(0,0,0,0.14), 0px 9px 46px 8px rgba(0,0,0,0.12)",
162
- transition: "color 0.3s ease, border-color 0.3s ease",
163
- }, children: instructionText }), _jsxs("div", { ref: containerRef, style: {
164
- position: "relative",
165
- width: `${CONTAINER_WIDTH}px`,
166
- height: `${CONTAINER_HEIGHT}px`,
167
- background: "linear-gradient(135deg, #e8eaf6 0%, #c5cae9 50%, #e8eaf6 100%)",
168
- borderRadius: "0 0 8px 8px",
169
- overflow: "hidden",
170
- userSelect: "none",
171
- boxShadow: "0px 11px 15px -7px rgba(0,0,0,0.2), 0px 24px 38px 3px rgba(0,0,0,0.14), 0px 9px 46px 8px rgba(0,0,0,0.12)",
172
- opacity: submitting ? 0.6 : 1,
173
- pointerEvents: submitting ? "none" : "auto",
174
- transition: "opacity 0.2s ease",
175
- }, children: [_jsx("div", { style: {
176
- position: "absolute",
177
- left: `${targetX - TARGET_SIZE / 2}px`,
178
- top: `${targetY - TARGET_SIZE / 2}px`,
179
- width: `${TARGET_SIZE}px`,
180
- height: `${TARGET_SIZE}px`,
181
- borderRadius: "50%",
182
- border: "2px dashed rgba(74, 144, 217, 0.6)",
183
- backgroundColor: "rgba(74, 144, 217, 0.08)",
184
- boxSizing: "border-box",
185
- } }), _jsx("div", { onMouseDown: handlePieceMouseDown, onTouchStart: handlePieceTouchStart, style: {
186
- position: "absolute",
187
- left: `${posX - PIECE_SIZE / 2}px`,
188
- top: `${posY - PIECE_SIZE / 2}px`,
189
- width: `${PIECE_SIZE}px`,
190
- height: `${PIECE_SIZE}px`,
191
- borderRadius: "50%",
192
- background: "radial-gradient(circle at 40% 40%, #6ab0ff, #4a90d9)",
193
- cursor: submitting
194
- ? "default"
195
- : isDragging.current
196
- ? "grabbing"
197
- : "grab",
198
- boxShadow: isDragging.current
199
- ? "0 4px 12px rgba(74, 144, 217, 0.5)"
200
- : "0 2px 6px rgba(74, 144, 217, 0.3)",
201
- transition: isDragging.current
202
- ? "none"
203
- : "box-shadow 0.2s ease, left 0.3s ease, top 0.3s ease",
204
- } })] })] })] }));
152
+ }
153
+ },
154
+ [getContainerOffset, posX, posY, submitting]
155
+ );
156
+ const instructionText = showRetry ? "Not quite — try again" : "Drag the piece to the target";
157
+ const headerBorderColor = showRetry ? "rgba(220, 53, 69, 0.4)" : "transparent";
158
+ const headerTextColor = showRetry ? "#dc3545" : "#333";
159
+ return /* @__PURE__ */ jsxs(
160
+ "div",
161
+ {
162
+ style: {
163
+ position: "fixed",
164
+ inset: 0,
165
+ zIndex: 2147483646,
166
+ display: "flex",
167
+ alignItems: "center",
168
+ justifyContent: "center",
169
+ backgroundColor: visible ? "rgba(0, 0, 0, 0.4)" : "rgba(0, 0, 0, 0)",
170
+ transition: "background-color 0.3s ease"
171
+ },
172
+ children: [
173
+ /* @__PURE__ */ jsx("style", { children: SHAKE_KEYFRAMES }),
174
+ /* @__PURE__ */ jsxs(
175
+ "div",
176
+ {
177
+ style: {
178
+ display: "flex",
179
+ flexDirection: "column",
180
+ alignItems: "center",
181
+ gap: "0",
182
+ zIndex: 2147483647,
183
+ opacity: visible ? 1 : 0,
184
+ transform: visible ? "scale(1)" : "scale(0.9)",
185
+ transition: "opacity 0.3s ease, transform 0.3s ease",
186
+ animation: shaking ? "prosopo-puzzle-shake 0.5s ease" : "none"
187
+ },
188
+ children: [
189
+ /* @__PURE__ */ jsx(
190
+ "div",
191
+ {
192
+ style: {
193
+ backgroundColor: "#fff",
194
+ borderRadius: "8px 8px 0 0",
195
+ padding: "12px 20px",
196
+ width: `${CONTAINER_WIDTH}px`,
197
+ boxSizing: "border-box",
198
+ textAlign: "center",
199
+ fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
200
+ fontSize: "14px",
201
+ fontWeight: 500,
202
+ color: headerTextColor,
203
+ borderBottom: `2px solid ${headerBorderColor}`,
204
+ boxShadow: "0px 11px 15px -7px rgba(0,0,0,0.2), 0px 24px 38px 3px rgba(0,0,0,0.14), 0px 9px 46px 8px rgba(0,0,0,0.12)",
205
+ transition: "color 0.3s ease, border-color 0.3s ease"
206
+ },
207
+ children: instructionText
208
+ }
209
+ ),
210
+ /* @__PURE__ */ jsxs(
211
+ "div",
212
+ {
213
+ ref: containerRef,
214
+ style: {
215
+ position: "relative",
216
+ width: `${CONTAINER_WIDTH}px`,
217
+ height: `${CONTAINER_HEIGHT}px`,
218
+ background: "linear-gradient(135deg, #e8eaf6 0%, #c5cae9 50%, #e8eaf6 100%)",
219
+ borderRadius: "0 0 8px 8px",
220
+ overflow: "hidden",
221
+ userSelect: "none",
222
+ boxShadow: "0px 11px 15px -7px rgba(0,0,0,0.2), 0px 24px 38px 3px rgba(0,0,0,0.14), 0px 9px 46px 8px rgba(0,0,0,0.12)",
223
+ opacity: submitting ? 0.6 : 1,
224
+ pointerEvents: submitting ? "none" : "auto",
225
+ transition: "opacity 0.2s ease"
226
+ },
227
+ children: [
228
+ /* @__PURE__ */ jsx(
229
+ "div",
230
+ {
231
+ style: {
232
+ position: "absolute",
233
+ left: `${targetX - TARGET_SIZE / 2}px`,
234
+ top: `${targetY - TARGET_SIZE / 2}px`,
235
+ width: `${TARGET_SIZE}px`,
236
+ height: `${TARGET_SIZE}px`,
237
+ borderRadius: "50%",
238
+ border: "2px dashed rgba(74, 144, 217, 0.6)",
239
+ backgroundColor: "rgba(74, 144, 217, 0.08)",
240
+ boxSizing: "border-box"
241
+ }
242
+ }
243
+ ),
244
+ /* @__PURE__ */ jsx(
245
+ "div",
246
+ {
247
+ onMouseDown: handlePieceMouseDown,
248
+ onTouchStart: handlePieceTouchStart,
249
+ style: {
250
+ position: "absolute",
251
+ left: `${posX - PIECE_SIZE / 2}px`,
252
+ top: `${posY - PIECE_SIZE / 2}px`,
253
+ width: `${PIECE_SIZE}px`,
254
+ height: `${PIECE_SIZE}px`,
255
+ borderRadius: "50%",
256
+ background: "radial-gradient(circle at 40% 40%, #6ab0ff, #4a90d9)",
257
+ cursor: submitting ? "default" : isDragging.current ? "grabbing" : "grab",
258
+ boxShadow: isDragging.current ? "0 4px 12px rgba(74, 144, 217, 0.5)" : "0 2px 6px rgba(74, 144, 217, 0.3)",
259
+ transition: isDragging.current ? "none" : "box-shadow 0.2s ease, left 0.3s ease, top 0.3s ease"
260
+ }
261
+ }
262
+ )
263
+ ]
264
+ }
265
+ )
266
+ ]
267
+ }
268
+ )
269
+ ]
270
+ }
271
+ );
272
+ };
273
+ export {
274
+ PuzzleCanvas
205
275
  };
206
- //# sourceMappingURL=PuzzleCanvas.js.map
package/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
- export * from "./components/ProcaptchaWidget.js";
2
- export * from "./components/ProcaptchaPuzzle.js";
3
- //# sourceMappingURL=index.js.map
1
+ import "./components/ProcaptchaWidget.js";
2
+ import { ProcaptchaPuzzle } from "./components/ProcaptchaPuzzle.js";
3
+ export {
4
+ ProcaptchaPuzzle
5
+ };