biometry-react-components 1.3.3 → 1.3.5

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/index.esm.js CHANGED
@@ -1,19 +1,60 @@
1
1
  import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,useEffect}from'react';const DocScan = ({ rectWidth = 640, rectHeight = 400, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
2
+ var _a;
2
3
  const [isConfirming, setIsConfirming] = useState(false);
3
4
  const [capturedUrl, setCapturedUrl] = useState(null);
4
5
  const [capturedFile, setCapturedFile] = useState(null);
5
6
  const videoRef = useRef(null);
6
7
  const streamRef = useRef(null);
8
+ const [devices, setDevices] = useState([]);
9
+ const [selectedDeviceId, setSelectedDeviceId] = useState(null);
10
+ const [isFrontCamera, setIsFrontCamera] = useState(false);
11
+ const [capabilities, setCapabilities] = useState({});
12
+ const [torchOn, setTorchOn] = useState(false);
7
13
  const QUALITY_MULTIPLIER = 3;
8
14
  // Crop area (dashed area) as percentages
9
15
  const cropTop = 0.075, cropLeft = 0.075, cropWidth = 0.85, cropHeight = 0.85;
16
+ const getDevices = async () => {
17
+ try {
18
+ const all = await navigator.mediaDevices.enumerateDevices();
19
+ return all.filter((d) => d.kind === "videoinput");
20
+ }
21
+ catch (_a) {
22
+ return [];
23
+ }
24
+ };
25
+ useEffect(() => {
26
+ const handleDeviceChange = async () => {
27
+ const vids = await getDevices();
28
+ setDevices(vids);
29
+ if (selectedDeviceId && !vids.find((d) => d.deviceId === selectedDeviceId)) {
30
+ // Active device disconnected, clear it to fallback to default
31
+ setSelectedDeviceId(null);
32
+ }
33
+ };
34
+ navigator.mediaDevices.addEventListener("devicechange", handleDeviceChange);
35
+ return () => navigator.mediaDevices.removeEventListener("devicechange", handleDeviceChange);
36
+ }, [selectedDeviceId]);
10
37
  useEffect(() => {
11
38
  let isMounted = true;
12
39
  if (!isConfirming) {
13
40
  (async () => {
14
41
  try {
15
- const stream = await navigator.mediaDevices.getUserMedia({ video: { width: rectWidth, height: rectHeight } });
16
- if (videoRef.current && isMounted) {
42
+ const videoConstraints = {
43
+ width: rectWidth,
44
+ height: rectHeight,
45
+ };
46
+ if (selectedDeviceId) {
47
+ videoConstraints.deviceId = { exact: selectedDeviceId };
48
+ }
49
+ else {
50
+ videoConstraints.facingMode = "environment";
51
+ }
52
+ const stream = await navigator.mediaDevices.getUserMedia({ video: videoConstraints });
53
+ if (!isMounted) {
54
+ stream.getTracks().forEach((track) => track.stop());
55
+ return;
56
+ }
57
+ if (videoRef.current) {
17
58
  videoRef.current.srcObject = stream;
18
59
  videoRef.current.onloadedmetadata = () => {
19
60
  var _a;
@@ -21,6 +62,40 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
21
62
  };
22
63
  }
23
64
  streamRef.current = stream;
65
+ const track = stream.getVideoTracks()[0];
66
+ if (track) {
67
+ if (track.getCapabilities) {
68
+ setCapabilities(track.getCapabilities());
69
+ }
70
+ const settings = track.getSettings();
71
+ let isFront = false;
72
+ if (settings.facingMode) {
73
+ isFront = settings.facingMode === "user";
74
+ }
75
+ else {
76
+ const allVids = await getDevices();
77
+ const currDev = allVids.find(d => d.deviceId === settings.deviceId);
78
+ if (currDev)
79
+ isFront = currDev.label.toLowerCase().includes("front");
80
+ }
81
+ if (isMounted)
82
+ setIsFrontCamera(isFront);
83
+ if (!selectedDeviceId) {
84
+ const currentId = track.getSettings().deviceId;
85
+ if (currentId && isMounted)
86
+ setSelectedDeviceId(currentId);
87
+ }
88
+ track.onended = async () => {
89
+ if (!isMounted)
90
+ return;
91
+ const vids = await getDevices();
92
+ setDevices(vids);
93
+ setSelectedDeviceId(null);
94
+ };
95
+ }
96
+ const vids = await getDevices();
97
+ if (isMounted)
98
+ setDevices(vids);
24
99
  }
25
100
  catch (err) {
26
101
  console.error(err);
@@ -34,7 +109,7 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
34
109
  streamRef.current = null;
35
110
  }
36
111
  };
37
- }, [isConfirming, rectWidth, rectHeight]);
112
+ }, [isConfirming, rectWidth, rectHeight, selectedDeviceId]);
38
113
  const handleCapture = () => {
39
114
  const video = videoRef.current;
40
115
  if (!video)
@@ -49,18 +124,20 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
49
124
  const targetAspect = rectWidth / rectHeight;
50
125
  let sx = 0, sy = 0, sw = video.videoWidth, sh = video.videoHeight;
51
126
  if (videoAspect > targetAspect) {
52
- // Video is wider than target: crop sides
53
127
  sw = video.videoHeight * targetAspect;
54
128
  sx = (video.videoWidth - sw) / 2;
55
129
  }
56
130
  else {
57
- // Video is taller than target: crop top/bottom
58
131
  sh = video.videoWidth / targetAspect;
59
132
  sy = (video.videoHeight - sh) / 2;
60
133
  }
61
134
  ctx.drawImage(video, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
62
135
  canvas.toBlob((blob) => {
63
136
  if (blob) {
137
+ if (streamRef.current) {
138
+ streamRef.current.getTracks().forEach((track) => track.stop());
139
+ streamRef.current = null;
140
+ }
64
141
  const file = new File([blob], "document.jpg", { type: "image/jpeg" });
65
142
  setCapturedFile(file);
66
143
  setCapturedUrl(URL.createObjectURL(blob));
@@ -70,7 +147,7 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
70
147
  onCapture(file);
71
148
  }
72
149
  catch (err) {
73
- console.error('onCapture callback threw:', err);
150
+ console.error(err);
74
151
  }
75
152
  }
76
153
  }
@@ -90,6 +167,53 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
90
167
  setCapturedFile(null);
91
168
  setIsConfirming(false);
92
169
  };
170
+ const cycleCamera = (e) => {
171
+ e.stopPropagation();
172
+ if (devices.length <= 1)
173
+ return;
174
+ const currentIndex = devices.findIndex((d) => d.deviceId === selectedDeviceId);
175
+ const nextIndex = (currentIndex + 1) % devices.length;
176
+ setSelectedDeviceId(devices[nextIndex].deviceId);
177
+ setTorchOn(false);
178
+ };
179
+ const toggleTorch = async (e) => {
180
+ e.stopPropagation();
181
+ if (!streamRef.current)
182
+ return;
183
+ const track = streamRef.current.getVideoTracks()[0];
184
+ if (track && capabilities.torch) {
185
+ try {
186
+ const nextTorch = !torchOn;
187
+ await track.applyConstraints({ advanced: [{ torch: nextTorch }] });
188
+ setTorchOn(nextTorch);
189
+ }
190
+ catch (err) {
191
+ console.error("Torch toggle failed:", err);
192
+ }
193
+ }
194
+ };
195
+ const handleCameraAreaClick = async (e) => {
196
+ if (!streamRef.current)
197
+ return;
198
+ const track = streamRef.current.getVideoTracks()[0];
199
+ if (track && capabilities.focusMode) {
200
+ const focusType = capabilities.focusMode.includes("single-shot") ? "single-shot" : capabilities.focusMode.includes("manual") ? "manual" : null;
201
+ if (!focusType)
202
+ return;
203
+ const rect = e.currentTarget.getBoundingClientRect();
204
+ let x = (e.clientX - rect.left) / rect.width;
205
+ const y = (e.clientY - rect.top) / rect.height;
206
+ if (isFrontCamera) {
207
+ x = 1 - x;
208
+ }
209
+ try {
210
+ await track.applyConstraints({ advanced: [{ focusMode: focusType, pointsOfInterest: [{ x, y }] }] });
211
+ }
212
+ catch (err) {
213
+ console.error("Focus failed:", err);
214
+ }
215
+ }
216
+ };
93
217
  const wrapperStyle = {
94
218
  background: '#fff',
95
219
  borderRadius: noShadow ? 0 : 10,
@@ -112,6 +236,7 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
112
236
  display: 'flex',
113
237
  alignItems: 'center',
114
238
  justifyContent: 'center',
239
+ cursor: ((_a = capabilities.focusMode) === null || _a === void 0 ? void 0 : _a.includes("manual")) ? "crosshair" : "default"
115
240
  };
116
241
  const defaultDashedArea = {
117
242
  position: 'absolute',
@@ -127,6 +252,7 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
127
252
  display: 'flex',
128
253
  flexDirection: 'column',
129
254
  alignItems: 'center',
255
+ pointerEvents: 'none'
130
256
  };
131
257
  const defaultGuidanceText = {
132
258
  color: '#fff',
@@ -178,13 +304,34 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
178
304
  transition: 'background 0.2s',
179
305
  margin: '0 8px',
180
306
  };
181
- return (jsx("div", { className: className, style: wrapperStyle, children: !isConfirming ? (jsxs(Fragment, { children: [jsxs("div", { className: "docscan-camera-area", style: defaultCameraArea, children: [jsx("video", { ref: videoRef, width: rectWidth - 40, height: rectHeight - 40, className: "docscan-video", style: {
307
+ const controlsOverlayStyle = {
308
+ position: 'absolute',
309
+ top: 8,
310
+ right: 8,
311
+ zIndex: 3,
312
+ display: 'flex',
313
+ flexDirection: 'column',
314
+ gap: 12,
315
+ };
316
+ const overlayBtnStyle = {
317
+ background: 'rgba(0,0,0,0.5)',
318
+ border: 'none',
319
+ borderRadius: '50%',
320
+ width: 36,
321
+ height: 36,
322
+ display: 'flex',
323
+ alignItems: 'center',
324
+ justifyContent: 'center',
325
+ cursor: 'pointer',
326
+ color: '#fff',
327
+ };
328
+ return (jsx("div", { className: className, style: wrapperStyle, children: !isConfirming ? (jsxs(Fragment, { children: [jsxs("div", { className: "docscan-camera-area", style: defaultCameraArea, onClick: handleCameraAreaClick, children: [jsxs("div", { style: controlsOverlayStyle, children: [devices.length > 1 && (jsx("button", { type: "button", onClick: cycleCamera, style: overlayBtnStyle, title: "Switch Camera", children: jsxs("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [jsx("path", { d: "M21 12A9 9 0 0 0 6 5.3L3 8" }), jsx("path", { d: "M21 3v5h-5" }), jsx("path", { d: "M3 12a9 9 0 0 0 15 6.7l3-2.7" }), jsx("path", { d: "M3 21v-5h5" })] }) })), capabilities.torch && (jsx("button", { type: "button", onClick: toggleTorch, style: { ...overlayBtnStyle, color: torchOn ? '#ffd700' : '#fff' }, title: "Toggle Flash", children: jsx("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: jsx("polygon", { points: "13 2 3 14 12 14 11 22 21 10 12 10 13 2" }) }) }))] }), jsx("video", { ref: videoRef, width: rectWidth - 40, height: rectHeight - 40, className: "docscan-video", style: {
182
329
  width: rectWidth - 40,
183
330
  height: rectHeight - 40,
184
331
  objectFit: 'cover',
185
332
  borderRadius: noShadow ? 0 : 8,
186
333
  zIndex: 0,
187
- transform: 'scaleX(-1)'
334
+ transform: isFrontCamera ? 'scaleX(-1)' : 'none'
188
335
  }, playsInline: true, muted: true, autoPlay: true }), jsx("div", { className: "docscan-dark-overlay", style: {
189
336
  position: 'absolute',
190
337
  top: 0,
@@ -194,6 +341,7 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
194
341
  background: 'rgba(0,0,0,0.4)',
195
342
  zIndex: 1,
196
343
  borderRadius: noShadow ? 0 : 8,
344
+ pointerEvents: 'none'
197
345
  } }), jsx("div", { className: "docscan-dashed-area", style: defaultDashedArea, children: jsx("span", { className: "docscan-guidance-text", style: defaultGuidanceText, children: "Place your document here" }) })] }), jsx("button", { type: "button", className: "docscan-capture-btn", style: defaultCaptureButton, onClick: handleCapture, children: "Capture" })] })) : (jsxs(Fragment, { children: [jsx("div", { className: "docscan-preview", style: defaultPreview, children: capturedUrl && (jsx("img", { src: capturedUrl, alt: "Captured document", className: "docscan-preview-img", style: { maxWidth: '100%', maxHeight: '100%', borderRadius: 8 } })) }), jsx("div", { className: "docscan-confirm-text", style: defaultConfirmText, children: "Do you want to use this photo?" }), jsxs("div", { className: "docscan-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, width: '100%' }, children: [jsx("button", { type: "button", "aria-label": "Retake", className: "docscan-icon-btn", style: defaultIconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsx("button", { type: "button", "aria-label": "Confirm", className: "docscan-icon-btn", style: defaultIconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] })) }));
198
346
  };const FaceCapture = ({ rectWidth = 360, rectHeight = 576, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
199
347
  const [isConfirming, setIsConfirming] = useState(false);
@@ -208,7 +356,11 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
208
356
  (async () => {
209
357
  try {
210
358
  const stream = await navigator.mediaDevices.getUserMedia({ video: { width: rectWidth, height: rectHeight } });
211
- if (videoRef.current && isMounted) {
359
+ if (!isMounted) {
360
+ stream.getTracks().forEach((track) => track.stop());
361
+ return;
362
+ }
363
+ if (videoRef.current) {
212
364
  videoRef.current.srcObject = stream;
213
365
  videoRef.current.onloadedmetadata = () => {
214
366
  var _a;
@@ -242,6 +394,11 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
242
394
  ctx.drawImage(video, 0, 0, rectWidth, rectHeight, 0, 0, rectWidth * QUALITY_MULTIPLIER, rectHeight * QUALITY_MULTIPLIER);
243
395
  canvas.toBlob((blob) => {
244
396
  if (blob) {
397
+ // Stop the stream immediately — don't wait for useEffect cleanup
398
+ if (streamRef.current) {
399
+ streamRef.current.getTracks().forEach((track) => track.stop());
400
+ streamRef.current = null;
401
+ }
245
402
  const file = new File([blob], "face.jpg", { type: "image/jpeg" });
246
403
  setCapturedFile(file);
247
404
  setCapturedUrl(URL.createObjectURL(blob));
@@ -7179,11 +7336,13 @@ const useRecorder = (stream, type, onStopRecording) => {
7179
7336
  const [permission, setPermission] = useState(false);
7180
7337
  const [stream, setStream] = useState(null);
7181
7338
  const [isLoading, setIsLoading] = useState(true);
7339
+ const streamRef = useRef(null);
7340
+ const isMountedRef = useRef(false);
7182
7341
  const stopStreamTracks = () => {
7183
- if (stream) {
7184
- stream.getTracks().forEach(track => {
7185
- track.stop();
7186
- });
7342
+ const currentStream = streamRef.current;
7343
+ if (currentStream) {
7344
+ currentStream.getTracks().forEach(track => track.stop());
7345
+ streamRef.current = null;
7187
7346
  setStream(null);
7188
7347
  }
7189
7348
  };
@@ -7253,19 +7412,35 @@ const useRecorder = (stream, type, onStopRecording) => {
7253
7412
  const constraints = getAdaptiveConstraints();
7254
7413
  const streamData = await navigator.mediaDevices.getUserMedia(constraints);
7255
7414
  console.log('Media stream obtained, active:', streamData.active);
7415
+ // If the component unmounted while getUserMedia was resolving, stop immediately
7416
+ if (!isMountedRef.current) {
7417
+ streamData.getTracks().forEach(track => track.stop());
7418
+ return;
7419
+ }
7420
+ streamRef.current = streamData;
7256
7421
  setPermission(true);
7257
7422
  setStream(streamData);
7258
7423
  }
7259
7424
  catch (err) {
7260
7425
  console.error('Camera/mic permission denied:', err);
7261
- setPermission(false);
7426
+ if (isMountedRef.current)
7427
+ setPermission(false);
7262
7428
  }
7263
7429
  finally {
7264
- setIsLoading(false);
7430
+ if (isMountedRef.current)
7431
+ setIsLoading(false);
7265
7432
  }
7266
7433
  };
7267
7434
  useEffect(() => {
7435
+ isMountedRef.current = true;
7268
7436
  void requestPermissions();
7437
+ return () => {
7438
+ isMountedRef.current = false;
7439
+ if (streamRef.current) {
7440
+ streamRef.current.getTracks().forEach(track => track.stop());
7441
+ streamRef.current = null;
7442
+ }
7443
+ };
7269
7444
  }, [rectWidth, rectHeight]);
7270
7445
  return { permission, stream, stopStreamTracks, requestPermissions, isLoading };
7271
7446
  };// spinner animation
@@ -7289,7 +7464,6 @@ if (typeof window !== 'undefined' && typeof console !== 'undefined') {
7289
7464
  const RECORD_SECONDS$1 = VIDEO_LENGTH_MS / 1000;
7290
7465
  const PREP_SECONDS$1 = 3;
7291
7466
  const DIGITS_NUMBER$1 = 7;
7292
- const VIDEO_DISPLAY_HEIGHT = 536; // fixed height for consistent zoom regardless of container size
7293
7467
  function generateRandomDigits$1(count) {
7294
7468
  const digits = Array.from({ length: 10 }, (_, i) => i);
7295
7469
  for (let i = digits.length - 1; i > 0; i--) {
@@ -7298,7 +7472,7 @@ function generateRandomDigits$1(count) {
7298
7472
  }
7299
7473
  return digits.slice(0, count);
7300
7474
  }
7301
- const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
7475
+ const FaceRecorder = ({ rectWidth = 320, rectHeight = 512, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
7302
7476
  const [state, setState] = useState('preparation');
7303
7477
  const [prepTimer, setPrepTimer] = useState(PREP_SECONDS$1);
7304
7478
  const [recordTimer, setRecordTimer] = useState(RECORD_SECONDS$1);
@@ -7523,7 +7697,7 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7523
7697
  alignItems: 'center',
7524
7698
  justifyContent: 'center',
7525
7699
  width: rectWidth,
7526
- height: rectHeight + 100,
7700
+ height: rectHeight + 80,
7527
7701
  margin: '0 auto',
7528
7702
  ...style,
7529
7703
  };
@@ -7541,11 +7715,11 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7541
7715
  };
7542
7716
  const ovalArea = {
7543
7717
  position: 'absolute',
7544
- top: '15%',
7545
- left: '50%',
7546
- transform: 'translateX(-50%)',
7547
- width: '75%',
7548
- height: '70%',
7718
+ top: '25%',
7719
+ left: '20%',
7720
+ // transform: 'translateX(-50%)',
7721
+ width: '60%',
7722
+ height: '50%',
7549
7723
  zIndex: 2,
7550
7724
  display: 'flex',
7551
7725
  flexDirection: 'column',
@@ -7697,12 +7871,13 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7697
7871
  if (!permission) {
7698
7872
  return (jsx("div", { className: className, style: wrapperStyle, children: jsxs("div", { style: { textAlign: 'center', padding: '20px' }, children: [jsx("h3", { children: "Camera Permission Required" }), jsx("p", { children: "Please allow camera and microphone access to use this component." }), jsx("button", { type: "button", style: actionButton, onClick: requestPermissions, children: "Grant Permissions" })] }) }));
7699
7873
  }
7700
- return (jsxs("div", { className: className, style: wrapperStyle, children: [state !== 'result' && state !== 'processing' && !isConfirming && (jsxs(Fragment, { children: [jsx("div", { className: "facerecorder-number-row", style: numberRow, children: (state === 'recording' ? digits : Array(DIGITS_NUMBER$1).fill('*')).map((num, i) => (jsx("div", { style: numberBox, children: num }, i))) }), jsxs("div", { className: "facerecorder-camera-area", style: cameraArea, children: [jsx("video", { ref: videoRef, width: rectWidth - 40, height: VIDEO_DISPLAY_HEIGHT, className: "facerecorder-video", style: {
7874
+ return (jsxs("div", { className: className, style: wrapperStyle, children: [state !== 'result' && state !== 'processing' && !isConfirming && (jsxs(Fragment, { children: [jsx("div", { className: "facerecorder-number-row", style: numberRow, children: (state === 'recording' ? digits : Array(DIGITS_NUMBER$1).fill('*')).map((num, i) => (jsx("div", { style: numberBox, children: num }, i))) }), jsxs("div", { className: "facerecorder-camera-area", style: cameraArea, children: [jsx("video", { ref: videoRef, width: rectWidth - 40, height: rectHeight - 40, className: "facerecorder-video", style: {
7701
7875
  width: rectWidth - 40,
7702
- height: VIDEO_DISPLAY_HEIGHT,
7876
+ height: rectHeight - 40,
7703
7877
  objectFit: 'cover',
7878
+ borderRadius: noShadow ? 0 : 8,
7704
7879
  zIndex: 0,
7705
- transform: 'scaleX(-1)',
7880
+ transform: 'scaleX(-1)'
7706
7881
  }, playsInline: true, muted: true, autoPlay: true }), jsx("div", { className: "facerecorder-dark-overlay", style: {
7707
7882
  position: 'absolute',
7708
7883
  top: 0,
@@ -7737,11 +7912,13 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7737
7912
  const [permission, setPermission] = useState(false);
7738
7913
  const [stream, setStream] = useState(null);
7739
7914
  const [isLoading, setIsLoading] = useState(true);
7915
+ const streamRef = useRef(null);
7916
+ const isMountedRef = useRef(false);
7740
7917
  const stopStreamTracks = () => {
7741
- if (stream) {
7742
- stream.getTracks().forEach(track => {
7743
- track.stop();
7744
- });
7918
+ const currentStream = streamRef.current;
7919
+ if (currentStream) {
7920
+ currentStream.getTracks().forEach(track => track.stop());
7921
+ streamRef.current = null;
7745
7922
  setStream(null);
7746
7923
  }
7747
7924
  };
@@ -7789,19 +7966,35 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7789
7966
  setIsLoading(true);
7790
7967
  const constraints = getAudioConstraints();
7791
7968
  const streamData = await navigator.mediaDevices.getUserMedia(constraints);
7969
+ // If the component unmounted while getUserMedia was resolving, stop immediately
7970
+ if (!isMountedRef.current) {
7971
+ streamData.getTracks().forEach(track => track.stop());
7972
+ return;
7973
+ }
7974
+ streamRef.current = streamData;
7792
7975
  setPermission(true);
7793
7976
  setStream(streamData);
7794
7977
  }
7795
7978
  catch (err) {
7796
7979
  console.error('Microphone permission denied:', err);
7797
- setPermission(false);
7980
+ if (isMountedRef.current)
7981
+ setPermission(false);
7798
7982
  }
7799
7983
  finally {
7800
- setIsLoading(false);
7984
+ if (isMountedRef.current)
7985
+ setIsLoading(false);
7801
7986
  }
7802
7987
  };
7803
7988
  useEffect(() => {
7989
+ isMountedRef.current = true;
7804
7990
  void requestPermissions();
7991
+ return () => {
7992
+ isMountedRef.current = false;
7993
+ if (streamRef.current) {
7994
+ streamRef.current.getTracks().forEach(track => track.stop());
7995
+ streamRef.current = null;
7996
+ }
7997
+ };
7805
7998
  }, []);
7806
7999
  return { permission, stream, stopStreamTracks, requestPermissions, isLoading };
7807
8000
  };// spinner animation
package/dist/index.js CHANGED
@@ -1,19 +1,60 @@
1
1
  'use strict';var jsxRuntime=require('react/jsx-runtime'),React=require('react');const DocScan = ({ rectWidth = 640, rectHeight = 400, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
2
+ var _a;
2
3
  const [isConfirming, setIsConfirming] = React.useState(false);
3
4
  const [capturedUrl, setCapturedUrl] = React.useState(null);
4
5
  const [capturedFile, setCapturedFile] = React.useState(null);
5
6
  const videoRef = React.useRef(null);
6
7
  const streamRef = React.useRef(null);
8
+ const [devices, setDevices] = React.useState([]);
9
+ const [selectedDeviceId, setSelectedDeviceId] = React.useState(null);
10
+ const [isFrontCamera, setIsFrontCamera] = React.useState(false);
11
+ const [capabilities, setCapabilities] = React.useState({});
12
+ const [torchOn, setTorchOn] = React.useState(false);
7
13
  const QUALITY_MULTIPLIER = 3;
8
14
  // Crop area (dashed area) as percentages
9
15
  const cropTop = 0.075, cropLeft = 0.075, cropWidth = 0.85, cropHeight = 0.85;
16
+ const getDevices = async () => {
17
+ try {
18
+ const all = await navigator.mediaDevices.enumerateDevices();
19
+ return all.filter((d) => d.kind === "videoinput");
20
+ }
21
+ catch (_a) {
22
+ return [];
23
+ }
24
+ };
25
+ React.useEffect(() => {
26
+ const handleDeviceChange = async () => {
27
+ const vids = await getDevices();
28
+ setDevices(vids);
29
+ if (selectedDeviceId && !vids.find((d) => d.deviceId === selectedDeviceId)) {
30
+ // Active device disconnected, clear it to fallback to default
31
+ setSelectedDeviceId(null);
32
+ }
33
+ };
34
+ navigator.mediaDevices.addEventListener("devicechange", handleDeviceChange);
35
+ return () => navigator.mediaDevices.removeEventListener("devicechange", handleDeviceChange);
36
+ }, [selectedDeviceId]);
10
37
  React.useEffect(() => {
11
38
  let isMounted = true;
12
39
  if (!isConfirming) {
13
40
  (async () => {
14
41
  try {
15
- const stream = await navigator.mediaDevices.getUserMedia({ video: { width: rectWidth, height: rectHeight } });
16
- if (videoRef.current && isMounted) {
42
+ const videoConstraints = {
43
+ width: rectWidth,
44
+ height: rectHeight,
45
+ };
46
+ if (selectedDeviceId) {
47
+ videoConstraints.deviceId = { exact: selectedDeviceId };
48
+ }
49
+ else {
50
+ videoConstraints.facingMode = "environment";
51
+ }
52
+ const stream = await navigator.mediaDevices.getUserMedia({ video: videoConstraints });
53
+ if (!isMounted) {
54
+ stream.getTracks().forEach((track) => track.stop());
55
+ return;
56
+ }
57
+ if (videoRef.current) {
17
58
  videoRef.current.srcObject = stream;
18
59
  videoRef.current.onloadedmetadata = () => {
19
60
  var _a;
@@ -21,6 +62,40 @@
21
62
  };
22
63
  }
23
64
  streamRef.current = stream;
65
+ const track = stream.getVideoTracks()[0];
66
+ if (track) {
67
+ if (track.getCapabilities) {
68
+ setCapabilities(track.getCapabilities());
69
+ }
70
+ const settings = track.getSettings();
71
+ let isFront = false;
72
+ if (settings.facingMode) {
73
+ isFront = settings.facingMode === "user";
74
+ }
75
+ else {
76
+ const allVids = await getDevices();
77
+ const currDev = allVids.find(d => d.deviceId === settings.deviceId);
78
+ if (currDev)
79
+ isFront = currDev.label.toLowerCase().includes("front");
80
+ }
81
+ if (isMounted)
82
+ setIsFrontCamera(isFront);
83
+ if (!selectedDeviceId) {
84
+ const currentId = track.getSettings().deviceId;
85
+ if (currentId && isMounted)
86
+ setSelectedDeviceId(currentId);
87
+ }
88
+ track.onended = async () => {
89
+ if (!isMounted)
90
+ return;
91
+ const vids = await getDevices();
92
+ setDevices(vids);
93
+ setSelectedDeviceId(null);
94
+ };
95
+ }
96
+ const vids = await getDevices();
97
+ if (isMounted)
98
+ setDevices(vids);
24
99
  }
25
100
  catch (err) {
26
101
  console.error(err);
@@ -34,7 +109,7 @@
34
109
  streamRef.current = null;
35
110
  }
36
111
  };
37
- }, [isConfirming, rectWidth, rectHeight]);
112
+ }, [isConfirming, rectWidth, rectHeight, selectedDeviceId]);
38
113
  const handleCapture = () => {
39
114
  const video = videoRef.current;
40
115
  if (!video)
@@ -49,18 +124,20 @@
49
124
  const targetAspect = rectWidth / rectHeight;
50
125
  let sx = 0, sy = 0, sw = video.videoWidth, sh = video.videoHeight;
51
126
  if (videoAspect > targetAspect) {
52
- // Video is wider than target: crop sides
53
127
  sw = video.videoHeight * targetAspect;
54
128
  sx = (video.videoWidth - sw) / 2;
55
129
  }
56
130
  else {
57
- // Video is taller than target: crop top/bottom
58
131
  sh = video.videoWidth / targetAspect;
59
132
  sy = (video.videoHeight - sh) / 2;
60
133
  }
61
134
  ctx.drawImage(video, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
62
135
  canvas.toBlob((blob) => {
63
136
  if (blob) {
137
+ if (streamRef.current) {
138
+ streamRef.current.getTracks().forEach((track) => track.stop());
139
+ streamRef.current = null;
140
+ }
64
141
  const file = new File([blob], "document.jpg", { type: "image/jpeg" });
65
142
  setCapturedFile(file);
66
143
  setCapturedUrl(URL.createObjectURL(blob));
@@ -70,7 +147,7 @@
70
147
  onCapture(file);
71
148
  }
72
149
  catch (err) {
73
- console.error('onCapture callback threw:', err);
150
+ console.error(err);
74
151
  }
75
152
  }
76
153
  }
@@ -90,6 +167,53 @@
90
167
  setCapturedFile(null);
91
168
  setIsConfirming(false);
92
169
  };
170
+ const cycleCamera = (e) => {
171
+ e.stopPropagation();
172
+ if (devices.length <= 1)
173
+ return;
174
+ const currentIndex = devices.findIndex((d) => d.deviceId === selectedDeviceId);
175
+ const nextIndex = (currentIndex + 1) % devices.length;
176
+ setSelectedDeviceId(devices[nextIndex].deviceId);
177
+ setTorchOn(false);
178
+ };
179
+ const toggleTorch = async (e) => {
180
+ e.stopPropagation();
181
+ if (!streamRef.current)
182
+ return;
183
+ const track = streamRef.current.getVideoTracks()[0];
184
+ if (track && capabilities.torch) {
185
+ try {
186
+ const nextTorch = !torchOn;
187
+ await track.applyConstraints({ advanced: [{ torch: nextTorch }] });
188
+ setTorchOn(nextTorch);
189
+ }
190
+ catch (err) {
191
+ console.error("Torch toggle failed:", err);
192
+ }
193
+ }
194
+ };
195
+ const handleCameraAreaClick = async (e) => {
196
+ if (!streamRef.current)
197
+ return;
198
+ const track = streamRef.current.getVideoTracks()[0];
199
+ if (track && capabilities.focusMode) {
200
+ const focusType = capabilities.focusMode.includes("single-shot") ? "single-shot" : capabilities.focusMode.includes("manual") ? "manual" : null;
201
+ if (!focusType)
202
+ return;
203
+ const rect = e.currentTarget.getBoundingClientRect();
204
+ let x = (e.clientX - rect.left) / rect.width;
205
+ const y = (e.clientY - rect.top) / rect.height;
206
+ if (isFrontCamera) {
207
+ x = 1 - x;
208
+ }
209
+ try {
210
+ await track.applyConstraints({ advanced: [{ focusMode: focusType, pointsOfInterest: [{ x, y }] }] });
211
+ }
212
+ catch (err) {
213
+ console.error("Focus failed:", err);
214
+ }
215
+ }
216
+ };
93
217
  const wrapperStyle = {
94
218
  background: '#fff',
95
219
  borderRadius: noShadow ? 0 : 10,
@@ -112,6 +236,7 @@
112
236
  display: 'flex',
113
237
  alignItems: 'center',
114
238
  justifyContent: 'center',
239
+ cursor: ((_a = capabilities.focusMode) === null || _a === void 0 ? void 0 : _a.includes("manual")) ? "crosshair" : "default"
115
240
  };
116
241
  const defaultDashedArea = {
117
242
  position: 'absolute',
@@ -127,6 +252,7 @@
127
252
  display: 'flex',
128
253
  flexDirection: 'column',
129
254
  alignItems: 'center',
255
+ pointerEvents: 'none'
130
256
  };
131
257
  const defaultGuidanceText = {
132
258
  color: '#fff',
@@ -178,13 +304,34 @@
178
304
  transition: 'background 0.2s',
179
305
  margin: '0 8px',
180
306
  };
181
- return (jsxRuntime.jsx("div", { className: className, style: wrapperStyle, children: !isConfirming ? (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs("div", { className: "docscan-camera-area", style: defaultCameraArea, children: [jsxRuntime.jsx("video", { ref: videoRef, width: rectWidth - 40, height: rectHeight - 40, className: "docscan-video", style: {
307
+ const controlsOverlayStyle = {
308
+ position: 'absolute',
309
+ top: 8,
310
+ right: 8,
311
+ zIndex: 3,
312
+ display: 'flex',
313
+ flexDirection: 'column',
314
+ gap: 12,
315
+ };
316
+ const overlayBtnStyle = {
317
+ background: 'rgba(0,0,0,0.5)',
318
+ border: 'none',
319
+ borderRadius: '50%',
320
+ width: 36,
321
+ height: 36,
322
+ display: 'flex',
323
+ alignItems: 'center',
324
+ justifyContent: 'center',
325
+ cursor: 'pointer',
326
+ color: '#fff',
327
+ };
328
+ return (jsxRuntime.jsx("div", { className: className, style: wrapperStyle, children: !isConfirming ? (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs("div", { className: "docscan-camera-area", style: defaultCameraArea, onClick: handleCameraAreaClick, children: [jsxRuntime.jsxs("div", { style: controlsOverlayStyle, children: [devices.length > 1 && (jsxRuntime.jsx("button", { type: "button", onClick: cycleCamera, style: overlayBtnStyle, title: "Switch Camera", children: jsxRuntime.jsxs("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [jsxRuntime.jsx("path", { d: "M21 12A9 9 0 0 0 6 5.3L3 8" }), jsxRuntime.jsx("path", { d: "M21 3v5h-5" }), jsxRuntime.jsx("path", { d: "M3 12a9 9 0 0 0 15 6.7l3-2.7" }), jsxRuntime.jsx("path", { d: "M3 21v-5h5" })] }) })), capabilities.torch && (jsxRuntime.jsx("button", { type: "button", onClick: toggleTorch, style: { ...overlayBtnStyle, color: torchOn ? '#ffd700' : '#fff' }, title: "Toggle Flash", children: jsxRuntime.jsx("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: jsxRuntime.jsx("polygon", { points: "13 2 3 14 12 14 11 22 21 10 12 10 13 2" }) }) }))] }), jsxRuntime.jsx("video", { ref: videoRef, width: rectWidth - 40, height: rectHeight - 40, className: "docscan-video", style: {
182
329
  width: rectWidth - 40,
183
330
  height: rectHeight - 40,
184
331
  objectFit: 'cover',
185
332
  borderRadius: noShadow ? 0 : 8,
186
333
  zIndex: 0,
187
- transform: 'scaleX(-1)'
334
+ transform: isFrontCamera ? 'scaleX(-1)' : 'none'
188
335
  }, playsInline: true, muted: true, autoPlay: true }), jsxRuntime.jsx("div", { className: "docscan-dark-overlay", style: {
189
336
  position: 'absolute',
190
337
  top: 0,
@@ -194,6 +341,7 @@
194
341
  background: 'rgba(0,0,0,0.4)',
195
342
  zIndex: 1,
196
343
  borderRadius: noShadow ? 0 : 8,
344
+ pointerEvents: 'none'
197
345
  } }), jsxRuntime.jsx("div", { className: "docscan-dashed-area", style: defaultDashedArea, children: jsxRuntime.jsx("span", { className: "docscan-guidance-text", style: defaultGuidanceText, children: "Place your document here" }) })] }), jsxRuntime.jsx("button", { type: "button", className: "docscan-capture-btn", style: defaultCaptureButton, onClick: handleCapture, children: "Capture" })] })) : (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { className: "docscan-preview", style: defaultPreview, children: capturedUrl && (jsxRuntime.jsx("img", { src: capturedUrl, alt: "Captured document", className: "docscan-preview-img", style: { maxWidth: '100%', maxHeight: '100%', borderRadius: 8 } })) }), jsxRuntime.jsx("div", { className: "docscan-confirm-text", style: defaultConfirmText, children: "Do you want to use this photo?" }), jsxRuntime.jsxs("div", { className: "docscan-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, width: '100%' }, children: [jsxRuntime.jsx("button", { type: "button", "aria-label": "Retake", className: "docscan-icon-btn", style: defaultIconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxRuntime.jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsxRuntime.jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsxRuntime.jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsxRuntime.jsx("button", { type: "button", "aria-label": "Confirm", className: "docscan-icon-btn", style: defaultIconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxRuntime.jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsxRuntime.jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] })) }));
198
346
  };const FaceCapture = ({ rectWidth = 360, rectHeight = 576, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
199
347
  const [isConfirming, setIsConfirming] = React.useState(false);
@@ -208,7 +356,11 @@
208
356
  (async () => {
209
357
  try {
210
358
  const stream = await navigator.mediaDevices.getUserMedia({ video: { width: rectWidth, height: rectHeight } });
211
- if (videoRef.current && isMounted) {
359
+ if (!isMounted) {
360
+ stream.getTracks().forEach((track) => track.stop());
361
+ return;
362
+ }
363
+ if (videoRef.current) {
212
364
  videoRef.current.srcObject = stream;
213
365
  videoRef.current.onloadedmetadata = () => {
214
366
  var _a;
@@ -242,6 +394,11 @@
242
394
  ctx.drawImage(video, 0, 0, rectWidth, rectHeight, 0, 0, rectWidth * QUALITY_MULTIPLIER, rectHeight * QUALITY_MULTIPLIER);
243
395
  canvas.toBlob((blob) => {
244
396
  if (blob) {
397
+ // Stop the stream immediately — don't wait for useEffect cleanup
398
+ if (streamRef.current) {
399
+ streamRef.current.getTracks().forEach((track) => track.stop());
400
+ streamRef.current = null;
401
+ }
245
402
  const file = new File([blob], "face.jpg", { type: "image/jpeg" });
246
403
  setCapturedFile(file);
247
404
  setCapturedUrl(URL.createObjectURL(blob));
@@ -7179,11 +7336,13 @@ const useRecorder = (stream, type, onStopRecording) => {
7179
7336
  const [permission, setPermission] = React.useState(false);
7180
7337
  const [stream, setStream] = React.useState(null);
7181
7338
  const [isLoading, setIsLoading] = React.useState(true);
7339
+ const streamRef = React.useRef(null);
7340
+ const isMountedRef = React.useRef(false);
7182
7341
  const stopStreamTracks = () => {
7183
- if (stream) {
7184
- stream.getTracks().forEach(track => {
7185
- track.stop();
7186
- });
7342
+ const currentStream = streamRef.current;
7343
+ if (currentStream) {
7344
+ currentStream.getTracks().forEach(track => track.stop());
7345
+ streamRef.current = null;
7187
7346
  setStream(null);
7188
7347
  }
7189
7348
  };
@@ -7253,19 +7412,35 @@ const useRecorder = (stream, type, onStopRecording) => {
7253
7412
  const constraints = getAdaptiveConstraints();
7254
7413
  const streamData = await navigator.mediaDevices.getUserMedia(constraints);
7255
7414
  console.log('Media stream obtained, active:', streamData.active);
7415
+ // If the component unmounted while getUserMedia was resolving, stop immediately
7416
+ if (!isMountedRef.current) {
7417
+ streamData.getTracks().forEach(track => track.stop());
7418
+ return;
7419
+ }
7420
+ streamRef.current = streamData;
7256
7421
  setPermission(true);
7257
7422
  setStream(streamData);
7258
7423
  }
7259
7424
  catch (err) {
7260
7425
  console.error('Camera/mic permission denied:', err);
7261
- setPermission(false);
7426
+ if (isMountedRef.current)
7427
+ setPermission(false);
7262
7428
  }
7263
7429
  finally {
7264
- setIsLoading(false);
7430
+ if (isMountedRef.current)
7431
+ setIsLoading(false);
7265
7432
  }
7266
7433
  };
7267
7434
  React.useEffect(() => {
7435
+ isMountedRef.current = true;
7268
7436
  void requestPermissions();
7437
+ return () => {
7438
+ isMountedRef.current = false;
7439
+ if (streamRef.current) {
7440
+ streamRef.current.getTracks().forEach(track => track.stop());
7441
+ streamRef.current = null;
7442
+ }
7443
+ };
7269
7444
  }, [rectWidth, rectHeight]);
7270
7445
  return { permission, stream, stopStreamTracks, requestPermissions, isLoading };
7271
7446
  };// spinner animation
@@ -7289,7 +7464,6 @@ if (typeof window !== 'undefined' && typeof console !== 'undefined') {
7289
7464
  const RECORD_SECONDS$1 = VIDEO_LENGTH_MS / 1000;
7290
7465
  const PREP_SECONDS$1 = 3;
7291
7466
  const DIGITS_NUMBER$1 = 7;
7292
- const VIDEO_DISPLAY_HEIGHT = 536; // fixed height for consistent zoom regardless of container size
7293
7467
  function generateRandomDigits$1(count) {
7294
7468
  const digits = Array.from({ length: 10 }, (_, i) => i);
7295
7469
  for (let i = digits.length - 1; i > 0; i--) {
@@ -7298,7 +7472,7 @@ function generateRandomDigits$1(count) {
7298
7472
  }
7299
7473
  return digits.slice(0, count);
7300
7474
  }
7301
- const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
7475
+ const FaceRecorder = ({ rectWidth = 320, rectHeight = 512, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
7302
7476
  const [state, setState] = React.useState('preparation');
7303
7477
  const [prepTimer, setPrepTimer] = React.useState(PREP_SECONDS$1);
7304
7478
  const [recordTimer, setRecordTimer] = React.useState(RECORD_SECONDS$1);
@@ -7523,7 +7697,7 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7523
7697
  alignItems: 'center',
7524
7698
  justifyContent: 'center',
7525
7699
  width: rectWidth,
7526
- height: rectHeight + 100,
7700
+ height: rectHeight + 80,
7527
7701
  margin: '0 auto',
7528
7702
  ...style,
7529
7703
  };
@@ -7541,11 +7715,11 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7541
7715
  };
7542
7716
  const ovalArea = {
7543
7717
  position: 'absolute',
7544
- top: '15%',
7545
- left: '50%',
7546
- transform: 'translateX(-50%)',
7547
- width: '75%',
7548
- height: '70%',
7718
+ top: '25%',
7719
+ left: '20%',
7720
+ // transform: 'translateX(-50%)',
7721
+ width: '60%',
7722
+ height: '50%',
7549
7723
  zIndex: 2,
7550
7724
  display: 'flex',
7551
7725
  flexDirection: 'column',
@@ -7697,12 +7871,13 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7697
7871
  if (!permission) {
7698
7872
  return (jsxRuntime.jsx("div", { className: className, style: wrapperStyle, children: jsxRuntime.jsxs("div", { style: { textAlign: 'center', padding: '20px' }, children: [jsxRuntime.jsx("h3", { children: "Camera Permission Required" }), jsxRuntime.jsx("p", { children: "Please allow camera and microphone access to use this component." }), jsxRuntime.jsx("button", { type: "button", style: actionButton, onClick: requestPermissions, children: "Grant Permissions" })] }) }));
7699
7873
  }
7700
- return (jsxRuntime.jsxs("div", { className: className, style: wrapperStyle, children: [state !== 'result' && state !== 'processing' && !isConfirming && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { className: "facerecorder-number-row", style: numberRow, children: (state === 'recording' ? digits : Array(DIGITS_NUMBER$1).fill('*')).map((num, i) => (jsxRuntime.jsx("div", { style: numberBox, children: num }, i))) }), jsxRuntime.jsxs("div", { className: "facerecorder-camera-area", style: cameraArea, children: [jsxRuntime.jsx("video", { ref: videoRef, width: rectWidth - 40, height: VIDEO_DISPLAY_HEIGHT, className: "facerecorder-video", style: {
7874
+ return (jsxRuntime.jsxs("div", { className: className, style: wrapperStyle, children: [state !== 'result' && state !== 'processing' && !isConfirming && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { className: "facerecorder-number-row", style: numberRow, children: (state === 'recording' ? digits : Array(DIGITS_NUMBER$1).fill('*')).map((num, i) => (jsxRuntime.jsx("div", { style: numberBox, children: num }, i))) }), jsxRuntime.jsxs("div", { className: "facerecorder-camera-area", style: cameraArea, children: [jsxRuntime.jsx("video", { ref: videoRef, width: rectWidth - 40, height: rectHeight - 40, className: "facerecorder-video", style: {
7701
7875
  width: rectWidth - 40,
7702
- height: VIDEO_DISPLAY_HEIGHT,
7876
+ height: rectHeight - 40,
7703
7877
  objectFit: 'cover',
7878
+ borderRadius: noShadow ? 0 : 8,
7704
7879
  zIndex: 0,
7705
- transform: 'scaleX(-1)',
7880
+ transform: 'scaleX(-1)'
7706
7881
  }, playsInline: true, muted: true, autoPlay: true }), jsxRuntime.jsx("div", { className: "facerecorder-dark-overlay", style: {
7707
7882
  position: 'absolute',
7708
7883
  top: 0,
@@ -7737,11 +7912,13 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7737
7912
  const [permission, setPermission] = React.useState(false);
7738
7913
  const [stream, setStream] = React.useState(null);
7739
7914
  const [isLoading, setIsLoading] = React.useState(true);
7915
+ const streamRef = React.useRef(null);
7916
+ const isMountedRef = React.useRef(false);
7740
7917
  const stopStreamTracks = () => {
7741
- if (stream) {
7742
- stream.getTracks().forEach(track => {
7743
- track.stop();
7744
- });
7918
+ const currentStream = streamRef.current;
7919
+ if (currentStream) {
7920
+ currentStream.getTracks().forEach(track => track.stop());
7921
+ streamRef.current = null;
7745
7922
  setStream(null);
7746
7923
  }
7747
7924
  };
@@ -7789,19 +7966,35 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 470, onConfirmRecording, o
7789
7966
  setIsLoading(true);
7790
7967
  const constraints = getAudioConstraints();
7791
7968
  const streamData = await navigator.mediaDevices.getUserMedia(constraints);
7969
+ // If the component unmounted while getUserMedia was resolving, stop immediately
7970
+ if (!isMountedRef.current) {
7971
+ streamData.getTracks().forEach(track => track.stop());
7972
+ return;
7973
+ }
7974
+ streamRef.current = streamData;
7792
7975
  setPermission(true);
7793
7976
  setStream(streamData);
7794
7977
  }
7795
7978
  catch (err) {
7796
7979
  console.error('Microphone permission denied:', err);
7797
- setPermission(false);
7980
+ if (isMountedRef.current)
7981
+ setPermission(false);
7798
7982
  }
7799
7983
  finally {
7800
- setIsLoading(false);
7984
+ if (isMountedRef.current)
7985
+ setIsLoading(false);
7801
7986
  }
7802
7987
  };
7803
7988
  React.useEffect(() => {
7989
+ isMountedRef.current = true;
7804
7990
  void requestPermissions();
7991
+ return () => {
7992
+ isMountedRef.current = false;
7993
+ if (streamRef.current) {
7994
+ streamRef.current.getTracks().forEach(track => track.stop());
7995
+ streamRef.current = null;
7996
+ }
7997
+ };
7805
7998
  }, []);
7806
7999
  return { permission, stream, stopStreamTracks, requestPermissions, isLoading };
7807
8000
  };// spinner animation
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "biometry-react-components",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "type": "module",
5
5
  "description": "React UI component library for capturing high-quality biometric data (voice, face, documents, video) to facilitate integration with 'Biometry' services",
6
6
  "main": "dist/index.js",
@@ -13,6 +13,7 @@
13
13
  "build": "rollup -c",
14
14
  "build:types": "rollup -c rollup.config.dts.js",
15
15
  "build:clean": "rm -rf dist && npm run build && npm run build:types",
16
+ "watch": "rollup -c --watch",
16
17
  "test": "echo \"Error: no test specified\" && exit 1",
17
18
  "prepublishOnly": "npm run build:clean"
18
19
  },
@@ -30,23 +31,15 @@
30
31
  ],
31
32
  "author": "Biometry Team",
32
33
  "license": "MIT",
33
- "repository": {
34
- "type": "git",
35
- "url": "https://github.com/your-username/biometry-react-components.git"
36
- },
37
- "bugs": {
38
- "url": "https://github.com/your-username/biometry-react-components/issues"
39
- },
40
- "homepage": "https://github.com/your-username/biometry-react-components#readme",
41
34
  "devDependencies": {
42
35
  "@rollup/plugin-commonjs": "^28.0.6",
43
36
  "@rollup/plugin-node-resolve": "^16.0.1",
44
37
  "@rollup/plugin-typescript": "^12.1.4",
45
- "@types/react": "^19.1.8",
46
- "@types/react-dom": "^19.1.6",
38
+ "@types/react": "^18.3.1",
39
+ "@types/react-dom": "^18.3.1",
47
40
  "@types/recordrtc": "^5.6.14",
48
- "react": "^19.1.0",
49
- "react-dom": "^19.1.0",
41
+ "react": "^18.3.1",
42
+ "react-dom": "^18.3.1",
50
43
  "rollup": "^4.45.1",
51
44
  "rollup-plugin-dts": "^6.2.1",
52
45
  "rollup-plugin-peer-deps-external": "^2.2.4",