@fiscozen/composables 0.1.12 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/composables.js +17 -17
- package/dist/composables.umd.cjs +1 -1
- package/package.json +3 -3
- package/src/FzFloating.vue +14 -1
- package/src/composables/useClickOutside.ts +18 -3
- package/src/types.ts +1 -0
package/dist/composables.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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
|
|
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 O, toRef as T, watch as $, openBlock as D, createElementBlock as W, createElementVNode as F, renderSlot as k, unref as S, withDirectives as q, normalizeClass as I, vShow as U } from "vue";
|
|
2
2
|
const x = (o, t, i, n) => {
|
|
3
3
|
let a = "right", e = "right-start";
|
|
4
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;
|
|
@@ -49,7 +49,7 @@ const x = (o, t, i, n) => {
|
|
|
49
49
|
r.value.observe(t.value), r.value.observe(i.value);
|
|
50
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(h.marginLeft) - parseFloat(h.marginRight),
|
|
52
|
+
const g = l.left - parseFloat(h.marginLeft) - parseFloat(h.marginRight), L = l.left - parseFloat(h.marginLeft), y = l.top - parseFloat(h.marginTop) - parseFloat(h.marginBottom), B = l.top - parseFloat(h.marginTop);
|
|
53
53
|
switch (v) {
|
|
54
54
|
case "auto":
|
|
55
55
|
v = x(
|
|
@@ -77,7 +77,7 @@ const x = (o, t, i, n) => {
|
|
|
77
77
|
}
|
|
78
78
|
switch (v) {
|
|
79
79
|
case "bottom":
|
|
80
|
-
e.position.y = l.bottom, e.position.x =
|
|
80
|
+
e.position.y = l.bottom, e.position.x = L + l.width / 2, u = -50, p = 0;
|
|
81
81
|
break;
|
|
82
82
|
case "bottom-start":
|
|
83
83
|
e.position.y = l.bottom, e.position.x = g, u = 0, p = 0;
|
|
@@ -89,7 +89,7 @@ const x = (o, t, i, n) => {
|
|
|
89
89
|
e.position.y = y, e.position.x = g, u = -100, p = 0;
|
|
90
90
|
break;
|
|
91
91
|
case "left":
|
|
92
|
-
e.position.y =
|
|
92
|
+
e.position.y = B + l.height / 2, e.position.x = g, p = -50, u = -100;
|
|
93
93
|
break;
|
|
94
94
|
case "left-end":
|
|
95
95
|
e.position.y = l.bottom, e.position.x = g, p = -100, u = -100;
|
|
@@ -98,7 +98,7 @@ const x = (o, t, i, n) => {
|
|
|
98
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 =
|
|
101
|
+
e.position.y = y, e.position.x = L + l.width / 2, u = -50, p = -100;
|
|
102
102
|
break;
|
|
103
103
|
case "top-end":
|
|
104
104
|
e.position.y = y, e.position.x = l.right, u = -100, p = -100;
|
|
@@ -107,7 +107,7 @@ const x = (o, t, i, n) => {
|
|
|
107
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 =
|
|
110
|
+
e.position.y = B + l.height / 2, e.position.x = l.right, p = -50, u = 0;
|
|
111
111
|
break;
|
|
112
112
|
case "right-end":
|
|
113
113
|
e.position.y = l.bottom, e.position.x = l.right, p = -100, u = 0;
|
|
@@ -179,18 +179,18 @@ function Y(o) {
|
|
|
179
179
|
}
|
|
180
180
|
};
|
|
181
181
|
}
|
|
182
|
-
function G(o, t
|
|
182
|
+
function G(o, t) {
|
|
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
|
|
188
|
-
!o.value ||
|
|
187
|
+
const i = (n) => {
|
|
188
|
+
!o.value || n.target === o.value || n.composedPath().includes(o.value) || typeof t == "function" && t();
|
|
189
189
|
};
|
|
190
190
|
w(() => {
|
|
191
|
-
|
|
191
|
+
window.addEventListener("click", i);
|
|
192
192
|
}), C(() => {
|
|
193
|
-
|
|
193
|
+
window.removeEventListener("click", i);
|
|
194
194
|
});
|
|
195
195
|
}
|
|
196
196
|
function H(o, t) {
|
|
@@ -239,29 +239,29 @@ const J = /* @__PURE__ */ A({
|
|
|
239
239
|
},
|
|
240
240
|
setup(o) {
|
|
241
241
|
const t = o, i = f(null), n = f(null), a = N(), e = {
|
|
242
|
-
position:
|
|
242
|
+
position: O(() => t.position),
|
|
243
243
|
element: {
|
|
244
244
|
domRef: n
|
|
245
245
|
},
|
|
246
246
|
container: {
|
|
247
|
-
domRef:
|
|
247
|
+
domRef: T(t.container || document.body)
|
|
248
248
|
}
|
|
249
249
|
};
|
|
250
250
|
a.opener && (e.opener = {
|
|
251
251
|
domRef: i
|
|
252
252
|
});
|
|
253
253
|
const c = Q(e);
|
|
254
|
-
|
|
254
|
+
$(
|
|
255
255
|
() => t.position,
|
|
256
256
|
() => t.isOpen && c.setPosition()
|
|
257
|
-
),
|
|
257
|
+
), $(
|
|
258
258
|
() => t.isOpen,
|
|
259
259
|
(r) => {
|
|
260
260
|
!r || !n.value || (n.value.style.top = "0px", n.value.style.left = "0px", n.value.style.transform = "none", c.setPosition());
|
|
261
261
|
}
|
|
262
262
|
);
|
|
263
|
-
const d =
|
|
264
|
-
return (r, b) => (
|
|
263
|
+
const d = O(() => t.overrideContentClass ? t.contentClass : ["bg-core-white fixed p-4 z-10", t.contentClass]);
|
|
264
|
+
return (r, b) => (D(), W("div", null, [
|
|
265
265
|
F("div", {
|
|
266
266
|
ref_key: "opener",
|
|
267
267
|
ref: i,
|
package/dist/composables.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(f,
|
|
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 x=(n,t,i,s)=>{let r="right",e="right-start";const d=n.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=(n,t,i,s)=>({x:t.x+n.width*i/100,y:t.y+n.height*s/100}),R=n=>{const t=o.ref(null),i=o.ref(null),s=o.ref(null),r=o.ref(null),e=o.reactive({position:{x:0,y:0}}),d={root:null,rootMargin:"0px",threshold:1,...n.element.intersectionOptions},h=(p,a)=>{},l=o.ref(new IntersectionObserver(h,d)),k=()=>o.nextTick(()=>{var C;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)throw new Error("missing reference element for floating behavior");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();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=n.position?n.position.value:"auto",c=0,u=0;if(n.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 o.onUnmounted(()=>{l.value.disconnect()}),{float:e,rect:r,floatObserver:l,setPosition:k}};function w(n){const t=window.matchMedia(n),i=o.ref(t.matches);function s(r){i.value=r.matches}return o.onMounted(()=>{t.addEventListener("change",s)}),o.onUnmounted(()=>{t.removeEventListener("change",s)}),i}function O(n){return{isGreater(t){return w(`(min-width: ${n[t]})`)},isSmaller(t){return w(`(max-width: ${n[t]})`)},isInBetween(t,i){return w(`(min-width: ${n[t]}) and (max-width: ${n[i]})`)}}}function F(n,t){if(!n)throw new Error("A target component has to be provided.");if(!t)throw new Error("A callback has to be provided.");const i=s=>{!n.value||s.target===n.value||s.composedPath().includes(n.value)||typeof t=="function"&&t()};o.onMounted(()=>{window.addEventListener("click",i)}),o.onBeforeUnmount(()=>{window.removeEventListener("click",i)})}function S(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 i=s=>{t(s)};o.onMounted(()=>{n.value.addEventListener("keydown",i)}),o.onBeforeUnmount(()=>{n.value.removeEventListener("keydown",i)})}function $(n,t){if(!n||typeof n!="function")throw new Error("A callback has to be provided.");const i=s=>{n(s)};o.onMounted(()=>{if(!t){document.addEventListener("keyup",i);return}t.value.addEventListener("keyup",i)}),o.onBeforeUnmount(()=>{if(!t){document.removeEventListener("keyup",i);return}t.value.removeEventListener("keyup",i)})}const M=o.defineComponent({__name:"FzFloating",props:{isOpen:{type:Boolean,default:!1},position:{default:"auto"},container:{},contentClass:{},openerClass:{},overrideContentClass:{type:Boolean}},setup(n){const t=n,i=o.ref(null),s=o.ref(null),r=o.useSlots(),e={position:o.computed(()=>t.position),element:{domRef:s},container:{domRef:o.toRef(t.container||document.body)}};r.opener&&(e.opener={domRef:i});const d=R(e);o.watch(()=>t.position,()=>t.isOpen&&d.setPosition()),o.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=o.computed(()=>t.overrideContentClass?t.contentClass:["bg-core-white fixed p-4 z-10",t.contentClass]);return(l,k)=>(o.openBlock(),o.createElementBlock("div",null,[o.createElementVNode("div",{ref_key:"opener",ref:i,class:"inline-flex"},[o.renderSlot(l.$slots,"opener",{isOpen:l.isOpen,floating:o.unref(d)})],512),o.renderSlot(l.$slots,"opener-end"),o.withDirectives(o.createElementVNode("div",{ref_key:"content",ref:s,class:o.normalizeClass(h.value)},[o.renderSlot(l.$slots,"default",{isOpen:l.isOpen,floating:o.unref(d)})],2),[[o.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.
|
|
3
|
+
"version": "0.1.14",
|
|
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": {
|
package/src/FzFloating.vue
CHANGED
|
@@ -5,7 +5,8 @@ import { FzFloatingProps, FzUseFloatingArgs } from './types'
|
|
|
5
5
|
|
|
6
6
|
const props = withDefaults(defineProps<FzFloatingProps>(), {
|
|
7
7
|
position: 'auto',
|
|
8
|
-
isOpen: false
|
|
8
|
+
isOpen: false,
|
|
9
|
+
teleport: false
|
|
9
10
|
})
|
|
10
11
|
|
|
11
12
|
const opener = ref(null)
|
|
@@ -63,12 +64,24 @@ const contentClass = computed(() => {
|
|
|
63
64
|
</div>
|
|
64
65
|
<slot name="opener-end"></slot>
|
|
65
66
|
<div
|
|
67
|
+
v-if="!teleport"
|
|
66
68
|
ref="content"
|
|
67
69
|
v-show="$slots.default && (!$slots.opener || ($slots.opener && isOpen))"
|
|
70
|
+
class="fz__floating__content bg-core-white fixed p-4 z-10"
|
|
68
71
|
:class="contentClass"
|
|
69
72
|
>
|
|
70
73
|
<slot :isOpen :floating></slot>
|
|
71
74
|
</div>
|
|
75
|
+
<Teleport to="body" v-if="teleport">
|
|
76
|
+
<div
|
|
77
|
+
ref="content"
|
|
78
|
+
v-show="$slots.default && (!$slots.opener || ($slots.opener && isOpen))"
|
|
79
|
+
class="fz__floating__content bg-core-white fixed p-4 z-10"
|
|
80
|
+
:class="contentClass"
|
|
81
|
+
>
|
|
82
|
+
<slot :isOpen :floating></slot>
|
|
83
|
+
</div>
|
|
84
|
+
</Teleport>
|
|
72
85
|
</div>
|
|
73
86
|
</template>
|
|
74
87
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { onBeforeUnmount, onMounted, Ref } from 'vue'
|
|
1
|
+
import { onBeforeUnmount, onMounted, Ref, watch } from 'vue'
|
|
2
2
|
|
|
3
3
|
function useClickOutside(
|
|
4
4
|
component: Ref<HTMLElement | undefined>,
|
|
@@ -27,12 +27,27 @@ function useClickOutside(
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
if (elementToListenClicksOn) {
|
|
31
|
+
watch(elementToListenClicksOn, (newVal: HTMLElement | undefined, oldVal: HTMLElement | undefined) => {
|
|
32
|
+
if (oldVal) {
|
|
33
|
+
oldVal.removeEventListener('click', listener)
|
|
34
|
+
}
|
|
35
|
+
newVal?.addEventListener('click', listener)
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
30
39
|
onMounted(() => {
|
|
31
|
-
(elementToListenClicksOn
|
|
40
|
+
if (!elementToListenClicksOn) {
|
|
41
|
+
document.addEventListener('click', listener)
|
|
42
|
+
}
|
|
32
43
|
})
|
|
33
44
|
|
|
34
45
|
onBeforeUnmount(() => {
|
|
35
|
-
(elementToListenClicksOn
|
|
46
|
+
if (elementToListenClicksOn) {
|
|
47
|
+
elementToListenClicksOn.value!.removeEventListener('click', listener)
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
document.removeEventListener('click', listener)
|
|
36
51
|
})
|
|
37
52
|
}
|
|
38
53
|
|
package/src/types.ts
CHANGED