generated from lit/lit-element-starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
RomanenkoStud
committed
Oct 31, 2023
1 parent
c2ca8d4
commit 6c1e4dc
Showing
6 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { ReactiveControllerHost } from 'lit'; | ||
|
||
type Anchor = { | ||
x: number; | ||
y: number; | ||
} | ||
|
||
type Size = { | ||
width: string; | ||
height: string; | ||
} | ||
|
||
export class AnchorController { | ||
private targetSelector = "[popovertarget]"; | ||
private popoverSelector = "[popover]"; | ||
|
||
constructor( | ||
private host: ReactiveControllerHost & HTMLElement, | ||
private popoverSize: Size | null = null, | ||
) { | ||
this.host.addController(this); | ||
} | ||
|
||
get popoverElement(): HTMLElement { | ||
return this.host.shadowRoot?.querySelector(this.popoverSelector) as HTMLElement; | ||
} | ||
|
||
get targetElement(): HTMLElement { | ||
return this.host.shadowRoot?.querySelector(this.targetSelector) as HTMLElement; | ||
} | ||
|
||
hostConnected() { | ||
window.addEventListener('resize', this.positionPopover.bind(this)); | ||
} | ||
|
||
hostUpdated() { | ||
this.popoverElement.addEventListener('toggle', this.positionPopover.bind(this)); | ||
} | ||
|
||
hostDisconnected() { | ||
window.removeEventListener('resize', this.positionPopover.bind(this)); | ||
} | ||
|
||
computePosition(targetElement: HTMLElement): Promise<Anchor> { | ||
return new Promise((resolve) => { | ||
const targetRect = targetElement.getBoundingClientRect(); | ||
|
||
const x = targetRect.right; | ||
const y = targetRect.top; | ||
|
||
resolve({ x, y }); | ||
}); | ||
} | ||
|
||
positionPopover() { | ||
if (this.popoverElement && this.targetElement) { | ||
const popoverRect = this.popoverElement.getBoundingClientRect(); | ||
const width = this.popoverSize?.width || `${popoverRect.width}px`; | ||
const height = this.popoverSize?.height || `${popoverRect.height}px`; | ||
const calculatePosition = () => { | ||
this.computePosition(this.targetElement).then(({ x, y }: Anchor) => { | ||
Object.assign(this.popoverElement.style, { | ||
margin: '0', | ||
left: `calc(${x}px - ${width})`, | ||
top: `calc(${y}px - ${height} - 1rem)`, | ||
}); | ||
}); | ||
}; | ||
|
||
// Use requestAnimationFrame to ensure synchronized calculation | ||
requestAnimationFrame(calculatePosition); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters