Skip to content

fix: Improperly aligned unfolding sub-items in context menu in data browser #2726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: alpha
Choose a base branch
from

Conversation

dblythy
Copy link
Member

@dblythy dblythy commented Apr 7, 2025

New Pull Request Checklist

Issue Description

Closes: #2635

Approach

TODOs before merging

  • Add tests
  • Add changes to documentation (guides, repository pages, in-code descriptions)

Summary by CodeRabbit

  • New Features

    • Improved submenu positioning to better fit within the visible screen area, ensuring menus do not overflow the viewport.
    • Enhanced submenu navigation for more consistent visibility when hovering over menu items.
  • Bug Fixes

    • Addressed issues with submenu alignment and offset, resulting in more accurate menu placement.

Copy link

parse-github-assistant bot commented Apr 7, 2025

🚀 Thanks for opening this pull request! We appreciate your effort in improving the project. Please let us know once your pull request is ready for review.

@dblythy dblythy changed the title Update ContextMenu.react.js fix: context menu position Apr 7, 2025
Copy link

I will reformat the title to use the proper commit message syntax.

@parse-github-assistant parse-github-assistant bot changed the title fix: context menu position fix: Context menu position Apr 7, 2025
Copy link

uffizzi-cloud bot commented Apr 7, 2025

Uffizzi Ephemeral Environment deployment-62506

⌚ Updated Apr 07, 2025, 12:11 UTC

☁️ https://app.uffizzi.com/github.com/parse-community/parse-dashboard/pull/2726

📄 View Application Logs etc.

What is Uffizzi? Learn more

@mtrezza
Copy link
Member

mtrezza commented Apr 7, 2025

I tried out all the positions to see which ones work. It's painful that we have to spend time on fixing something that should work OOTB. I hope we get to #2460 soon.

OK

top-right short: OK

image

top-right long: OK

image

bottom-right long: OK

image

bottom-left long: OK

image

top-left short: OK

image

top-left long: OK

image

NOK

bottom-right short: NOK - subitem should align with top border; I guess the logic could be, if the count of sub-items <= count of main items, then align with the top border, as we know there is enough space towards the bottom.

image

bottom-left short: NOK

image

@mtrezza mtrezza changed the title fix: Context menu position fix: Improperly aligned unfolding sub-items in context menu in data browser Apr 7, 2025
@dblythy dblythy requested a review from a team April 9, 2025 10:51
@mtrezza mtrezza closed this Apr 10, 2025
@mtrezza mtrezza reopened this Apr 10, 2025
@mtrezza mtrezza closed this Apr 10, 2025
@mtrezza mtrezza reopened this Apr 10, 2025
Copy link

uffizzi-cloud bot commented Apr 10, 2025

Uffizzi Ephemeral Environment deployment-62642

⌚ Updated Apr 10, 2025, 01:15 UTC

☁️ https://app.uffizzi.com/github.com/parse-community/parse-dashboard/pull/2726

📄 View Application Logs etc.

What is Uffizzi? Learn more

Copy link
Member

@mtrezza mtrezza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dashboard crashes when opening session with uffizzi link

@mtrezza
Copy link
Member

mtrezza commented Apr 10, 2025

The issue seems to be a different one; I've opened #2743 with high priority, as it's currently blocking the other PRs.

@mtrezza mtrezza closed this Apr 13, 2025
@mtrezza mtrezza reopened this Apr 13, 2025
Copy link

uffizzi-cloud bot commented Apr 13, 2025

Uffizzi Ephemeral Environment deployment-62729

⌚ Updated Apr 13, 2025, 14:24 UTC

☁️ https://app.uffizzi.com/github.com/parse-community/parse-dashboard/pull/2726

📄 View Application Logs etc.

What is Uffizzi? Learn more

Copy link
Member

@mtrezza mtrezza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sub-menu is always shown, even if the mouse is not over its parent item. In the screenshot below the mouse was hovering over "Edit row":

image

@parseplatformorg
Copy link
Contributor

parseplatformorg commented Apr 25, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

@dblythy dblythy requested a review from a team April 26, 2025 09:47
Copy link

coderabbitai bot commented Apr 29, 2025

📝 Walkthrough

Walkthrough

The changes enhance the context menu component by refining how submenu positioning is calculated and rendered. The positioning logic in getPositionToFitVisibleScreen now accounts for offsets, menu item counts, and viewport boundaries to ensure submenus appear aligned with their parent items and remain fully visible. The MenuSection and ContextMenu components were updated to support and utilize these new parameters, passing necessary data for accurate positioning and improving submenu hover behavior. The code structure was also cleaned up, particularly around state and ref management.

