Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit d38b2c4

Browse files
committed
refactor: rename scrollElement -> relativeElement
BREAKING CHANGE: changes API
1 parent 0b248b7 commit d38b2c4

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

src/hoverifier.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@ interface HoverifierOptions {
3939
* Emit the HoverOverlay element on this after it was rerendered when its content changed and it needs to be repositioned.
4040
*/
4141
hoverOverlayRerenders: Observable<{
42+
/**
43+
* The HoverOverlay element
44+
*/
4245
hoverOverlayElement: HTMLElement
43-
scrollElement: HTMLElement
46+
47+
/**
48+
* The closest parent element that is `position: relative`
49+
*/
50+
relativeElement: HTMLElement
4451
}>
4552

4653
/**
@@ -334,8 +341,8 @@ export const createHoverifier = ({
334341
.pipe(
335342
// with the latest target that came from either a mouseover, click or location change (whatever was the most recent)
336343
withLatestFrom(merge(codeMouseOverTargets, codeClickTargets, jumpTargets)),
337-
map(([{ hoverOverlayElement, scrollElement }, { target }]) =>
338-
calculateOverlayPosition(scrollElement, target, hoverOverlayElement)
344+
map(([{ hoverOverlayElement, relativeElement }, { target }]) =>
345+
calculateOverlayPosition({ relativeElement, target, hoverOverlayElement })
339346
)
340347
)
341348
.subscribe(hoverOverlayPosition => {

src/overlay_position.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,41 @@
44
*/
55
const BLOB_PADDING_TOP = 8
66

7+
interface CalculateOverlayPositionOptions {
8+
/** The closest parent element that is `position: relative` */
9+
relativeElement: HTMLElement
10+
/** The DOM Node that was hovered */
11+
target: HTMLElement
12+
/** The DOM Node of the tooltip */
13+
hoverOverlayElement: HTMLElement
14+
}
15+
716
/**
817
* Calculates the desired position of the hover overlay depending on the container,
918
* the hover target and the size of the hover overlay
10-
*
11-
* @param scrollable The closest container that is scrollable
12-
* @param target The DOM Node that was hovered
13-
* @param tooltip The DOM Node of the tooltip
1419
*/
15-
export const calculateOverlayPosition = (
16-
scrollable: HTMLElement,
17-
target: HTMLElement,
18-
tooltip: HTMLElement
19-
): { left: number; top: number } => {
20+
export const calculateOverlayPosition = ({
21+
relativeElement,
22+
target,
23+
hoverOverlayElement,
24+
}: CalculateOverlayPositionOptions): { left: number; top: number } => {
2025
// The scrollable element is the one with scrollbars. The scrolling element is the one with the content.
21-
const scrollableBounds = scrollable.getBoundingClientRect()
26+
const relativeElementBounds = relativeElement.getBoundingClientRect()
2227
const targetBound = target.getBoundingClientRect() // our target elements bounds
2328

2429
// Anchor it horizontally, prior to rendering to account for wrapping
2530
// changes to vertical height if the tooltip is at the edge of the viewport.
26-
const relLeft = targetBound.left - scrollableBounds.left
31+
const relLeft = targetBound.left - relativeElementBounds.left
2732

2833
// Anchor the tooltip vertically.
29-
const tooltipBound = tooltip.getBoundingClientRect()
30-
const relTop = targetBound.top + scrollable.scrollTop - scrollableBounds.top
34+
const tooltipBound = hoverOverlayElement.getBoundingClientRect()
35+
const relTop = targetBound.top + relativeElement.scrollTop - relativeElementBounds.top
3136
// This is the padding-top of the blob element
3237
let tooltipTop = relTop - (tooltipBound.height - BLOB_PADDING_TOP)
33-
if (tooltipTop - scrollable.scrollTop < 0) {
38+
if (tooltipTop - relativeElement.scrollTop < 0) {
3439
// Tooltip wouldn't be visible from the top, so display it at the
3540
// bottom.
36-
const relBottom = targetBound.bottom + scrollable.scrollTop - scrollableBounds.top
41+
const relBottom = targetBound.bottom + relativeElement.scrollTop - relativeElementBounds.top
3742
tooltipTop = relBottom
3843
} else {
3944
tooltipTop -= BLOB_PADDING_TOP

0 commit comments

Comments
 (0)