Skip to content

Commit f4b7681

Browse files
authored
Merge pull request #46 from marsidev/feature/widget-loading-callback
feat: Add `onWidgetLoad` callback
2 parents cbc17ff + b1026b1 commit f4b7681

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

demos/nextjs/.node-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v20.10.0

docs/props.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ These are the props you can pass to the `Turnstile` component.
1111
| `siteKey` | string | Your sitekey key, get one from [here](https://developers.cloudflare.com/turnstile/get-started/). ||
1212
| ``options`` | object | Widget render options. More info about this options [below](/props#options-prop). | |
1313
| `scriptOptions` | object | You can customize the injected `script` tag with this prop. It allows you to add `'async'`, `'defer'`, `'nonce'` attributes to the script tag. You can also control whether the injected script will be added to the document body or head with `appendTo` attribute. More info about this options [below](/props#scriptoptions-prop). | |
14+
| `onWidgetLoad` | function | Callback invoked when the widget is loaded or reloaded (e.g. after a reset). | |
1415
| `onSuccess` | function | Callback invoked upon success of the challenge. The callback is passed a token that can be validated. | |
1516
| `onExpire` | function | Callback invoked when a challenge expires. Read the [Cloudflare docs](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#refreshing-a-widget) for more info about handling expired widgets. | |
1617
| `onError` | function | Callback invoked when there is an error (e.g. network error or the challenge failed). Refer to [Client-side errors](https://developers.cloudflare.com/turnstile/reference/client-side-errors). | |

packages/lib/src/lib.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
1717
scriptOptions,
1818
options = {},
1919
siteKey,
20+
onWidgetLoad,
2021
onSuccess,
2122
onExpire,
2223
onError,
@@ -244,10 +245,12 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
244245
if (!widgetId) return
245246
if (!checkElementExistence(widgetId)) return
246247

248+
onWidgetLoad?.(widgetId)
249+
247250
return () => {
248251
window.turnstile!.remove(widgetId)
249252
}
250-
}, [widgetId])
253+
}, [widgetId, onWidgetLoad])
251254

252255
useEffect(() => {
253256
setContainerStyle(

packages/lib/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ interface TurnstileProps extends React.HTMLAttributes<HTMLDivElement> {
299299
*/
300300
siteKey: RenderOptions['sitekey']
301301

302+
/**
303+
* Callback invoked when the widget is loaded or reloaded (e.g. after a reset).
304+
*/
305+
onWidgetLoad?: (widgetID: string) => void
306+
302307
/**
303308
* Callback invoked upon success of the challenge. The callback is passed a token that can be validated.
304309
* @param token - Token response.

0 commit comments

Comments
 (0)