feat(sl-popup): Add contextElement property to VirtualElement interface (#1874)

This commit is contained in:
stefanholzapfel
2024-02-20 19:44:53 +01:00
committed by GitHub
parent 9b19c4c782
commit 6e288a80a3
2 changed files with 14 additions and 1 deletions

View File

@@ -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
};
```

View File

@@ -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);
}
/**