Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,24 @@ import { basenameOfPath } from "../../pierre-icons";
import { cn, randomUUID } from "~/lib/utils";
import { Separator } from "../ui/separator";

type ComposerCommandMenuPosition = {
bottom: number;
left: number;
maxHeight: number;
width: number;
};

function composerCommandMenuPositionsEqual(
a: ComposerCommandMenuPosition,
b: ComposerCommandMenuPosition,
): boolean {
return (
a.bottom === b.bottom && a.left === b.left && a.maxHeight === b.maxHeight && a.width === b.width
);
}

function ComposerCommandMenuLayer(props: { anchor: HTMLElement | null; children: ReactNode }) {
const [position, setPosition] = useState<{
bottom: number;
left: number;
maxHeight: number;
width: number;
} | null>(null);
const [position, setPosition] = useState<ComposerCommandMenuPosition | null>(null);

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

const updatePosition = () => {
const rect = anchor.getBoundingClientRect();
setPosition({
const next = {
bottom: window.innerHeight - rect.top + 8,
left: rect.left,
maxHeight: Math.max(96, rect.top - 24),
width: rect.width,
});
};
setPosition((current) =>
current && composerCommandMenuPositionsEqual(current, next) ? current : next,
);
};

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

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

return () => {
observer?.disconnect();
Expand Down
Loading