diff --git a/docs/pages/components/popup.md b/docs/pages/components/popup.md index 8903918f..5aed6a2e 100644 --- a/docs/pages/components/popup.md +++ b/docs/pages/components/popup.md @@ -1839,3 +1839,15 @@ const App = () => { ); }; ``` + +Sometimes the `getBoundingClientRects` might be derived from a real element. In this case provide the anchor element as context to ensure clipping and position updates for the popup work well. + +```ts +const virtualElement = { + getBoundingClientRect() { + // ... + return { width, height, x, y, top, left, right, bottom }; + }, + contextElement: anchorElement +}; +``` diff --git a/src/components/popup/popup.component.ts b/src/components/popup/popup.component.ts index dd7e524c..d724fa1c 100644 --- a/src/components/popup/popup.component.ts +++ b/src/components/popup/popup.component.ts @@ -10,10 +10,11 @@ import type { CSSResultGroup } from 'lit'; export interface VirtualElement { getBoundingClientRect: () => DOMRect; + contextElement?: Element; } function isVirtualElement(e: unknown): e is VirtualElement { - return e !== null && typeof e === 'object' && 'getBoundingClientRect' in e; + return e !== null && typeof e === 'object' && 'getBoundingClientRect' in e && ('contextElement' in e ? e instanceof Element : true); } /**