Changes

File(s) Change Summary
src/components/ContextMenu/ContextMenu.react.js Enhanced getPositionToFitVisibleScreen to accept offset and item count parameters, improving vertical positioning logic. Updated MenuSection and ContextMenu to pass and use these parameters. Added hover handling for submenus.

Assessment against linked issues

Objective Addressed Explanation
Correct context menu offset and submenu alignment (#2635)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
src/components/ContextMenu/ContextMenu.react.js (1)

110-113: Minor: avoid unnecessary state churn on leaf hover
On a leaf item, newPath is identical to path.slice(0, level + 1) which is already the current value. Calling setPath redundantly triggers a re-render.

-            onMouseEnter={() => {
-              const newPath = path.slice(0, level + 1);
-              setPath(newPath);
-            }}
+            onMouseEnter={() => {
+              /* no-op when hovering the same leaf */
+              if (path.length !== level + 1) {
+                setPath(path.slice(0, level + 1));
+              }
+            }}

This is a tiny optimisation but helps keep React DevTools noise down.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 06fc86a and f572a28.

📒 Files selected for processing (1)
  • src/components/ContextMenu/ContextMenu.react.js (4 hunks)
🧰 Additional context used
🪛 ESLint
src/components/ContextMenu/ContextMenu.react.js

[error] 56-56: 'MenuSection' is assigned a value but never used.

(no-unused-vars)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Docker linux/amd64
🔇 Additional comments (1)
src/components/ContextMenu/ContextMenu.react.js (1)

124-150: 👍 Nice refactor around menuRef and outside-click handling
Moving menuRef to a single useRef and de-duplicating the outside-click logic makes the component easier to reason about.

Comment on lines +38 to +44
const prevElStyle = window.getComputedStyle(prevEl);
const prevElTop = parseInt(prevElStyle.top, 10);

if (!shouldApplyOffset) {
y = prevElTop + offset;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

parseInt(...).top may return NaN → submenu disappears
window.getComputedStyle(prevEl).top returns 'auto' for non-positioned elements.
parseInt('auto', 10)NaN which then contaminates y.

-      const prevElStyle = window.getComputedStyle(prevEl);
-      const prevElTop = parseInt(prevElStyle.top, 10);
+      const prevElStyle = window.getComputedStyle(prevEl);
+      const rawTop      = prevElStyle.top;
+      const prevElTop   =
+        rawTop === 'auto' ? prevElBox.top : parseInt(rawTop, 10);

Guarding this value makes the positioning routine robust regardless of the parent element’s CSS positioning context.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents (early access)
In src/components/ContextMenu/ContextMenu.react.js around lines 38 to 44, the code uses parseInt on prevElStyle.top which can be 'auto' for non-positioned elements, resulting in NaN and causing the submenu to disappear. Fix this by checking if prevElStyle.top is a valid numeric value before parsing; if it is 'auto' or invalid, default prevElTop to 0 or another safe fallback value to ensure y is always a valid number for positioning.

Comment on lines 60 to 68
useEffect(() => {
const newPosition = getPositionToFitVisibleScreen(sectionRef);
const newPosition = getPositionToFitVisibleScreen(
sectionRef,
path[level] * 30,
parentItemCount,
items.length
);
newPosition && setPosition(newPosition);
}, [sectionRef]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Missing dependencies in useEffect cause stale positions
useEffect only depends on sectionRef, but the calculated position also relies on path[level], items.length, and parentItemCount. When those change (e.g., the user hovers over a different root item, altering path[level]), the effect will not re-fire because the component instance is reused if the key stays stable.

-  }, [sectionRef]);
+  }, [sectionRef, path, level, items.length, parentItemCount]);

Adding the full dependency list prevents subtle mis-alignments and silences the React-hooks ESLint rule.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
const newPosition = getPositionToFitVisibleScreen(sectionRef);
const newPosition = getPositionToFitVisibleScreen(
sectionRef,
path[level] * 30,
parentItemCount,
items.length
);
newPosition && setPosition(newPosition);
}, [sectionRef]);
useEffect(() => {
const newPosition = getPositionToFitVisibleScreen(
sectionRef,
path[level] * 30,
parentItemCount,
items.length
);
newPosition && setPosition(newPosition);
}, [sectionRef, path, level, items.length, parentItemCount]);
🤖 Prompt for AI Agents (early access)
In src/components/ContextMenu/ContextMenu.react.js around lines 60 to 68, the useEffect hook depends only on sectionRef but also uses path[level], items.length, and parentItemCount. To fix this, add path[level], items.length, and parentItemCount to the dependency array of the useEffect hook so it re-runs correctly when any of these values change, preventing stale position calculations and satisfying React-hooks ESLint rules.

