@zag-js/popper 0.10.2 → 0.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auto-update.d.ts +4 -7
- package/dist/auto-update.js +14 -36
- package/dist/auto-update.mjs +40 -5
- package/dist/get-placement.d.ts +5 -8
- package/dist/get-placement.js +29 -144
- package/dist/get-placement.mjs +120 -9
- package/dist/get-styles.d.ts +3 -5
- package/dist/get-styles.js +11 -44
- package/dist/get-styles.mjs +37 -6
- package/dist/index.d.ts +3 -5
- package/dist/index.js +7 -276
- package/dist/index.mjs +2 -14
- package/dist/middleware.d.ts +5 -7
- package/dist/middleware.js +12 -37
- package/dist/middleware.mjs +51 -9
- package/dist/types.d.ts +5 -8
- package/package.json +6 -11
- package/src/get-placement.ts +1 -1
- package/dist/chunk-EC2KMZAZ.mjs +0 -130
- package/dist/chunk-MLNMIWN3.mjs +0 -43
- package/dist/chunk-V4C4UQPN.mjs +0 -43
- package/dist/chunk-X5LLREVI.mjs +0 -57
- package/dist/types.js +0 -18
- package/dist/types.mjs +0 -0
package/dist/auto-update.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import { ReferenceElement } from
|
|
2
|
-
export { Placement }
|
|
3
|
-
|
|
4
|
-
type AutoUpdateOptions = {
|
|
1
|
+
import type { Placement, ReferenceElement } from "@floating-ui/dom";
|
|
2
|
+
export type { Placement };
|
|
3
|
+
export type AutoUpdateOptions = {
|
|
5
4
|
ancestorScroll?: boolean;
|
|
6
5
|
ancestorResize?: boolean;
|
|
7
6
|
referenceResize?: boolean;
|
|
8
7
|
};
|
|
9
|
-
declare function autoUpdate(reference: ReferenceElement, floating: HTMLElement, update: () => void, options?: boolean | AutoUpdateOptions): () => void;
|
|
10
|
-
|
|
11
|
-
export { AutoUpdateOptions, autoUpdate };
|
|
8
|
+
export declare function autoUpdate(reference: ReferenceElement, floating: HTMLElement, update: () => void, options?: boolean | AutoUpdateOptions): () => void;
|
package/dist/auto-update.js
CHANGED
|
@@ -1,35 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
19
2
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
var import_element_rect = require("@zag-js/element-rect");
|
|
28
|
-
var callAll = (...fns) => () => fns.forEach((fn) => fn());
|
|
29
|
-
var isHTMLElement = (el) => {
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const dom = require('@floating-ui/dom');
|
|
6
|
+
const elementRect = require('@zag-js/element-rect');
|
|
7
|
+
|
|
8
|
+
const callAll = (...fns) => () => fns.forEach((fn) => fn());
|
|
9
|
+
const isHTMLElement = (el) => {
|
|
30
10
|
return typeof el === "object" && el !== null && el.nodeType === 1;
|
|
31
11
|
};
|
|
32
|
-
|
|
12
|
+
const addDomEvent = (el, type, fn, options) => {
|
|
33
13
|
el.addEventListener(type, fn, options);
|
|
34
14
|
return () => el.removeEventListener(type, fn, options);
|
|
35
15
|
};
|
|
@@ -46,12 +26,12 @@ function autoUpdate(reference, floating, update, options = false) {
|
|
|
46
26
|
const useAncestors = ancestorScroll || ancestorResize;
|
|
47
27
|
const ancestors = [];
|
|
48
28
|
if (useAncestors && isHTMLElement(reference)) {
|
|
49
|
-
ancestors.push(...
|
|
29
|
+
ancestors.push(...dom.getOverflowAncestors(reference));
|
|
50
30
|
}
|
|
51
31
|
function addResizeListeners() {
|
|
52
|
-
let cleanups = [
|
|
32
|
+
let cleanups = [elementRect.trackElementRect(floating, { scope: "size", onChange: update })];
|
|
53
33
|
if (referenceResize && isHTMLElement(reference)) {
|
|
54
|
-
cleanups.push(
|
|
34
|
+
cleanups.push(elementRect.trackElementRect(reference, { onChange: update }));
|
|
55
35
|
}
|
|
56
36
|
cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
|
|
57
37
|
return () => cleanups.forEach((fn) => fn());
|
|
@@ -61,7 +41,5 @@ function autoUpdate(reference, floating, update, options = false) {
|
|
|
61
41
|
}
|
|
62
42
|
return callAll(addResizeListeners(), addScrollListeners());
|
|
63
43
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
autoUpdate
|
|
67
|
-
});
|
|
44
|
+
|
|
45
|
+
exports.autoUpdate = autoUpdate;
|
package/dist/auto-update.mjs
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { getOverflowAncestors } from '@floating-ui/dom';
|
|
2
|
+
import { trackElementRect } from '@zag-js/element-rect';
|
|
3
|
+
|
|
4
|
+
const callAll = (...fns) => () => fns.forEach((fn) => fn());
|
|
5
|
+
const isHTMLElement = (el) => {
|
|
6
|
+
return typeof el === "object" && el !== null && el.nodeType === 1;
|
|
6
7
|
};
|
|
8
|
+
const addDomEvent = (el, type, fn, options) => {
|
|
9
|
+
el.addEventListener(type, fn, options);
|
|
10
|
+
return () => el.removeEventListener(type, fn, options);
|
|
11
|
+
};
|
|
12
|
+
function resolveOptions(option) {
|
|
13
|
+
const bool = typeof option === "boolean";
|
|
14
|
+
return {
|
|
15
|
+
ancestorResize: bool ? option : option.ancestorResize ?? true,
|
|
16
|
+
ancestorScroll: bool ? option : option.ancestorScroll ?? true,
|
|
17
|
+
referenceResize: bool ? option : option.referenceResize ?? true
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function autoUpdate(reference, floating, update, options = false) {
|
|
21
|
+
const { ancestorScroll, ancestorResize, referenceResize } = resolveOptions(options);
|
|
22
|
+
const useAncestors = ancestorScroll || ancestorResize;
|
|
23
|
+
const ancestors = [];
|
|
24
|
+
if (useAncestors && isHTMLElement(reference)) {
|
|
25
|
+
ancestors.push(...getOverflowAncestors(reference));
|
|
26
|
+
}
|
|
27
|
+
function addResizeListeners() {
|
|
28
|
+
let cleanups = [trackElementRect(floating, { scope: "size", onChange: update })];
|
|
29
|
+
if (referenceResize && isHTMLElement(reference)) {
|
|
30
|
+
cleanups.push(trackElementRect(reference, { onChange: update }));
|
|
31
|
+
}
|
|
32
|
+
cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
|
|
33
|
+
return () => cleanups.forEach((fn) => fn());
|
|
34
|
+
}
|
|
35
|
+
function addScrollListeners() {
|
|
36
|
+
return callAll(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
|
|
37
|
+
}
|
|
38
|
+
return callAll(addResizeListeners(), addScrollListeners());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { autoUpdate };
|
package/dist/get-placement.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import { Placement, VirtualElement } from
|
|
2
|
-
import { BasePlacement, PositioningOptions } from
|
|
3
|
-
import './auto-update.js';
|
|
4
|
-
|
|
1
|
+
import type { Placement, VirtualElement } from "@floating-ui/dom";
|
|
2
|
+
import type { BasePlacement, PositioningOptions } from "./types";
|
|
5
3
|
type MaybeRectElement = HTMLElement | VirtualElement | null;
|
|
6
4
|
type MaybeElement = HTMLElement | null;
|
|
7
5
|
type MaybeFn<T> = T | (() => T);
|
|
8
|
-
declare function getBasePlacement(placement: Placement): BasePlacement;
|
|
9
|
-
declare function getPlacement(referenceOrFn: MaybeFn<MaybeRectElement>, floatingOrFn: MaybeFn<MaybeElement>, opts?: PositioningOptions & {
|
|
6
|
+
export declare function getBasePlacement(placement: Placement): BasePlacement;
|
|
7
|
+
export declare function getPlacement(referenceOrFn: MaybeFn<MaybeRectElement>, floatingOrFn: MaybeFn<MaybeElement>, opts?: PositioningOptions & {
|
|
10
8
|
defer?: boolean;
|
|
11
9
|
}): () => void;
|
|
12
|
-
|
|
13
|
-
export { getBasePlacement, getPlacement };
|
|
10
|
+
export {};
|
package/dist/get-placement.js
CHANGED
|
@@ -1,127 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
19
2
|
|
|
20
|
-
|
|
21
|
-
var get_placement_exports = {};
|
|
22
|
-
__export(get_placement_exports, {
|
|
23
|
-
getBasePlacement: () => getBasePlacement,
|
|
24
|
-
getPlacement: () => getPlacement
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(get_placement_exports);
|
|
27
|
-
var import_dom2 = require("@floating-ui/dom");
|
|
28
|
-
var import_dom_query = require("@zag-js/dom-query");
|
|
29
|
-
var import_utils = require("@zag-js/utils");
|
|
30
|
-
|
|
31
|
-
// src/auto-update.ts
|
|
32
|
-
var import_dom = require("@floating-ui/dom");
|
|
33
|
-
var import_element_rect = require("@zag-js/element-rect");
|
|
34
|
-
var callAll = (...fns) => () => fns.forEach((fn) => fn());
|
|
35
|
-
var isHTMLElement = (el) => {
|
|
36
|
-
return typeof el === "object" && el !== null && el.nodeType === 1;
|
|
37
|
-
};
|
|
38
|
-
var addDomEvent = (el, type, fn, options) => {
|
|
39
|
-
el.addEventListener(type, fn, options);
|
|
40
|
-
return () => el.removeEventListener(type, fn, options);
|
|
41
|
-
};
|
|
42
|
-
function resolveOptions(option) {
|
|
43
|
-
const bool = typeof option === "boolean";
|
|
44
|
-
return {
|
|
45
|
-
ancestorResize: bool ? option : option.ancestorResize ?? true,
|
|
46
|
-
ancestorScroll: bool ? option : option.ancestorScroll ?? true,
|
|
47
|
-
referenceResize: bool ? option : option.referenceResize ?? true
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
function autoUpdate(reference, floating, update, options = false) {
|
|
51
|
-
const { ancestorScroll, ancestorResize, referenceResize } = resolveOptions(options);
|
|
52
|
-
const useAncestors = ancestorScroll || ancestorResize;
|
|
53
|
-
const ancestors = [];
|
|
54
|
-
if (useAncestors && isHTMLElement(reference)) {
|
|
55
|
-
ancestors.push(...(0, import_dom.getOverflowAncestors)(reference));
|
|
56
|
-
}
|
|
57
|
-
function addResizeListeners() {
|
|
58
|
-
let cleanups = [(0, import_element_rect.trackElementRect)(floating, { scope: "size", onChange: update })];
|
|
59
|
-
if (referenceResize && isHTMLElement(reference)) {
|
|
60
|
-
cleanups.push((0, import_element_rect.trackElementRect)(reference, { onChange: update }));
|
|
61
|
-
}
|
|
62
|
-
cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
|
|
63
|
-
return () => cleanups.forEach((fn) => fn());
|
|
64
|
-
}
|
|
65
|
-
function addScrollListeners() {
|
|
66
|
-
return callAll(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
|
|
67
|
-
}
|
|
68
|
-
return callAll(addResizeListeners(), addScrollListeners());
|
|
69
|
-
}
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
70
4
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
arrowBg: toVar("--arrow-background"),
|
|
77
|
-
transformOrigin: toVar("--transform-origin"),
|
|
78
|
-
arrowOffset: toVar("--arrow-offset")
|
|
79
|
-
};
|
|
80
|
-
var getTransformOrigin = (arrow2) => ({
|
|
81
|
-
top: "bottom center",
|
|
82
|
-
"top-start": arrow2 ? `${arrow2.x}px bottom` : "left bottom",
|
|
83
|
-
"top-end": arrow2 ? `${arrow2.x}px bottom` : "right bottom",
|
|
84
|
-
bottom: "top center",
|
|
85
|
-
"bottom-start": arrow2 ? `${arrow2.x}px top` : "top left",
|
|
86
|
-
"bottom-end": arrow2 ? `${arrow2.x}px top` : "top right",
|
|
87
|
-
left: "right center",
|
|
88
|
-
"left-start": arrow2 ? `right ${arrow2.y}px` : "right top",
|
|
89
|
-
"left-end": arrow2 ? `right ${arrow2.y}px` : "right bottom",
|
|
90
|
-
right: "left center",
|
|
91
|
-
"right-start": arrow2 ? `left ${arrow2.y}px` : "left top",
|
|
92
|
-
"right-end": arrow2 ? `left ${arrow2.y}px` : "left bottom"
|
|
93
|
-
});
|
|
94
|
-
var transformOrigin = {
|
|
95
|
-
name: "transformOrigin",
|
|
96
|
-
fn({ placement, elements, middlewareData }) {
|
|
97
|
-
const { arrow: arrow2 } = middlewareData;
|
|
98
|
-
const transformOrigin2 = getTransformOrigin(arrow2)[placement];
|
|
99
|
-
const { floating } = elements;
|
|
100
|
-
floating.style.setProperty(cssVars.transformOrigin.variable, transformOrigin2);
|
|
101
|
-
return {
|
|
102
|
-
data: { transformOrigin: transformOrigin2 }
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
var shiftArrow = (opts) => ({
|
|
107
|
-
name: "shiftArrow",
|
|
108
|
-
fn({ placement, middlewareData }) {
|
|
109
|
-
const { element: arrow2 } = opts;
|
|
110
|
-
if (middlewareData.arrow) {
|
|
111
|
-
const { x, y } = middlewareData.arrow;
|
|
112
|
-
const dir = placement.split("-")[0];
|
|
113
|
-
Object.assign(arrow2.style, {
|
|
114
|
-
left: x != null ? `${x}px` : "",
|
|
115
|
-
top: y != null ? `${y}px` : "",
|
|
116
|
-
[dir]: `calc(100% + ${cssVars.arrowOffset.reference})`
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
return {};
|
|
120
|
-
}
|
|
121
|
-
});
|
|
5
|
+
const dom = require('@floating-ui/dom');
|
|
6
|
+
const domQuery = require('@zag-js/dom-query');
|
|
7
|
+
const utils = require('@zag-js/utils');
|
|
8
|
+
const autoUpdate = require('./auto-update.js');
|
|
9
|
+
const middleware = require('./middleware.js');
|
|
122
10
|
|
|
123
|
-
|
|
124
|
-
var defaultOptions = {
|
|
11
|
+
const defaultOptions = {
|
|
125
12
|
strategy: "absolute",
|
|
126
13
|
placement: "bottom",
|
|
127
14
|
listeners: true,
|
|
@@ -135,11 +22,11 @@ function getPlacementImpl(reference, floating, opts = {}) {
|
|
|
135
22
|
return;
|
|
136
23
|
const options = Object.assign({}, defaultOptions, opts);
|
|
137
24
|
const arrowEl = floating.querySelector("[data-part=arrow]");
|
|
138
|
-
const middleware = [];
|
|
25
|
+
const middleware$1 = [];
|
|
139
26
|
const boundary = typeof options.boundary === "function" ? options.boundary() : options.boundary;
|
|
140
27
|
if (options.flip) {
|
|
141
|
-
middleware.push(
|
|
142
|
-
|
|
28
|
+
middleware$1.push(
|
|
29
|
+
dom.flip({
|
|
143
30
|
boundary,
|
|
144
31
|
padding: options.overflowPadding
|
|
145
32
|
})
|
|
@@ -147,27 +34,27 @@ function getPlacementImpl(reference, floating, opts = {}) {
|
|
|
147
34
|
}
|
|
148
35
|
if (options.gutter || options.offset) {
|
|
149
36
|
const arrowOffset = arrowEl ? arrowEl.offsetHeight / 2 : 0;
|
|
150
|
-
const data = options.
|
|
37
|
+
const data = options.offset ? options.offset : { mainAxis: options.gutter };
|
|
151
38
|
if (data?.mainAxis != null)
|
|
152
39
|
data.mainAxis += arrowOffset;
|
|
153
|
-
middleware.push(
|
|
40
|
+
middleware$1.push(dom.offset(data));
|
|
154
41
|
}
|
|
155
|
-
middleware.push(
|
|
156
|
-
|
|
42
|
+
middleware$1.push(
|
|
43
|
+
dom.shift({
|
|
157
44
|
boundary,
|
|
158
45
|
crossAxis: options.overlap,
|
|
159
46
|
padding: options.overflowPadding
|
|
160
47
|
})
|
|
161
48
|
);
|
|
162
49
|
if (arrowEl) {
|
|
163
|
-
middleware.push(
|
|
164
|
-
|
|
165
|
-
shiftArrow({ element: arrowEl })
|
|
50
|
+
middleware$1.push(
|
|
51
|
+
dom.arrow({ element: arrowEl, padding: 8 }),
|
|
52
|
+
middleware.shiftArrow({ element: arrowEl })
|
|
166
53
|
);
|
|
167
54
|
}
|
|
168
|
-
middleware.push(transformOrigin);
|
|
169
|
-
middleware.push(
|
|
170
|
-
|
|
55
|
+
middleware$1.push(middleware.transformOrigin);
|
|
56
|
+
middleware$1.push(
|
|
57
|
+
dom.size({
|
|
171
58
|
padding: options.overflowPadding,
|
|
172
59
|
apply({ rects, availableHeight, availableWidth }) {
|
|
173
60
|
const referenceWidth = Math.round(rects.reference.width);
|
|
@@ -193,9 +80,9 @@ function getPlacementImpl(reference, floating, opts = {}) {
|
|
|
193
80
|
if (!reference || !floating)
|
|
194
81
|
return;
|
|
195
82
|
const { placement, strategy, onComplete } = options;
|
|
196
|
-
|
|
83
|
+
dom.computePosition(reference, floating, {
|
|
197
84
|
placement,
|
|
198
|
-
middleware,
|
|
85
|
+
middleware: middleware$1,
|
|
199
86
|
strategy,
|
|
200
87
|
...config
|
|
201
88
|
}).then((data) => {
|
|
@@ -211,8 +98,8 @@ function getPlacementImpl(reference, floating, opts = {}) {
|
|
|
211
98
|
});
|
|
212
99
|
}
|
|
213
100
|
compute();
|
|
214
|
-
return
|
|
215
|
-
options.listeners ? autoUpdate(reference, floating, compute, options.listeners) : void 0,
|
|
101
|
+
return utils.callAll(
|
|
102
|
+
options.listeners ? autoUpdate.autoUpdate(reference, floating, compute, options.listeners) : void 0,
|
|
216
103
|
options.onCleanup
|
|
217
104
|
);
|
|
218
105
|
}
|
|
@@ -221,7 +108,7 @@ function getBasePlacement(placement) {
|
|
|
221
108
|
}
|
|
222
109
|
function getPlacement(referenceOrFn, floatingOrFn, opts = {}) {
|
|
223
110
|
const { defer, ...restOptions } = opts;
|
|
224
|
-
const func = defer ?
|
|
111
|
+
const func = defer ? domQuery.raf : (v) => v();
|
|
225
112
|
const cleanups = [];
|
|
226
113
|
cleanups.push(
|
|
227
114
|
func(() => {
|
|
@@ -234,8 +121,6 @@ function getPlacement(referenceOrFn, floatingOrFn, opts = {}) {
|
|
|
234
121
|
cleanups.forEach((fn) => fn?.());
|
|
235
122
|
};
|
|
236
123
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
getPlacement
|
|
241
|
-
});
|
|
124
|
+
|
|
125
|
+
exports.getBasePlacement = getBasePlacement;
|
|
126
|
+
exports.getPlacement = getPlacement;
|
package/dist/get-placement.mjs
CHANGED
|
@@ -1,10 +1,121 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { flip, offset, shift, arrow, size, computePosition } from '@floating-ui/dom';
|
|
2
|
+
import { raf } from '@zag-js/dom-query';
|
|
3
|
+
import { callAll } from '@zag-js/utils';
|
|
4
|
+
import { autoUpdate } from './auto-update.mjs';
|
|
5
|
+
import { shiftArrow, transformOrigin } from './middleware.mjs';
|
|
6
|
+
|
|
7
|
+
const defaultOptions = {
|
|
8
|
+
strategy: "absolute",
|
|
9
|
+
placement: "bottom",
|
|
10
|
+
listeners: true,
|
|
11
|
+
gutter: 8,
|
|
12
|
+
flip: true,
|
|
13
|
+
sameWidth: false,
|
|
14
|
+
overflowPadding: 8
|
|
10
15
|
};
|
|
16
|
+
function getPlacementImpl(reference, floating, opts = {}) {
|
|
17
|
+
if (!floating || !reference)
|
|
18
|
+
return;
|
|
19
|
+
const options = Object.assign({}, defaultOptions, opts);
|
|
20
|
+
const arrowEl = floating.querySelector("[data-part=arrow]");
|
|
21
|
+
const middleware = [];
|
|
22
|
+
const boundary = typeof options.boundary === "function" ? options.boundary() : options.boundary;
|
|
23
|
+
if (options.flip) {
|
|
24
|
+
middleware.push(
|
|
25
|
+
flip({
|
|
26
|
+
boundary,
|
|
27
|
+
padding: options.overflowPadding
|
|
28
|
+
})
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
if (options.gutter || options.offset) {
|
|
32
|
+
const arrowOffset = arrowEl ? arrowEl.offsetHeight / 2 : 0;
|
|
33
|
+
const data = options.offset ? options.offset : { mainAxis: options.gutter };
|
|
34
|
+
if (data?.mainAxis != null)
|
|
35
|
+
data.mainAxis += arrowOffset;
|
|
36
|
+
middleware.push(offset(data));
|
|
37
|
+
}
|
|
38
|
+
middleware.push(
|
|
39
|
+
shift({
|
|
40
|
+
boundary,
|
|
41
|
+
crossAxis: options.overlap,
|
|
42
|
+
padding: options.overflowPadding
|
|
43
|
+
})
|
|
44
|
+
);
|
|
45
|
+
if (arrowEl) {
|
|
46
|
+
middleware.push(
|
|
47
|
+
arrow({ element: arrowEl, padding: 8 }),
|
|
48
|
+
shiftArrow({ element: arrowEl })
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
middleware.push(transformOrigin);
|
|
52
|
+
middleware.push(
|
|
53
|
+
size({
|
|
54
|
+
padding: options.overflowPadding,
|
|
55
|
+
apply({ rects, availableHeight, availableWidth }) {
|
|
56
|
+
const referenceWidth = Math.round(rects.reference.width);
|
|
57
|
+
floating.style.setProperty("--reference-width", `${referenceWidth}px`);
|
|
58
|
+
floating.style.setProperty("--available-width", `${availableWidth}px`);
|
|
59
|
+
floating.style.setProperty("--available-height", `${availableHeight}px`);
|
|
60
|
+
if (options.sameWidth) {
|
|
61
|
+
Object.assign(floating.style, {
|
|
62
|
+
width: `${referenceWidth}px`,
|
|
63
|
+
minWidth: "unset"
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (options.fitViewport) {
|
|
67
|
+
Object.assign(floating.style, {
|
|
68
|
+
maxWidth: `${availableWidth}px`,
|
|
69
|
+
maxHeight: `${availableHeight}px`
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
function compute(config = {}) {
|
|
76
|
+
if (!reference || !floating)
|
|
77
|
+
return;
|
|
78
|
+
const { placement, strategy, onComplete } = options;
|
|
79
|
+
computePosition(reference, floating, {
|
|
80
|
+
placement,
|
|
81
|
+
middleware,
|
|
82
|
+
strategy,
|
|
83
|
+
...config
|
|
84
|
+
}).then((data) => {
|
|
85
|
+
const x = Math.round(data.x);
|
|
86
|
+
const y = Math.round(data.y);
|
|
87
|
+
Object.assign(floating.style, {
|
|
88
|
+
position: data.strategy,
|
|
89
|
+
top: "0px",
|
|
90
|
+
left: "0px",
|
|
91
|
+
transform: `translate3d(${x}px, ${y}px, 0)`
|
|
92
|
+
});
|
|
93
|
+
onComplete?.(data);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
compute();
|
|
97
|
+
return callAll(
|
|
98
|
+
options.listeners ? autoUpdate(reference, floating, compute, options.listeners) : void 0,
|
|
99
|
+
options.onCleanup
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
function getBasePlacement(placement) {
|
|
103
|
+
return placement.split("-")[0];
|
|
104
|
+
}
|
|
105
|
+
function getPlacement(referenceOrFn, floatingOrFn, opts = {}) {
|
|
106
|
+
const { defer, ...restOptions } = opts;
|
|
107
|
+
const func = defer ? raf : (v) => v();
|
|
108
|
+
const cleanups = [];
|
|
109
|
+
cleanups.push(
|
|
110
|
+
func(() => {
|
|
111
|
+
const reference = typeof referenceOrFn === "function" ? referenceOrFn() : referenceOrFn;
|
|
112
|
+
const floating = typeof floatingOrFn === "function" ? floatingOrFn() : floatingOrFn;
|
|
113
|
+
cleanups.push(getPlacementImpl(reference, floating, restOptions));
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
return () => {
|
|
117
|
+
cleanups.forEach((fn) => fn?.());
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { getBasePlacement, getPlacement };
|
package/dist/get-styles.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Placement } from
|
|
2
|
-
|
|
1
|
+
import type { Placement } from "@floating-ui/dom";
|
|
3
2
|
type Options = {
|
|
4
3
|
placement?: Placement;
|
|
5
4
|
};
|
|
6
|
-
declare function getPlacementStyles(options: Options): {
|
|
5
|
+
export declare function getPlacementStyles(options: Options): {
|
|
7
6
|
arrow: {
|
|
8
7
|
readonly [x: string]: string;
|
|
9
8
|
readonly position: "absolute";
|
|
@@ -27,5 +26,4 @@ declare function getPlacementStyles(options: Options): {
|
|
|
27
26
|
readonly left: "0px";
|
|
28
27
|
};
|
|
29
28
|
};
|
|
30
|
-
|
|
31
|
-
export { getPlacementStyles };
|
|
29
|
+
export {};
|
package/dist/get-styles.js
CHANGED
|
@@ -1,41 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
19
2
|
|
|
20
|
-
|
|
21
|
-
var get_styles_exports = {};
|
|
22
|
-
__export(get_styles_exports, {
|
|
23
|
-
getPlacementStyles: () => getPlacementStyles
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(get_styles_exports);
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
26
4
|
|
|
27
|
-
|
|
28
|
-
var toVar = (value) => ({ variable: value, reference: `var(${value})` });
|
|
29
|
-
var cssVars = {
|
|
30
|
-
arrowSize: toVar("--arrow-size"),
|
|
31
|
-
arrowSizeHalf: toVar("--arrow-size-half"),
|
|
32
|
-
arrowBg: toVar("--arrow-background"),
|
|
33
|
-
transformOrigin: toVar("--transform-origin"),
|
|
34
|
-
arrowOffset: toVar("--arrow-offset")
|
|
35
|
-
};
|
|
5
|
+
const middleware = require('./middleware.js');
|
|
36
6
|
|
|
37
|
-
|
|
38
|
-
var ARROW_FLOATING_STYLE = {
|
|
7
|
+
const ARROW_FLOATING_STYLE = {
|
|
39
8
|
bottom: "rotate(45deg)",
|
|
40
9
|
left: "rotate(135deg)",
|
|
41
10
|
top: "rotate(225deg)",
|
|
@@ -46,14 +15,14 @@ function getPlacementStyles(options) {
|
|
|
46
15
|
return {
|
|
47
16
|
arrow: {
|
|
48
17
|
position: "absolute",
|
|
49
|
-
width: cssVars.arrowSize.reference,
|
|
50
|
-
height: cssVars.arrowSize.reference,
|
|
51
|
-
[cssVars.arrowSizeHalf.variable]: `calc(${cssVars.arrowSize.reference} / 2)`,
|
|
52
|
-
[cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`
|
|
18
|
+
width: middleware.cssVars.arrowSize.reference,
|
|
19
|
+
height: middleware.cssVars.arrowSize.reference,
|
|
20
|
+
[middleware.cssVars.arrowSizeHalf.variable]: `calc(${middleware.cssVars.arrowSize.reference} / 2)`,
|
|
21
|
+
[middleware.cssVars.arrowOffset.variable]: `calc(${middleware.cssVars.arrowSizeHalf.reference} * -1)`
|
|
53
22
|
},
|
|
54
23
|
arrowTip: {
|
|
55
24
|
transform: ARROW_FLOATING_STYLE[placement.split("-")[0]],
|
|
56
|
-
background: cssVars.arrowBg.reference,
|
|
25
|
+
background: middleware.cssVars.arrowBg.reference,
|
|
57
26
|
top: "0",
|
|
58
27
|
left: "0",
|
|
59
28
|
width: "100%",
|
|
@@ -69,7 +38,5 @@ function getPlacementStyles(options) {
|
|
|
69
38
|
}
|
|
70
39
|
};
|
|
71
40
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
getPlacementStyles
|
|
75
|
-
});
|
|
41
|
+
|
|
42
|
+
exports.getPlacementStyles = getPlacementStyles;
|
package/dist/get-styles.mjs
CHANGED
|
@@ -1,7 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { cssVars } from './middleware.mjs';
|
|
2
|
+
|
|
3
|
+
const ARROW_FLOATING_STYLE = {
|
|
4
|
+
bottom: "rotate(45deg)",
|
|
5
|
+
left: "rotate(135deg)",
|
|
6
|
+
top: "rotate(225deg)",
|
|
7
|
+
right: "rotate(315deg)"
|
|
7
8
|
};
|
|
9
|
+
function getPlacementStyles(options) {
|
|
10
|
+
const { placement = "bottom" } = options;
|
|
11
|
+
return {
|
|
12
|
+
arrow: {
|
|
13
|
+
position: "absolute",
|
|
14
|
+
width: cssVars.arrowSize.reference,
|
|
15
|
+
height: cssVars.arrowSize.reference,
|
|
16
|
+
[cssVars.arrowSizeHalf.variable]: `calc(${cssVars.arrowSize.reference} / 2)`,
|
|
17
|
+
[cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`
|
|
18
|
+
},
|
|
19
|
+
arrowTip: {
|
|
20
|
+
transform: ARROW_FLOATING_STYLE[placement.split("-")[0]],
|
|
21
|
+
background: cssVars.arrowBg.reference,
|
|
22
|
+
top: "0",
|
|
23
|
+
left: "0",
|
|
24
|
+
width: "100%",
|
|
25
|
+
height: "100%",
|
|
26
|
+
position: "absolute",
|
|
27
|
+
zIndex: "inherit"
|
|
28
|
+
},
|
|
29
|
+
floating: {
|
|
30
|
+
position: "absolute",
|
|
31
|
+
minWidth: "max-content",
|
|
32
|
+
top: "0px",
|
|
33
|
+
left: "0px"
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { getPlacementStyles };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { getPlacementStyles } from
|
|
3
|
-
export { PositioningOptions } from
|
|
4
|
-
export { Placement } from '@floating-ui/dom';
|
|
5
|
-
import './auto-update.js';
|
|
1
|
+
export { getPlacement, getBasePlacement } from "./get-placement";
|
|
2
|
+
export { getPlacementStyles } from "./get-styles";
|
|
3
|
+
export type { Placement, PositioningOptions } from "./types";
|