@kcaptcha/react 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.
@@ -0,0 +1,31 @@
1
+ import type { CSSProperties } from 'react';
2
+ export interface KCaptchaProps {
3
+ /**
4
+ * REQUIRED. Base path of the kCAPTCHA router on your server, e.g. "/login/kcaptcha".
5
+ * If it is missing at runtime, an error is printed to the console and shown in place of the widget.
6
+ */
7
+ endpoint: string;
8
+ /** Width of the checkbox button in px (160-600). Default 300. */
9
+ width?: number;
10
+ /** Height of the checkbox button in px (40-120). Default 74. */
11
+ height?: number;
12
+ /** Accent colour of the checkbox button (any CSS colour). Default "#3fb950". */
13
+ color?: string;
14
+ /** Name of the hidden form field that receives the pass token. Default "kcaptcha_token". */
15
+ name?: string;
16
+ /** Called with the pass token once the user solves the CAPTCHA. */
17
+ onVerify?: (token: string) => void;
18
+ /** Called when a solved CAPTCHA's pass token expires unused. */
19
+ onExpire?: () => void;
20
+ className?: string;
21
+ /** Layout styles for the wrapper (margin, alignment, ...). Size the button with width/height instead. */
22
+ style?: CSSProperties;
23
+ }
24
+ /** Methods available through a ref. */
25
+ export interface KCaptchaRef {
26
+ /** The pass token, or null if not verified. */
27
+ getToken(): string | null;
28
+ /** Uncheck the widget. Call this after a failed submit: a pass token works only once. */
29
+ reset(): void;
30
+ }
31
+ export declare const KCaptcha: import("react").ForwardRefExoticComponent<KCaptchaProps & import("react").RefAttributes<KCaptchaRef>>;
@@ -0,0 +1,45 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react';
4
+ import { mount } from '@kcaptcha/client';
5
+ export const KCaptcha = forwardRef(function KCaptcha(props, ref) {
6
+ const { endpoint, width, height, color, name, onVerify, onExpire, className, style } = props;
7
+ const hostRef = useRef(null);
8
+ const handleRef = useRef(null);
9
+ // Keep the latest callbacks in refs so an inline arrow function in the parent
10
+ // does not tear down and rebuild the widget on every render.
11
+ const onVerifyRef = useRef(onVerify);
12
+ const onExpireRef = useRef(onExpire);
13
+ useEffect(() => {
14
+ onVerifyRef.current = onVerify;
15
+ onExpireRef.current = onExpire;
16
+ });
17
+ useEffect(() => {
18
+ const host = hostRef.current;
19
+ if (!host)
20
+ return;
21
+ // mount() validates the options. With no endpoint it logs the error and
22
+ // renders a red message into `host`, then returns null.
23
+ const handle = mount(host, {
24
+ endpoint,
25
+ width,
26
+ height,
27
+ color,
28
+ name,
29
+ onVerify: (token) => onVerifyRef.current?.(token),
30
+ onExpire: () => onExpireRef.current?.(),
31
+ });
32
+ handleRef.current = handle;
33
+ return () => {
34
+ handle?.destroy();
35
+ handleRef.current = null;
36
+ host.replaceChildren(); // also clears the error box, if one was shown
37
+ };
38
+ }, [endpoint, width, height, color, name]);
39
+ useImperativeHandle(ref, () => ({
40
+ getToken: () => handleRef.current?.getToken() ?? null,
41
+ reset: () => handleRef.current?.reset(),
42
+ }), []);
43
+ return _jsx("div", { ref: hostRef, className: className, style: style });
44
+ });
45
+ //# sourceMappingURL=KCaptcha.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KCaptcha.js","sourceRoot":"","sources":["../src/KCaptcha.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE3E,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAkCzC,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAA6B,SAAS,QAAQ,CAAC,KAAK,EAAE,GAAG;IACzF,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC;IAE7F,MAAM,OAAO,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAC;IAC7C,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,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,KAAK,EAAE;KACxC,CAAC,EACF,EAAE,CACH,CAAC;IAEF,OAAO,cAAK,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,GAAI,CAAC;AACnE,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { KCaptcha } from './KCaptcha.js';
2
+ export type { KCaptchaProps, KCaptchaRef } from './KCaptcha.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { KCaptcha } from './KCaptcha.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC"}
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@kcaptcha/react",
3
+ "version": "0.1.0",
4
+ "description": "React component for kCAPTCHA",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": ["dist"],
16
+ "scripts": {
17
+ "build": "tsc -p tsconfig.json",
18
+ "typecheck": "tsc -p tsconfig.json --noEmit"
19
+ },
20
+ "dependencies": {
21
+ "@kcaptcha/client": "^0.1.0"
22
+ },
23
+ "peerDependencies": {
24
+ "react": ">=17"
25
+ },
26
+ "devDependencies": {
27
+ "@types/react": "^18.3.0",
28
+ "react": "^18.3.0"
29
+ }
30
+ }