MCPcopy Create free account
hub / github.com/ColmapView/Colmapview.github.io / clampToViewport

Function clampToViewport

src/hooks/useModalDrag.ts:42–55  ·  view source on GitHub ↗

Clamp a modal position so it stays within the viewport

(
  x: number,
  y: number,
  width: number,
  height: number,
)

Source from the content-addressed store, hash-verified

40
41/** Clamp a modal position so it stays within the viewport */
42function clampToViewport(
43 x: number,
44 y: number,
45 width: number,
46 height: number,
47): { x: number; y: number } {
48 const vw = typeof window !== 'undefined' ? window.innerWidth : VIEWPORT_FALLBACK.width;
49 const vh = typeof window !== 'undefined' ? window.innerHeight : VIEWPORT_FALLBACK.height;
50 const padding = MODAL_POSITION.viewportPadding;
51 return {
52 x: Math.max(padding, Math.min(x, vw - width - padding)),
53 y: Math.max(padding, Math.min(y, vh - height - padding)),
54 };
55}
56
57function getViewportSize(): { width: number; height: number } {
58 return typeof window !== 'undefined'

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected