trustcaptcha-svelte 0.0.7 → 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Frederic Lehmann
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 CHANGED
@@ -1,5 +1,7 @@
1
1
  # TrustCaptcha for Svelte
2
2
 
3
+ > **Note:** This is a community integration, not officially maintained by TrustCaptcha.
4
+
3
5
  A Svelte 5 component for integrating [TrustCaptcha](https://www.trustcomponent.com/) CAPTCHA widget into your Svelte applications.
4
6
 
5
7
  ## Features
@@ -63,6 +65,7 @@ yarn add trustcaptcha-svelte
63
65
  | `customTranslations` | `string \| CustomTranslation[]` | - | Custom text translations |
64
66
  | `customDesign` | `string \| CustomDesign` | - | Custom design configuration (requires license) |
65
67
  | `privacyUrl` | `string` | - | Link to your privacy policy (requires license) |
68
+ | `children` | `Snippet` | - | Optional loading placeholder content |
66
69
 
67
70
  ## Events
68
71
 
@@ -103,6 +106,22 @@ You can call these methods using a component reference:
103
106
  - `startVerification()` - Manually start the CAPTCHA verification
104
107
  - `reset()` - Reset the CAPTCHA to initial state
105
108
 
109
+ ## Loading Placeholder
110
+
111
+ You can provide custom loading UI that displays while the CAPTCHA script is loading:
112
+
113
+ ```svelte
114
+ <script lang="ts">
115
+ import { TrustCaptcha } from 'trustcaptcha-svelte';
116
+ </script>
117
+
118
+ <TrustCaptcha sitekey="your_site_key_here">
119
+ <div>Loading CAPTCHA...</div>
120
+ </TrustCaptcha>
121
+ ```
122
+
123
+ The loading placeholder will be displayed until the TrustCaptcha script is fully loaded and the component is mounted. If no children are provided, nothing will render during the loading phase.
124
+
106
125
  ## TypeScript Support
107
126
 
108
127
  Full TypeScript definitions are included:
@@ -1,4 +1,5 @@
1
1
  <script lang="ts">
2
+ import { onMount } from 'svelte';
2
3
  import type { TrustCaptchaProps } from './index.js';
3
4
 
4
5
  let {
@@ -15,13 +16,13 @@
15
16
  oncaptchaSolved,
16
17
  oncaptchaFailed,
17
18
  oncaptchaReset,
19
+ children,
18
20
  ...rest
19
21
  }: TrustCaptchaProps = $props();
20
22
 
21
23
  let captchaElement: HTMLElement | null = $state(null);
22
- let scriptLoaded = $state(
23
- typeof customElements !== 'undefined' && !!customElements.get('trustcaptcha-component')
24
- );
24
+ let mounted = $state(false);
25
+ let scriptLoaded = $state(false);
25
26
 
26
27
  const customTranslationsStr = $derived(
27
28
  typeof customTranslations === 'object' ? JSON.stringify(customTranslations) : customTranslations
@@ -31,12 +32,31 @@
31
32
  typeof customDesign === 'object' ? JSON.stringify(customDesign) : customDesign
32
33
  );
33
34
 
34
- function onScriptLoad() {
35
- scriptLoaded = true;
36
- }
35
+ onMount(() => {
36
+ mounted = true;
37
+
38
+ if (typeof customElements !== 'undefined' && customElements.get('trustcaptcha-component')) {
39
+ scriptLoaded = true;
40
+ return;
41
+ }
42
+
43
+ const script = document.createElement('script');
44
+ script.type = 'module';
45
+ script.src = 'https://cdn.trustcomponent.com/trustcaptcha/2.1.x/trustcaptcha.esm.min.js';
46
+ script.onload = () => {
47
+ scriptLoaded = true;
48
+ };
49
+ document.head.appendChild(script);
50
+
51
+ return () => {
52
+ if (script.parentNode) {
53
+ script.parentNode.removeChild(script);
54
+ }
55
+ };
56
+ });
37
57
 
38
58
  $effect(() => {
39
- if (!captchaElement || !scriptLoaded) return;
59
+ if (!captchaElement || !scriptLoaded || !mounted) return;
40
60
 
41
61
  const handlers: Record<string, EventListener | undefined> = {
42
62
  captchaStarted: oncaptchaStarted as EventListener | undefined,
@@ -69,26 +89,20 @@
69
89
  }
70
90
  </script>
71
91
 
72
- <svelte:head>
73
- <!-- eslint-disable svelte/no-useless-mustaches -->
74
- <script
75
- onload={onScriptLoad}
76
- type={'module'}
77
- src="https://cdn.trustcomponent.com/trustcaptcha/2.1.x/trustcaptcha.esm.min.js"
78
- ></script>
79
- <!-- eslint-enable svelte/no-useless-mustaches -->
80
- </svelte:head>
81
-
82
- <trustcaptcha-component
83
- bind:this={captchaElement}
84
- autostart={autostart ? undefined : 'false'}
85
- hide-branding={hideBranding ? 'true' : undefined}
86
- invisible={invisible ? 'true' : undefined}
87
- invisible-hint={invisibleHint}
88
- bypass-token={bypassToken}
89
- token-field-name={tokenFieldName}
90
- custom-translations={customTranslationsStr}
91
- custom-design={customDesignStr}
92
- privacy-url={privacyUrl}
93
- {...rest}
94
- ></trustcaptcha-component>
92
+ {#if mounted && scriptLoaded}
93
+ <trustcaptcha-component
94
+ bind:this={captchaElement}
95
+ autostart={autostart ? undefined : 'false'}
96
+ hide-branding={hideBranding ? 'true' : undefined}
97
+ invisible={invisible ? 'true' : undefined}
98
+ invisible-hint={invisibleHint}
99
+ bypass-token={bypassToken}
100
+ token-field-name={tokenFieldName}
101
+ custom-translations={customTranslationsStr}
102
+ custom-design={customDesignStr}
103
+ privacy-url={privacyUrl}
104
+ {...rest}
105
+ ></trustcaptcha-component>
106
+ {:else}
107
+ {@render children?.()}
108
+ {/if}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default as TrustCaptcha } from './captcha.svelte';
2
+ import type { Snippet } from 'svelte';
2
3
  export interface TrustCaptchaProps {
3
4
  /** Sitekey of the TrustCaptcha. You can find this on the administration page of your TrustCaptcha. */
4
5
  sitekey: string;
@@ -40,6 +41,8 @@ export interface TrustCaptchaProps {
40
41
  oncaptchaReset?: (event: CustomEvent) => void;
41
42
  /** Custom CSS class for the TrustCaptcha component. */
42
43
  class?: string;
44
+ /** Optional loading placeholder content shown while the CAPTCHA script is loading. */
45
+ children?: Snippet;
43
46
  }
44
47
  export interface CaptchaError {
45
48
  errorCode: ErrorCode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trustcaptcha-svelte",
3
- "version": "0.0.7",
3
+ "version": "0.1.0",
4
4
  "license": "MIT",
5
5
  "description": "A Svelte 5 component for integrating TrustCaptcha CAPTCHA widget into your Svelte applications",
6
6
  "author": "Frederic Lehmann",
@@ -49,10 +49,10 @@
49
49
  "prettier": "^3.8.1",
50
50
  "prettier-plugin-svelte": "^3.4.1",
51
51
  "publint": "^0.3.17",
52
- "svelte": "^5.48.1",
52
+ "svelte": "^5.48.3",
53
53
  "svelte-check": "^4.3.5",
54
54
  "typescript": "^5.9.3",
55
- "typescript-eslint": "^8.53.1",
55
+ "typescript-eslint": "^8.54.0",
56
56
  "vite": "^7.3.1",
57
57
  "vitest": "^4.0.18",
58
58
  "vitest-browser-svelte": "^2.0.2"