@fiscozen/composables 0.1.10 → 0.1.12

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,260 +1,299 @@
1
- import { ref as f, reactive as S, onUnmounted as L, nextTick as z, onMounted as E, onBeforeUnmount as M, defineComponent as P, useSlots as N, computed as T, toRef as W, watch as O, openBlock as q, createElementBlock as A, createElementVNode as $, renderSlot as w, unref as F, withDirectives as D, normalizeClass as I, vShow as Q } from "vue";
2
- const x = (o, t, a, n) => {
3
- let s = "right", e = "right-start";
4
- const u = o.getBoundingClientRect(), r = t.getBoundingClientRect(), h = a.getBoundingClientRect(), b = (h.left - u.left - r.width) / u.width, l = (u.right - h.right - r.width) / u.width, i = (h.top - u.top - r.height) / u.height, d = (u.bottom - h.bottom - r.height) / u.height;
5
- if (s = [
1
+ import { ref as f, reactive as M, onUnmounted as z, nextTick as P, onMounted as w, onBeforeUnmount as C, defineComponent as A, useSlots as N, computed as $, toRef as D, watch as O, openBlock as T, createElementBlock as W, createElementVNode as F, renderSlot as k, unref as S, withDirectives as q, normalizeClass as I, vShow as U } from "vue";
2
+ const x = (o, t, i, n) => {
3
+ let a = "right", e = "right-start";
4
+ const c = o.getBoundingClientRect(), d = t.getBoundingClientRect(), r = i.getBoundingClientRect(), b = (r.left - c.left - d.width) / c.width, l = (c.right - r.right - d.width) / c.width, s = (r.top - c.top - d.height) / c.height, h = (c.bottom - r.bottom - d.height) / c.height;
5
+ if (a = [
6
6
  {
7
7
  key: "right",
8
8
  space: l
9
9
  },
10
10
  {
11
11
  key: "top",
12
- space: i
12
+ space: s
13
13
  },
14
14
  {
15
15
  key: "bottom",
16
- space: d
16
+ space: h
17
17
  },
18
18
  {
19
19
  key: "left",
20
20
  space: b
21
21
  }
22
- ].sort((p, c) => c.space - p.space)[0].key, !n)
23
- switch (s) {
22
+ ].sort((p, u) => u.space - p.space)[0].key, !n)
23
+ switch (a) {
24
24
  case "right":
25
25
  case "left":
26
- const p = (r.height - h.height) / 2;
27
- u.top > h.top - p && (n = "end"), u.bottom < h.top - p && (n = "start");
26
+ const p = (d.height - r.height) / 2;
27
+ c.top > r.top - p && (n = "end"), c.bottom < r.top - p && (n = "start");
28
28
  break;
29
29
  }
30
- return e = n ? `${s}-${n}` : s, e;
31
- }, U = (o, t, a, n) => ({
32
- x: t.x + o.width * a / 100,
30
+ return e = n ? `${a}-${n}` : a, e;
31
+ }, K = (o, t, i, n) => ({
32
+ x: t.x + o.width * i / 100,
33
33
  y: t.y + o.height * n / 100
34
- }), X = (o) => {
35
- const t = f(null), a = f(null), n = f(null), s = f(null), e = S({
34
+ }), Q = (o) => {
35
+ const t = f(null), i = f(null), n = f(null), a = f(null), e = M({
36
36
  position: { x: 0, y: 0 }
37
- }), u = {
37
+ }), c = {
38
38
  root: null,
39
39
  rootMargin: "0px",
40
40
  threshold: 1,
41
41
  ...o.element.intersectionOptions
42
- }, r = (l, i) => {
43
- }, h = f(new IntersectionObserver(r, u)), b = () => z(() => {
44
- var R;
45
- if (t.value = typeof o.element.domRef.value == "string" ? document.querySelector(o.element.domRef.value) : o.element.domRef.value, o.container ? (a.value = typeof o.container.domRef.value == "string" ? document.querySelector(o.container.domRef.value) : o.container.domRef.value, a.value ?? (a.value = document.body)) : a.value = document.body, !t.value)
42
+ }, d = (l, s) => {
43
+ }, r = f(new IntersectionObserver(d, c)), b = () => P(() => {
44
+ var E;
45
+ if (t.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, !t.value)
46
46
  throw new Error("missing reference element for floating behavior");
47
- o.opener && (n.value = typeof o.opener.domRef.value == "string" ? document.querySelector(o.opener.domRef.value) : o.opener.domRef.value), s.value = t.value.getBoundingClientRect();
48
- const l = (R = n.value) == null ? void 0 : R.getBoundingClientRect(), i = a.value.getBoundingClientRect(), d = window.getComputedStyle(t.value);
49
- h.value.observe(t.value), h.value.observe(a.value);
50
- let m = o.position ? o.position.value : "auto", p = 0, c = 0;
47
+ o.opener && (n.value = typeof o.opener.domRef.value == "string" ? document.querySelector(o.opener.domRef.value) : o.opener.domRef.value), a.value = t.value.getBoundingClientRect();
48
+ const l = (E = n.value) == null ? void 0 : E.getBoundingClientRect(), s = i.value.getBoundingClientRect(), h = window.getComputedStyle(t.value);
49
+ r.value.observe(t.value), r.value.observe(i.value);
50
+ let v = o.position ? o.position.value : "auto", p = 0, u = 0;
51
51
  if (o.opener && n.value && l) {
52
- const g = l.left - parseFloat(d.marginLeft) - parseFloat(d.marginRight), B = l.left - parseFloat(d.marginLeft), y = l.top - parseFloat(d.marginTop) - parseFloat(d.marginBottom), C = l.top - parseFloat(d.marginTop);
53
- switch (m) {
52
+ const g = l.left - parseFloat(h.marginLeft) - parseFloat(h.marginRight), B = l.left - parseFloat(h.marginLeft), y = l.top - parseFloat(h.marginTop) - parseFloat(h.marginBottom), L = l.top - parseFloat(h.marginTop);
53
+ switch (v) {
54
54
  case "auto":
55
- m = x(
56
- a.value,
55
+ v = x(
56
+ i.value,
57
57
  t.value,
58
58
  n.value
59
59
  );
60
60
  break;
61
61
  case "auto-start":
62
- m = x(
63
- a.value,
62
+ v = x(
63
+ i.value,
64
64
  t.value,
65
65
  n.value,
66
66
  "start"
67
67
  );
68
68
  break;
69
69
  case "auto-end":
70
- m = x(
71
- a.value,
70
+ v = x(
71
+ i.value,
72
72
  t.value,
73
73
  n.value,
74
74
  "end"
75
75
  );
76
76
  break;
77
77
  }
78
- switch (m) {
78
+ switch (v) {
79
79
  case "bottom":
80
- e.position.y = l.bottom, e.position.x = B + l.width / 2, c = -50, p = 0;
80
+ e.position.y = l.bottom, e.position.x = B + l.width / 2, u = -50, p = 0;
81
81
  break;
82
82
  case "bottom-start":
83
- e.position.y = l.bottom, e.position.x = g, c = 0, p = 0;
83
+ e.position.y = l.bottom, e.position.x = g, u = 0, p = 0;
84
84
  break;
85
85
  case "bottom-end":
86
- e.position.y = l.bottom, e.position.x = l.right, c = -100, p = 0;
86
+ e.position.y = l.bottom, e.position.x = l.right, u = -100, p = 0;
87
87
  break;
88
88
  case "left-start":
89
- e.position.y = y, e.position.x = g, c = -100, p = 0;
89
+ e.position.y = y, e.position.x = g, u = -100, p = 0;
90
90
  break;
91
91
  case "left":
92
- e.position.y = C + l.height / 2, e.position.x = g, p = -50, c = -100;
92
+ e.position.y = L + l.height / 2, e.position.x = g, p = -50, u = -100;
93
93
  break;
94
94
  case "left-end":
95
- e.position.y = l.bottom, e.position.x = g, p = -100, c = -100;
95
+ e.position.y = l.bottom, e.position.x = g, p = -100, u = -100;
96
96
  break;
97
97
  case "top-start":
98
- e.position.y = y, e.position.x = g, p = -100, c = 0;
98
+ e.position.y = y, e.position.x = g, p = -100, u = 0;
99
99
  break;
100
100
  case "top":
101
- e.position.y = y, e.position.x = B + l.width / 2, c = -50, p = -100;
101
+ e.position.y = y, e.position.x = B + l.width / 2, u = -50, p = -100;
102
102
  break;
103
103
  case "top-end":
104
- e.position.y = y, e.position.x = l.right, c = -100, p = -100;
104
+ e.position.y = y, e.position.x = l.right, u = -100, p = -100;
105
105
  break;
106
106
  case "right-start":
107
- e.position.y = y, e.position.x = l.right, c = 0, p = 0;
107
+ e.position.y = y, e.position.x = l.right, u = 0, p = 0;
108
108
  break;
109
109
  case "right":
110
- e.position.y = C + l.height / 2, e.position.x = l.right, p = -50, c = 0;
110
+ e.position.y = L + l.height / 2, e.position.x = l.right, p = -50, u = 0;
111
111
  break;
112
112
  case "right-end":
113
- e.position.y = l.bottom, e.position.x = l.right, p = -100, c = 0;
113
+ e.position.y = l.bottom, e.position.x = l.right, p = -100, u = 0;
114
114
  break;
115
115
  }
116
116
  } else
117
- switch (m) {
117
+ switch (v) {
118
118
  case "bottom":
119
- e.position.y = i.bottom - s.value.height, e.position.x = i.left + i.width / 2, c = -50;
119
+ e.position.y = s.bottom - a.value.height, e.position.x = s.left + s.width / 2, u = -50;
120
120
  break;
121
121
  case "left-end":
122
122
  case "bottom-start":
123
- e.position.y = i.bottom - s.value.height, e.position.x = i.left;
123
+ e.position.y = s.bottom - a.value.height, e.position.x = s.left;
124
124
  break;
125
125
  case "right-end":
126
126
  case "bottom-end":
127
- e.position.y = i.bottom - s.value.height, e.position.x = i.right - s.value.width;
127
+ e.position.y = s.bottom - a.value.height, e.position.x = s.right - a.value.width;
128
128
  break;
129
129
  case "left":
130
- e.position.y = i.top + (i.height - s.value.height) / 2, e.position.x = i.left;
130
+ e.position.y = s.top + (s.height - a.value.height) / 2, e.position.x = s.left;
131
131
  break;
132
132
  case "top-start":
133
133
  case "left-start":
134
- e.position.y = i.top, e.position.x = i.left;
134
+ e.position.y = s.top, e.position.x = s.left;
135
135
  break;
136
136
  case "top":
137
- e.position.y = i.top, e.position.x = i.left + (i.width - s.value.width) / 2;
137
+ e.position.y = s.top, e.position.x = s.left + (s.width - a.value.width) / 2;
138
138
  break;
139
139
  case "top-end":
140
140
  case "right-start":
141
- e.position.y = i.top, e.position.x = i.right - s.value.width;
141
+ e.position.y = s.top, e.position.x = s.right - a.value.width;
142
142
  case "right":
143
- e.position.y = i.top + (i.height - s.value.height) / 2, e.position.x = i.right - s.value.width;
143
+ e.position.y = s.top + (s.height - a.value.height) / 2, e.position.x = s.right - a.value.width;
144
144
  break;
145
145
  }
146
- const v = U(s.value, e.position, c, p);
147
- e.position.x = v.x, e.position.y = v.y, v.x < i.left && (e.position.x = i.left, c = 0), v.x + s.value.width > i.right && (e.position.x = i.right - s.value.width, c = 0), v.y < i.top && (e.position.y = i.top, p = 0), v.y + s.value.height > i.bottom && (e.position.y = i.bottom - s.value.height, p = 0), t.value.style.top = `${e.position.y}px`, t.value.style.left = `${e.position.x}px`, t.value.style.position = "fixed", t.value.style.display = "flex", s.value = t.value.getBoundingClientRect();
146
+ const m = K(a.value, e.position, u, p);
147
+ e.position.x = m.x, e.position.y = m.y, m.x < s.left && (e.position.x = s.left, u = 0), m.x + a.value.width > s.right && (e.position.x = s.right - a.value.width, u = 0), m.y < s.top && (e.position.y = s.top, p = 0), m.y + a.value.height > s.bottom && (e.position.y = s.bottom - a.value.height, p = 0), t.value.style.top = `${e.position.y}px`, t.value.style.left = `${e.position.x}px`, t.value.style.position = "fixed", t.value.style.display = "flex", a.value = t.value.getBoundingClientRect();
148
148
  });
149
- return L(() => {
150
- h.value.disconnect();
149
+ return z(() => {
150
+ r.value.disconnect();
151
151
  }), {
152
152
  float: e,
153
- rect: s,
154
- floatObserver: h,
153
+ rect: a,
154
+ floatObserver: r,
155
155
  setPosition: b
156
156
  };
157
157
  };
158
- function k(o) {
159
- const t = window.matchMedia(o), a = f(t.matches);
160
- function n(s) {
161
- a.value = s.matches;
158
+ function R(o) {
159
+ const t = window.matchMedia(o), i = f(t.matches);
160
+ function n(a) {
161
+ i.value = a.matches;
162
162
  }
163
- return E(() => {
163
+ return w(() => {
164
164
  t.addEventListener("change", n);
165
- }), L(() => {
165
+ }), z(() => {
166
166
  t.removeEventListener("change", n);
167
- }), a;
167
+ }), i;
168
168
  }
169
- function G(o) {
169
+ function Y(o) {
170
170
  return {
171
171
  isGreater(t) {
172
- return k(`(min-width: ${o[t]})`);
172
+ return R(`(min-width: ${o[t]})`);
173
173
  },
174
174
  isSmaller(t) {
175
- return k(`(max-width: ${o[t]})`);
175
+ return R(`(max-width: ${o[t]})`);
176
176
  },
177
- isInBetween(t, a) {
178
- return k(`(min-width: ${o[t]}) and (max-width: ${o[a]})`);
177
+ isInBetween(t, i) {
178
+ return R(`(min-width: ${o[t]}) and (max-width: ${o[i]})`);
179
179
  }
180
180
  };
181
181
  }
182
- function H(o, t) {
182
+ function G(o, t, i) {
183
183
  if (!o)
184
184
  throw new Error("A target component has to be provided.");
185
185
  if (!t)
186
186
  throw new Error("A callback has to be provided.");
187
- const a = (n) => {
188
- !o.value || n.target === o.value || n.composedPath().includes(o.value) || typeof t == "function" && t();
187
+ const n = (a) => {
188
+ !o.value || a.target === o.value || a.composedPath().includes(o.value) || typeof t == "function" && t();
189
+ };
190
+ w(() => {
191
+ (i ?? o).value.addEventListener("click", n);
192
+ }), C(() => {
193
+ (i ?? o).value.removeEventListener("click", n);
194
+ });
195
+ }
196
+ function H(o, t) {
197
+ if (!o)
198
+ throw new Error("A target component has to be provided.");
199
+ if (!t || typeof t != "function")
200
+ throw new Error("A callback has to be provided.");
201
+ const i = (n) => {
202
+ t(n);
189
203
  };
190
- E(() => {
191
- window.addEventListener("click", a);
192
- }), M(() => {
193
- window.removeEventListener("click", a);
204
+ w(() => {
205
+ o.value.addEventListener("keydown", i);
206
+ }), C(() => {
207
+ o.value.removeEventListener("keydown", i);
208
+ });
209
+ }
210
+ function V(o, t) {
211
+ if (!o || typeof o != "function")
212
+ throw new Error("A callback has to be provided.");
213
+ const i = (n) => {
214
+ o(n);
215
+ };
216
+ w(() => {
217
+ if (!t) {
218
+ document.addEventListener("keyup", i);
219
+ return;
220
+ }
221
+ t.value.addEventListener("keyup", i);
222
+ }), C(() => {
223
+ if (!t) {
224
+ document.removeEventListener("keyup", i);
225
+ return;
226
+ }
227
+ t.value.removeEventListener("keyup", i);
194
228
  });
195
229
  }
196
- const V = /* @__PURE__ */ P({
230
+ const J = /* @__PURE__ */ A({
197
231
  __name: "FzFloating",
198
232
  props: {
199
233
  isOpen: { type: Boolean, default: !1 },
200
234
  position: { default: "auto" },
201
235
  container: {},
202
236
  contentClass: {},
203
- openerClass: {}
237
+ openerClass: {},
238
+ overrideContentClass: { type: Boolean }
204
239
  },
205
240
  setup(o) {
206
- const t = o, a = f(null), n = f(null), s = N(), e = {
207
- position: T(() => t.position),
241
+ const t = o, i = f(null), n = f(null), a = N(), e = {
242
+ position: $(() => t.position),
208
243
  element: {
209
244
  domRef: n
210
245
  },
211
246
  container: {
212
- domRef: W(t.container || document.body)
247
+ domRef: D(t.container || document.body)
213
248
  }
214
249
  };
215
- s.opener && (e.opener = {
216
- domRef: a
250
+ a.opener && (e.opener = {
251
+ domRef: i
217
252
  });
218
- const u = X(e);
219
- return O(
253
+ const c = Q(e);
254
+ O(
220
255
  () => t.position,
221
- () => t.isOpen && u.setPosition()
256
+ () => t.isOpen && c.setPosition()
222
257
  ), O(
223
258
  () => t.isOpen,
224
259
  (r) => {
225
- !r || !n.value || (n.value.style.top = "0px", n.value.style.left = "0px", n.value.style.transform = "none", u.setPosition());
260
+ !r || !n.value || (n.value.style.top = "0px", n.value.style.left = "0px", n.value.style.transform = "none", c.setPosition());
226
261
  }
227
- ), (r, h) => (q(), A("div", null, [
228
- $("div", {
262
+ );
263
+ const d = $(() => t.overrideContentClass ? t.contentClass : ["bg-core-white fixed p-4 z-10", t.contentClass]);
264
+ return (r, b) => (T(), W("div", null, [
265
+ F("div", {
229
266
  ref_key: "opener",
230
- ref: a,
267
+ ref: i,
231
268
  class: "inline-flex"
232
269
  }, [
233
- w(r.$slots, "opener", {
270
+ k(r.$slots, "opener", {
234
271
  isOpen: r.isOpen,
235
- floating: F(u)
272
+ floating: S(c)
236
273
  })
237
274
  ], 512),
238
- w(r.$slots, "opener-end"),
239
- D($("div", {
275
+ k(r.$slots, "opener-end"),
276
+ q(F("div", {
240
277
  ref_key: "content",
241
278
  ref: n,
242
- class: I(["bg-core-white fixed p-4 z-10", r.contentClass])
279
+ class: I(d.value)
243
280
  }, [
244
- w(r.$slots, "default", {
281
+ k(r.$slots, "default", {
245
282
  isOpen: r.isOpen,
246
- floating: F(u)
283
+ floating: S(c)
247
284
  })
248
285
  ], 2), [
249
- [Q, r.$slots.default && (!r.$slots.opener || r.$slots.opener && r.isOpen)]
286
+ [U, r.$slots.default && (!r.$slots.opener || r.$slots.opener && r.isOpen)]
250
287
  ])
251
288
  ]));
252
289
  }
253
290
  });
254
291
  export {
255
- V as FzFloating,
256
- G as useBreakpoints,
257
- H as useClickOutside,
258
- X as useFloating,
259
- k as useMediaQuery
292
+ J as FzFloating,
293
+ Y as useBreakpoints,
294
+ G as useClickOutside,
295
+ Q as useFloating,
296
+ H as useKeyDown,
297
+ V as useKeyUp,
298
+ R as useMediaQuery
260
299
  };
@@ -1 +1 @@
1
- (function(f,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],o):(f=typeof globalThis<"u"?globalThis:f||self,o(f.FzComposables={},f.Vue))})(this,function(f,o){"use strict";const k=(i,t,l,s)=>{let a="right",e="right-start";const u=i.getBoundingClientRect(),p=t.getBoundingClientRect(),h=l.getBoundingClientRect(),x=(h.left-u.left-p.width)/u.width,r=(u.right-h.right-p.width)/u.width,n=(h.top-u.top-p.height)/u.height,m=(u.bottom-h.bottom-p.height)/u.height;if(a=[{key:"right",space:r},{key:"top",space:n},{key:"bottom",space:m},{key:"left",space:x}].sort((c,d)=>d.space-c.space)[0].key,!s)switch(a){case"right":case"left":const c=(p.height-h.height)/2;u.top>h.top-c&&(s="end"),u.bottom<h.top-c&&(s="start");break}return e=s?`${a}-${s}`:a,e},F=(i,t,l,s)=>({x:t.x+i.width*l/100,y:t.y+i.height*s/100}),R=i=>{const t=o.ref(null),l=o.ref(null),s=o.ref(null),a=o.ref(null),e=o.reactive({position:{x:0,y:0}}),u={root:null,rootMargin:"0px",threshold:1,...i.element.intersectionOptions},p=(r,n)=>{},h=o.ref(new IntersectionObserver(p,u)),x=()=>o.nextTick(()=>{var B;if(t.value=typeof i.element.domRef.value=="string"?document.querySelector(i.element.domRef.value):i.element.domRef.value,i.container?(l.value=typeof i.container.domRef.value=="string"?document.querySelector(i.container.domRef.value):i.container.domRef.value,l.value??(l.value=document.body)):l.value=document.body,!t.value)throw new Error("missing reference element for floating behavior");i.opener&&(s.value=typeof i.opener.domRef.value=="string"?document.querySelector(i.opener.domRef.value):i.opener.domRef.value),a.value=t.value.getBoundingClientRect();const r=(B=s.value)==null?void 0:B.getBoundingClientRect(),n=l.value.getBoundingClientRect(),m=window.getComputedStyle(t.value);h.value.observe(t.value),h.value.observe(l.value);let g=i.position?i.position.value:"auto",c=0,d=0;if(i.opener&&s.value&&r){const b=r.left-parseFloat(m.marginLeft)-parseFloat(m.marginRight),C=r.left-parseFloat(m.marginLeft),v=r.top-parseFloat(m.marginTop)-parseFloat(m.marginBottom),O=r.top-parseFloat(m.marginTop);switch(g){case"auto":g=k(l.value,t.value,s.value);break;case"auto-start":g=k(l.value,t.value,s.value,"start");break;case"auto-end":g=k(l.value,t.value,s.value,"end");break}switch(g){case"bottom":e.position.y=r.bottom,e.position.x=C+r.width/2,d=-50,c=0;break;case"bottom-start":e.position.y=r.bottom,e.position.x=b,d=0,c=0;break;case"bottom-end":e.position.y=r.bottom,e.position.x=r.right,d=-100,c=0;break;case"left-start":e.position.y=v,e.position.x=b,d=-100,c=0;break;case"left":e.position.y=O+r.height/2,e.position.x=b,c=-50,d=-100;break;case"left-end":e.position.y=r.bottom,e.position.x=b,c=-100,d=-100;break;case"top-start":e.position.y=v,e.position.x=b,c=-100,d=0;break;case"top":e.position.y=v,e.position.x=C+r.width/2,d=-50,c=-100;break;case"top-end":e.position.y=v,e.position.x=r.right,d=-100,c=-100;break;case"right-start":e.position.y=v,e.position.x=r.right,d=0,c=0;break;case"right":e.position.y=O+r.height/2,e.position.x=r.right,c=-50,d=0;break;case"right-end":e.position.y=r.bottom,e.position.x=r.right,c=-100,d=0;break}}else switch(g){case"bottom":e.position.y=n.bottom-a.value.height,e.position.x=n.left+n.width/2,d=-50;break;case"left-end":case"bottom-start":e.position.y=n.bottom-a.value.height,e.position.x=n.left;break;case"right-end":case"bottom-end":e.position.y=n.bottom-a.value.height,e.position.x=n.right-a.value.width;break;case"left":e.position.y=n.top+(n.height-a.value.height)/2,e.position.x=n.left;break;case"top-start":case"left-start":e.position.y=n.top,e.position.x=n.left;break;case"top":e.position.y=n.top,e.position.x=n.left+(n.width-a.value.width)/2;break;case"top-end":case"right-start":e.position.y=n.top,e.position.x=n.right-a.value.width;case"right":e.position.y=n.top+(n.height-a.value.height)/2,e.position.x=n.right-a.value.width;break}const y=F(a.value,e.position,d,c);e.position.x=y.x,e.position.y=y.y,y.x<n.left&&(e.position.x=n.left,d=0),y.x+a.value.width>n.right&&(e.position.x=n.right-a.value.width,d=0),y.y<n.top&&(e.position.y=n.top,c=0),y.y+a.value.height>n.bottom&&(e.position.y=n.bottom-a.value.height,c=0),t.value.style.top=`${e.position.y}px`,t.value.style.left=`${e.position.x}px`,t.value.style.position="fixed",t.value.style.display="flex",a.value=t.value.getBoundingClientRect()});return o.onUnmounted(()=>{h.value.disconnect()}),{float:e,rect:a,floatObserver:h,setPosition:x}};function w(i){const t=window.matchMedia(i),l=o.ref(t.matches);function s(a){l.value=a.matches}return o.onMounted(()=>{t.addEventListener("change",s)}),o.onUnmounted(()=>{t.removeEventListener("change",s)}),l}function S(i){return{isGreater(t){return w(`(min-width: ${i[t]})`)},isSmaller(t){return w(`(max-width: ${i[t]})`)},isInBetween(t,l){return w(`(min-width: ${i[t]}) and (max-width: ${i[l]})`)}}}function $(i,t){if(!i)throw new Error("A target component has to be provided.");if(!t)throw new Error("A callback has to be provided.");const l=s=>{!i.value||s.target===i.value||s.composedPath().includes(i.value)||typeof t=="function"&&t()};o.onMounted(()=>{window.addEventListener("click",l)}),o.onBeforeUnmount(()=>{window.removeEventListener("click",l)})}const E=o.defineComponent({__name:"FzFloating",props:{isOpen:{type:Boolean,default:!1},position:{default:"auto"},container:{},contentClass:{},openerClass:{}},setup(i){const t=i,l=o.ref(null),s=o.ref(null),a=o.useSlots(),e={position:o.computed(()=>t.position),element:{domRef:s},container:{domRef:o.toRef(t.container||document.body)}};a.opener&&(e.opener={domRef:l});const u=R(e);return o.watch(()=>t.position,()=>t.isOpen&&u.setPosition()),o.watch(()=>t.isOpen,p=>{!p||!s.value||(s.value.style.top="0px",s.value.style.left="0px",s.value.style.transform="none",u.setPosition())}),(p,h)=>(o.openBlock(),o.createElementBlock("div",null,[o.createElementVNode("div",{ref_key:"opener",ref:l,class:"inline-flex"},[o.renderSlot(p.$slots,"opener",{isOpen:p.isOpen,floating:o.unref(u)})],512),o.renderSlot(p.$slots,"opener-end"),o.withDirectives(o.createElementVNode("div",{ref_key:"content",ref:s,class:o.normalizeClass(["bg-core-white fixed p-4 z-10",p.contentClass])},[o.renderSlot(p.$slots,"default",{isOpen:p.isOpen,floating:o.unref(u)})],2),[[o.vShow,p.$slots.default&&(!p.$slots.opener||p.$slots.opener&&p.isOpen)]])]))}});f.FzFloating=E,f.useBreakpoints=S,f.useClickOutside=$,f.useFloating=R,f.useMediaQuery=w,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
1
+ (function(f,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],n):(f=typeof globalThis<"u"?globalThis:f||self,n(f.FzComposables={},f.Vue))})(this,function(f,n){"use strict";const x=(o,t,i,s)=>{let r="right",e="right-start";const d=o.getBoundingClientRect(),h=t.getBoundingClientRect(),l=i.getBoundingClientRect(),k=(l.left-d.left-h.width)/d.width,p=(d.right-l.right-h.width)/d.width,a=(l.top-d.top-h.height)/d.height,m=(d.bottom-l.bottom-h.height)/d.height;if(r=[{key:"right",space:p},{key:"top",space:a},{key:"bottom",space:m},{key:"left",space:k}].sort((c,u)=>u.space-c.space)[0].key,!s)switch(r){case"right":case"left":const c=(h.height-l.height)/2;d.top>l.top-c&&(s="end"),d.bottom<l.top-c&&(s="start");break}return e=s?`${r}-${s}`:r,e},L=(o,t,i,s)=>({x:t.x+o.width*i/100,y:t.y+o.height*s/100}),R=o=>{const t=n.ref(null),i=n.ref(null),s=n.ref(null),r=n.ref(null),e=n.reactive({position:{x:0,y:0}}),d={root:null,rootMargin:"0px",threshold:1,...o.element.intersectionOptions},h=(p,a)=>{},l=n.ref(new IntersectionObserver(h,d)),k=()=>n.nextTick(()=>{var C;if(t.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,!t.value)throw new Error("missing reference element for floating behavior");o.opener&&(s.value=typeof o.opener.domRef.value=="string"?document.querySelector(o.opener.domRef.value):o.opener.domRef.value),r.value=t.value.getBoundingClientRect();const p=(C=s.value)==null?void 0:C.getBoundingClientRect(),a=i.value.getBoundingClientRect(),m=window.getComputedStyle(t.value);l.value.observe(t.value),l.value.observe(i.value);let g=o.position?o.position.value:"auto",c=0,u=0;if(o.opener&&s.value&&p){const v=p.left-parseFloat(m.marginLeft)-parseFloat(m.marginRight),E=p.left-parseFloat(m.marginLeft),b=p.top-parseFloat(m.marginTop)-parseFloat(m.marginBottom),B=p.top-parseFloat(m.marginTop);switch(g){case"auto":g=x(i.value,t.value,s.value);break;case"auto-start":g=x(i.value,t.value,s.value,"start");break;case"auto-end":g=x(i.value,t.value,s.value,"end");break}switch(g){case"bottom":e.position.y=p.bottom,e.position.x=E+p.width/2,u=-50,c=0;break;case"bottom-start":e.position.y=p.bottom,e.position.x=v,u=0,c=0;break;case"bottom-end":e.position.y=p.bottom,e.position.x=p.right,u=-100,c=0;break;case"left-start":e.position.y=b,e.position.x=v,u=-100,c=0;break;case"left":e.position.y=B+p.height/2,e.position.x=v,c=-50,u=-100;break;case"left-end":e.position.y=p.bottom,e.position.x=v,c=-100,u=-100;break;case"top-start":e.position.y=b,e.position.x=v,c=-100,u=0;break;case"top":e.position.y=b,e.position.x=E+p.width/2,u=-50,c=-100;break;case"top-end":e.position.y=b,e.position.x=p.right,u=-100,c=-100;break;case"right-start":e.position.y=b,e.position.x=p.right,u=0,c=0;break;case"right":e.position.y=B+p.height/2,e.position.x=p.right,c=-50,u=0;break;case"right-end":e.position.y=p.bottom,e.position.x=p.right,c=-100,u=0;break}}else switch(g){case"bottom":e.position.y=a.bottom-r.value.height,e.position.x=a.left+a.width/2,u=-50;break;case"left-end":case"bottom-start":e.position.y=a.bottom-r.value.height,e.position.x=a.left;break;case"right-end":case"bottom-end":e.position.y=a.bottom-r.value.height,e.position.x=a.right-r.value.width;break;case"left":e.position.y=a.top+(a.height-r.value.height)/2,e.position.x=a.left;break;case"top-start":case"left-start":e.position.y=a.top,e.position.x=a.left;break;case"top":e.position.y=a.top,e.position.x=a.left+(a.width-r.value.width)/2;break;case"top-end":case"right-start":e.position.y=a.top,e.position.x=a.right-r.value.width;case"right":e.position.y=a.top+(a.height-r.value.height)/2,e.position.x=a.right-r.value.width;break}const y=L(r.value,e.position,u,c);e.position.x=y.x,e.position.y=y.y,y.x<a.left&&(e.position.x=a.left,u=0),y.x+r.value.width>a.right&&(e.position.x=a.right-r.value.width,u=0),y.y<a.top&&(e.position.y=a.top,c=0),y.y+r.value.height>a.bottom&&(e.position.y=a.bottom-r.value.height,c=0),t.value.style.top=`${e.position.y}px`,t.value.style.left=`${e.position.x}px`,t.value.style.position="fixed",t.value.style.display="flex",r.value=t.value.getBoundingClientRect()});return n.onUnmounted(()=>{l.value.disconnect()}),{float:e,rect:r,floatObserver:l,setPosition:k}};function w(o){const t=window.matchMedia(o),i=n.ref(t.matches);function s(r){i.value=r.matches}return n.onMounted(()=>{t.addEventListener("change",s)}),n.onUnmounted(()=>{t.removeEventListener("change",s)}),i}function O(o){return{isGreater(t){return w(`(min-width: ${o[t]})`)},isSmaller(t){return w(`(max-width: ${o[t]})`)},isInBetween(t,i){return w(`(min-width: ${o[t]}) and (max-width: ${o[i]})`)}}}function F(o,t,i){if(!o)throw new Error("A target component has to be provided.");if(!t)throw new Error("A callback has to be provided.");const s=r=>{!o.value||r.target===o.value||r.composedPath().includes(o.value)||typeof t=="function"&&t()};n.onMounted(()=>{(i??o).value.addEventListener("click",s)}),n.onBeforeUnmount(()=>{(i??o).value.removeEventListener("click",s)})}function S(o,t){if(!o)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 i=s=>{t(s)};n.onMounted(()=>{o.value.addEventListener("keydown",i)}),n.onBeforeUnmount(()=>{o.value.removeEventListener("keydown",i)})}function $(o,t){if(!o||typeof o!="function")throw new Error("A callback has to be provided.");const i=s=>{o(s)};n.onMounted(()=>{if(!t){document.addEventListener("keyup",i);return}t.value.addEventListener("keyup",i)}),n.onBeforeUnmount(()=>{if(!t){document.removeEventListener("keyup",i);return}t.value.removeEventListener("keyup",i)})}const M=n.defineComponent({__name:"FzFloating",props:{isOpen:{type:Boolean,default:!1},position:{default:"auto"},container:{},contentClass:{},openerClass:{},overrideContentClass:{type:Boolean}},setup(o){const t=o,i=n.ref(null),s=n.ref(null),r=n.useSlots(),e={position:n.computed(()=>t.position),element:{domRef:s},container:{domRef:n.toRef(t.container||document.body)}};r.opener&&(e.opener={domRef:i});const d=R(e);n.watch(()=>t.position,()=>t.isOpen&&d.setPosition()),n.watch(()=>t.isOpen,l=>{!l||!s.value||(s.value.style.top="0px",s.value.style.left="0px",s.value.style.transform="none",d.setPosition())});const h=n.computed(()=>t.overrideContentClass?t.contentClass:["bg-core-white fixed p-4 z-10",t.contentClass]);return(l,k)=>(n.openBlock(),n.createElementBlock("div",null,[n.createElementVNode("div",{ref_key:"opener",ref:i,class:"inline-flex"},[n.renderSlot(l.$slots,"opener",{isOpen:l.isOpen,floating:n.unref(d)})],512),n.renderSlot(l.$slots,"opener-end"),n.withDirectives(n.createElementVNode("div",{ref_key:"content",ref:s,class:n.normalizeClass(h.value)},[n.renderSlot(l.$slots,"default",{isOpen:l.isOpen,floating:n.unref(d)})],2),[[n.vShow,l.$slots.default&&(!l.$slots.opener||l.$slots.opener&&l.isOpen)]])]))}});f.FzFloating=M,f.useBreakpoints=O,f.useClickOutside=F,f.useFloating=R,f.useKeyDown=S,f.useKeyUp=$,f.useMediaQuery=w,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fiscozen/composables",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "Design System utility composables",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -26,8 +26,8 @@
26
26
  "vitest": "^1.2.0",
27
27
  "vue-tsc": "^1.8.25",
28
28
  "@fiscozen/tsconfig": "^0.1.0",
29
- "@fiscozen/prettier-config": "^0.1.0",
30
- "@fiscozen/eslint-config": "^0.1.0"
29
+ "@fiscozen/eslint-config": "^0.1.0",
30
+ "@fiscozen/prettier-config": "^0.1.0"
31
31
  },
32
32
  "license": "ISC",
33
33
  "scripts": {
@@ -2,4 +2,5 @@ export * from './useFloating'
2
2
  export * from './useMediaQuery'
3
3
  export * from './useBreakpoints'
4
4
  export * from './useClickOutside'
5
- export * from './useKeyDown'
5
+ export * from './useKeyDown'
6
+ export * from './useKeyUp'
@@ -1,6 +1,10 @@
1
1
  import { onBeforeUnmount, onMounted, Ref } from 'vue'
2
2
 
3
- function useClickOutside(component: Ref<HTMLElement | undefined>, callback: () => void) {
3
+ function useClickOutside(
4
+ component: Ref<HTMLElement | undefined>,
5
+ callback: () => void,
6
+ elementToListenClicksOn?: Ref<HTMLElement | undefined>,
7
+ ) {
4
8
  // fail early if any of the required params is missing
5
9
  if (!component) {
6
10
  throw new Error('A target component has to be provided.')
@@ -24,11 +28,11 @@ function useClickOutside(component: Ref<HTMLElement | undefined>, callback: () =
24
28
  }
25
29
 
26
30
  onMounted(() => {
27
- window.addEventListener('click', listener)
31
+ (elementToListenClicksOn ?? component).value!.addEventListener('click', listener)
28
32
  })
29
33
 
30
34
  onBeforeUnmount(() => {
31
- window.removeEventListener('click', listener)
35
+ (elementToListenClicksOn ?? component).value!.removeEventListener('click', listener)
32
36
  })
33
37
  }
34
38
 
@@ -0,0 +1,30 @@
1
+ import { onBeforeUnmount, onMounted, Ref } from 'vue'
2
+
3
+ function useKeyUp(callback: (event: KeyboardEvent) => void, component?: Ref<HTMLElement | undefined>) {
4
+
5
+ if (!callback || typeof callback !== 'function') {
6
+ throw new Error('A callback has to be provided.')
7
+ }
8
+
9
+ const listener = (event: KeyboardEvent) => {
10
+ callback(event)
11
+ }
12
+
13
+ onMounted(() => {
14
+ if (!component) {
15
+ document.addEventListener('keyup', listener)
16
+ return;
17
+ }
18
+ component.value!.addEventListener('keyup', listener)
19
+ })
20
+
21
+ onBeforeUnmount(() => {
22
+ if (!component) {
23
+ document.removeEventListener('keyup', listener)
24
+ return;
25
+ }
26
+ component.value!.removeEventListener('keyup', listener)
27
+ })
28
+ }
29
+
30
+ export { useKeyUp }