@redwoodjs/sdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
26 lines (25 loc) • 932 B
JavaScript
"use client";
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useRef, useState } from "react";
export function TurnstileWidget({ siteKey, enabled, onSuccess, }) {
const ref = useRef(null);
const [shown, setShown] = useState(false);
useEffect(() => {
if (!enabled || !ref.current || !window.turnstile)
return;
// Render Turnstile widget **only once per session**
if (!shown) {
setShown(true);
const widgetId = window.turnstile.render(ref.current, {
sitekey: siteKey,
callback: onSuccess, // Token is passed here
});
return () => {
if (window.turnstile && widgetId) {
window.turnstile.remove(widgetId);
}
};
}
}, [siteKey, enabled, onSuccess, shown]);
return enabled ? _jsx("div", { ref: ref }) : null;
}