Skip to content

Commit 4f68017

Browse files
StiensWoutclaude
andcommitted
fix(web): keep the composer command menu anchored to the composer
The `/`, `@`, and `$` menus are portaled to the body and positioned from the anchor's rect, refreshed on window resize, scroll, and a ResizeObserver on the anchor itself. The composer is centered and capped at `max-w-3xl`, so opening the right panel or the terminal drawer slides it without ever resizing it: the observer stays silent and the menu is left behind, floating next to or on top of the composer it belongs to. Observe the anchor's ancestors as well. They are what actually shrink, and they resize on every frame of the panel animation, so the menu tracks the composer through the transition instead of snapping late. Positions are compared before committing to state to keep the extra observations from re-rendering the menu on no-op resizes. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent c30a6d9 commit 4f68017

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,24 @@ import { basenameOfPath } from "../../pierre-icons";
108108
import { cn, randomUUID } from "~/lib/utils";
109109
import { Separator } from "../ui/separator";
110110

111+
type ComposerCommandMenuPosition = {
112+
bottom: number;
113+
left: number;
114+
maxHeight: number;
115+
width: number;
116+
};
117+
118+
function composerCommandMenuPositionsEqual(
119+
a: ComposerCommandMenuPosition,
120+
b: ComposerCommandMenuPosition,
121+
): boolean {
122+
return (
123+
a.bottom === b.bottom && a.left === b.left && a.maxHeight === b.maxHeight && a.width === b.width
124+
);
125+
}
126+
111127
function ComposerCommandMenuLayer(props: { anchor: HTMLElement | null; children: ReactNode }) {
112-
const [position, setPosition] = useState<{
113-
bottom: number;
114-
left: number;
115-
maxHeight: number;
116-
width: number;
117-
} | null>(null);
128+
const [position, setPosition] = useState<ComposerCommandMenuPosition | null>(null);
118129

119130
useLayoutEffect(() => {
120131
const anchor = props.anchor;
@@ -125,12 +136,15 @@ function ComposerCommandMenuLayer(props: { anchor: HTMLElement | null; children:
125136

126137
const updatePosition = () => {
127138
const rect = anchor.getBoundingClientRect();
128-
setPosition({
139+
const next = {
129140
bottom: window.innerHeight - rect.top + 8,
130141
left: rect.left,
131142
maxHeight: Math.max(96, rect.top - 24),
132143
width: rect.width,
133-
});
144+
};
145+
setPosition((current) =>
146+
current && composerCommandMenuPositionsEqual(current, next) ? current : next,
147+
);
134148
};
135149

136150
updatePosition();
@@ -139,7 +153,16 @@ function ComposerCommandMenuLayer(props: { anchor: HTMLElement | null; children:
139153

140154
const observer =
141155
typeof ResizeObserver === "undefined" ? null : new ResizeObserver(updatePosition);
142-
observer?.observe(anchor);
156+
if (observer) {
157+
// The composer is centered and capped at a max width, so opening a side
158+
// panel slides it sideways without ever resizing it. Watching the anchor
159+
// alone would leave the menu behind; the ancestors are what shrink, and
160+
// they resize on every frame of the panel animation.
161+
observer.observe(anchor);
162+
for (let element = anchor.parentElement; element; element = element.parentElement) {
163+
observer.observe(element);
164+
}
165+
}
143166

144167
return () => {
145168
observer?.disconnect();

0 commit comments

Comments
 (0)