Skip to content

Commit

Permalink
feat: Add onWidgetLoad callback
Browse files Browse the repository at this point in the history
fix #45
  • Loading branch information
marsidev committed Jan 8, 2024
1 parent 759a392 commit 6811bce
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ These are the props you can pass to the `Turnstile` component.
| `siteKey` | string | Your sitekey key, get one from [here](https://developers.cloudflare.com/turnstile/get-started/). ||
| ``options`` | object | Widget render options. More info about this options [below](/props#options-prop). | |
| `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). | |
| `onWidgetLoad` | function | Callback invoked when the widget is loaded or reloaded (e.g. after a reset). | |
| `onSuccess` | function | Callback invoked upon success of the challenge. The callback is passed a token that can be validated. | |
| `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. | |
| `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). | |
Expand Down
5 changes: 4 additions & 1 deletion packages/lib/src/lib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
scriptOptions,
options = {},
siteKey,
onWidgetLoad,
onSuccess,
onExpire,
onError,
Expand Down Expand Up @@ -244,10 +245,12 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
if (!widgetId) return
if (!checkElementExistence(widgetId)) return

onWidgetLoad?.(widgetId)

return () => {
window.turnstile!.remove(widgetId)
}
}, [widgetId])
}, [widgetId, onWidgetLoad])

useEffect(() => {
setContainerStyle(
Expand Down
5 changes: 5 additions & 0 deletions packages/lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ interface TurnstileProps extends React.HTMLAttributes<HTMLDivElement> {
*/
siteKey: RenderOptions['sitekey']

/**
* Callback invoked when the widget is loaded or reloaded (e.g. after a reset).
*/
onWidgetLoad?: (widgetID: string) => void

/**
* Callback invoked upon success of the challenge. The callback is passed a token that can be validated.
* @param token - Token response.
Expand Down

0 comments on commit 6811bce

Please sign in to comment.