Skip to content

Conversation

@caio-pizzol
Copy link
Contributor

Clamp menu position to viewport bounds to prevent it from appearing off-screen near window edges.

This is achieved by introducing a clampToViewport function that uses hard-coded approximate menu dimensions and a fixed padding to adjust the menu's coordinates before setting its position, as per the issue's recommended simple fix.


Linear Issue: SD-546

Open in Cursor Open in Web

Co-authored-by: caiopizzol <caiopizzol@gmail.com>
@cursor
Copy link

cursor bot commented Oct 9, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@linear
Copy link

linear bot commented Oct 9, 2025

SD-546 Fix menu positioning near viewport edges

Summary

Clamp menu position to viewport bounds so menu doesn't appear off-screen when trigger is near window edges

Tasks

  • Create simple viewport boundary checking function
  • Apply clamping to menu position before setting state
  • Test trigger near all four viewport edges
  • Verify menu stays visible and properly positioned

Notes

Problem: When user types trigger near right/bottom edge of viewport, menu appears partially or fully off-screen

Current code (src/index.tsx):

const coords = e.view.coordsAtPos(from);
const bounds = new DOMRect(coords.left, coords.top, 0, 0);
setMenuPosition(bounds); // No boundary checking

Simple fix (don't over-engineer):

const clampToViewport = (rect: DOMRect): DOMRect => {
  const menuWidth = 250; // Approximate menu width
  const menuHeight = 300; // Approximate menu height
  
  const left = Math.min(
    rect.left,
    window.innerWidth - menuWidth - 10 // 10px padding
  );
  
  const top = Math.min(
    rect.top,
    window.innerHeight - menuHeight - 10
  );
  
  return new DOMRect(Math.max(left, 10), Math.max(top, 10), 0, 0);
};

// Usage
const coords = e.view.coordsAtPos(from);
const bounds = clampToViewport(new DOMRect(coords.left, coords.top, 0, 0));
setMenuPosition(bounds);

Don't:

  • Calculate actual menu dimensions (expensive)
  • Add complex positioning logic (top/left/right/bottom)
  • Use portal/overlay libraries

Do: Simple hard-coded approximations work fine for 99% of cases

@caio-pizzol caio-pizzol closed this Oct 9, 2025
@caio-pizzol caio-pizzol deleted the cursor/SD-546-clamp-menu-position-to-viewport-b801 branch October 9, 2025 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants