refactor contact form
- Change positions of submit button and turnstile widget - Turnstile widget only appears on interaction with contact form
This commit is contained in:
@@ -15,10 +15,14 @@
|
|||||||
let errorMessage = $state('')
|
let errorMessage = $state('')
|
||||||
let turnstileWidgetId: string | null = $state(null)
|
let turnstileWidgetId: string | null = $state(null)
|
||||||
let turnstileContainer: HTMLElement
|
let turnstileContainer: HTMLElement
|
||||||
|
let turnstileInitialized = false
|
||||||
|
|
||||||
const TURNSTILE_SITEKEY = sitekey
|
const TURNSTILE_SITEKEY = sitekey
|
||||||
|
|
||||||
onMount(() => {
|
function initTurnstile() {
|
||||||
|
if (turnstileInitialized) return
|
||||||
|
turnstileInitialized = true
|
||||||
|
|
||||||
// Wait for Turnstile script to load
|
// Wait for Turnstile script to load
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
if (typeof window.turnstile !== 'undefined') {
|
if (typeof window.turnstile !== 'undefined') {
|
||||||
@@ -32,9 +36,10 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}, 100)
|
}, 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
return () => {
|
return () => {
|
||||||
clearInterval(interval)
|
|
||||||
if (turnstileWidgetId !== null && typeof window.turnstile !== 'undefined') {
|
if (turnstileWidgetId !== null && typeof window.turnstile !== 'undefined') {
|
||||||
window.turnstile.remove(turnstileWidgetId)
|
window.turnstile.remove(turnstileWidgetId)
|
||||||
}
|
}
|
||||||
@@ -111,7 +116,7 @@
|
|||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<form onsubmit={handleSubmit} class="flex flex-col gap-4">
|
<form onsubmit={handleSubmit} onfocusin={initTurnstile} class="flex flex-col gap-4">
|
||||||
<div class="flex flex-col gap-2">
|
<div class="flex flex-col gap-2">
|
||||||
<label for="name" class="text-sm font-medium text-foreground">
|
<label for="name" class="text-sm font-medium text-foreground">
|
||||||
Name <span class="text-destructive">*</span>
|
Name <span class="text-destructive">*</span>
|
||||||
@@ -157,8 +162,7 @@
|
|||||||
></textarea>
|
></textarea>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Turnstile Widget -->
|
|
||||||
<div bind:this={turnstileContainer} class="flex justify-center"></div>
|
|
||||||
|
|
||||||
{#if formState === 'success'}
|
{#if formState === 'success'}
|
||||||
<div class="flex items-center gap-2 rounded-md bg-primary/10 border border-primary/20 px-4 py-3 text-sm text-primary">
|
<div class="flex items-center gap-2 rounded-md bg-primary/10 border border-primary/20 px-4 py-3 text-sm text-primary">
|
||||||
@@ -174,6 +178,10 @@
|
|||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
<div class="flex items-start justify-between gap-4 min-h-[65px]">
|
||||||
|
<!-- Turnstile Widget (300x65 for normal size) -->
|
||||||
|
<div bind:this={turnstileContainer} class="min-w-[300px]"></div>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
disabled={formState === 'submitting'}
|
disabled={formState === 'submitting'}
|
||||||
@@ -187,4 +195,5 @@
|
|||||||
Send Message
|
Send Message
|
||||||
{/if}
|
{/if}
|
||||||
</button>
|
</button>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Reference in New Issue
Block a user