Skip to content

Commit 4613b7f

Browse files
maitamdevDHV Team
andcommitted
feat(utils): add DOM utility functions
Co-authored-by: DHV Team <dhvteam@users.noreply.github.com>
1 parent 153dc05 commit 4613b7f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/utils/domUtils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// DOM utility functions
2+
export function isElementInViewport(el: Element): boolean {
3+
const rect = el.getBoundingClientRect();
4+
return rect.top >= 0 && rect.left >= 0 && rect.bottom <= window.innerHeight && rect.right <= window.innerWidth;
5+
}
6+
export function copyToClipboard(text: string): Promise<void> {
7+
if (navigator.clipboard) return navigator.clipboard.writeText(text);
8+
const textarea = document.createElement('textarea');
9+
textarea.value = text;
10+
textarea.style.position = 'fixed';
11+
textarea.style.opacity = '0';
12+
document.body.appendChild(textarea);
13+
textarea.select();
14+
document.execCommand('copy');
15+
document.body.removeChild(textarea);
16+
return Promise.resolve();
17+
}
18+
export function getScrollPercentage(): number {
19+
const h = document.documentElement;
20+
const b = document.body;
21+
const scrollTop = h.scrollTop || b.scrollTop;
22+
const scrollHeight = (h.scrollHeight || b.scrollHeight) - h.clientHeight;
23+
return scrollHeight > 0 ? Math.round((scrollTop / scrollHeight) * 100) : 0;
24+
}

0 commit comments

Comments
 (0)