Comment on lines +12 to 34
const getPositionToFitVisibleScreen = (ref, offset = 0, mainItemCount = 0, subItemCount = 0) => {
if (ref.current) {
const elBox = ref.current.getBoundingClientRect();
const y = elBox.y + elBox.height < window.innerHeight ? 0 : 0 - elBox.y + 100;
let y = 0;

const footerHeight = 50;
const lowerLimit = window.innerHeight - footerHeight;
const upperLimit = 0;

if (elBox.bottom > lowerLimit) {
y = lowerLimit - elBox.bottom;
} else if (elBox.top < upperLimit) {
y = upperLimit - elBox.top;
}

const projectedTop = elBox.top + y + offset;
const projectedBottom = projectedTop + elBox.height;

const shouldApplyOffset = subItemCount > mainItemCount;
if (shouldApplyOffset && projectedTop >= upperLimit && projectedBottom <= lowerLimit) {
y += offset;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Guard against NaN offset & out-of-viewport jumps
getPositionToFitVisibleScreen nicely improves submenu placement, but two edge-cases can still break the computed y:

  1. When mainItemCount === 0 (top-level menu) shouldApplyOffset is always true, yet offset is often 0, so the early projectedTop/Bottom check may skip a necessary upward shift if the root element is already partially off-screen.
  2. parseInt later in the function (see below) may yield NaN, propagating into y and producing style={{ top: NaN }} which React silently drops, leaving the menu in an undefined position.

Consider calculating a safe offset and using Number.isFinite(...) guards:

-    const projectedTop = elBox.top + y + offset;
-    const projectedBottom = projectedTop + elBox.height;
-
-    const shouldApplyOffset = subItemCount > mainItemCount;
-    if (shouldApplyOffset && projectedTop >= upperLimit && projectedBottom <= lowerLimit) {
-      y += offset;
-    }
+    const projectedTop    = elBox.top + y + offset;
+    const projectedBottom = projectedTop + elBox.height;
+
+    const shouldApplyOffset = subItemCount > mainItemCount && offset !== 0;
+    if (
+      shouldApplyOffset &&
+      projectedTop    >= upperLimit &&
+      projectedBottom <= lowerLimit
+    ) {
+      y += offset;
+    }

This prevents a “0-offset no-op” and protects against accidental NaN propagation a few lines later.


🏁 Script executed:

#!/bin/bash
# Search for parseInt usage in ContextMenu.react.js with context
grep -R "parseInt" -n src/components/ContextMenu/ContextMenu.react.js -A3 -B3

Length of output: 351


🏁 Script executed:

#!/bin/bash
# Display the complete getPositionToFitVisibleScreen function for context
ast-grep --pattern $'const getPositionToFitVisibleScreen =($_$, $_$, $_$, $_$) => { $$$ }' --quiet -A 50

Length of output: 288


Guard against NaN offsets and zero-offset no-ops
We should tighten getPositionToFitVisibleScreen to handle two remaining edge‐cases:

  1. Zero-offset no-ops on top-level menus.
    When mainItemCount === 0, subItemCount > mainItemCount is always true yet offset is often 0—so we end up skipping a needed reposition for a root menu already off-screen.
  2. NaN from parseInt.
    If window.getComputedStyle(...).top is "auto" (or any non-numeric), parseInt(...,10) returns NaN, which propagates into y and yields style={{ top: NaN }}.

Apply these fixes:

--- a/src/components/ContextMenu/ContextMenu.react.js
+++ b/src/components/ContextMenu/ContextMenu.react.js
@@ –16,13 +16,18 @@ const getPositionToFitVisibleScreen = (ref, offset = 0, mainItemCount = 0, s
     const elBox = ref.current.getBoundingClientRect();
     let y = 0;

-    const footerHeight = 50;
+    const footerHeight = 50;
     const lowerLimit   = window.innerHeight - footerHeight;
     const upperLimit   = 0;

     // existing vertical-clamp logic…
@@ –30,9 +35,13 @@ const getPositionToFitVisibleScreen = (ref, offset = 0, mainItemCount = 0, s
-    const projectedTop    = elBox.top + y + offset;
-    const projectedBottom = projectedTop + elBox.height;
-
-    const shouldApplyOffset = subItemCount > mainItemCount;
+    const projectedTop    = elBox.top + y + offset;
+    const projectedBottom = projectedTop + elBox.height;
+
+    // only shift when we actually have a non-zero offset
+    const shouldApplyOffset = subItemCount > mainItemCount && offset !== 0;
     if (
       shouldApplyOffset &&
       projectedTop    >= upperLimit &&
       projectedBottom <= lowerLimit
     ) {
       y += offset;
     }
@@ -36,8 +45,13 @@ const getPositionToFitVisibleScreen = (ref, offset = 0, mainItemCount = 0, s
     if (prevEl) {
       const prevElBox   = prevEl.getBoundingClientRect();
       const prevElStyle = window.getComputedStyle(prevEl);
-      const prevElTop   = parseInt(prevElStyle.top,  10);
+      // Guard against “auto” or non-numeric styles
+      let prevElTop = parseInt(prevElStyle.top, 10);
+      if (!Number.isFinite(prevElTop)) {
+        prevElTop = 0;
+      }
 
       if (!shouldApplyOffset) {
         y = prevElTop + offset;
  • Ensures offset === 0 won’t silently bypass needed shifts on root menus
  • Wraps parseInt in a Number.isFinite guard to avoid NaN leaking into y and the rendered top

Please integrate these changes to shore up both edge-cases.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const getPositionToFitVisibleScreen = (ref, offset = 0, mainItemCount = 0, subItemCount = 0) => {
if (ref.current) {
const elBox = ref.current.getBoundingClientRect();
const y = elBox.y + elBox.height < window.innerHeight ? 0 : 0 - elBox.y + 100;
let y = 0;
const footerHeight = 50;
const lowerLimit = window.innerHeight - footerHeight;
const upperLimit = 0;
if (elBox.bottom > lowerLimit) {
y = lowerLimit - elBox.bottom;
} else if (elBox.top < upperLimit) {
y = upperLimit - elBox.top;
}
const projectedTop = elBox.top + y + offset;
const projectedBottom = projectedTop + elBox.height;
const shouldApplyOffset = subItemCount > mainItemCount;
if (shouldApplyOffset && projectedTop >= upperLimit && projectedBottom <= lowerLimit) {
y += offset;
}
const getPositionToFitVisibleScreen = (ref, offset = 0, mainItemCount = 0, subItemCount = 0) => {
if (ref.current) {
const elBox = ref.current.getBoundingClientRect();
let y = 0;
const footerHeight = 50;
const lowerLimit = window.innerHeight - footerHeight;
const upperLimit = 0;
if (elBox.bottom > lowerLimit) {
y = lowerLimit - elBox.bottom;
} else if (elBox.top < upperLimit) {
y = upperLimit - elBox.top;
}
const projectedTop = elBox.top + y + offset;
const projectedBottom = projectedTop + elBox.height;
// only shift when we actually have a non-zero offset
const shouldApplyOffset = subItemCount > mainItemCount && offset !== 0;
if (
shouldApplyOffset &&
projectedTop >= upperLimit &&
projectedBottom <= lowerLimit
) {
y += offset;
}
if (prevEl) {
const prevElBox = prevEl.getBoundingClientRect();
const prevElStyle = window.getComputedStyle(prevEl);
// Guard against “auto” or non-numeric styles
let prevElTop = parseInt(prevElStyle.top, 10);
if (!Number.isFinite(prevElTop)) {
prevElTop = 0;
}
if (!shouldApplyOffset) {
y = prevElTop + offset;
}
}
return y;
}
return 0;
};
🤖 Prompt for AI Agents (early access)
In src/components/ContextMenu/ContextMenu.react.js around lines 12 to 34, fix getPositionToFitVisibleScreen to handle two edge cases: first, when mainItemCount is 0, ensure offset is applied even if it is zero to avoid skipping necessary repositioning for root menus; second, wrap the parseInt call used to compute y in a Number.isFinite check to prevent NaN values from propagating into y and causing invalid style top values. Adjust the logic to calculate a safe offset and only apply it when valid, ensuring the menu position stays within viewport bounds without NaN or no-op shifts.

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.

Context menu offset incorrect
3 participants