Skip to content

Commit

Permalink
feat(sl-popup): Add contextElement property to VirtualElement interfa…
Browse files Browse the repository at this point in the history
…ce (#1874)
  • Loading branch information
stefanholzapfel authored Feb 20, 2024
1 parent 9b19c4c commit 6e288a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/pages/components/popup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
```
3 changes: 2 additions & 1 deletion src/components/popup/popup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 6e288a8

Please sign in to comment.