Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(composer): keep the format menu beside a scrolled editor's selection (#9028)

gbirmand4e0f77e6bbc
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md
index b6450411bb..bd665bc29b 100644
--- a/apps/web/AGENTS.md
+++ b/apps/web/AGENTS.md
@@ -36,7 +36,9 @@ sheet that must still press its own controls:
Composer typeahead menus choosing the side with room for their full list
(`floatWithSelection`'s `fullHeight`), above a composer at the bottom of a
-phone screen and below one near the top, have a backend-free browser regression:
+phone screen and below one near the top, and the floating format menu staying
+beside a scrolling editor whose selection runs past its visible bottom
+(`clipToVisible`), have a backend-free browser regression:
`bunx playwright test --config src/lib/core/component/LexicalMarkdown/directive/browser-test/playwright.config.ts`.
Event details placement has a backend-free browser regression: the desktop
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/menu/FloatingFormatMenu.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/component/menu/FloatingFormatMenu.tsx
index 47838b06f0..d1c2488367 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/component/menu/FloatingFormatMenu.tsx
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/component/menu/FloatingFormatMenu.tsx
@@ -374,6 +374,7 @@ export function FloatingFormatMenu(props: {
reactiveOnContainer: editor.getRootElement(),
useBlockBoundary: true,
moveWithSelection: true,
+ clipToVisible: true,
}}
>
<Toolbar>
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/directive/browser-test/composer-menu.browser.e2e.ts b/apps/web/src/lib/core/component/LexicalMarkdown/directive/browser-test/composer-menu.browser.e2e.ts
index 61daef2a27..a029acd2ad 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/directive/browser-test/composer-menu.browser.e2e.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/directive/browser-test/composer-menu.browser.e2e.ts
@@ -33,6 +33,30 @@ test('a menu under a composer near the top still opens below it', async ({
expect(menu.height).toBeGreaterThanOrEqual(256);
});
+async function formatMenuGap(page: Page, query: string) {
+ await page.goto(`${fixture}?fixture=clipped-selection${query}`);
+ const menu = page.getByTestId('format-menu');
+ await expect.poll(() => menu.evaluate((el) => el.style.top)).toMatch(/px$/);
+ const menuBox = await menu.boundingBox();
+ const shell = await page.getByTestId('editor-shell').boundingBox();
+ if (!menuBox || !shell) throw new Error('menu or editor not laid out');
+ return menuBox.y - (shell.y + shell.height);
+}
+
+test('a format menu over a selection clipped by a scrolling editor stays beside the editor', async ({
+ page,
+}) => {
+ const gap = await formatMenuGap(page, '');
+ expect(gap).toBeGreaterThanOrEqual(0);
+ expect(gap).toBeLessThanOrEqual(16);
+});
+
+test('without clipping, the menu follows the hidden end of the selection', async ({
+ page,
+}) => {
+ expect(await formatMenuGap(page, '&clip=0')).toBeGreaterThan(200);
+});
+
test('a picker that focuses its search field stays visible above a bottom composer', async ({
page,
}) => {
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/directive/browser-test/composer-menu.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/directive/browser-test/composer-menu.tsx
index b7d7007a7a..22bafeafda 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/directive/browser-test/composer-menu.tsx
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/directive/browser-test/composer-menu.tsx
@@ -93,4 +93,53 @@ function Fixture() {
);
}
-render(() => <Fixture />, document.getElementById('root')!);
+// A height-capped editor that scrolls, as the channel composer does, with a
+// selection that runs past its visible bottom.
+function ClippedSelectionFixture() {
+ const clip = new URLSearchParams(location.search).get('clip') !== '0';
+ const [open, setOpen] = createSignal(false);
+ let editor!: HTMLDivElement;
+ onMount(() => {
+ editor.focus();
+ const range = document.createRange();
+ range.selectNodeContents(editor);
+ document.getSelection()?.removeAllRanges();
+ document.getSelection()?.addRange(range);
+ setOpen(true);
+ });
+ return (
+ <main class="relative h-screen bg-page p-4 text-ink">
+ <div
+ data-testid="editor-shell"
+ class="max-h-[160px] overflow-y-auto rounded-xl border border-edge p-2"
+ >
+ <div ref={editor} contentEditable aria-label="Editor">
+ <For each={Array.from({ length: 30 }, (_, i) => i + 1)}>
+ {(line) => <p>Line {line} of a long message being edited.</p>}
+ </For>
+ </div>
+ </div>
+ <Show when={open()}>
+ <div
+ data-testid="format-menu"
+ class="h-10 w-48 rounded-lg menu-surface"
+ ref={(el) =>
+ floatWithSelection(el, () => ({
+ selection: document.getSelection(),
+ reactiveOnContainer: editor,
+ clipToVisible: clip,
+ }))
+ }
+ />
+ </Show>
+ </main>
+ );
+}
+
+const isClippedSelection =
+ new URLSearchParams(location.search).get('fixture') === 'clipped-selection';
+
+render(
+ () => (isClippedSelection ? <ClippedSelectionFixture /> : <Fixture />),
+ document.getElementById('root')!
+);
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/directive/floatWithSelection.ts b/apps/web/src/lib/core/component/LexicalMarkdown/directive/floatWithSelection.ts
index 0e118fdb16..92621ced3b 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/directive/floatWithSelection.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/directive/floatWithSelection.ts
@@ -10,8 +10,10 @@ import {
hide,
offset,
type Placement,
+ type ReferenceElement,
shift,
size,
+ type VirtualElement,
} from '@floating-ui/dom';
import { Telemetry } from '@macro-inc/observability';
import type { Accessor, JSX } from 'solid-js';
@@ -47,8 +49,50 @@ type FloatWithSelectionOptions = {
* stay below the caret and shrink.
*/
fullHeight?: number;
+ /**
+ * Anchor to the part of a range selection that is not clipped by a
+ * scrolling ancestor. A selection that runs past the bottom of a
+ * height-capped editor would otherwise place the menu below its hidden end.
+ */
+ clipToVisible?: boolean;
};
+function intersectRects(a: DOMRect, b: DOMRect): DOMRect | null {
+ const left = Math.max(a.left, b.left);
+ const top = Math.max(a.top, b.top);
+ const right = Math.min(a.right, b.right);
+ const bottom = Math.min(a.bottom, b.bottom);
+ if (right < left || bottom < top) return null;
+ return new DOMRect(left, top, right - left, bottom - top);
+}
+
+function visibleRangeReference(range: Range): VirtualElement {
+ const container = range.commonAncestorContainer;
+ const contextElement =
+ container instanceof Element ? container : container.parentElement;
+ return {
+ contextElement: contextElement ?? undefined,
+ getBoundingClientRect() {
+ const rect = range.getBoundingClientRect();
+ let visible: DOMRect | null = rect;
+ for (let el = contextElement; el && visible; el = el.parentElement) {
+ const { overflowX, overflowY } = getComputedStyle(el);
+ if (overflowX !== 'visible' || overflowY !== 'visible') {
+ visible = intersectRects(visible, el.getBoundingClientRect());
+ }
+ }
+ if (visible) {
+ visible = intersectRects(
+ visible,
+ new DOMRect(0, 0, window.innerWidth, window.innerHeight)
+ );
+ }
+ // Fully clipped: keep the real rect so hide() can hide the menu.
+ return visible ?? rect;
+ },
+ };
+}
+
export function floatWithSelection(
floatingEl: HTMLElement,
accessor: Accessor<FloatWithSelectionOptions | undefined>
@@ -67,13 +111,19 @@ export function floatWithSelection(
);
let decidedPlacement: Placement | null = null;
+ function toReference(anchor: Range | Element): ReferenceElement {
+ return accessor()?.clipToVisible && anchor instanceof Range
+ ? visibleRangeReference(anchor)
+ : anchor;
+ }
+
async function roomOn(
anchor: Range | Element,
placement: Placement,
spacing: number
) {
let room = 0;
- await computePosition(anchor, floatingEl, {
+ await computePosition(toReference(anchor), floatingEl, {
placement,
middleware: [
offset(spacing),
@@ -133,25 +183,29 @@ export function floatWithSelection(
}
const floatingOptions = accessor()?.floatingOptions ?? {};
const preferredPlacement = floatingOptions.placement ?? 'bottom-start';
- const { placement } = await computePosition(anchor, floatingEl, {
- placement: preferredPlacement,
- middleware: [
- flip({
- // Default menus sit below the caret and flip above. A caller that
- // asked for another side (e.g. `placement: 'top'`) should flip to
- // the opposite side instead of being stuck on `top-start`.
- ...(preferredPlacement === 'bottom-start'
- ? { fallbackPlacements: ['top-start'] as const }
- : {}),
- boundary,
- padding: iosSafePadding(spacing),
- }),
- offset(spacing),
- shift({ padding: spacing, boundary }),
- hide(),
- ],
- ...floatingOptions,
- });
+ const { placement } = await computePosition(
+ toReference(anchor),
+ floatingEl,
+ {
+ placement: preferredPlacement,
+ middleware: [
+ flip({
+ // Default menus sit below the caret and flip above. A caller that
+ // asked for another side (e.g. `placement: 'top'`) should flip to
+ // the opposite side instead of being stuck on `top-start`.
+ ...(preferredPlacement === 'bottom-start'
+ ? { fallbackPlacements: ['top-start'] as const }
+ : {}),
+ boundary,
+ padding: iosSafePadding(spacing),
+ }),
+ offset(spacing),
+ shift({ padding: spacing, boundary }),
+ hide(),
+ ],
+ ...floatingOptions,
+ }
+ );
decidedPlacement = placement;
await updatePosition();
@@ -165,7 +219,7 @@ export function floatWithSelection(
const spacing = accessor()?.spacing ?? DEFAULT_SPACING;
const onAvailableHeight = accessor()?.onAvailableHeight;
const { x, y, middlewareData } = await computePosition(
- current,
+ toReference(current),
floatingEl,
{
placement: decidedPlacement,
@@ -251,11 +305,16 @@ export function floatWithSelection(
console.log('No reference for autoUpdate');
return;
}
- cleanupAutoUpdate = autoUpdate(reference, floatingEl, updatePosition, {
- elementResize: true,
- ancestorScroll: true,
- ancestorResize: true,
- });
+ cleanupAutoUpdate = autoUpdate(
+ toReference(reference),
+ floatingEl,
+ updatePosition,
+ {
+ elementResize: true,
+ ancestorScroll: true,
+ ancestorResize: true,
+ }
+ );
});
onCleanup(() => {