Skip to content

Commit dc2795a

Browse files
committed
refactor: popper anchor element
1 parent e041f12 commit dc2795a

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

packages/utilities/popper/src/get-anchor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function getAnchorElement(
3030
getAnchorRect?: (anchor: MaybeRectElement) => AnchorRect | null,
3131
): VirtualElement {
3232
return {
33-
contextElement: isHTMLElement(anchorElement) ? anchorElement : undefined,
33+
contextElement: isHTMLElement(anchorElement) ? anchorElement : anchorElement?.contextElement,
3434
getBoundingClientRect: () => {
3535
const anchor = anchorElement
3636
const anchorRect = getAnchorRect?.(anchor)

packages/utilities/popper/src/get-placement.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ const defaultOptions: PositioningOptions = {
2424
type NonNullable<T> = T extends null | undefined ? never : T
2525
type RequiredBy<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> }
2626

27-
interface Options
28-
extends RequiredBy<
29-
PositioningOptions,
30-
| "strategy"
31-
| "placement"
32-
| "listeners"
33-
| "gutter"
34-
| "flip"
35-
| "slide"
36-
| "overlap"
37-
| "sameWidth"
38-
| "fitViewport"
39-
| "overflowPadding"
40-
| "arrowPadding"
41-
> {}
27+
interface Options extends RequiredBy<
28+
PositioningOptions,
29+
| "strategy"
30+
| "placement"
31+
| "listeners"
32+
| "gutter"
33+
| "flip"
34+
| "slide"
35+
| "overlap"
36+
| "sameWidth"
37+
| "fitViewport"
38+
| "overflowPadding"
39+
| "arrowPadding"
40+
> {}
4241

4342
function roundByDpr(win: Window, value: number) {
4443
const dpr = win.devicePixelRatio || 1
@@ -131,7 +130,8 @@ function getAutoUpdateOptions(opts?: boolean | AutoUpdateOptions): AutoUpdateOpt
131130
}
132131

133132
function getPlacementImpl(referenceOrVirtual: MaybeRectElement, floating: MaybeElement, opts: PositioningOptions = {}) {
134-
const reference = getAnchorElement(referenceOrVirtual, opts.getAnchorRect)
133+
const anchor = opts.getAnchorElement?.() ?? referenceOrVirtual
134+
const reference = getAnchorElement(anchor, opts.getAnchorRect)
135135
if (!floating || !reference) return
136136
const options = Object.assign({}, defaultOptions, opts) as Options
137137

packages/utilities/popper/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ export interface PositioningOptions {
8989
* Function called when the floating element is positioned or not
9090
*/
9191
onPositioned?: ((data: { placed: boolean }) => void) | undefined
92+
/**
93+
* Function that returns the anchor element.
94+
* Useful when you want to use a different element as the anchor.
95+
*/
96+
getAnchorElement?: (() => HTMLElement | VirtualElement | null) | undefined
9297
/**
9398
* Function that returns the anchor rect
99+
* @deprecated Use `getAnchorElement` instead
94100
*/
95101
getAnchorRect?: ((element: HTMLElement | VirtualElement | null) => AnchorRect | null) | undefined
96102
/**

0 commit comments

Comments
 (0)