@fiscozen/composables 0.1.23 → 0.1.24

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.
@@ -1,11 +1,12 @@
1
- import { ref as v, reactive as W, onUnmounted as I, nextTick as q, onMounted as R, watch as k, onBeforeUnmount as $, getCurrentInstance as T, computed as C, defineComponent as U, useSlots as K, toRef as Q, openBlock as x, createElementBlock as z, renderSlot as w, createElementVNode as S, unref as E, withDirectives as M, normalizeClass as P, vShow as A, createCommentVNode as D, createBlock as X, Teleport as Y } from "vue";
2
- const L = (n, t, i, s) => {
3
- let r = "right", e = "right-start";
4
- const u = n.getBoundingClientRect(), d = t.getBoundingClientRect(), o = i.getBoundingClientRect(), c = (o.left - u.left - d.width) / u.width, l = (u.right - o.right - d.width) / u.width, a = (o.top - u.top - d.height) / u.height, h = (u.bottom - o.bottom - d.height) / u.height;
5
- if (r = [
1
+ import { ref as d, reactive as H, onUnmounted as q, nextTick as T, onMounted as B, watch as R, onBeforeUnmount as C, getCurrentInstance as U, computed as L, defineComponent as G, useSlots as K, toRef as Q, openBlock as $, createElementBlock as S, renderSlot as x, createElementVNode as M, unref as F, withDirectives as A, normalizeClass as D, vShow as W, createCommentVNode as I, createBlock as X, Teleport as Y } from "vue";
2
+ const E = new DOMRect(0, 0, window.innerWidth, window.innerHeight), w = (o, e, i, u, s) => {
3
+ let n = s ? "bottom" : "right", h = s ? "bottom-start" : "right-start";
4
+ E.width = window.innerWidth, E.height = window.innerHeight, E.x = 0, E.y = 0;
5
+ const l = o ? o.getBoundingClientRect() : E, t = e.getBoundingClientRect(), v = i.getBoundingClientRect(), r = (v.left - l.left - t.width) / l.width, c = (l.right - v.right - t.width) / l.width, a = (v.top - l.top - t.height) / l.height, y = (l.bottom - v.bottom - t.height) / l.height;
6
+ let m = [
6
7
  {
7
8
  key: "right",
8
- space: l
9
+ space: c
9
10
  },
10
11
  {
11
12
  key: "top",
@@ -13,256 +14,314 @@ const L = (n, t, i, s) => {
13
14
  },
14
15
  {
15
16
  key: "bottom",
16
- space: h
17
+ space: y
17
18
  },
18
19
  {
19
20
  key: "left",
20
- space: c
21
+ space: r
21
22
  }
22
- ].sort((p, f) => f.space - p.space)[0].key, !s)
23
- switch (r) {
23
+ ].sort((p, f) => f.space - p.space);
24
+ if (s && (m = m.filter((p) => p.key === "top" || p.key === "bottom")), n = m[0].key, !u)
25
+ switch (n) {
24
26
  case "right":
25
27
  case "left":
26
- const p = (d.height - o.height) / 2;
27
- u.top > o.top - p && (s = "end"), u.bottom < o.top - p && (s = "start");
28
+ const p = (t.height - v.height) / 2;
29
+ l.top > v.top - p && (u = "end"), l.bottom < v.top - p && (u = "start");
28
30
  break;
29
31
  }
30
- return e = s ? `${r}-${s}` : r, e;
31
- }, G = (n, t, i, s) => ({
32
- x: t.x + n.width * i / 100,
33
- y: t.y + n.height * s / 100
34
- }), H = (n) => {
35
- const t = v(null), i = v(null), s = v(null), r = v(null), e = W({
32
+ return h = u ? `${n}-${u}` : n, h;
33
+ }, J = (o, e, i, u) => ({
34
+ x: e.x + o.width * i / 100,
35
+ y: e.y + o.height * u / 100
36
+ }), Z = (o) => {
37
+ const e = d(null), i = d(null), u = d(null), s = d(), n = d(), h = d("auto"), l = d(), t = H({
36
38
  position: { x: 0, y: 0 }
37
- }), u = {
39
+ }), v = {
38
40
  root: null,
39
41
  rootMargin: "0px",
40
42
  threshold: 1,
41
- ...n.element.intersectionOptions
42
- }, d = (l, a) => {
43
- }, o = v(new IntersectionObserver(d, u)), c = () => q(() => {
44
- var F;
45
- if (t.value = typeof n.element.domRef.value == "string" ? document.querySelector(n.element.domRef.value) : n.element.domRef.value, n.container ? (i.value = typeof n.container.domRef.value == "string" ? document.querySelector(n.container.domRef.value) : n.container.domRef.value, i.value ?? (i.value = document.body)) : i.value = document.body, !t.value)
43
+ ...o.element.intersectionOptions
44
+ }, r = (m, p) => {
45
+ }, c = d(new IntersectionObserver(r, v)), a = d(), y = () => T(() => {
46
+ var N;
47
+ if (a.value = o.position ? o.position.value : "auto", e.value = typeof o.element.domRef.value == "string" ? document.querySelector(o.element.domRef.value) : o.element.domRef.value, o.container ? (i.value = typeof o.container.domRef.value == "string" ? document.querySelector(o.container.domRef.value) : o.container.domRef.value, i.value ?? (i.value = document.body)) : i.value = document.body, !e.value)
46
48
  throw new Error("missing reference element for floating behavior");
47
- n.opener && (s.value = typeof n.opener.domRef.value == "string" ? document.querySelector(n.opener.domRef.value) : n.opener.domRef.value), r.value = t.value.getBoundingClientRect();
48
- const l = (F = s.value) == null ? void 0 : F.getBoundingClientRect(), a = i.value.getBoundingClientRect(), h = window.getComputedStyle(t.value);
49
- o.value.observe(t.value), o.value.observe(i.value);
50
- let m = n.position ? n.position.value : "auto", p = 0, f = 0;
51
- if (n.opener && s.value && l) {
52
- const y = l.left - parseFloat(h.marginLeft) - parseFloat(h.marginRight), N = l.left - parseFloat(h.marginLeft), b = l.top - parseFloat(h.marginTop) - parseFloat(h.marginBottom), O = l.top - parseFloat(h.marginTop);
53
- switch (m) {
49
+ o.opener && (u.value = typeof o.opener.domRef.value == "string" ? document.querySelector(o.opener.domRef.value) : o.opener.domRef.value), l.value = e.value.getBoundingClientRect(), s.value = (N = u.value) == null ? void 0 : N.getBoundingClientRect(), n.value = i.value.getBoundingClientRect();
50
+ const m = window.getComputedStyle(e.value);
51
+ c.value.observe(e.value), c.value.observe(i.value);
52
+ let p = 0, f = 0;
53
+ if (o.opener && u.value && s && s.value) {
54
+ const g = s.value.left - parseFloat(m.marginLeft) - parseFloat(m.marginRight), z = s.value.left - parseFloat(m.marginLeft), k = s.value.top - parseFloat(m.marginTop) - parseFloat(m.marginBottom), P = s.value.top - parseFloat(m.marginTop);
55
+ switch (a.value) {
54
56
  case "auto":
55
- m = L(
56
- i.value,
57
- t.value,
58
- s.value
57
+ a.value = w(
58
+ o.useViewport ? null : i.value,
59
+ e.value,
60
+ u.value
61
+ );
62
+ break;
63
+ case "auto-vertical":
64
+ a.value = w(
65
+ o.useViewport ? null : i.value,
66
+ e.value,
67
+ u.value,
68
+ void 0,
69
+ !0
59
70
  );
60
71
  break;
61
72
  case "auto-start":
62
- m = L(
63
- i.value,
64
- t.value,
65
- s.value,
73
+ a.value = w(
74
+ o.useViewport ? null : i.value,
75
+ e.value,
76
+ u.value,
66
77
  "start"
67
78
  );
68
79
  break;
80
+ case "auto-vertical-start":
81
+ a.value = w(
82
+ o.useViewport ? null : i.value,
83
+ e.value,
84
+ u.value,
85
+ "start",
86
+ !0
87
+ );
88
+ break;
69
89
  case "auto-end":
70
- m = L(
71
- i.value,
72
- t.value,
73
- s.value,
90
+ a.value = w(
91
+ o.useViewport ? null : i.value,
92
+ e.value,
93
+ u.value,
74
94
  "end"
75
95
  );
76
96
  break;
97
+ case "auto-vertical-end":
98
+ a.value = w(
99
+ o.useViewport ? null : i.value,
100
+ e.value,
101
+ u.value,
102
+ "end",
103
+ !0
104
+ );
105
+ break;
77
106
  }
78
- switch (m) {
107
+ switch (a.value) {
79
108
  case "bottom":
80
- e.position.y = l.bottom, e.position.x = N + l.width / 2, f = -50, p = 0;
109
+ t.position.y = s.value.bottom, t.position.x = z + s.value.width / 2, f = -50, p = 0;
81
110
  break;
82
111
  case "bottom-start":
83
- e.position.y = l.bottom, e.position.x = y, f = 0, p = 0;
112
+ t.position.y = s.value.bottom, t.position.x = g, f = 0, p = 0;
84
113
  break;
85
114
  case "bottom-end":
86
- e.position.y = l.bottom, e.position.x = l.right, f = -100, p = 0;
115
+ t.position.y = s.value.bottom, t.position.x = s.value.right, f = -100, p = 0;
87
116
  break;
88
117
  case "left-start":
89
- e.position.y = b, e.position.x = y, f = -100, p = 0;
118
+ t.position.y = k, t.position.x = g, f = -100, p = 0;
90
119
  break;
91
120
  case "left":
92
- e.position.y = O + l.height / 2, e.position.x = y, p = -50, f = -100;
121
+ t.position.y = P + s.value.height / 2, t.position.x = g, p = -50, f = -100;
93
122
  break;
94
123
  case "left-end":
95
- e.position.y = l.bottom, e.position.x = y, p = -100, f = -100;
124
+ t.position.y = s.value.bottom, t.position.x = g, p = -100, f = -100;
96
125
  break;
97
126
  case "top-start":
98
- e.position.y = b, e.position.x = y, p = -100, f = 0;
127
+ t.position.y = k, t.position.x = g, p = -100, f = 0;
99
128
  break;
100
129
  case "top":
101
- e.position.y = b, e.position.x = N + l.width / 2, f = -50, p = -100;
130
+ t.position.y = k, t.position.x = z + s.value.width / 2, f = -50, p = -100;
102
131
  break;
103
132
  case "top-end":
104
- e.position.y = b, e.position.x = l.right, f = -100, p = -100;
133
+ t.position.y = k, t.position.x = s.value.right, f = -100, p = -100;
105
134
  break;
106
135
  case "right-start":
107
- e.position.y = b, e.position.x = l.right, f = 0, p = 0;
136
+ t.position.y = k, t.position.x = s.value.right, f = 0, p = 0;
108
137
  break;
109
138
  case "right":
110
- e.position.y = O + l.height / 2, e.position.x = l.right, p = -50, f = 0;
139
+ t.position.y = P + s.value.height / 2, t.position.x = s.value.right, p = -50, f = 0;
111
140
  break;
112
141
  case "right-end":
113
- e.position.y = l.bottom, e.position.x = l.right, p = -100, f = 0;
142
+ t.position.y = s.value.bottom, t.position.x = s.value.right, p = -100, f = 0;
114
143
  break;
115
144
  }
116
145
  } else
117
- switch (m) {
146
+ switch (a.value) {
118
147
  case "bottom":
119
- e.position.y = a.bottom - r.value.height, e.position.x = a.left + a.width / 2, f = -50;
148
+ t.position.y = n.value.bottom - l.value.height, t.position.x = n.value.left + n.value.width / 2, f = -50;
120
149
  break;
121
150
  case "left-end":
122
151
  case "bottom-start":
123
- e.position.y = a.bottom - r.value.height, e.position.x = a.left;
152
+ t.position.y = n.value.bottom - l.value.height, t.position.x = n.value.left;
124
153
  break;
125
154
  case "right-end":
126
155
  case "bottom-end":
127
- e.position.y = a.bottom - r.value.height, e.position.x = a.right - r.value.width;
156
+ t.position.y = n.value.bottom - l.value.height, t.position.x = n.value.right - l.value.width;
128
157
  break;
129
158
  case "left":
130
- e.position.y = a.top + (a.height - r.value.height) / 2, e.position.x = a.left;
159
+ t.position.y = n.value.top + (n.value.height - l.value.height) / 2, t.position.x = n.value.left;
131
160
  break;
132
161
  case "top-start":
133
162
  case "left-start":
134
- e.position.y = a.top, e.position.x = a.left;
163
+ t.position.y = n.value.top, t.position.x = n.value.left;
135
164
  break;
136
165
  case "top":
137
- e.position.y = a.top, e.position.x = a.left + (a.width - r.value.width) / 2;
166
+ t.position.y = n.value.top, t.position.x = n.value.left + (n.value.width - l.value.width) / 2;
138
167
  break;
139
168
  case "top-end":
140
169
  case "right-start":
141
- e.position.y = a.top, e.position.x = a.right - r.value.width;
170
+ t.position.y = n.value.top, t.position.x = n.value.right - l.value.width;
142
171
  case "right":
143
- e.position.y = a.top + (a.height - r.value.height) / 2, e.position.x = a.right - r.value.width;
172
+ t.position.y = n.value.top + (n.value.height - l.value.height) / 2, t.position.x = n.value.right - l.value.width;
144
173
  break;
145
174
  }
146
- const g = G(r.value, e.position, f, p);
147
- e.position.x = g.x, e.position.y = g.y, g.x < a.left && (e.position.x = a.left, f = 0), g.x + r.value.width > a.right && (e.position.x = a.right - r.value.width, f = 0), g.y < a.top && (e.position.y = a.top, p = 0), g.y + r.value.height > a.bottom && (e.position.y = a.bottom - r.value.height, p = 0), t.value.style.top = `${e.position.y}px`, t.value.style.left = `${e.position.x}px`, t.value.style.display = "flex", r.value = t.value.getBoundingClientRect();
175
+ const b = J(l.value, t.position, f, p);
176
+ if (t.position.x = b.x, t.position.y = b.y, b.x < n.value.left && (t.position.x = n.value.left, f = 0), b.x + l.value.width > n.value.right) {
177
+ const g = n.value.right - l.value.width;
178
+ g > 0 && (t.position.x = g), f = 0;
179
+ }
180
+ if (b.y < n.value.top && (t.position.y = n.value.top, p = 0), b.y + l.value.height > n.value.bottom) {
181
+ const g = n.value.bottom - l.value.height;
182
+ g > 0 && (t.position.y = g), p = 0;
183
+ }
184
+ e.value.style.top = `${t.position.y}px`, e.value.style.left = `${t.position.x}px`, e.value.style.position = "fixed", e.value.style.display = "flex", l.value = e.value.getBoundingClientRect(), o.callback && o.callback(l, s, n, h, a);
148
185
  });
149
- return I(() => {
150
- o.value.disconnect();
186
+ return q(() => {
187
+ c.value.disconnect();
151
188
  }), {
152
- float: e,
153
- rect: r,
154
- floatObserver: o,
155
- setPosition: c
189
+ float: t,
190
+ rect: l,
191
+ setPosition: y,
192
+ position: h,
193
+ actualPosition: a,
194
+ openerRect: s,
195
+ containerRect: n
156
196
  };
157
197
  };
158
- function B(n) {
159
- const t = window.matchMedia(n), i = v(t.matches);
160
- function s(r) {
161
- i.value = r.matches;
198
+ function O(o) {
199
+ const e = window.matchMedia(o), i = d(e.matches);
200
+ function u(s) {
201
+ i.value = s.matches;
162
202
  }
163
- return R(() => {
164
- t.addEventListener("change", s);
165
- }), I(() => {
166
- t.removeEventListener("change", s);
203
+ return B(() => {
204
+ e.addEventListener("change", u);
205
+ }), q(() => {
206
+ e.removeEventListener("change", u);
167
207
  }), i;
168
208
  }
169
- function Z(n) {
209
+ function j(o) {
170
210
  return {
171
- isGreater(t) {
172
- return B(`(min-width: ${n[t]})`);
211
+ isGreater(e) {
212
+ return O(`(min-width: ${o[e]})`);
173
213
  },
174
- isSmaller(t) {
175
- return B(`(max-width: ${n[t]})`);
214
+ isSmaller(e) {
215
+ return O(`(max-width: ${o[e]})`);
176
216
  },
177
- isInBetween(t, i) {
178
- return B(`(min-width: ${n[t]}) and (max-width: ${n[i]})`);
217
+ isInBetween(e, i) {
218
+ return O(`(min-width: ${o[e]}) and (max-width: ${o[i]})`);
179
219
  }
180
220
  };
181
221
  }
182
- function _(n, t, i) {
183
- if (!n)
222
+ function V(o, e, i) {
223
+ if (!o)
184
224
  throw new Error("A target component has to be provided.");
185
- if (!t)
225
+ if (!e)
186
226
  throw new Error("A callback has to be provided.");
187
- const s = (r) => {
188
- !n.value || r.target === n.value || r.composedPath().includes(n.value) || typeof t == "function" && t();
227
+ const u = (s) => {
228
+ !o.value || s.target === o.value || s.composedPath().includes(o.value) || typeof e == "function" && e();
189
229
  };
190
- i && k(i, (r, e) => {
191
- e && e.removeEventListener("click", s), r == null || r.addEventListener("click", s);
192
- }), R(() => {
193
- i || document.addEventListener("click", s);
194
- }), $(() => {
230
+ i && R(i, (s, n) => {
231
+ n && n.removeEventListener("click", u), s == null || s.addEventListener("click", u);
232
+ }), B(() => {
233
+ i || document.addEventListener("click", u);
234
+ }), C(() => {
195
235
  if (i) {
196
- i.value.removeEventListener("click", s);
236
+ i.value.removeEventListener("click", u);
197
237
  return;
198
238
  }
199
- document.removeEventListener("click", s);
239
+ document.removeEventListener("click", u);
200
240
  });
201
241
  }
202
- function j(n, t) {
203
- if (!n)
242
+ function ee(o, e) {
243
+ if (!o)
204
244
  throw new Error("A target component has to be provided.");
205
- if (!t || typeof t != "function")
245
+ if (!e || typeof e != "function")
206
246
  throw new Error("A callback has to be provided.");
207
- const i = (s) => {
208
- t(s);
247
+ const i = (u) => {
248
+ e(u);
209
249
  };
210
- R(() => {
211
- n.value.addEventListener("keydown", i);
212
- }), $(() => {
213
- n.value.removeEventListener("keydown", i);
250
+ B(() => {
251
+ o.value.addEventListener("keydown", i);
252
+ }), C(() => {
253
+ o.value.removeEventListener("keydown", i);
214
254
  });
215
255
  }
216
- function V(n, t) {
217
- if (!n || typeof n != "function")
256
+ function te(o, e) {
257
+ if (!o || typeof o != "function")
218
258
  throw new Error("A callback has to be provided.");
219
- const i = (s) => {
220
- n(s);
259
+ const i = (u) => {
260
+ o(u);
221
261
  };
222
- R(() => {
223
- if (!t) {
262
+ B(() => {
263
+ if (!e) {
224
264
  document.addEventListener("keyup", i);
225
265
  return;
226
266
  }
227
- t.value.addEventListener("keyup", i);
228
- }), $(() => {
229
- if (!t) {
267
+ e.value.addEventListener("keyup", i);
268
+ }), C(() => {
269
+ if (!e) {
230
270
  document.removeEventListener("keyup", i);
231
271
  return;
232
272
  }
233
- t.value.removeEventListener("keyup", i);
273
+ e.value.removeEventListener("keyup", i);
234
274
  });
235
275
  }
236
- const ee = () => {
237
- const n = v(null), t = T(), i = C(() => t == null ? void 0 : t.props.modelValue), s = (o) => o.toLocaleString("it-IT", {
276
+ const oe = () => {
277
+ const o = d(null), e = U(), i = L(() => e == null ? void 0 : e.props.amount), u = d(), s = (r) => r === null ? "" : r.toLocaleString("it-IT", {
238
278
  minimumFractionDigits: 2,
239
- maximumFractionDigits: 2
240
- }), r = (o) => parseFloat(o), e = (o) => {
241
- Number.isNaN(o) || t && t.emit("update:modelValue", o);
242
- }, u = (o) => (c) => {
243
- if (!n.value || !c.target)
279
+ maximumFractionDigits: 2,
280
+ useGrouping: !1
281
+ }), n = (r) => parseFloat(r.replace(/,/g, ".")), h = (r) => {
282
+ if (!Number.isNaN(r) && e) {
283
+ if (u.value = r, e.props.nullOnEmpty && !r) {
284
+ e.emit("update:amount", null);
285
+ return;
286
+ }
287
+ e.emit("update:amount", typeof i.value == "number" ? r : r.toString());
288
+ }
289
+ }, l = (r) => {
290
+ setTimeout(() => {
291
+ o.value && (o.value.value = r);
292
+ }, 0);
293
+ }, t = (r) => (c) => {
294
+ if (!o.value || !c.target)
244
295
  return;
245
- let { value: l } = o;
246
- l = l.replace(/[^0-9,.]/g, ""), n.value.value = l;
247
- const a = r(l);
248
- e(Number.isNaN(a) ? 0 : a);
249
- }, d = (o) => {
250
- if (!n.value || !o.target)
296
+ let { value: a } = r;
297
+ a = a.replace(/[^0-9,.]/g, ""), l(a);
298
+ const y = e != null && e.props.nullOnEmpty && a === "" ? null : n(a);
299
+ h(Number.isNaN(y) ? 0 : y);
300
+ }, v = (r) => {
301
+ if (!o.value || !r.target)
251
302
  return;
252
- let c = r(o.target.value.replace(/,/g, "."));
253
- Number.isNaN(c) && (c = 0);
254
- const l = s(c);
255
- n.value.value = l, e(c);
303
+ let c = r.target.value.replace(/,/g, "."), a;
304
+ c === "" && (e != null && e.props.nullOnEmpty) ? a = null : (a = n(c), Number.isNaN(a) && (a = 0));
305
+ const y = s(a);
306
+ l(y), h(n(y));
256
307
  };
257
- return k(n, (o, c) => {
258
- o && (c && (c == null || c.removeEventListener("input", u(o)), c == null || c.removeEventListener("blur", d)), o.addEventListener("input", u(o)), o.addEventListener("blur", d), i.value && (o.value = s(i.value)));
308
+ return R(o, (r, c) => {
309
+ r && (c && (c == null || c.removeEventListener("input", t(r)), c == null || c.removeEventListener("blur", v)), r.addEventListener("input", t(r)), r.addEventListener("blur", v), e != null && e.props.amount && (r.value = s(i.value)));
310
+ }), R(i, (r) => {
311
+ T(() => {
312
+ if (!(!o.value || !r) && u.value !== r) {
313
+ const c = s(r);
314
+ o.value.value = c, u.value = r;
315
+ }
316
+ });
259
317
  }), {
260
- inputRef: n,
261
- parse: r,
318
+ inputRef: o,
319
+ parse: n,
262
320
  format: s,
263
- setValue: e
321
+ emitAmount: h,
322
+ setValue: l
264
323
  };
265
- }, te = /* @__PURE__ */ U({
324
+ }, ne = /* @__PURE__ */ G({
266
325
  __name: "FzFloating",
267
326
  props: {
268
327
  isOpen: { type: Boolean, default: !1 },
@@ -271,85 +330,105 @@ const ee = () => {
271
330
  contentClass: {},
272
331
  openerClass: {},
273
332
  overrideContentClass: { type: Boolean },
274
- teleport: { type: Boolean, default: !1 }
333
+ teleport: { type: Boolean, default: !1 },
334
+ useViewport: { type: Boolean }
275
335
  },
276
- setup(n) {
277
- const t = n, i = v(null), s = v(null), r = K(), e = {
278
- position: C(() => t.position),
336
+ emits: ["fzfloating:setPosition"],
337
+ setup(o, { emit: e }) {
338
+ const i = o, u = e, s = d(null), n = d(null), h = K();
339
+ let l = !1;
340
+ const t = {
341
+ position: L(() => i.position),
279
342
  element: {
280
- domRef: s
343
+ // @ts-ignore
344
+ domRef: n
281
345
  },
282
346
  container: {
283
- domRef: Q(t.container || document.body)
347
+ // @ts-ignore
348
+ domRef: Q(i.container || document.body)
349
+ },
350
+ useViewport: L(() => i.useViewport),
351
+ callback(...a) {
352
+ u("fzfloating:setPosition", ...a);
284
353
  }
285
354
  };
286
- r.opener && (e.opener = {
287
- domRef: i
355
+ h.opener && (t.opener = {
356
+ domRef: s
288
357
  });
289
- const u = H(e);
290
- k(
291
- () => t.position,
292
- () => t.isOpen && u.setPosition()
293
- ), k(
294
- () => t.isOpen,
295
- (o) => {
296
- !o || !s.value || (s.value.style.top = "0px", s.value.style.left = "0px", s.value.style.transform = "none", u.setPosition());
358
+ const v = Z(t), r = () => {
359
+ l || (l = !0, requestAnimationFrame(() => {
360
+ i.isOpen && v.setPosition(), l = !1;
361
+ }));
362
+ };
363
+ R(
364
+ () => i.position,
365
+ () => r()
366
+ ), R(
367
+ () => i.isOpen,
368
+ (a) => {
369
+ if (!a || !n.value) {
370
+ window.removeEventListener("scroll", r);
371
+ return;
372
+ }
373
+ window.addEventListener("scroll", r), n.value.style.top = "0px", n.value.style.left = "0px", n.value.style.transform = "none", v.setPosition();
297
374
  }
298
- );
299
- const d = C(() => t.overrideContentClass ? t.contentClass : ["bg-core-white fixed p-4 z-10", t.contentClass]);
300
- return (o, c) => (x(), z("div", null, [
301
- w(o.$slots, "opener-start"),
302
- S("div", {
375
+ ), C(() => {
376
+ window.removeEventListener("scroll", r);
377
+ });
378
+ const c = L(() => i.overrideContentClass ? i.contentClass : ["bg-core-white fixed p-4 z-10", i.contentClass]);
379
+ return (a, y) => ($(), S("div", null, [
380
+ x(a.$slots, "opener-start"),
381
+ M("div", {
303
382
  ref_key: "opener",
304
- ref: i,
383
+ ref: s,
305
384
  class: "inline-flex"
306
385
  }, [
307
- w(o.$slots, "opener", {
308
- isOpen: o.isOpen,
309
- floating: E(u)
386
+ x(a.$slots, "opener", {
387
+ isOpen: a.isOpen,
388
+ floating: F(v)
310
389
  })
311
390
  ], 512),
312
- w(o.$slots, "opener-end"),
313
- o.teleport ? D("", !0) : M((x(), z("div", {
391
+ x(a.$slots, "opener-end"),
392
+ a.teleport ? I("", !0) : A(($(), S("div", {
314
393
  key: 0,
315
394
  ref_key: "content",
316
- ref: s,
317
- class: P(["fz__floating__content bg-core-white fixed p-4 z-10", d.value])
395
+ ref: n,
396
+ class: D(["fz__floating__content bg-core-white fixed z-10 p-4", c.value])
318
397
  }, [
319
- w(o.$slots, "default", {
320
- isOpen: o.isOpen,
321
- floating: E(u)
398
+ x(a.$slots, "default", {
399
+ isOpen: a.isOpen,
400
+ floating: F(v)
322
401
  })
323
402
  ], 2)), [
324
- [A, o.$slots.default && (!o.$slots.opener || o.$slots.opener && o.isOpen)]
403
+ [W, a.$slots.default && (!a.$slots.opener || a.$slots.opener && a.isOpen)]
325
404
  ]),
326
- o.teleport ? (x(), X(Y, {
405
+ a.teleport ? ($(), X(Y, {
327
406
  key: 1,
328
407
  to: "body"
329
408
  }, [
330
- M(S("div", {
409
+ A(M("div", {
331
410
  ref_key: "content",
332
- ref: s,
333
- class: P(["fz__floating__content bg-core-white absolute p-4 z-10", d.value])
411
+ ref: n,
412
+ class: D(["fz__floating__content bg-core-white fixed z-10 p-4", c.value])
334
413
  }, [
335
- w(o.$slots, "default", {
336
- isOpen: o.isOpen,
337
- floating: E(u)
414
+ x(a.$slots, "default", {
415
+ isOpen: a.isOpen,
416
+ floating: F(v)
338
417
  })
339
418
  ], 2), [
340
- [A, o.$slots.default && (!o.$slots.opener || o.$slots.opener && o.isOpen)]
419
+ [W, a.$slots.default && (!a.$slots.opener || a.$slots.opener && a.isOpen)]
341
420
  ])
342
- ])) : D("", !0)
421
+ ])) : I("", !0)
343
422
  ]));
344
423
  }
345
424
  });
346
425
  export {
347
- te as FzFloating,
348
- Z as useBreakpoints,
349
- _ as useClickOutside,
350
- ee as useCurrency,
351
- H as useFloating,
352
- j as useKeyDown,
353
- V as useKeyUp,
354
- B as useMediaQuery
426
+ ne as FzFloating,
427
+ j as useBreakpoints,
428
+ V as useClickOutside,
429
+ oe as useCurrency,
430
+ Z as useFloating,
431
+ ee as useKeyDown,
432
+ te as useKeyUp,
433
+ O as useMediaQuery
355
434
  };
@@ -1 +1 @@
1
- (function(h,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],t):(h=typeof globalThis<"u"?globalThis:h||self,t(h.FzComposables={},h.Vue))})(this,function(h,t){"use strict";const R=(i,o,s,r)=>{let a="right",e="right-start";const c=i.getBoundingClientRect(),m=o.getBoundingClientRect(),n=s.getBoundingClientRect(),f=(n.left-c.left-m.width)/c.width,p=(c.right-n.right-m.width)/c.width,l=(n.top-c.top-m.height)/c.height,g=(c.bottom-n.bottom-m.height)/c.height;if(a=[{key:"right",space:p},{key:"top",space:l},{key:"bottom",space:g},{key:"left",space:f}].sort((u,d)=>d.space-u.space)[0].key,!r)switch(a){case"right":case"left":const u=(m.height-n.height)/2;c.top>n.top-u&&(r="end"),c.bottom<n.top-u&&(r="start");break}return e=r?`${a}-${r}`:a,e},L=(i,o,s,r)=>({x:o.x+i.width*s/100,y:o.y+i.height*r/100}),E=i=>{const o=t.ref(null),s=t.ref(null),r=t.ref(null),a=t.ref(null),e=t.reactive({position:{x:0,y:0}}),c={root:null,rootMargin:"0px",threshold:1,...i.element.intersectionOptions},m=(p,l)=>{},n=t.ref(new IntersectionObserver(m,c)),f=()=>t.nextTick(()=>{var B;if(o.value=typeof i.element.domRef.value=="string"?document.querySelector(i.element.domRef.value):i.element.domRef.value,i.container?(s.value=typeof i.container.domRef.value=="string"?document.querySelector(i.container.domRef.value):i.container.domRef.value,s.value??(s.value=document.body)):s.value=document.body,!o.value)throw new Error("missing reference element for floating behavior");i.opener&&(r.value=typeof i.opener.domRef.value=="string"?document.querySelector(i.opener.domRef.value):i.opener.domRef.value),a.value=o.value.getBoundingClientRect();const p=(B=r.value)==null?void 0:B.getBoundingClientRect(),l=s.value.getBoundingClientRect(),g=window.getComputedStyle(o.value);n.value.observe(o.value),n.value.observe(s.value);let y=i.position?i.position.value:"auto",u=0,d=0;if(i.opener&&r.value&&p){const b=p.left-parseFloat(g.marginLeft)-parseFloat(g.marginRight),C=p.left-parseFloat(g.marginLeft),w=p.top-parseFloat(g.marginTop)-parseFloat(g.marginBottom),x=p.top-parseFloat(g.marginTop);switch(y){case"auto":y=R(s.value,o.value,r.value);break;case"auto-start":y=R(s.value,o.value,r.value,"start");break;case"auto-end":y=R(s.value,o.value,r.value,"end");break}switch(y){case"bottom":e.position.y=p.bottom,e.position.x=C+p.width/2,d=-50,u=0;break;case"bottom-start":e.position.y=p.bottom,e.position.x=b,d=0,u=0;break;case"bottom-end":e.position.y=p.bottom,e.position.x=p.right,d=-100,u=0;break;case"left-start":e.position.y=w,e.position.x=b,d=-100,u=0;break;case"left":e.position.y=x+p.height/2,e.position.x=b,u=-50,d=-100;break;case"left-end":e.position.y=p.bottom,e.position.x=b,u=-100,d=-100;break;case"top-start":e.position.y=w,e.position.x=b,u=-100,d=0;break;case"top":e.position.y=w,e.position.x=C+p.width/2,d=-50,u=-100;break;case"top-end":e.position.y=w,e.position.x=p.right,d=-100,u=-100;break;case"right-start":e.position.y=w,e.position.x=p.right,d=0,u=0;break;case"right":e.position.y=x+p.height/2,e.position.x=p.right,u=-50,d=0;break;case"right-end":e.position.y=p.bottom,e.position.x=p.right,u=-100,d=0;break}}else switch(y){case"bottom":e.position.y=l.bottom-a.value.height,e.position.x=l.left+l.width/2,d=-50;break;case"left-end":case"bottom-start":e.position.y=l.bottom-a.value.height,e.position.x=l.left;break;case"right-end":case"bottom-end":e.position.y=l.bottom-a.value.height,e.position.x=l.right-a.value.width;break;case"left":e.position.y=l.top+(l.height-a.value.height)/2,e.position.x=l.left;break;case"top-start":case"left-start":e.position.y=l.top,e.position.x=l.left;break;case"top":e.position.y=l.top,e.position.x=l.left+(l.width-a.value.width)/2;break;case"top-end":case"right-start":e.position.y=l.top,e.position.x=l.right-a.value.width;case"right":e.position.y=l.top+(l.height-a.value.height)/2,e.position.x=l.right-a.value.width;break}const v=L(a.value,e.position,d,u);e.position.x=v.x,e.position.y=v.y,v.x<l.left&&(e.position.x=l.left,d=0),v.x+a.value.width>l.right&&(e.position.x=l.right-a.value.width,d=0),v.y<l.top&&(e.position.y=l.top,u=0),v.y+a.value.height>l.bottom&&(e.position.y=l.bottom-a.value.height,u=0),o.value.style.top=`${e.position.y}px`,o.value.style.left=`${e.position.x}px`,o.value.style.display="flex",a.value=o.value.getBoundingClientRect()});return t.onUnmounted(()=>{n.value.disconnect()}),{float:e,rect:a,floatObserver:n,setPosition:f}};function k(i){const o=window.matchMedia(i),s=t.ref(o.matches);function r(a){s.value=a.matches}return t.onMounted(()=>{o.addEventListener("change",r)}),t.onUnmounted(()=>{o.removeEventListener("change",r)}),s}function $(i){return{isGreater(o){return k(`(min-width: ${i[o]})`)},isSmaller(o){return k(`(max-width: ${i[o]})`)},isInBetween(o,s){return k(`(min-width: ${i[o]}) and (max-width: ${i[s]})`)}}}function S(i,o,s){if(!i)throw new Error("A target component has to be provided.");if(!o)throw new Error("A callback has to be provided.");const r=a=>{!i.value||a.target===i.value||a.composedPath().includes(i.value)||typeof o=="function"&&o()};s&&t.watch(s,(a,e)=>{e&&e.removeEventListener("click",r),a==null||a.addEventListener("click",r)}),t.onMounted(()=>{s||document.addEventListener("click",r)}),t.onBeforeUnmount(()=>{if(s){s.value.removeEventListener("click",r);return}document.removeEventListener("click",r)})}function F(i,o){if(!i)throw new Error("A target component has to be provided.");if(!o||typeof o!="function")throw new Error("A callback has to be provided.");const s=r=>{o(r)};t.onMounted(()=>{i.value.addEventListener("keydown",s)}),t.onBeforeUnmount(()=>{i.value.removeEventListener("keydown",s)})}function N(i,o){if(!i||typeof i!="function")throw new Error("A callback has to be provided.");const s=r=>{i(r)};t.onMounted(()=>{if(!o){document.addEventListener("keyup",s);return}o.value.addEventListener("keyup",s)}),t.onBeforeUnmount(()=>{if(!o){document.removeEventListener("keyup",s);return}o.value.removeEventListener("keyup",s)})}const O=()=>{const i=t.ref(null),o=t.getCurrentInstance(),s=t.computed(()=>o==null?void 0:o.props.modelValue),r=n=>n.toLocaleString("it-IT",{minimumFractionDigits:2,maximumFractionDigits:2}),a=n=>parseFloat(n),e=n=>{Number.isNaN(n)||o&&o.emit("update:modelValue",n)},c=n=>f=>{if(!i.value||!f.target)return;let{value:p}=n;p=p.replace(/[^0-9,.]/g,""),i.value.value=p;const l=a(p);e(Number.isNaN(l)?0:l)},m=n=>{if(!i.value||!n.target)return;let f=a(n.target.value.replace(/,/g,"."));Number.isNaN(f)&&(f=0);const p=r(f);i.value.value=p,e(f)};return t.watch(i,(n,f)=>{n&&(f&&(f==null||f.removeEventListener("input",c(n)),f==null||f.removeEventListener("blur",m)),n.addEventListener("input",c(n)),n.addEventListener("blur",m),s.value&&(n.value=r(s.value)))}),{inputRef:i,parse:a,format:r,setValue:e}},z=t.defineComponent({__name:"FzFloating",props:{isOpen:{type:Boolean,default:!1},position:{default:"auto"},container:{},contentClass:{},openerClass:{},overrideContentClass:{type:Boolean},teleport:{type:Boolean,default:!1}},setup(i){const o=i,s=t.ref(null),r=t.ref(null),a=t.useSlots(),e={position:t.computed(()=>o.position),element:{domRef:r},container:{domRef:t.toRef(o.container||document.body)}};a.opener&&(e.opener={domRef:s});const c=E(e);t.watch(()=>o.position,()=>o.isOpen&&c.setPosition()),t.watch(()=>o.isOpen,n=>{!n||!r.value||(r.value.style.top="0px",r.value.style.left="0px",r.value.style.transform="none",c.setPosition())});const m=t.computed(()=>o.overrideContentClass?o.contentClass:["bg-core-white fixed p-4 z-10",o.contentClass]);return(n,f)=>(t.openBlock(),t.createElementBlock("div",null,[t.renderSlot(n.$slots,"opener-start"),t.createElementVNode("div",{ref_key:"opener",ref:s,class:"inline-flex"},[t.renderSlot(n.$slots,"opener",{isOpen:n.isOpen,floating:t.unref(c)})],512),t.renderSlot(n.$slots,"opener-end"),n.teleport?t.createCommentVNode("",!0):t.withDirectives((t.openBlock(),t.createElementBlock("div",{key:0,ref_key:"content",ref:r,class:t.normalizeClass(["fz__floating__content bg-core-white fixed p-4 z-10",m.value])},[t.renderSlot(n.$slots,"default",{isOpen:n.isOpen,floating:t.unref(c)})],2)),[[t.vShow,n.$slots.default&&(!n.$slots.opener||n.$slots.opener&&n.isOpen)]]),n.teleport?(t.openBlock(),t.createBlock(t.Teleport,{key:1,to:"body"},[t.withDirectives(t.createElementVNode("div",{ref_key:"content",ref:r,class:t.normalizeClass(["fz__floating__content bg-core-white absolute p-4 z-10",m.value])},[t.renderSlot(n.$slots,"default",{isOpen:n.isOpen,floating:t.unref(c)})],2),[[t.vShow,n.$slots.default&&(!n.$slots.opener||n.$slots.opener&&n.isOpen)]])])):t.createCommentVNode("",!0)]))}});h.FzFloating=z,h.useBreakpoints=$,h.useClickOutside=S,h.useCurrency=O,h.useFloating=E,h.useKeyDown=F,h.useKeyUp=N,h.useMediaQuery=k,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
1
+ (function(h,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],e):(h=typeof globalThis<"u"?globalThis:h||self,e(h.FzComposables={},h.Vue))})(this,function(h,e){"use strict";const E=new DOMRect(0,0,window.innerWidth,window.innerHeight),w=(n,t,a,c,s)=>{let i=s?"bottom":"right",m=s?"bottom-start":"right-start";E.width=window.innerWidth,E.height=window.innerHeight,E.x=0,E.y=0;const u=n?n.getBoundingClientRect():E,o=t.getBoundingClientRect(),d=a.getBoundingClientRect(),l=(d.left-u.left-o.width)/u.width,f=(u.right-d.right-o.width)/u.width,r=(d.top-u.top-o.height)/u.height,b=(u.bottom-d.bottom-o.height)/u.height;let g=[{key:"right",space:f},{key:"top",space:r},{key:"bottom",space:b},{key:"left",space:l}].sort((p,v)=>v.space-p.space);if(s&&(g=g.filter(p=>p.key==="top"||p.key==="bottom")),i=g[0].key,!c)switch(i){case"right":case"left":const p=(o.height-d.height)/2;u.top>d.top-p&&(c="end"),u.bottom<d.top-p&&(c="start");break}return m=c?`${i}-${c}`:i,m},O=(n,t,a,c)=>({x:t.x+n.width*a/100,y:t.y+n.height*c/100}),B=n=>{const t=e.ref(null),a=e.ref(null),c=e.ref(null),s=e.ref(),i=e.ref(),m=e.ref("auto"),u=e.ref(),o=e.reactive({position:{x:0,y:0}}),d={root:null,rootMargin:"0px",threshold:1,...n.element.intersectionOptions},l=(g,p)=>{},f=e.ref(new IntersectionObserver(l,d)),r=e.ref(),b=()=>e.nextTick(()=>{var C;if(r.value=n.position?n.position.value:"auto",t.value=typeof n.element.domRef.value=="string"?document.querySelector(n.element.domRef.value):n.element.domRef.value,n.container?(a.value=typeof n.container.domRef.value=="string"?document.querySelector(n.container.domRef.value):n.container.domRef.value,a.value??(a.value=document.body)):a.value=document.body,!t.value)throw new Error("missing reference element for floating behavior");n.opener&&(c.value=typeof n.opener.domRef.value=="string"?document.querySelector(n.opener.domRef.value):n.opener.domRef.value),u.value=t.value.getBoundingClientRect(),s.value=(C=c.value)==null?void 0:C.getBoundingClientRect(),i.value=a.value.getBoundingClientRect();const g=window.getComputedStyle(t.value);f.value.observe(t.value),f.value.observe(a.value);let p=0,v=0;if(n.opener&&c.value&&s&&s.value){const y=s.value.left-parseFloat(g.marginLeft)-parseFloat(g.marginRight),L=s.value.left-parseFloat(g.marginLeft),R=s.value.top-parseFloat(g.marginTop)-parseFloat(g.marginBottom),F=s.value.top-parseFloat(g.marginTop);switch(r.value){case"auto":r.value=w(n.useViewport?null:a.value,t.value,c.value);break;case"auto-vertical":r.value=w(n.useViewport?null:a.value,t.value,c.value,void 0,!0);break;case"auto-start":r.value=w(n.useViewport?null:a.value,t.value,c.value,"start");break;case"auto-vertical-start":r.value=w(n.useViewport?null:a.value,t.value,c.value,"start",!0);break;case"auto-end":r.value=w(n.useViewport?null:a.value,t.value,c.value,"end");break;case"auto-vertical-end":r.value=w(n.useViewport?null:a.value,t.value,c.value,"end",!0);break}switch(r.value){case"bottom":o.position.y=s.value.bottom,o.position.x=L+s.value.width/2,v=-50,p=0;break;case"bottom-start":o.position.y=s.value.bottom,o.position.x=y,v=0,p=0;break;case"bottom-end":o.position.y=s.value.bottom,o.position.x=s.value.right,v=-100,p=0;break;case"left-start":o.position.y=R,o.position.x=y,v=-100,p=0;break;case"left":o.position.y=F+s.value.height/2,o.position.x=y,p=-50,v=-100;break;case"left-end":o.position.y=s.value.bottom,o.position.x=y,p=-100,v=-100;break;case"top-start":o.position.y=R,o.position.x=y,p=-100,v=0;break;case"top":o.position.y=R,o.position.x=L+s.value.width/2,v=-50,p=-100;break;case"top-end":o.position.y=R,o.position.x=s.value.right,v=-100,p=-100;break;case"right-start":o.position.y=R,o.position.x=s.value.right,v=0,p=0;break;case"right":o.position.y=F+s.value.height/2,o.position.x=s.value.right,p=-50,v=0;break;case"right-end":o.position.y=s.value.bottom,o.position.x=s.value.right,p=-100,v=0;break}}else switch(r.value){case"bottom":o.position.y=i.value.bottom-u.value.height,o.position.x=i.value.left+i.value.width/2,v=-50;break;case"left-end":case"bottom-start":o.position.y=i.value.bottom-u.value.height,o.position.x=i.value.left;break;case"right-end":case"bottom-end":o.position.y=i.value.bottom-u.value.height,o.position.x=i.value.right-u.value.width;break;case"left":o.position.y=i.value.top+(i.value.height-u.value.height)/2,o.position.x=i.value.left;break;case"top-start":case"left-start":o.position.y=i.value.top,o.position.x=i.value.left;break;case"top":o.position.y=i.value.top,o.position.x=i.value.left+(i.value.width-u.value.width)/2;break;case"top-end":case"right-start":o.position.y=i.value.top,o.position.x=i.value.right-u.value.width;case"right":o.position.y=i.value.top+(i.value.height-u.value.height)/2,o.position.x=i.value.right-u.value.width;break}const k=O(u.value,o.position,v,p);if(o.position.x=k.x,o.position.y=k.y,k.x<i.value.left&&(o.position.x=i.value.left,v=0),k.x+u.value.width>i.value.right){const y=i.value.right-u.value.width;y>0&&(o.position.x=y),v=0}if(k.y<i.value.top&&(o.position.y=i.value.top,p=0),k.y+u.value.height>i.value.bottom){const y=i.value.bottom-u.value.height;y>0&&(o.position.y=y),p=0}t.value.style.top=`${o.position.y}px`,t.value.style.left=`${o.position.x}px`,t.value.style.position="fixed",t.value.style.display="flex",u.value=t.value.getBoundingClientRect(),n.callback&&n.callback(u,s,i,m,r)});return e.onUnmounted(()=>{f.value.disconnect()}),{float:o,rect:u,setPosition:b,position:m,actualPosition:r,openerRect:s,containerRect:i}};function x(n){const t=window.matchMedia(n),a=e.ref(t.matches);function c(s){a.value=s.matches}return e.onMounted(()=>{t.addEventListener("change",c)}),e.onUnmounted(()=>{t.removeEventListener("change",c)}),a}function S(n){return{isGreater(t){return x(`(min-width: ${n[t]})`)},isSmaller(t){return x(`(max-width: ${n[t]})`)},isInBetween(t,a){return x(`(min-width: ${n[t]}) and (max-width: ${n[a]})`)}}}function $(n,t,a){if(!n)throw new Error("A target component has to be provided.");if(!t)throw new Error("A callback has to be provided.");const c=s=>{!n.value||s.target===n.value||s.composedPath().includes(n.value)||typeof t=="function"&&t()};a&&e.watch(a,(s,i)=>{i&&i.removeEventListener("click",c),s==null||s.addEventListener("click",c)}),e.onMounted(()=>{a||document.addEventListener("click",c)}),e.onBeforeUnmount(()=>{if(a){a.value.removeEventListener("click",c);return}document.removeEventListener("click",c)})}function N(n,t){if(!n)throw new Error("A target component has to be provided.");if(!t||typeof t!="function")throw new Error("A callback has to be provided.");const a=c=>{t(c)};e.onMounted(()=>{n.value.addEventListener("keydown",a)}),e.onBeforeUnmount(()=>{n.value.removeEventListener("keydown",a)})}function z(n,t){if(!n||typeof n!="function")throw new Error("A callback has to be provided.");const a=c=>{n(c)};e.onMounted(()=>{if(!t){document.addEventListener("keyup",a);return}t.value.addEventListener("keyup",a)}),e.onBeforeUnmount(()=>{if(!t){document.removeEventListener("keyup",a);return}t.value.removeEventListener("keyup",a)})}const M=()=>{const n=e.ref(null),t=e.getCurrentInstance(),a=e.computed(()=>t==null?void 0:t.props.amount),c=e.ref(),s=l=>l===null?"":l.toLocaleString("it-IT",{minimumFractionDigits:2,maximumFractionDigits:2,useGrouping:!1}),i=l=>parseFloat(l.replace(/,/g,".")),m=l=>{if(!Number.isNaN(l)&&t){if(c.value=l,t.props.nullOnEmpty&&!l){t.emit("update:amount",null);return}t.emit("update:amount",typeof a.value=="number"?l:l.toString())}},u=l=>{setTimeout(()=>{n.value&&(n.value.value=l)},0)},o=l=>f=>{if(!n.value||!f.target)return;let{value:r}=l;r=r.replace(/[^0-9,.]/g,""),u(r);const b=t!=null&&t.props.nullOnEmpty&&r===""?null:i(r);m(Number.isNaN(b)?0:b)},d=l=>{if(!n.value||!l.target)return;let f=l.target.value.replace(/,/g,"."),r;f===""&&(t!=null&&t.props.nullOnEmpty)?r=null:(r=i(f),Number.isNaN(r)&&(r=0));const b=s(r);u(b),m(i(b))};return e.watch(n,(l,f)=>{l&&(f&&(f==null||f.removeEventListener("input",o(l)),f==null||f.removeEventListener("blur",d)),l.addEventListener("input",o(l)),l.addEventListener("blur",d),t!=null&&t.props.amount&&(l.value=s(a.value)))}),e.watch(a,l=>{e.nextTick(()=>{if(!(!n.value||!l)&&c.value!==l){const f=s(l);n.value.value=f,c.value=l}})}),{inputRef:n,parse:i,format:s,emitAmount:m,setValue:u}},P=e.defineComponent({__name:"FzFloating",props:{isOpen:{type:Boolean,default:!1},position:{default:"auto"},container:{},contentClass:{},openerClass:{},overrideContentClass:{type:Boolean},teleport:{type:Boolean,default:!1},useViewport:{type:Boolean}},emits:["fzfloating:setPosition"],setup(n,{emit:t}){const a=n,c=t,s=e.ref(null),i=e.ref(null),m=e.useSlots();let u=!1;const o={position:e.computed(()=>a.position),element:{domRef:i},container:{domRef:e.toRef(a.container||document.body)},useViewport:e.computed(()=>a.useViewport),callback(...r){c("fzfloating:setPosition",...r)}};m.opener&&(o.opener={domRef:s});const d=B(o),l=()=>{u||(u=!0,requestAnimationFrame(()=>{a.isOpen&&d.setPosition(),u=!1}))};e.watch(()=>a.position,()=>l()),e.watch(()=>a.isOpen,r=>{if(!r||!i.value){window.removeEventListener("scroll",l);return}window.addEventListener("scroll",l),i.value.style.top="0px",i.value.style.left="0px",i.value.style.transform="none",d.setPosition()}),e.onBeforeUnmount(()=>{window.removeEventListener("scroll",l)});const f=e.computed(()=>a.overrideContentClass?a.contentClass:["bg-core-white fixed p-4 z-10",a.contentClass]);return(r,b)=>(e.openBlock(),e.createElementBlock("div",null,[e.renderSlot(r.$slots,"opener-start"),e.createElementVNode("div",{ref_key:"opener",ref:s,class:"inline-flex"},[e.renderSlot(r.$slots,"opener",{isOpen:r.isOpen,floating:e.unref(d)})],512),e.renderSlot(r.$slots,"opener-end"),r.teleport?e.createCommentVNode("",!0):e.withDirectives((e.openBlock(),e.createElementBlock("div",{key:0,ref_key:"content",ref:i,class:e.normalizeClass(["fz__floating__content bg-core-white fixed z-10 p-4",f.value])},[e.renderSlot(r.$slots,"default",{isOpen:r.isOpen,floating:e.unref(d)})],2)),[[e.vShow,r.$slots.default&&(!r.$slots.opener||r.$slots.opener&&r.isOpen)]]),r.teleport?(e.openBlock(),e.createBlock(e.Teleport,{key:1,to:"body"},[e.withDirectives(e.createElementVNode("div",{ref_key:"content",ref:i,class:e.normalizeClass(["fz__floating__content bg-core-white fixed z-10 p-4",f.value])},[e.renderSlot(r.$slots,"default",{isOpen:r.isOpen,floating:e.unref(d)})],2),[[e.vShow,r.$slots.default&&(!r.$slots.opener||r.$slots.opener&&r.isOpen)]])])):e.createCommentVNode("",!0)]))}});h.FzFloating=P,h.useBreakpoints=S,h.useClickOutside=$,h.useCurrency=M,h.useFloating=B,h.useKeyDown=N,h.useKeyUp=z,h.useMediaQuery=x,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fiscozen/composables",
3
- "version": "0.1.23",
3
+ "version": "0.1.24",
4
4
  "description": "Design System utility composables",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -25,9 +25,9 @@
25
25
  "vite": "^5.0.10",
26
26
  "vitest": "^1.2.0",
27
27
  "vue-tsc": "^1.8.25",
28
- "@fiscozen/tsconfig": "^0.1.0",
29
28
  "@fiscozen/eslint-config": "^0.1.0",
30
- "@fiscozen/prettier-config": "^0.1.0"
29
+ "@fiscozen/prettier-config": "^0.1.0",
30
+ "@fiscozen/tsconfig": "^0.1.0"
31
31
  },
32
32
  "license": "ISC",
33
33
  "scripts": {
@@ -1,34 +1,50 @@
1
- import { Ref, watch, getCurrentInstance, computed, ref, onMounted } from 'vue'
1
+ import { Ref, watch, getCurrentInstance, computed, ref, nextTick } from 'vue'
2
2
 
3
3
  export const useCurrency = () => {
4
4
  const inputRef: Ref<HTMLInputElement | null> = ref(null)
5
5
  const vm = getCurrentInstance()
6
6
 
7
- const modelValue = computed<number | undefined>(
8
- () => vm?.props.modelValue as unknown as number | undefined
9
- )
7
+ const computedModel = computed<number | null>(() => vm?.props.amount as unknown as number | null)
8
+ const internalVal = ref<number | null>()
10
9
 
11
10
  const format = (input: number | null) => {
12
11
  if (input === null) {
13
- return '';
12
+ return ''
14
13
  }
15
14
  return input.toLocaleString('it-IT', {
16
15
  minimumFractionDigits: 2,
17
- maximumFractionDigits: 2
16
+ maximumFractionDigits: 2,
17
+ useGrouping: false
18
18
  })
19
19
  }
20
20
 
21
21
  const parse = (text: string) => {
22
22
  // strip currency, handle edge cases...
23
- return parseFloat(text)
23
+ return parseFloat(text.replace(/,/g, '.'))
24
24
  }
25
25
 
26
- const setValue = (val: number | null) => {
26
+ const emitAmount = (val: number | null) => {
27
27
  if (Number.isNaN(val)) {
28
28
  return
29
29
  }
30
+ if (vm) {
31
+ internalVal.value = val
32
+ if (vm.props.nullOnEmpty && !val) {
33
+ vm.emit('update:amount', null)
34
+ return
35
+ }
36
+ vm.emit('update:amount', typeof computedModel.value === 'number' ? val : val.toString())
37
+ }
38
+ }
30
39
 
31
- vm && vm.emit('update:modelValue', val)
40
+ const setValue = (val: string) => {
41
+ // nextTick doesn't seem to work
42
+ setTimeout(() => {
43
+ if (!inputRef.value) {
44
+ return
45
+ }
46
+ inputRef.value.value = val
47
+ }, 0)
32
48
  }
33
49
 
34
50
  const onInput = (el: HTMLInputElement) => (e: Event) => {
@@ -37,31 +53,29 @@ export const useCurrency = () => {
37
53
  }
38
54
  let { value } = el
39
55
  value = value.replace(/[^0-9,.]/g, '')
40
- inputRef.value.value = value
41
- const numberValue = (vm?.props.nullOnEmpty && value === '') ? null : parse(value)
42
- setValue(Number.isNaN(numberValue) ? 0 : numberValue)
56
+ setValue(value)
57
+ const numberValue = vm?.props.nullOnEmpty && value === '' ? null : parse(value)
58
+ emitAmount(Number.isNaN(numberValue) ? 0 : numberValue)
43
59
  }
44
60
 
45
61
  const onBlur = (e: FocusEvent) => {
46
62
  if (!inputRef.value || !e.target) {
47
63
  return
48
64
  }
49
- let rawValue = (e.target as HTMLInputElement).value.replace(/,/g, '.');
50
- let number: number | null;
65
+ let rawValue = (e.target as HTMLInputElement).value.replace(/,/g, '.')
66
+ let number: number | null
51
67
 
52
68
  if (rawValue === '' && vm?.props.nullOnEmpty) {
53
69
  number = null
54
- }
55
- else {
70
+ } else {
56
71
  number = parse(rawValue)
57
72
  if (Number.isNaN(number)) {
58
73
  number = 0
59
74
  }
60
75
  }
61
76
  const text = format(number)
62
-
63
- inputRef.value.value = text
64
- setValue(number)
77
+ setValue(text)
78
+ emitAmount(parse(text))
65
79
  }
66
80
 
67
81
  watch(inputRef, (newVal, oldVal) => {
@@ -76,15 +90,31 @@ export const useCurrency = () => {
76
90
  newVal.addEventListener('input', onInput(newVal))
77
91
  newVal.addEventListener('blur', onBlur)
78
92
 
79
- if (modelValue.value) {
80
- newVal.value = format(modelValue.value)
93
+ if (vm?.props.amount) {
94
+ newVal.value = format(computedModel.value)
81
95
  }
82
96
  })
83
97
 
98
+ watch(computedModel, (newVal) => {
99
+ nextTick(() => {
100
+ if (!inputRef.value || !newVal) {
101
+ return
102
+ }
103
+ // we need to format here only if someone externally set the
104
+ // value of the amount model
105
+ if (internalVal.value !== newVal) {
106
+ const formatted = format(newVal)
107
+ inputRef.value.value = formatted
108
+ internalVal.value = newVal
109
+ }
110
+ })
111
+ })
112
+
84
113
  return {
85
114
  inputRef,
86
115
  parse,
87
116
  format,
117
+ emitAmount,
88
118
  setValue
89
119
  }
90
120
  }