@zag-js/popper 0.2.1 → 0.2.3

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.
@@ -0,0 +1,11 @@
1
+ import { ReferenceElement } from '@floating-ui/dom';
2
+ export { Placement } from '@floating-ui/dom';
3
+
4
+ type AutoUpdateOptions = {
5
+ ancestorScroll?: boolean;
6
+ ancestorResize?: boolean;
7
+ referenceResize?: boolean;
8
+ };
9
+ declare function autoUpdate(reference: ReferenceElement, floating: HTMLElement, update: () => void, options?: boolean | AutoUpdateOptions): () => void;
10
+
11
+ export { AutoUpdateOptions, autoUpdate };
@@ -0,0 +1,144 @@
1
+ "use strict";
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);
19
+
20
+ // src/auto-update.ts
21
+ var auto_update_exports = {};
22
+ __export(auto_update_exports, {
23
+ autoUpdate: () => autoUpdate
24
+ });
25
+ module.exports = __toCommonJS(auto_update_exports);
26
+ var import_dom = require("@floating-ui/dom");
27
+
28
+ // ../core/src/functions.ts
29
+ var runIfFn = (v, ...a) => {
30
+ const res = typeof v === "function" ? v(...a) : v;
31
+ return res ?? void 0;
32
+ };
33
+ var callAll = (...fns) => (...a) => {
34
+ fns.forEach(function(fn) {
35
+ fn?.(...a);
36
+ });
37
+ };
38
+
39
+ // ../core/src/guard.ts
40
+ var isBoolean = (v) => v === true || v === false;
41
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
42
+
43
+ // ../dom/src/query.ts
44
+ function isHTMLElement(v) {
45
+ return typeof v === "object" && v?.nodeType === Node.ELEMENT_NODE && typeof v?.nodeName === "string";
46
+ }
47
+
48
+ // ../dom/src/listener.ts
49
+ var isRef = (v) => hasProp(v, "current");
50
+ function addDomEvent(target, eventName, handler, options) {
51
+ const node = isRef(target) ? target.current : runIfFn(target);
52
+ node?.addEventListener(eventName, handler, options);
53
+ return () => {
54
+ node?.removeEventListener(eventName, handler, options);
55
+ };
56
+ }
57
+
58
+ // ../dom/src/rect-observer.ts
59
+ function getObservedElements() {
60
+ ;
61
+ globalThis.__rectObserverMap__ = globalThis.__rectObserverMap__ || /* @__PURE__ */ new Map();
62
+ return globalThis.__rectObserverMap__;
63
+ }
64
+ function observeElementRect(el, fn) {
65
+ const observedElements = getObservedElements();
66
+ const data = observedElements.get(el);
67
+ if (!data) {
68
+ observedElements.set(el, { rect: {}, callbacks: [fn] });
69
+ if (observedElements.size === 1) {
70
+ rafId = requestAnimationFrame(runLoop);
71
+ }
72
+ } else {
73
+ data.callbacks.push(fn);
74
+ fn(el.getBoundingClientRect());
75
+ }
76
+ return function unobserve() {
77
+ const data2 = observedElements.get(el);
78
+ if (!data2)
79
+ return;
80
+ const index = data2.callbacks.indexOf(fn);
81
+ if (index > -1) {
82
+ data2.callbacks.splice(index, 1);
83
+ }
84
+ if (data2.callbacks.length === 0) {
85
+ observedElements.delete(el);
86
+ if (observedElements.size === 0) {
87
+ cancelAnimationFrame(rafId);
88
+ }
89
+ }
90
+ };
91
+ }
92
+ var rafId;
93
+ function runLoop() {
94
+ const observedElements = getObservedElements();
95
+ const changedRectsData = [];
96
+ observedElements.forEach((data, element) => {
97
+ const newRect = element.getBoundingClientRect();
98
+ if (!isEqual(data.rect, newRect)) {
99
+ data.rect = newRect;
100
+ changedRectsData.push(data);
101
+ }
102
+ });
103
+ changedRectsData.forEach((data) => {
104
+ data.callbacks.forEach((callback) => callback(data.rect));
105
+ });
106
+ rafId = requestAnimationFrame(runLoop);
107
+ }
108
+ function isEqual(rect1, rect2) {
109
+ return rect1.width === rect2.width && rect1.height === rect2.height && rect1.top === rect2.top && rect1.right === rect2.right && rect1.bottom === rect2.bottom && rect1.left === rect2.left;
110
+ }
111
+
112
+ // src/auto-update.ts
113
+ function resolveOptions(option) {
114
+ const bool = isBoolean(option);
115
+ return {
116
+ ancestorResize: bool ? option : option.ancestorResize ?? true,
117
+ ancestorScroll: bool ? option : option.ancestorScroll ?? true,
118
+ referenceResize: bool ? option : option.referenceResize ?? true
119
+ };
120
+ }
121
+ function autoUpdate(reference, floating, update, options = false) {
122
+ const { ancestorScroll, ancestorResize, referenceResize } = resolveOptions(options);
123
+ const useAncestors = ancestorScroll || ancestorResize;
124
+ const ancestors = [];
125
+ if (useAncestors && isHTMLElement(reference)) {
126
+ ancestors.push(...(0, import_dom.getOverflowAncestors)(reference));
127
+ }
128
+ function addResizeListeners() {
129
+ let cleanups = [observeElementRect(floating, update)];
130
+ if (referenceResize && isHTMLElement(reference)) {
131
+ cleanups.push(observeElementRect(reference, update));
132
+ }
133
+ cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
134
+ return () => cleanups.forEach((fn) => fn());
135
+ }
136
+ function addScrollListeners() {
137
+ return callAll(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
138
+ }
139
+ return callAll(addResizeListeners(), addScrollListeners());
140
+ }
141
+ // Annotate the CommonJS export names for ESM import in node:
142
+ 0 && (module.exports = {
143
+ autoUpdate
144
+ });
@@ -0,0 +1,6 @@
1
+ import {
2
+ autoUpdate
3
+ } from "./chunk-A2YMJLJJ.mjs";
4
+ export {
5
+ autoUpdate
6
+ };
@@ -0,0 +1,121 @@
1
+ // src/auto-update.ts
2
+ import { getOverflowAncestors } from "@floating-ui/dom";
3
+
4
+ // ../core/src/functions.ts
5
+ var runIfFn = (v, ...a) => {
6
+ const res = typeof v === "function" ? v(...a) : v;
7
+ return res ?? void 0;
8
+ };
9
+ var callAll = (...fns) => (...a) => {
10
+ fns.forEach(function(fn) {
11
+ fn?.(...a);
12
+ });
13
+ };
14
+
15
+ // ../core/src/guard.ts
16
+ var isBoolean = (v) => v === true || v === false;
17
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
18
+
19
+ // ../dom/src/query.ts
20
+ function isHTMLElement(v) {
21
+ return typeof v === "object" && v?.nodeType === Node.ELEMENT_NODE && typeof v?.nodeName === "string";
22
+ }
23
+
24
+ // ../dom/src/listener.ts
25
+ var isRef = (v) => hasProp(v, "current");
26
+ function addDomEvent(target, eventName, handler, options) {
27
+ const node = isRef(target) ? target.current : runIfFn(target);
28
+ node?.addEventListener(eventName, handler, options);
29
+ return () => {
30
+ node?.removeEventListener(eventName, handler, options);
31
+ };
32
+ }
33
+
34
+ // ../dom/src/rect-observer.ts
35
+ function getObservedElements() {
36
+ ;
37
+ globalThis.__rectObserverMap__ = globalThis.__rectObserverMap__ || /* @__PURE__ */ new Map();
38
+ return globalThis.__rectObserverMap__;
39
+ }
40
+ function observeElementRect(el, fn) {
41
+ const observedElements = getObservedElements();
42
+ const data = observedElements.get(el);
43
+ if (!data) {
44
+ observedElements.set(el, { rect: {}, callbacks: [fn] });
45
+ if (observedElements.size === 1) {
46
+ rafId = requestAnimationFrame(runLoop);
47
+ }
48
+ } else {
49
+ data.callbacks.push(fn);
50
+ fn(el.getBoundingClientRect());
51
+ }
52
+ return function unobserve() {
53
+ const data2 = observedElements.get(el);
54
+ if (!data2)
55
+ return;
56
+ const index = data2.callbacks.indexOf(fn);
57
+ if (index > -1) {
58
+ data2.callbacks.splice(index, 1);
59
+ }
60
+ if (data2.callbacks.length === 0) {
61
+ observedElements.delete(el);
62
+ if (observedElements.size === 0) {
63
+ cancelAnimationFrame(rafId);
64
+ }
65
+ }
66
+ };
67
+ }
68
+ var rafId;
69
+ function runLoop() {
70
+ const observedElements = getObservedElements();
71
+ const changedRectsData = [];
72
+ observedElements.forEach((data, element) => {
73
+ const newRect = element.getBoundingClientRect();
74
+ if (!isEqual(data.rect, newRect)) {
75
+ data.rect = newRect;
76
+ changedRectsData.push(data);
77
+ }
78
+ });
79
+ changedRectsData.forEach((data) => {
80
+ data.callbacks.forEach((callback) => callback(data.rect));
81
+ });
82
+ rafId = requestAnimationFrame(runLoop);
83
+ }
84
+ function isEqual(rect1, rect2) {
85
+ return rect1.width === rect2.width && rect1.height === rect2.height && rect1.top === rect2.top && rect1.right === rect2.right && rect1.bottom === rect2.bottom && rect1.left === rect2.left;
86
+ }
87
+
88
+ // src/auto-update.ts
89
+ function resolveOptions(option) {
90
+ const bool = isBoolean(option);
91
+ return {
92
+ ancestorResize: bool ? option : option.ancestorResize ?? true,
93
+ ancestorScroll: bool ? option : option.ancestorScroll ?? true,
94
+ referenceResize: bool ? option : option.referenceResize ?? true
95
+ };
96
+ }
97
+ function autoUpdate(reference, floating, update, options = false) {
98
+ const { ancestorScroll, ancestorResize, referenceResize } = resolveOptions(options);
99
+ const useAncestors = ancestorScroll || ancestorResize;
100
+ const ancestors = [];
101
+ if (useAncestors && isHTMLElement(reference)) {
102
+ ancestors.push(...getOverflowAncestors(reference));
103
+ }
104
+ function addResizeListeners() {
105
+ let cleanups = [observeElementRect(floating, update)];
106
+ if (referenceResize && isHTMLElement(reference)) {
107
+ cleanups.push(observeElementRect(reference, update));
108
+ }
109
+ cleanups.push(callAll(...ancestors.map((el) => addDomEvent(el, "resize", update))));
110
+ return () => cleanups.forEach((fn) => fn());
111
+ }
112
+ function addScrollListeners() {
113
+ return callAll(...ancestors.map((el) => addDomEvent(el, "scroll", update, { passive: true })));
114
+ }
115
+ return callAll(addResizeListeners(), addScrollListeners());
116
+ }
117
+
118
+ export {
119
+ callAll,
120
+ autoUpdate
121
+ };
@@ -0,0 +1,51 @@
1
+ import {
2
+ cssVars
3
+ } from "./chunk-X5LLREVI.mjs";
4
+
5
+ // src/get-styles.ts
6
+ var UNMEASURED_FLOATING_STYLE = {
7
+ position: "fixed",
8
+ top: 0,
9
+ left: 0,
10
+ opacity: 0,
11
+ transform: "translate3d(0, -200%, 0)",
12
+ pointerEvents: "none"
13
+ };
14
+ var ARROW_FLOATING_STYLE = {
15
+ bottom: "rotate(45deg)",
16
+ left: "rotate(135deg)",
17
+ top: "rotate(225deg)",
18
+ right: "rotate(315deg)"
19
+ };
20
+ function getPlacementStyles(options) {
21
+ const { measured, strategy = "absolute", placement = "bottom" } = options;
22
+ return {
23
+ arrow: {
24
+ position: "absolute",
25
+ width: cssVars.arrowSize.reference,
26
+ height: cssVars.arrowSize.reference,
27
+ [cssVars.arrowSizeHalf.variable]: `calc(${cssVars.arrowSize.reference} / 2)`,
28
+ [cssVars.arrowOffset.variable]: `calc(${cssVars.arrowSizeHalf.reference} * -1)`,
29
+ opacity: !measured ? 0 : void 0
30
+ },
31
+ arrowTip: {
32
+ transform: ARROW_FLOATING_STYLE[placement.split("-")[0]],
33
+ background: cssVars.arrowBg.reference,
34
+ top: "0",
35
+ left: "0",
36
+ width: "100%",
37
+ height: "100%",
38
+ position: "absolute",
39
+ zIndex: "inherit"
40
+ },
41
+ floating: {
42
+ position: strategy,
43
+ minWidth: "max-content",
44
+ ...!measured && UNMEASURED_FLOATING_STYLE
45
+ }
46
+ };
47
+ }
48
+
49
+ export {
50
+ getPlacementStyles
51
+ };
@@ -0,0 +1,114 @@
1
+ import {
2
+ autoUpdate,
3
+ callAll
4
+ } from "./chunk-A2YMJLJJ.mjs";
5
+ import {
6
+ shiftArrow,
7
+ transformOrigin
8
+ } from "./chunk-X5LLREVI.mjs";
9
+
10
+ // src/get-placement.ts
11
+ import { arrow, computePosition, flip, offset, shift, size } from "@floating-ui/dom";
12
+ var defaultOptions = {
13
+ strategy: "absolute",
14
+ placement: "bottom",
15
+ listeners: true,
16
+ gutter: 8,
17
+ flip: true,
18
+ sameWidth: false,
19
+ overflowPadding: 8
20
+ };
21
+ function getPlacement(reference, floating, opts = {}) {
22
+ if (!floating || !reference)
23
+ return;
24
+ const options = Object.assign({}, defaultOptions, opts);
25
+ const arrowEl = floating.querySelector("[data-part=arrow]");
26
+ const middleware = [];
27
+ const boundary = typeof options.boundary === "function" ? options.boundary() : options.boundary;
28
+ if (options.flip) {
29
+ middleware.push(
30
+ flip({
31
+ boundary,
32
+ padding: options.overflowPadding
33
+ })
34
+ );
35
+ }
36
+ if (options.gutter || options.offset) {
37
+ const arrowOffset = arrowEl ? arrowEl.offsetHeight / 2 : 0;
38
+ const data = options.gutter ? { mainAxis: options.gutter } : options.offset;
39
+ if (data?.mainAxis != null)
40
+ data.mainAxis += arrowOffset;
41
+ middleware.push(offset(data));
42
+ }
43
+ middleware.push(
44
+ shift({
45
+ boundary,
46
+ crossAxis: options.overlap,
47
+ padding: options.overflowPadding
48
+ })
49
+ );
50
+ if (arrowEl) {
51
+ middleware.push(
52
+ arrow({ element: arrowEl, padding: 8 }),
53
+ shiftArrow({ element: arrowEl })
54
+ );
55
+ }
56
+ middleware.push(transformOrigin);
57
+ middleware.push(
58
+ size({
59
+ padding: options.overflowPadding,
60
+ apply({ rects, availableHeight, availableWidth }) {
61
+ const referenceWidth = Math.round(rects.reference.width);
62
+ floating.style.setProperty("--reference-width", `${referenceWidth}px`);
63
+ floating.style.setProperty("--available-width", `${availableWidth}px`);
64
+ floating.style.setProperty("--available-height", `${availableHeight}px`);
65
+ if (options.sameWidth) {
66
+ Object.assign(floating.style, {
67
+ width: `${referenceWidth}px`,
68
+ minWidth: "unset"
69
+ });
70
+ }
71
+ if (options.fitViewport) {
72
+ Object.assign(floating.style, {
73
+ maxWidth: `${availableWidth}px`,
74
+ maxHeight: `${availableHeight}px`
75
+ });
76
+ }
77
+ }
78
+ })
79
+ );
80
+ function compute(config = {}) {
81
+ if (!reference || !floating)
82
+ return;
83
+ const { placement, strategy } = options;
84
+ computePosition(reference, floating, {
85
+ placement,
86
+ middleware,
87
+ strategy,
88
+ ...config
89
+ }).then((data) => {
90
+ const x = Math.round(data.x);
91
+ const y = Math.round(data.y);
92
+ Object.assign(floating.style, {
93
+ position: data.strategy,
94
+ top: "0",
95
+ left: "0",
96
+ transform: `translate3d(${x}px, ${y}px, 0)`
97
+ });
98
+ options.onComplete?.({ ...data, compute });
99
+ });
100
+ }
101
+ compute();
102
+ return callAll(
103
+ options.listeners ? autoUpdate(reference, floating, compute, options.listeners) : void 0,
104
+ options.onCleanup
105
+ );
106
+ }
107
+ function getBasePlacement(placement) {
108
+ return placement.split("-")[0];
109
+ }
110
+
111
+ export {
112
+ getPlacement,
113
+ getBasePlacement
114
+ };
@@ -0,0 +1,57 @@
1
+ // src/middleware.ts
2
+ var toVar = (value) => ({ variable: value, reference: `var(${value})` });
3
+ var cssVars = {
4
+ arrowSize: toVar("--arrow-size"),
5
+ arrowSizeHalf: toVar("--arrow-size-half"),
6
+ arrowBg: toVar("--arrow-background"),
7
+ transformOrigin: toVar("--transform-origin"),
8
+ arrowOffset: toVar("--arrow-offset")
9
+ };
10
+ var getTransformOrigin = (arrow) => ({
11
+ top: "bottom center",
12
+ "top-start": arrow ? `${arrow.x}px bottom` : "left bottom",
13
+ "top-end": arrow ? `${arrow.x}px bottom` : "right bottom",
14
+ bottom: "top center",
15
+ "bottom-start": arrow ? `${arrow.x}px top` : "top left",
16
+ "bottom-end": arrow ? `${arrow.x}px top` : "top right",
17
+ left: "right center",
18
+ "left-start": arrow ? `right ${arrow.y}px` : "right top",
19
+ "left-end": arrow ? `right ${arrow.y}px` : "right bottom",
20
+ right: "left center",
21
+ "right-start": arrow ? `left ${arrow.y}px` : "left top",
22
+ "right-end": arrow ? `left ${arrow.y}px` : "left bottom"
23
+ });
24
+ var transformOrigin = {
25
+ name: "transformOrigin",
26
+ fn({ placement, elements, middlewareData }) {
27
+ const { arrow } = middlewareData;
28
+ const transformOrigin2 = getTransformOrigin(arrow)[placement];
29
+ const { floating } = elements;
30
+ floating.style.setProperty(cssVars.transformOrigin.variable, transformOrigin2);
31
+ return {
32
+ data: { transformOrigin: transformOrigin2 }
33
+ };
34
+ }
35
+ };
36
+ var shiftArrow = (opts) => ({
37
+ name: "shiftArrow",
38
+ fn({ placement, middlewareData }) {
39
+ const { element: arrow } = opts;
40
+ if (middlewareData.arrow) {
41
+ const { x, y } = middlewareData.arrow;
42
+ const dir = placement.split("-")[0];
43
+ Object.assign(arrow.style, {
44
+ left: x != null ? `${x}px` : "",
45
+ top: y != null ? `${y}px` : "",
46
+ [dir]: `calc(100% + ${cssVars.arrowOffset.reference})`
47
+ });
48
+ }
49
+ return {};
50
+ }
51
+ });
52
+
53
+ export {
54
+ cssVars,
55
+ transformOrigin,
56
+ shiftArrow
57
+ };
@@ -0,0 +1,8 @@
1
+ import { VirtualElement, Placement } from '@floating-ui/dom';
2
+ import { PositioningOptions, BasePlacement } from './types.js';
3
+ import './auto-update.js';
4
+
5
+ declare function getPlacement(reference: HTMLElement | VirtualElement | null, floating: HTMLElement | null, opts?: PositioningOptions): (() => void) | undefined;
6
+ declare function getBasePlacement(placement: Placement): BasePlacement;
7
+
8
+ export { getBasePlacement, getPlacement };