From 4613b7fa58a1f3576261f2ed4dc60254b65ab02a Mon Sep 17 00:00:00 2001 From: maitamgk Date: Thu, 5 Mar 2026 02:20:48 +0700 Subject: [PATCH] feat(utils): add DOM utility functions Co-authored-by: DHV Team --- src/utils/domUtils.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/utils/domUtils.ts 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; +}