Skip to content

Commit 089b25b

Browse files
authored
Fix destroyed when element visibility changes
Problem: When the Turnstile field becomes hidden and then visible again (e.g., switching tabs, collapsing panels, or dynamic content changes), the Cloudflare Turnstile widget is destroyed and doesn't re-initialize, leaving users unable to complete the captcha. Solution: Added an IntersectionObserver to detect when the Turnstile container enters the viewport and automatically re-renders the widget if it's missing. Includes a safety check to ensure the Turnstile API is loaded before attempting to render. Changes: - Added IntersectionObserver in the Alpine.js x-init to monitor element visibility - Added window.turnstile check to prevent rendering before API loads - Added element existence check before observing Testing: - Place Turnstile field in collapsible content or tabs - Hide and show the container multiple times - Verify captcha re-appears and functions correctly
1 parent 23f00a2 commit 089b25b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

resources/views/components/turnstile.blade.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
3434
}
3535
3636
$wire.on('reset-captcha', () => resetCaptcha())
37+
38+
const observer = new IntersectionObserver((entries) => {
39+
entries.forEach(entry => {
40+
if (entry.isIntersecting &&
41+
window.turnstile &&
42+
!$refs.turnstile.querySelector('.cf-turnstile')) {
43+
turnstile.render($refs.turnstile, options);
44+
}
45+
});
46+
}, { threshold: 0.1 })
47+
48+
if ($refs.turnstile) {
49+
observer.observe($refs.turnstile);
50+
}
3751
})()"
3852
>
3953
<div data-sitekey="{{config('turnstile.turnstile_site_key')}}"

0 commit comments

Comments
 (0)