Improve context menu performance#288
Conversation
|
Thank you for dedicating some time investigating and fixing this performance issue! |
|
Maybe linked to this: huntabyte/bits-ui#1743 and huntabyte/bits-ui#1752 |
|
That's interesting. I'm not familiar with the internals of bits-ui, but it should in principle be possible to have much better performance with a long list of items with a context menu, so perhaps you're right and the fix should be made there rather than working around the issue here. |
|
Which browser and device are you using? On my laptop, context menus open instantly even with 100 files |
|
Try opening the last one in a list of 100. The ones near the top open nearly instantly, and the ones near the bottom take several seconds to open. I'm using Safari on a MacBook Pro. |
When many files are loaded and an item toward the end of the list is right-clicked, a delay occurs (around 10 seconds with 100 files) before the context menu opens. The delay is position-dependent: the context menu opens nearly instantly when right-clicking items near the beginning of the list.
Cause
Each file list item had its own
ContextMenu.Rootcomponent. When right-clicking, bits-ui iterates through all context menu instances to determine which one should handle the event.Solution
Replace per-item context menus with a single shared context menu. Native
contextmenuevents capture the click position and set the menu'sopenstate. A hidden anchor element positioned at the click coordinates is used with bits-ui'scustomAnchorprop to position the menu correctly.Changes
context-menu-state.svelte.ts- shared state for tracking clicked item and positionFileListContextMenu.svelte- single shared context menu componentFileList.svelte- include the shared context menuFileListNodeLabel.svelte- replaceContextMenu.Rootwith nativeoncontextmenuhandlerResult
Context menu now opens instantly regardless of file count or item position.