diff --git a/e2e/specs/mock/scenarios/nested-popovers.spec.ts b/e2e/specs/mock/scenarios/nested-popovers.spec.ts new file mode 100644 index 0000000000..2b4e0db761 --- /dev/null +++ b/e2e/specs/mock/scenarios/nested-popovers.spec.ts @@ -0,0 +1,140 @@ +import { expect, test } from '@playwright/test'; +import type { Page } from '@playwright/test'; +import { NEW_CHAT_PATH } from '../helpers'; +import { openSidebar } from './sidebar'; + +/** + * A modal OGDialog parks `pointer-events: none` on the body, so a menu portaled out of it inherits + * `none` unless it turns pointer events back on for itself. DropdownPopup does that with a class, + * not an inline style, and its menu must still take a real mouse pick while the dialog is open. + * The Select's check indicator draws its box at the icon size role, so a theme that resizes icons + * resizes the box with the glyph inside it. + */ + +/** Names an icon size apart from the default, so a box still drawn at a fixed size shows. */ +const REFERENCE_ICON_THEME = { + version: 1, + name: 'e2e-icon-reference', + modes: { + light: { appearance: { iconSize: '1.25rem' } }, + dark: { appearance: { iconSize: '1.25rem' } }, + }, +} as const; + +/** Leaves the color mode to the project (light, dark or mobile), so every scenario runs in each. */ +async function useTheme(page: Page, definition?: { name: string }) { + await page.addInitScript((stored) => { + localStorage.removeItem('theme-colors'); + localStorage.removeItem('theme-name'); + if (stored) { + localStorage.setItem('theme-definition', JSON.stringify(stored)); + localStorage.setItem('theme-source', 'definition'); + } else { + localStorage.removeItem('theme-definition'); + localStorage.removeItem('theme-source'); + } + }, definition ?? null); +} + +/** The stateful workspace Select only renders when the agents endpoint offers stateful sessions. */ +async function offerStatefulSessions(page: Page) { + await page.route('**/api/endpoints', async (route) => { + const response = await route.fetch(); + const endpoints = await response.json(); + const agents = endpoints.agents ?? {}; + agents.capabilities = [...(agents.capabilities ?? []), 'stateful_code_sessions']; + endpoints.agents = agents; + await route.fulfill({ response, json: endpoints }); + }); +} + +/** Opens the Settings Select's list. Only its geometry is read, which does not depend on how the + * list layers against the Settings dialog. */ +async function openWorkspaceSelect(page: Page) { + await page.goto(NEW_CHAT_PATH, { timeout: 15000 }); + /** Mobile keeps the account menu in the drawer. */ + await openSidebar(page); + await page.getByTestId('nav-user').locator('visible=true').first().click(); + await page.getByRole('menuitem', { name: 'Settings' }).click(); + const dialog = page.getByRole('dialog'); + await expect(dialog.getByRole('heading', { name: 'Settings', exact: true })).toBeVisible({ + timeout: 15000, + }); + await dialog.getByRole('tab', { name: /data/i }).click(); + const trigger = dialog.getByTestId('default-stateful-workspace'); + await expect(trigger).toBeVisible(); + await trigger.click(); + const listbox = page.getByRole('listbox'); + await expect(listbox).toBeVisible(); + /** The list zooms in, so a box read before the animation settles is scaled. */ + await listbox.evaluate((element) => + Promise.all(element.getAnimations({ subtree: true }).map((animation) => animation.finished)), + ); +} + +/** The checked option's indicator box is `expected` square and its glyph fills it exactly. */ +async function expectIndicator(page: Page, expected: string) { + const checked = page.getByRole('option', { selected: true }); + const box = checked.locator('> span').first(); + const glyph = checked.locator('svg'); + await expect(glyph).toBeVisible(); + const [boxRect, glyphRect] = await Promise.all([box.boundingBox(), glyph.boundingBox()]); + expect(boxRect).not.toBeNull(); + expect(glyphRect).not.toBeNull(); + expect(`${boxRect?.width}px`).toBe(expected); + expect(`${boxRect?.height}px`).toBe(expected); + /** The glyph sits inside its box rather than spilling past it. */ + expect(glyphRect?.width).toBe(boxRect?.width); + expect(glyphRect?.x).toBe(boxRect?.x); +} + +test.describe('popovers inside a modal dialog', () => { + test('the role menu inside Admin Settings takes a mouse pick @scenario:menu-in-dialog-takes-pointer', async ({ + page, + }) => { + await useTheme(page); + await page.goto('/agents/all', { timeout: 15000 }); + await page.getByRole('button', { name: 'Admin Settings' }).click(); + const dialog = page.getByRole('dialog'); + await expect(dialog).toBeVisible({ timeout: 15000 }); + + const trigger = dialog.locator('button[aria-haspopup="menu"]'); + const before = (await trigger.innerText()).trim(); + await trigger.click(); + const menu = page.getByRole('menu'); + await expect(menu).toBeVisible(); + + expect(await page.evaluate(() => getComputedStyle(document.body).pointerEvents)).toBe('none'); + expect(await menu.evaluate((element) => getComputedStyle(element).pointerEvents)).toBe('auto'); + + const items = menu.getByRole('menuitem'); + await expect(items.first()).toBeVisible(); + const labels = (await items.allInnerTexts()).map((label) => label.trim()); + const next = labels.find((label) => label !== before); + expect(next).toBeTruthy(); + await items + .filter({ hasText: next as string }) + .first() + .click(); + await expect(menu).toBeHidden(); + await expect(trigger).toHaveText(next as string); + }); + + test('the Select check indicator box takes the default icon size @scenario:select-indicator-icon-role-default', async ({ + page, + }) => { + await offerStatefulSessions(page); + await useTheme(page); + await openWorkspaceSelect(page); + await expectIndicator(page, '16px'); + }); + + test('the Select check indicator box follows a theme icon size @scenario:select-indicator-icon-role-reference', async ({ + page, + }) => { + await offerStatefulSessions(page); + await useTheme(page, REFERENCE_ICON_THEME); + await openWorkspaceSelect(page); + await expectIndicator(page, '20px'); + }); +}); diff --git a/eslint-suppressions.json b/eslint-suppressions.json index 3119cb1160..22095413f3 100644 --- a/eslint-suppressions.json +++ b/eslint-suppressions.json @@ -2154,11 +2154,6 @@ "count": 1 } }, - "packages/client/src/components/DropdownPopup.tsx": { - "shadcn/no-inline-styles": { - "count": 1 - } - }, "packages/client/src/components/FileUpload.tsx": { "shadcn/no-inline-styles": { "count": 1 @@ -2184,11 +2179,6 @@ "count": 2 } }, - "packages/client/src/components/Select.tsx": { - "shadcn/no-inline-styles": { - "count": 1 - } - }, "packages/client/src/components/SplitText.tsx": { "shadcn/no-inline-styles": { "count": 7 diff --git a/packages/client/src/components/DropdownPopup.spec.tsx b/packages/client/src/components/DropdownPopup.spec.tsx index 19ccacd546..01b2f54f6d 100644 --- a/packages/client/src/components/DropdownPopup.spec.tsx +++ b/packages/client/src/components/DropdownPopup.spec.tsx @@ -28,7 +28,7 @@ describe('DropdownPopup', () => { const menu = document.getElementById('portal-click-test-menu'); expect(menu).not.toBeNull(); - expect(menu?.style.pointerEvents).toBe('auto'); + expect(menu).toHaveClass('pointer-events-auto'); document.body.style.pointerEvents = ''; }); diff --git a/packages/client/src/components/DropdownPopup.tsx b/packages/client/src/components/DropdownPopup.tsx index da687a3bde..0da9a37edd 100644 --- a/packages/client/src/components/DropdownPopup.tsx +++ b/packages/client/src/components/DropdownPopup.tsx @@ -89,12 +89,12 @@ const Menu: React.FC = ({ finalFocus={finalFocus} unmountOnHide={unmountOnHide} preserveTabOrder={preserveTabOrder} + style={{ zIndex, ...style }} /* Portaled menus land beside modal OGDialog layers, which set `pointer-events: none` on body and re-enable it only on their own - content. Without this the menu inherits `none` and its items become - hit-transparent (danny-avila/LibreChat#14487). */ - style={{ zIndex, pointerEvents: 'auto', ...style }} - className={cn('popover-ui', className)} + content. Without `pointer-events-auto` the menu inherits `none` and its + items become hit-transparent (danny-avila/LibreChat#14487). */ + className={cn('popover-ui pointer-events-auto', className)} {...props} > {items diff --git a/packages/client/src/components/Select.tsx b/packages/client/src/components/Select.tsx index ea1e02c21e..4f120a5cfc 100644 --- a/packages/client/src/components/Select.tsx +++ b/packages/client/src/components/Select.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import * as SelectPrimitive from '@radix-ui/react-select'; import { CaretSortIcon, CheckIcon, ChevronDownIcon, ChevronUpIcon } from '@radix-ui/react-icons'; import { cn, disabledFillClasses, disabledInkClasses } from '~/utils'; -import { useNestedPopoverStyle } from './OriginalDialog'; +import { useDialogDepth, usePopoverZIndex } from './OriginalDialog'; // @ts-ignore - Radix UI type conflicts with React types const Select: React.FC = SelectPrimitive.Root; @@ -83,17 +83,21 @@ const SelectContent: React.ForwardRefExoticComponent< React.ElementRef, React.ComponentPropsWithoutRef >(({ className = '', children, position = 'popper', style, ...props }, ref) => { - const nestedStyle = useNestedPopoverStyle(); + const nested = useDialogDepth() > 0; + const zIndex = usePopoverZIndex(); return ( - + diff --git a/packages/client/src/components/__tests__/popoverLayering.spec.tsx b/packages/client/src/components/__tests__/popoverLayering.spec.tsx index ea111cafc3..1648e96bd8 100644 --- a/packages/client/src/components/__tests__/popoverLayering.spec.tsx +++ b/packages/client/src/components/__tests__/popoverLayering.spec.tsx @@ -58,7 +58,7 @@ describe('portaled popovers inside a dialog', () => { const listbox = screen.getByRole('listbox'); expect(zIndexOf(listbox)).toBeGreaterThan(DIALOG_CONTENT_Z_INDEX); - expect(listbox.style.pointerEvents).toBe('auto'); + expect(listbox).toHaveClass('pointer-events-auto'); }); it('opens a hover card above the dialog content it belongs to', () => { @@ -95,6 +95,7 @@ describe('portaled popovers inside a dialog', () => { const card = screen.getByText('What stateful sessions do'); expect(listbox.style.zIndex).toBe(''); expect(listbox).toHaveClass('z-40'); + expect(listbox).not.toHaveClass('pointer-events-auto'); expect(card.style.zIndex).toBe(''); expect(card).toHaveClass('z-50'); }); diff --git a/packages/client/src/theme/primitives.spec.ts b/packages/client/src/theme/primitives.spec.ts index bebb126a0c..d55f753c7b 100644 --- a/packages/client/src/theme/primitives.spec.ts +++ b/packages/client/src/theme/primitives.spec.ts @@ -28,21 +28,22 @@ const primitives: Record = { Tooltip: ['Tooltip.tsx', 'Tooltip.css'], }; -const themeDriven = ['Button', 'Input', 'Dialog', 'Tabs', 'Switch', 'Checkbox', 'Table', 'Tooltip']; +const themeDriven = [ + 'Button', + 'Input', + 'Dialog', + 'Menu', + 'Tabs', + 'Switch', + 'Checkbox', + 'Table', + 'Tooltip', +]; -/** What the two others still hard-code, and why it stays. */ +/** What the other still hard-codes, and why it stays. */ const remaining: Record = { - /** The content's nested-popover layering is a runtime style; the item indicator box, the list's - * scroll cap and its minimum width have no role. */ - Select: [ - 'Select.tsx: arbitrary size min-w-[8rem]', - 'Select.tsx: fixed size h-3.5', - 'Select.tsx: fixed size h-96', - 'Select.tsx: fixed size w-3.5', - 'Select.tsx: suppression shadcn/no-inline-styles', - ], - /** The menu's z-index comes from the popover stack at runtime. */ - Menu: ['DropdownPopup.tsx: suppression shadcn/no-inline-styles'], + /** The list's scroll cap and its minimum width have no role. */ + Select: ['Select.tsx: arbitrary size min-w-[8rem]', 'Select.tsx: fixed size h-96'], }; const suppressions: Record> = JSON.parse(