Files
nuxtfiber/app/composables/useTurnstileScale.ts
Beyhan Oğur 7b2b27a42c first commit
2026-04-26 22:18:17 +03:00

39 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { onBeforeUnmount, onMounted, type Ref } from 'vue'
/** Cloudflare Turnstile varsayılan genişliği (px) */
const TURNSTILE_WIDTH = 300
/**
* Turnstile widget'ı sarmalayan elementin genişliğine göre --turnstile-scale CSS değişkenini günceller.
* Böylece widget form alanı genişliğinde görünür.
*/
export function useTurnstileScale(wrapperRef: Ref<HTMLElement | null>) {
function updateScale() {
const el = wrapperRef.value
if (!el) return
const w = el.offsetWidth
if (w > 0) {
const scale = w / TURNSTILE_WIDTH
el.style.setProperty('--turnstile-scale', String(scale))
}
}
let observer: ResizeObserver | null = null
onMounted(() => {
updateScale()
const el = wrapperRef.value
if (el && typeof ResizeObserver !== 'undefined') {
observer = new ResizeObserver(updateScale)
observer.observe(el)
}
})
onBeforeUnmount(() => {
if (observer && wrapperRef.value) {
observer.unobserve(wrapperRef.value)
observer = null
}
})
}