Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(dify-ui): hide positioners while their anchor is hidden (#43935)

yyh6af1bb709b7f
diff --git a/packages/dify-ui/docs/overlays.md b/packages/dify-ui/docs/overlays.md
index 60066f19f73..7a6e52d72a8 100644
--- a/packages/dify-ui/docs/overlays.md
+++ b/packages/dify-ui/docs/overlays.md
@@ -52,6 +52,10 @@ clipping.
Overlays at `z-50` rely on portal DOM order; the portal mounted later appears above earlier ones.
Toast remains one layer above ordinary overlays.
+Positioners hide with their anchor: Base UI marks them `data-anchor-hidden` when the anchor is
+clipped, `display: none` or unmounted, and the shared positioner class turns that into
+`visibility: hidden` plus `pointer-events: none` so a popup never lingers at the viewport origin.
+
- Do not add call-site `z-*` overrides. Fix the owning overlay structure when content is clipped
or hidden.
- Do not wrap a Dify UI overlay in another manual portal.
diff --git a/packages/dify-ui/src/autocomplete/index.tsx b/packages/dify-ui/src/autocomplete/index.tsx
index a5d0497c9a3..f213bacbe28 100644
--- a/packages/dify-ui/src/autocomplete/index.tsx
+++ b/packages/dify-ui/src/autocomplete/index.tsx
@@ -13,6 +13,7 @@ import {
floatingItemIndicatorClassName,
floatingPopupAnimationClassName,
floatingSeparatorClassName,
+ positionerClassName,
} from '../internal/overlay'
import { parsePlacement } from '../internal/placement'
import { resolveClassName } from '../internal/resolve-class-name'
@@ -284,7 +285,7 @@ function AutocompletePositioner({
side={side}
align={align}
sideOffset={sideOffset}
- className={(state) => cn('z-50 outline-hidden', resolveClassName(className, state))}
+ className={(state) => cn(positionerClassName, resolveClassName(className, state))}
{...props}
/>
)
diff --git a/packages/dify-ui/src/combobox/index.tsx b/packages/dify-ui/src/combobox/index.tsx
index bcc69b7fd90..be359576f1f 100644
--- a/packages/dify-ui/src/combobox/index.tsx
+++ b/packages/dify-ui/src/combobox/index.tsx
@@ -13,6 +13,7 @@ import {
floatingItemIndicatorClassName,
floatingPopupAnimationClassName,
floatingSeparatorClassName,
+ positionerClassName,
} from '../internal/overlay'
import { parsePlacement } from '../internal/placement'
import { resolveClassName } from '../internal/resolve-class-name'
@@ -306,7 +307,7 @@ function ComboboxPositioner({
side={side}
align={align}
sideOffset={sideOffset}
- className={(state) => cn('z-50 outline-hidden', resolveClassName(className, state))}
+ className={(state) => cn(positionerClassName, resolveClassName(className, state))}
{...props}
/>
)
diff --git a/packages/dify-ui/src/context-menu/__tests__/index.spec.tsx b/packages/dify-ui/src/context-menu/__tests__/index.spec.tsx
index d97c63064f9..f8f2d841586 100644
--- a/packages/dify-ui/src/context-menu/__tests__/index.spec.tsx
+++ b/packages/dify-ui/src/context-menu/__tests__/index.spec.tsx
@@ -1,4 +1,5 @@
import type * as React from 'react'
+import type { RenderResult } from 'vitest-browser-react'
import { render } from 'vitest-browser-react'
import {
ContextMenu,
@@ -12,20 +13,31 @@ import {
ContextMenuTrigger,
} from '../index'
-const renderWithSafeViewport = (ui: React.ReactNode) =>
- render(<div style={{ minHeight: '100vh', minWidth: '100vw', padding: '240px' }}>{ui}</div>)
+// Leaves room above and below the trigger so the requested side is not flipped by a collision.
+const renderWithRoomAround = (ui: React.ReactNode) =>
+ render(<div style={{ padding: '240px 16px' }}>{ui}</div>)
+
+// A context menu is anchored to the pointer, so it is opened with a real right-click: a controlled
+// `open` has no pointer position and leaves the menu anchored to nothing.
+const openContextMenu = async (screen: RenderResult) => {
+ await screen
+ .getByLabelText('context trigger')
+ .click({ button: 'right', position: { x: 8, y: 8 } })
+ await expect.element(screen.getByRole('menu').first()).toBeVisible()
+}
describe('context-menu wrapper', () => {
describe('ContextMenuContent', () => {
it('should position content at bottom-start with default placement when props are omitted', async () => {
- const screen = await renderWithSafeViewport(
- <ContextMenu open>
+ const screen = await renderWithRoomAround(
+ <ContextMenu>
<ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem>Content action</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>,
)
+ await openContextMenu(screen)
await expect.element(screen.getByRole('menu')).toHaveAttribute('data-side', 'bottom')
await expect.element(screen.getByRole('menu')).toHaveAttribute('data-align', 'start')
@@ -35,14 +47,15 @@ describe('context-menu wrapper', () => {
})
it('should apply custom top placement and keep point-anchor alignment stable when custom positioning props are provided', async () => {
- const screen = await renderWithSafeViewport(
- <ContextMenu open>
+ const screen = await renderWithRoomAround(
+ <ContextMenu>
<ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
<ContextMenuContent placement="top-end" sideOffset={12} alignOffset={-3}>
<ContextMenuItem>Custom content</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>,
)
+ await openContextMenu(screen)
await expect.element(screen.getByRole('menu')).toHaveAttribute('data-side', 'top')
await expect.element(screen.getByRole('menu')).toHaveAttribute('data-align', 'start')
@@ -55,25 +68,27 @@ describe('context-menu wrapper', () => {
const handlePopupClick = vi.fn()
const screen = await render(
- <ContextMenu open>
+ <ContextMenu>
<ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
<ContextMenuContent id="context-content-popup" onClick={handlePopupClick}>
<ContextMenuItem>Passthrough content</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>,
)
+ await openContextMenu(screen)
+
+ await expect.element(screen.getByRole('menu')).toHaveAttribute('id', 'context-content-popup')
await screen.getByRole('menu').click()
- await expect.element(screen.getByRole('menu')).toHaveAttribute('id', 'context-content-popup')
expect(handlePopupClick).toHaveBeenCalledTimes(1)
})
})
describe('ContextMenuSubContent', () => {
it('should place sub-content inline-end-start when placement is omitted', async () => {
- const screen = await renderWithSafeViewport(
- <ContextMenu open>
+ const screen = await renderWithRoomAround(
+ <ContextMenu>
<ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuSub open>
@@ -85,6 +100,7 @@ describe('context-menu wrapper', () => {
</ContextMenuContent>
</ContextMenu>,
)
+ await openContextMenu(screen)
const submenu = screen.getByRole('menu', { name: 'More actions' })
const submenuTrigger = screen.getByRole('menuitem', { name: 'More actions' })
@@ -110,7 +126,7 @@ describe('context-menu wrapper', () => {
const handleClick = vi.fn()
const screen = await render(
- <ContextMenu open>
+ <ContextMenu>
<ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem
@@ -124,14 +140,17 @@ describe('context-menu wrapper', () => {
</ContextMenuContent>
</ContextMenu>,
)
+ await openContextMenu(screen)
- await screen.getByRole('menuitem', { name: 'menu action' }).click()
await expect
.element(screen.getByRole('menuitem', { name: 'menu action' }))
.toHaveAttribute('id', `context-item-${variant}`)
await expect
.element(screen.getByRole('menuitem', { name: 'menu action' }))
.toHaveAttribute('data-variant', variant)
+
+ await screen.getByRole('menuitem', { name: 'menu action' }).click()
+
expect(handleClick).toHaveBeenCalledTimes(1)
},
)
@@ -142,7 +161,7 @@ describe('context-menu wrapper', () => {
const handleClick = vi.fn()
const screen = await render(
- <ContextMenu open>
+ <ContextMenu>
<ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuSub open>
@@ -158,14 +177,17 @@ describe('context-menu wrapper', () => {
</ContextMenuContent>
</ContextMenu>,
)
+ await openContextMenu(screen)
- await screen.getByRole('menuitem', { name: 'submenu action' }).click()
await expect
.element(screen.getByRole('menuitem', { name: 'submenu action' }))
.toHaveAttribute('id', `context-sub-${variant}`)
await expect
.element(screen.getByRole('menuitem', { name: 'submenu action' }))
.toHaveAttribute('data-variant', variant)
+
+ await screen.getByRole('menuitem', { name: 'submenu action' }).click()
+
expect(handleClick).toHaveBeenCalledTimes(1)
},
)
@@ -174,7 +196,7 @@ describe('context-menu wrapper', () => {
'should remain interactive and set data-variant on link item when variant is %s',
async (variant) => {
const screen = await render(
- <ContextMenu open>
+ <ContextMenu>
<ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuLinkItem
@@ -190,6 +212,7 @@ describe('context-menu wrapper', () => {
</ContextMenuContent>
</ContextMenu>,
)
+ await openContextMenu(screen)
const link = screen.getByRole('menuitem', { name: 'context docs link' }).element()
expect(link.tagName.toLowerCase()).toBe('a')
@@ -202,7 +225,7 @@ describe('context-menu wrapper', () => {
describe('ContextMenuLinkItem close behavior', () => {
it('should keep link semantics and not leak closeOnClick prop when closeOnClick is false', async () => {
const screen = await render(
- <ContextMenu open>
+ <ContextMenu>
<ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuLinkItem
@@ -215,6 +238,7 @@ describe('context-menu wrapper', () => {
</ContextMenuContent>
</ContextMenu>,
)
+ await openContextMenu(screen)
const link = screen.getByRole('menuitem', { name: 'docs link' }).element()
expect(link.tagName.toLowerCase()).toBe('a')
@@ -234,27 +258,18 @@ describe('context-menu wrapper', () => {
</ContextMenu>,
)
- screen
- .getByLabelText('context trigger area')
- .element()
- .dispatchEvent(
- new MouseEvent('contextmenu', {
- bubbles: true,
- cancelable: true,
- button: 2,
- }),
- )
+ await screen.getByLabelText('context trigger area').click({ button: 'right' })
await expect
.element(screen.getByRole('menuitem', { name: 'Open on right click' }))
- .toBeInTheDocument()
+ .toBeVisible()
})
})
describe('ContextMenuSeparator', () => {
it('should render separator and keep surrounding rows when separator is between items', async () => {
const screen = await render(
- <ContextMenu open>
+ <ContextMenu>
<ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem>First action</ContextMenuItem>
@@ -263,6 +278,7 @@ describe('context-menu wrapper', () => {
</ContextMenuContent>
</ContextMenu>,
)
+ await openContextMenu(screen)
await expect
.element(screen.getByRole('menuitem', { name: 'First action' }))
diff --git a/packages/dify-ui/src/context-menu/index.tsx b/packages/dify-ui/src/context-menu/index.tsx
index f7ced75dd4b..2c9f8dac269 100644
--- a/packages/dify-ui/src/context-menu/index.tsx
+++ b/packages/dify-ui/src/context-menu/index.tsx
@@ -16,6 +16,7 @@ import {
floatingItemIndicatorClassName,
floatingPopupAnimationClassName,
floatingSeparatorClassName,
+ positionerClassName,
} from '../internal/overlay'
import { parsePlacement } from '../internal/placement'
import { resolveClassName } from '../internal/resolve-class-name'
@@ -75,7 +76,7 @@ function ContextMenuContent({
align={align}
sideOffset={sideOffset}
alignOffset={alignOffset}
- className="z-50 outline-hidden"
+ className={positionerClassName}
>
<BaseContextMenu.Popup
className={(state) =>
@@ -244,7 +245,7 @@ function ContextMenuSubContent({
align={align}
sideOffset={sideOffset}
alignOffset={alignOffset}
- className="z-50 outline-hidden"
+ className={positionerClassName}
>
<BaseContextMenu.Popup
className={(state) =>
diff --git a/packages/dify-ui/src/dropdown-menu/index.tsx b/packages/dify-ui/src/dropdown-menu/index.tsx
index 15bd5b8c2f2..9184d11220a 100644
--- a/packages/dify-ui/src/dropdown-menu/index.tsx
+++ b/packages/dify-ui/src/dropdown-menu/index.tsx
@@ -20,6 +20,7 @@ import {
floatingItemIndicatorClassName,
floatingPopupAnimationClassName,
floatingSeparatorClassName,
+ positionerClassName,
} from '../internal/overlay'
import { parsePlacement } from '../internal/placement'
import { resolveClassName } from '../internal/resolve-class-name'
@@ -266,7 +267,7 @@ function DropdownMenuPositioner({
align={align}
sideOffset={sideOffset}
alignOffset={alignOffset}
- className={(state) => cn('z-50 outline-hidden', resolveClassName(className, state))}
+ className={(state) => cn(positionerClassName, resolveClassName(className, state))}
{...props}
/>
)
diff --git a/packages/dify-ui/src/infotip/index.tsx b/packages/dify-ui/src/infotip/index.tsx
index 04797c3371b..ea17469f26e 100644
--- a/packages/dify-ui/src/infotip/index.tsx
+++ b/packages/dify-ui/src/infotip/index.tsx
@@ -5,7 +5,7 @@ import type { Placement } from '../internal/placement'
import { Popover as BasePopover } from '@base-ui/react/popover'
import { cn } from '../cn'
import { iconButtonVariants } from '../icon-button'
-import { hintPopupClassName } from '../internal/overlay'
+import { hintPopupClassName, positionerClassName } from '../internal/overlay'
import { parsePlacement } from '../internal/placement'
import { resolveClassName } from '../internal/resolve-class-name'
@@ -96,7 +96,7 @@ function InfotipContent({
align={align}
sideOffset={sideOffset}
alignOffset={alignOffset}
- className="z-50 outline-hidden"
+ className={positionerClassName}
>
<BasePopover.Popup
{...props}
diff --git a/packages/dify-ui/src/internal/overlay.ts b/packages/dify-ui/src/internal/overlay.ts
index 7872913a85d..1cfed9f68d4 100644
--- a/packages/dify-ui/src/internal/overlay.ts
+++ b/packages/dify-ui/src/internal/overlay.ts
@@ -7,6 +7,10 @@ export const floatingSeparatorClassName = 'my-1 h-px bg-divider-subtle'
export const floatingPopupAnimationClassName =
'origin-(--transform-origin) transition-[transform,scale,opacity] data-ending-style:scale-95 data-starting-style:scale-95 data-ending-style:opacity-0 data-starting-style:opacity-0 data-instant:transition-none motion-reduce:transition-none'
+// Positioners hide with their anchor: Base UI only emits `data-anchor-hidden` when the anchor is
+// clipped, display:none or unmounted, and would otherwise keep the popup at the viewport origin.
+export const positionerClassName =
+ 'z-50 outline-hidden data-anchor-hidden:invisible data-anchor-hidden:pointer-events-none'
export const hintPopupClassName =
'max-w-75 rounded-lg border-[0.5px] border-components-panel-border bg-components-tooltip-bg p-1.5 text-start system-xs-medium wrap-break-word text-text-secondary shadow-[0px_12px_16px_-4px_var(--color-shadow-shadow-5),0px_4px_6px_-2px_var(--color-shadow-shadow-1)] backdrop-blur-[5px] origin-(--transform-origin) transition-opacity data-ending-style:opacity-0 data-instant:transition-none data-starting-style:opacity-0 motion-reduce:transition-none'
export const modalBackdropClassName =
diff --git a/packages/dify-ui/src/popover/index.tsx b/packages/dify-ui/src/popover/index.tsx
index 905bd60f6d7..2c64f6b1f0e 100644
--- a/packages/dify-ui/src/popover/index.tsx
+++ b/packages/dify-ui/src/popover/index.tsx
@@ -5,7 +5,7 @@ import type { Placement } from '../internal/placement'
import { Popover as BasePopover } from '@base-ui/react/popover'
import { cn } from '../cn'
import { focusRingClassName } from '../internal/focus'
-import { floatingPopupAnimationClassName } from '../internal/overlay'
+import { floatingPopupAnimationClassName, positionerClassName } from '../internal/overlay'
import { parsePlacement } from '../internal/placement'
import { resolveClassName } from '../internal/resolve-class-name'
@@ -72,7 +72,7 @@ function PopoverPositioner({
align={align}
sideOffset={sideOffset}
alignOffset={alignOffset}
- className={(state) => cn('z-50 outline-hidden', resolveClassName(className, state))}
+ className={(state) => cn(positionerClassName, resolveClassName(className, state))}
{...props}
/>
)
diff --git a/packages/dify-ui/src/preview-card/index.tsx b/packages/dify-ui/src/preview-card/index.tsx
index 04c7d5df57c..0641874df4d 100644
--- a/packages/dify-ui/src/preview-card/index.tsx
+++ b/packages/dify-ui/src/preview-card/index.tsx
@@ -4,7 +4,7 @@ import type * as React from 'react'
import type { Placement } from '../internal/placement'
import { PreviewCard as BasePreviewCard } from '@base-ui/react/preview-card'
import { cn } from '../cn'
-import { floatingPopupAnimationClassName } from '../internal/overlay'
+import { floatingPopupAnimationClassName, positionerClassName } from '../internal/overlay'
import { parsePlacement } from '../internal/placement'
import { resolveClassName } from '../internal/resolve-class-name'
@@ -55,7 +55,7 @@ function PreviewCardPositioner({
align={align}
sideOffset={sideOffset}
alignOffset={alignOffset}
- className={(state) => cn('z-50 outline-hidden', resolveClassName(className, state))}
+ className={(state) => cn(positionerClassName, resolveClassName(className, state))}
{...props}
/>
)
diff --git a/packages/dify-ui/src/select/index.tsx b/packages/dify-ui/src/select/index.tsx
index 8e36b965d65..b8ffd72bdb5 100644
--- a/packages/dify-ui/src/select/index.tsx
+++ b/packages/dify-ui/src/select/index.tsx
@@ -14,6 +14,7 @@ import {
floatingItemIndicatorClassName,
floatingPopupAnimationClassName,
floatingSeparatorClassName,
+ positionerClassName,
} from '../internal/overlay'
import { parsePlacement } from '../internal/placement'
import { resolveClassName } from '../internal/resolve-class-name'
@@ -176,7 +177,7 @@ function SelectPositioner({
sideOffset={sideOffset}
alignOffset={alignOffset}
alignItemWithTrigger={alignItemWithTrigger}
- className={(state) => cn('z-50 outline-hidden', resolveClassName(className, state))}
+ className={(state) => cn(positionerClassName, resolveClassName(className, state))}
{...props}
/>
)
diff --git a/packages/dify-ui/src/tooltip/index.tsx b/packages/dify-ui/src/tooltip/index.tsx
index 32e6325e8cc..8ea6fbf536e 100644
--- a/packages/dify-ui/src/tooltip/index.tsx
+++ b/packages/dify-ui/src/tooltip/index.tsx
@@ -4,7 +4,7 @@ import type * as React from 'react'
import type { Placement } from '../internal/placement'
import { Tooltip as BaseTooltip } from '@base-ui/react/tooltip'
import { cn } from '../cn'
-import { hintPopupClassName } from '../internal/overlay'
+import { hintPopupClassName, positionerClassName } from '../internal/overlay'
import { parsePlacement } from '../internal/placement'
import { resolveClassName } from '../internal/resolve-class-name'
@@ -43,7 +43,7 @@ function TooltipContent({
align={align}
sideOffset={sideOffset}
alignOffset={alignOffset}
- className="z-50 outline-hidden"
+ className={positionerClassName}
>
<BaseTooltip.Popup
className={(state) => cn(hintPopupClassName, resolveClassName(className, state))}