Skip to content

Commit da44f4a

Browse files
committed
feat: call remove
1 parent 516f77a commit da44f4a

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/lib/Turnstile.svelte

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<script context="module" lang="ts">
2+
// Credit for the type defs go to @Le0Developer
3+
// https://github.com/Le0Developer/react-turnstile/blob/420eddf1e0bde2ad593dd78bd99b8f134ce8a754/src/index.tsx#L139
4+
25
declare global {
36
interface Window {
4-
onloadTurnstileCallback: () => void;
57
turnstile: {
68
render: (
79
element: string | HTMLElement,
810
options: TurnstileOptions,
911
) => string;
1012
reset: (widgetId: string) => void;
1113
getResponse: (widgetId: string) => string | undefined;
14+
remove: (widgetId: string) => void;
1215
};
1316
}
1417
}
@@ -22,13 +25,19 @@
2225
'expired-callback'?: () => void;
2326
theme?: TurnstileTheme;
2427
tabindex?: number;
28+
29+
// Undocumented Fields - not implemented in svelte-turnstile yet
30+
size?: 'normal' | 'invisible' | 'compact';
31+
'response-field'?: boolean;
32+
'response-field-name'?: string;
2533
}
2634
2735
export type TurnstileTheme = 'light' | 'dark' | 'auto';
2836
</script>
2937

3038
<script lang="ts">
3139
import { createEventDispatcher } from 'svelte';
40+
import type { Action } from 'svelte/action';
3241
import { onMount } from 'svelte';
3342
3443
const dispatch = createEventDispatcher<{
@@ -78,8 +87,8 @@
7887
dispatch('turnstile-callback', { token });
7988
}
8089
81-
const turnstile = (node: HTMLElement) => {
82-
widgetId = window.turnstile.render(node, {
90+
const turnstile: Action = (node) => {
91+
const id = window.turnstile.render(node, {
8392
'expired-callback': expired,
8493
'error-callback': error,
8594
callback,
@@ -91,6 +100,14 @@
91100
theme,
92101
cData,
93102
});
103+
104+
widgetId = id;
105+
106+
return {
107+
destroy: () => {
108+
window.turnstile.remove(id);
109+
},
110+
};
94111
};
95112
</script>
96113

0 commit comments

Comments
 (0)