@snutig/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.
- package/LICENSE +13 -0
- package/README.md +200 -0
- package/dist/SnutigCaptcha.d.ts +99 -0
- package/dist/SnutigCaptcha.d.ts.map +1 -0
- package/dist/index.cjs +375 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +361 -0
- package/dist/index.js.map +7 -0
- package/dist/labels.d.ts +32 -0
- package/dist/labels.d.ts.map +1 -0
- package/dist/loader.d.ts +35 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/theme.css +59 -0
- package/dist/theme.d.ts +15 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/types.d.ts +52 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +65 -0
- package/src/SnutigCaptcha.tsx +325 -0
- package/src/css.d.ts +5 -0
- package/src/index.ts +27 -0
- package/src/labels.ts +66 -0
- package/src/loader.ts +116 -0
- package/src/theme.css +59 -0
- package/src/theme.ts +33 -0
- package/src/types.ts +72 -0
package/src/theme.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import THEME_CSS_TEXT from "./theme.css";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The brand theme as a string, inlined at build time from `src/theme.css`.
|
|
5
|
+
*
|
|
6
|
+
* The widget renders into a shadow root, so it cannot be restyled from the
|
|
7
|
+
* outside — but CSS custom properties inherit through the shadow boundary, which
|
|
8
|
+
* is exactly how the WordPress plugin themes it. Same trick here.
|
|
9
|
+
*/
|
|
10
|
+
export const THEME_CSS: string = THEME_CSS_TEXT;
|
|
11
|
+
|
|
12
|
+
const STYLE_MARKER = "data-snutig-react-theme";
|
|
13
|
+
|
|
14
|
+
let injected = false;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Adds the theme `<style>` to `<head>` once per document. Idempotent, and a no-op
|
|
18
|
+
* without a DOM or when a tag is already present (e.g. `@snutig/react/theme.css`
|
|
19
|
+
* imported through a bundler instead).
|
|
20
|
+
*/
|
|
21
|
+
export function injectSnutigTheme(nonce?: string): void {
|
|
22
|
+
if (injected || typeof document === "undefined") return;
|
|
23
|
+
injected = true;
|
|
24
|
+
|
|
25
|
+
if (document.querySelector(`style[${STYLE_MARKER}], link[${STYLE_MARKER}]`)) return;
|
|
26
|
+
|
|
27
|
+
const style = document.createElement("style");
|
|
28
|
+
style.setAttribute(STYLE_MARKER, "");
|
|
29
|
+
if (nonce) style.nonce = nonce;
|
|
30
|
+
style.textContent = THEME_CSS;
|
|
31
|
+
// Prepended so app stylesheets of equal specificity still win.
|
|
32
|
+
document.head.prepend(style);
|
|
33
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Element + event types for `<snutig-captcha>`.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately a self-contained mirror of `@snutig/widget/src/widget.d.ts`:
|
|
5
|
+
* `@snutig/widget` is a private workspace package (its bundle is served by the
|
|
6
|
+
* server at `/widget/v1/widget.js`, never installed by customers), so a
|
|
7
|
+
* published `@snutig/react` cannot depend on it. **The two files must move
|
|
8
|
+
* together** — a new error code or i18n key there needs one here too.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Error codes raised by the widget itself. */
|
|
12
|
+
export type SnutigWidgetErrorCode =
|
|
13
|
+
| "missing_endpoint"
|
|
14
|
+
| "network_error"
|
|
15
|
+
| "challenge_parse_error"
|
|
16
|
+
| "challenge_unsupported"
|
|
17
|
+
| "solve_failed"
|
|
18
|
+
| "instr_timeout"
|
|
19
|
+
| "instr_blocked"
|
|
20
|
+
| "redeem_failed"
|
|
21
|
+
| "invalid_solution"
|
|
22
|
+
| "invalid_expires"
|
|
23
|
+
| "wasm_load_failed"
|
|
24
|
+
| "worker_spawn_failed"
|
|
25
|
+
| "unknown";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Widget codes plus the ones this binding adds: `script_load_failed` when
|
|
29
|
+
* `/widget/v1/widget.js` could not be fetched (the element then never upgrades,
|
|
30
|
+
* so the widget can raise nothing itself).
|
|
31
|
+
*/
|
|
32
|
+
export type SnutigErrorCode = SnutigWidgetErrorCode | "script_load_failed";
|
|
33
|
+
|
|
34
|
+
export interface SnutigErrorDetail {
|
|
35
|
+
isSnutig: boolean;
|
|
36
|
+
code: SnutigErrorCode;
|
|
37
|
+
message: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface SnutigSolveDetail {
|
|
41
|
+
token: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface SnutigProgressDetail {
|
|
45
|
+
progress: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface SolveResult {
|
|
49
|
+
success: boolean;
|
|
50
|
+
token: string | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The upgraded custom element. */
|
|
54
|
+
export interface SnutigCaptchaElement extends HTMLElement {
|
|
55
|
+
readonly token: string | null;
|
|
56
|
+
readonly tokenValue: string | null;
|
|
57
|
+
solve(): Promise<SolveResult>;
|
|
58
|
+
reset(): void;
|
|
59
|
+
setWorkersCount(workers: number): void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Globals the widget bundle reads off `window`. */
|
|
63
|
+
export interface SnutigWindow {
|
|
64
|
+
SNUTIG_CUSTOM_FETCH?: typeof fetch;
|
|
65
|
+
SNUTIG_WASM_URL?: string;
|
|
66
|
+
SNUTIG_CSS_NONCE?: string;
|
|
67
|
+
SNUTIG_SCRIPT_NONCE?: string;
|
|
68
|
+
SNUTIG_DONT_SKIP_REDEFINE?: boolean;
|
|
69
|
+
SNUTIG_DISABLE_HAPTICS?: boolean;
|
|
70
|
+
SNUTIG_DEBUG?: boolean;
|
|
71
|
+
SNUTIG_SILENT?: boolean;
|
|
72
|
+
}
|