diff --git a/src/utils/domUtils.ts b/src/utils/domUtils.ts new file mode 100644 index 0000000..6a4b0b1 --- /dev/null +++ b/src/utils/domUtils.ts @@ -0,0 +1,24 @@ +// DOM utility functions +export function isElementInViewport(el: Element): boolean { + const rect = el.getBoundingClientRect(); + return rect.top >= 0 && rect.left >= 0 && rect.bottom <= window.innerHeight && rect.right <= window.innerWidth; +} +export function copyToClipboard(text: string): Promise { + if (navigator.clipboard) return navigator.clipboard.writeText(text); + const textarea = document.createElement('textarea'); + textarea.value = text; + textarea.style.position = 'fixed'; + textarea.style.opacity = '0'; + document.body.appendChild(textarea); + textarea.select(); + document.execCommand('copy'); + document.body.removeChild(textarea); + return Promise.resolve(); +} +export function getScrollPercentage(): number { + const h = document.documentElement; + const b = document.body; + const scrollTop = h.scrollTop || b.scrollTop; + const scrollHeight = (h.scrollHeight || b.scrollHeight) - h.clientHeight; + return scrollHeight > 0 ? Math.round((scrollTop / scrollHeight) * 100) : 0; +}