Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

ui: fix submenu cutting off when there are a lot of lazy items (#9739)

01zulfie3ccd504da4d
diff --git a/apps/web/src/components/note/index.tsx b/apps/web/src/components/note/index.tsx
index d48ca2df3..cf09e6ee5 100644
--- a/apps/web/src/components/note/index.tsx
+++ b/apps/web/src/components/note/index.tsx
@@ -713,7 +713,7 @@ function colorsToMenuItems(
const isChecked = !!noteColor && noteColor.id === color.id;
return {
type: "button",
- key: color.title,
+ key: color.id,
title: color.title,
icon: Circle.path,
styles: { icon: { color: color.colorCode } },
diff --git a/packages/ui/src/components/menu/index.tsx b/packages/ui/src/components/menu/index.tsx
index 7084763c5..e25b4a673 100644
--- a/packages/ui/src/components/menu/index.tsx
+++ b/packages/ui/src/components/menu/index.tsx
@@ -21,8 +21,8 @@ import React, {
useCallback,
useRef,
useEffect,
- PropsWithChildren,
- useState
+ useState,
+ PropsWithChildren
} from "react";
import { Box, FlexProps, Text } from "@theme-ui/components";
import { getPosition } from "../../utils/position.js";
@@ -68,6 +68,7 @@ export function Menu(props: MenuProps) {
const focusedItem = items[focusIndex];
const subMenuRef = useRef<HTMLDivElement>(null);
+
useEffect(() => {
const item = items[focusIndex];
if (!item || !subMenuRef.current) return;
@@ -80,15 +81,23 @@ export function Menu(props: MenuProps) {
return;
}
- const { top, left } = getPosition(subMenuRef.current, {
- // yOffset: menuItemElement.offsetHeight,
- target: menuItemElement,
- location: "right"
- });
+ function repositionSubmenu() {
+ if (!subMenuRef.current || !menuItemElement) return;
+
+ const { top, left } = getPosition(subMenuRef.current, {
+ target: menuItemElement,
+ location: "right"
+ });
+ subMenuRef.current.style.top = `${top}px`;
+ subMenuRef.current.style.left = `${left}px`;
+ }
+ repositionSubmenu();
subMenuRef.current.style.visibility = "visible";
- subMenuRef.current.style.top = `${top}px`;
- subMenuRef.current.style.left = `${left}px`;
+
+ const observer = new ResizeObserver(() => repositionSubmenu());
+ observer.observe(subMenuRef.current);
+ return () => observer.disconnect();
}, [isSubmenuOpen, focusIndex, items]);
return (