Skip to content
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

Fix auto select in InspectDataMenu when opened above cursor #691

Merged
merged 23 commits into from
Nov 6, 2024

Conversation

p-malecki
Copy link
Contributor

@p-malecki p-malecki commented Nov 4, 2024

This PR addresses an issue where the inspector context menu automatically selects and closes upon appearing above the cursor. The problem was caused by the menu opening upon mouseUp immediately after a mouseDown event.

The solution ensures the menu displays adjacent to the cursor with an offset by introducing a span that positions the Trigger component at the mouse location, ensuring the menu opens next to the Trigger.

Additionally, this PR:

  • limits the number of inspect items displayed in InspectDataMenu to 5 using MAX_INSPECT_ITEMS
  • adds a "Show all" button to reveal all items
  • adds ellipsis truncation for long filenames in InspectDataMenu
  • adjusts menu alignment based on cursor position, displaying to the right if the cursor is on the left side of the screen and to the left if on the right

Fixes #687

How Has This Been Tested:

  • Tested by right-clicking near the screen's bottom, ensuring the menu opens above the cursor and doesn't auto-select any items.

After:
Screenshot 2024-11-04 at 20 39 32
Screenshot 2024-11-05 at 18 33 04

Screen.Recording.2024-11-05.at.18.31.10.mov

Copy link

vercel bot commented Nov 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
radon-ide ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 6, 2024 2:35pm

Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

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

I don't think this is the right fix for the issue. Instead of working around button events, I believe it should be enough to offset the menu such that it appears over the cursor rather than directly under. This would also be better from UX perspective, as now, the menu covers the element we are inspecting, which isn't the case when the menu displays under.

I think we should change from using an outer-level div for positioning, to adjusting the position of the Trigger component. This seems to be the way based on the examples from https://www.radix-ui.com/primitives/docs/components/dropdown-menu – right now the trigger component is empty while it seems the offset is added automatically such that the menu doesn't overlap the trigger.

Menu shown under:
image

Menu shown over:
image

Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

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

Looks good. As a final touch you may want to change the logic slightly such that if there are exacly LIMIT+1 elements, you still show them all, as LIMIT+1 items occupy exactly the same amount of space as LIMIT + "show more" button. So when you click "show more" you don't actually expand the menu at all.

Copy link
Contributor

@maciekstosio maciekstosio left a comment

Choose a reason for hiding this comment

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

Some nitpicks

left: inspectLocation.x,
top: inspectLocation.y,
opacity: 0,
}}></span>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}}></span>
}}/>

</DropdownMenu.Label>
{inspectItems.map((item, index) => {
// extract file name from path:
const fullFileName = item.source.fileName.split("/").pop() ?? "";
Copy link
Contributor

Choose a reason for hiding this comment

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

I would extract it to separate Component or at least helper for the strings

return (
<DropdownMenu.Item
className="inspect-data-menu-item"
key={index}
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, index as a key is a bad practice, I think in our case when we don't have state it shouldn't cause bugs, but maybe you could use something as unique ID here? For example filePath with code line

</DropdownMenu.Item>
);
})}
{filteredData.length > MAX_INSPECT_ITEMS + 1 && !shouldShowAll && (
Copy link
Contributor

Choose a reason for hiding this comment

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

extracting this condition to some variable would improve readability


interface InspectItemProps {
item: InspectDataStackItem;
onSelected: (item: any) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why any? Isn't it InspectDataStackItem?

{isOverMaxItems && !shouldShowAll && (
<DropdownMenu.Item
className="inspect-data-menu-item show-all"
key={"show-all"}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think now the key is unnecessary as the elements here differ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its needed to avoid warrning [WEBVIEW LOG] Warning: Each child in a list should have a unique "key" prop.%s%s in logs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting

}

const InspectItem = React.forwardRef<HTMLDivElement, InspectItemProps>(
({ item, onSelected, onHover }, forwardedRef) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why forwardRef? As far as I can tell, it's not used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's the way for creating custom components in radix https://www.radix-ui.com/primitives/docs/components/dropdown-menu


const InspectItem = React.forwardRef<HTMLDivElement, InspectItemProps>(
({ item, onSelected, onHover }, forwardedRef) => {
const extractFileDetails = (filePath: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Imo, now you don't need a helper (I meant component or helper, but I prefer the component) - just fyi, when you define a method in render, it's created on every rerender

Copy link
Contributor

@maciekstosio maciekstosio left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@p-malecki p-malecki merged commit ece3b3d into main Nov 6, 2024
3 of 4 checks passed
@p-malecki p-malecki deleted the @p-malecki/fix-menu-close-above-cursor branch November 6, 2024 14:37
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.

Inspector context menu closes immediately when displayed above the cursor
3 participants