Skip to content

Conversation

@caio-pizzol
Copy link
Contributor

This commit adds a new utility function, clampToViewport, to ensure that the menu is positioned within the visible area of the viewport. The function adjusts the menu's coordinates based on predefined padding and approximate dimensions, enhancing the user experience by preventing overflow.

…plateBuilder

This commit adds a new utility function, clampToViewport, to ensure that the menu is positioned within the visible area of the viewport. The function adjusts the menu's coordinates based on predefined padding and approximate dimensions, enhancing the user experience by preventing overflow.
@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 enabled auto-merge (squash) October 9, 2025 19:30
@caio-pizzol caio-pizzol merged commit 09e82ee into main Oct 9, 2025
2 checks passed
@caio-pizzol caio-pizzol deleted the cursor/SD-546-clamp-menu-position-to-viewport-b801 branch October 9, 2025 19:31
@superdoc-bot
Copy link

superdoc-bot bot commented Oct 9, 2025

🎉 This PR is included in version 0.2.0-next.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

superdoc-bot bot pushed a commit that referenced this pull request Nov 13, 2025
# [0.2.0](v0.1.0...v0.2.0) (2025-11-13)

### Bug Fixes

* force pre-release ([f752754](f752754))
* improve cleanup logic ([#11](#11)) ([01f0bf9](01f0bf9))
* update field ID type and improve field handling ([#14](#14)) ([e0e6d31](e0e6d31))
* update field IDs and categories in README and App component ([61a473d](61a473d))

### Features

* add import functionality for .docx files in the template builder ([#15](#15)) ([42faccc](42faccc))
* enhance exportTemplate functionality with configurable options ([#17](#17)) ([7e2a03d](7e2a03d))
* enhance field handling with mode support in template builder ([#16](#16)) ([d46ab5d](d46ab5d))
* implement viewport clamping for menu positioning in SuperDocTemplateBuilder ([#10](#10)) ([09e82ee](09e82ee))
@superdoc-bot
Copy link

superdoc-bot bot commented Nov 13, 2025

🎉 This PR is included in version 0.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants