UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

30 lines (29 loc) 863 B
import { ownerWindow } from '@base-ui-components/utils/owner'; // Calculates the viewport rect for the virtual cursor. export function getViewportRect(teleportDistance, scrubAreaEl) { const win = ownerWindow(scrubAreaEl); const rect = scrubAreaEl.getBoundingClientRect(); if (rect && teleportDistance != null) { return { x: rect.left - teleportDistance / 2, y: rect.top - teleportDistance / 2, width: rect.right + teleportDistance / 2, height: rect.bottom + teleportDistance / 2 }; } const vV = win.visualViewport; if (vV) { return { x: vV.offsetLeft, y: vV.offsetTop, width: vV.offsetLeft + vV.width, height: vV.offsetTop + vV.height }; } return { x: 0, y: 0, width: win.document.documentElement.clientWidth, height: win.document.documentElement.clientHeight }; }