mark-3 0.0.5 → 0.0.9
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/CHANGELOG.md +44 -0
- package/components/index.js +1 -0
- package/components/virtual-scroll.vue +255 -0
- package/composables/index.js +6 -0
- package/composables/use-css-metrics.ts +115 -0
- package/composables/use-event-listener.js +29 -7
- package/composables/use-outside-clicks.js +68 -0
- package/composables/use-pointer-swipe.js +138 -0
- package/composables/use-resize-observer.js +93 -0
- package/composables/use-scroll-event.js +25 -21
- package/composables/use-scroll-position.js +75 -0
- package/composables/use-swipeable-drawer.ts +139 -0
- package/helpers/array/chunk.ts +24 -0
- package/helpers/array/index.js +1 -0
- package/helpers/array/tests/chunk.test.js +41 -0
- package/helpers/number/compact.js +40 -0
- package/helpers/number/index.js +2 -0
- package/helpers/number/reduce.js +32 -0
- package/helpers/number/tests/compact.test.js +44 -0
- package/helpers/number/tests/reduce.test.js +30 -0
- package/helpers/string/encode-superscript.js +46 -0
- package/helpers/string/index.js +1 -0
- package/helpers/string/tests/encode-superscript.test.js +42 -0
- package/helpers/time/debounce.ts +33 -0
- package/helpers/time/index.js +1 -0
- package/helpers/time/tests/throttle.test.js +81 -0
- package/helpers/time/throttle.js +63 -0
- package/package.json +4 -1
- package/helpers/time/debounce.js +0 -29
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,50 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.0.9](https://github.com/ismailceylan/mark-3/compare/v0.0.8...v0.0.9) (2025-08-30)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **components:** add an option to ignore window size changes in virtual-scroll component ([2e27806](https://github.com/ismailceylan/mark-3/commit/2e2780680718824df973d050b109cf81ef657dc5))
|
|
11
|
+
* **components:** add support for two-way top scroll determination using the v-model method for virtual-scroll component ([123c89e](https://github.com/ismailceylan/mark-3/commit/123c89e6cc216305ac7d66e2c8d961d50cc7aaad))
|
|
12
|
+
* **helpers:** add new chunk method for arrays ([9150623](https://github.com/ismailceylan/mark-3/commit/9150623b9194830b5699c0863e8313fcde21c3d3))
|
|
13
|
+
* **helpers:** add new number/compact helper ([6d40850](https://github.com/ismailceylan/mark-3/commit/6d40850dcb174d32232e70e9139fe71f43a05d96))
|
|
14
|
+
* **helpers:** add new number/reduce helper ([1881c99](https://github.com/ismailceylan/mark-3/commit/1881c992547a42eb6b23aa60298d5abfc2ef0c56))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Bug Fixes
|
|
18
|
+
|
|
19
|
+
* **components:** add a watch getter to watch some ref elements in virtual-scroll component ([a74b0b7](https://github.com/ismailceylan/mark-3/commit/a74b0b7061a3fef9da8acc1177dda03f54edd9ae))
|
|
20
|
+
* **components:** add the immediate flag to process the already full item store immediately in virtual-scroll component ([a45a4da](https://github.com/ismailceylan/mark-3/commit/a45a4da63c3921714b0df8f86e33601956ad9a69))
|
|
21
|
+
* **components:** use a more secure value to ensure that the key value of the virtual scroll element is unique ([1db4081](https://github.com/ismailceylan/mark-3/commit/1db40812aadbbc186bcae6a7169a298060aa9ced))
|
|
22
|
+
* **components:** use the getter watch pattern to watch elements properly when they are sent as refs in virtual-scroll component ([6556ae8](https://github.com/ismailceylan/mark-3/commit/6556ae8abec1c2b5715fd914503d539bb1dd8e8a))
|
|
23
|
+
* **composables:** fix virtual-scroll artifact problems and add relative position to wrapper ([a7566c3](https://github.com/ismailceylan/mark-3/commit/a7566c352f0f83f8ab0df910d3e7d6517d0e5c3a))
|
|
24
|
+
* **composables:** virtual-scroll recalculate properly item heights when the screen size changed ([0749124](https://github.com/ismailceylan/mark-3/commit/07491247c28461c9cc65b49c98d992dfab07aeea))
|
|
25
|
+
|
|
26
|
+
### [0.0.8](https://github.com/ismailceylan/mark-3/compare/v0.0.7...v0.0.8) (2025-08-13)
|
|
27
|
+
|
|
28
|
+
### [0.0.7](https://github.com/ismailceylan/mark-3/compare/v0.0.6...v0.0.7) (2025-08-13)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Features
|
|
32
|
+
|
|
33
|
+
* **components:** add new virtual-scroll component ([c73a31d](https://github.com/ismailceylan/mark-3/commit/c73a31d6dc2ec37d8e70a15160bda495425712ca))
|
|
34
|
+
* **composables:** add new use-css-metrics composable ([8500859](https://github.com/ismailceylan/mark-3/commit/85008595788348956cd2f40cc0fff65d038672b0))
|
|
35
|
+
* **composables:** add new use-resize-observer composable ([6d110b5](https://github.com/ismailceylan/mark-3/commit/6d110b5c17facd4918a8a02a49d2f7644ada8f96))
|
|
36
|
+
* **composables:** add new use-scroll-position composable ([f847cb0](https://github.com/ismailceylan/mark-3/commit/f847cb0a9f19f09f82ab55d447227a7aa21516d5))
|
|
37
|
+
* **helpers:** add new time/throttle method ([60b8472](https://github.com/ismailceylan/mark-3/commit/60b8472fdb56119888c90446fe237b1fc9573f2b))
|
|
38
|
+
|
|
39
|
+
### [0.0.6](https://github.com/ismailceylan/mark-3/compare/v0.0.5...v0.0.6) (2025-08-09)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
### Features
|
|
43
|
+
|
|
44
|
+
* **composables:** add new use-outside-clicks composable ([6657b90](https://github.com/ismailceylan/mark-3/commit/6657b90c8330888d7f5d23b20cd85519bd44530b))
|
|
45
|
+
* **composables:** add new use-pointer-swipe composable ([c048b5c](https://github.com/ismailceylan/mark-3/commit/c048b5ce2aafe1f8d8a79aa7b1b37398a8e195e2))
|
|
46
|
+
* **composables:** add new use-swipeable-drawer composable ([f9e7c7a](https://github.com/ismailceylan/mark-3/commit/f9e7c7a0f757b04d63fef2ce078ed89d5e044dd8))
|
|
47
|
+
* **helpers:** add new string/encode-superscript method ([655fb59](https://github.com/ismailceylan/mark-3/commit/655fb59692486202e04da2f742c5ce0d432e3b71))
|
|
48
|
+
|
|
5
49
|
### [0.0.5](https://github.com/ismailceylan/mark-3/compare/v0.0.4...v0.0.5) (2025-08-03)
|
|
6
50
|
|
|
7
51
|
|
package/components/index.js
CHANGED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<!-- Mark-3/VirtualScroll -->
|
|
3
|
+
<div ref="container">
|
|
4
|
+
<div :style="{ transform: 'translateZ(0)', position: 'relative', minHeight: totalHeight + 'px' }">
|
|
5
|
+
<slot
|
|
6
|
+
v-for="{ item, index } of visibleItems"
|
|
7
|
+
:key="index"
|
|
8
|
+
:item
|
|
9
|
+
:index
|
|
10
|
+
:observe
|
|
11
|
+
:style="{ position: 'absolute', transform: 'translateY(' + offsets[ index ] + 'px)' }"
|
|
12
|
+
/>
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
<slot name="bottom" />
|
|
16
|
+
</div>
|
|
17
|
+
</template>
|
|
18
|
+
|
|
19
|
+
<script lang="ts" setup>
|
|
20
|
+
import { ref, watch, computed, reactive, nextTick } from "vue";
|
|
21
|
+
import { useCssMetrics, useEventListener, useResizeObserver, useScrollPosition } from "../composables";
|
|
22
|
+
import { debounce } from "../helpers/time";
|
|
23
|
+
import { clamp } from "../helpers/number";
|
|
24
|
+
|
|
25
|
+
const emit = defineEmits([ "threshold-reached" ]);
|
|
26
|
+
|
|
27
|
+
const scrollTop = defineModel( "scrollTop", { default: 0 });
|
|
28
|
+
|
|
29
|
+
const props = defineProps(
|
|
30
|
+
{
|
|
31
|
+
pageMode: Boolean,
|
|
32
|
+
dontWatchResizing: Boolean,
|
|
33
|
+
|
|
34
|
+
items: {
|
|
35
|
+
type: Array,
|
|
36
|
+
required: true
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
buffer: {
|
|
40
|
+
type: Number,
|
|
41
|
+
default: 5
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
minHeight: {
|
|
45
|
+
type: Number,
|
|
46
|
+
default: 20
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
itemGapClasses: {
|
|
50
|
+
type: String
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
threshold: {
|
|
54
|
+
type: Number,
|
|
55
|
+
default: 3
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const { max, floor } = Math;
|
|
60
|
+
const heights = ref<number[]>([]);
|
|
61
|
+
const identifiedItems = reactive<{ item: any, index: number }[]>([]);
|
|
62
|
+
const container = ref<HTMLDivElement>( null );
|
|
63
|
+
const metrics = useCssMetrics( props.itemGapClasses, [ "gap" ] as const, { throttle: 100 });
|
|
64
|
+
const isHeightsDirty = ref( false );
|
|
65
|
+
const dirtyItems = {}
|
|
66
|
+
|
|
67
|
+
const scrollableElement = computed(() =>
|
|
68
|
+
props.pageMode
|
|
69
|
+
? window
|
|
70
|
+
: container.value
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const scrollPos = useScrollPosition( scrollableElement, { throttle: 150 });
|
|
74
|
+
|
|
75
|
+
const offsets = computed<number[]>(() =>
|
|
76
|
+
{
|
|
77
|
+
const arr = new Array( heights.value.length );
|
|
78
|
+
let sum = 0;
|
|
79
|
+
|
|
80
|
+
for( let i = 0; i < heights.value.length; i++ )
|
|
81
|
+
{
|
|
82
|
+
arr[ i ] = sum;
|
|
83
|
+
sum += heights.value[ i ] || 0;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return arr;
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const totalHeight = computed(() =>
|
|
90
|
+
{
|
|
91
|
+
if( identifiedItems.length === 0 )
|
|
92
|
+
{
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const latestOffset = offsets.value[ offsets.value.length - 1 ] || 0;
|
|
97
|
+
const latestHeight = heights.value[ heights.value.length - 1 ] || 0;
|
|
98
|
+
const h = latestOffset + latestHeight;
|
|
99
|
+
|
|
100
|
+
if( h === metrics.value.gap * 2 )
|
|
101
|
+
{
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return h + 1;
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const startIndex = computed(() =>
|
|
109
|
+
{
|
|
110
|
+
let low = 0;
|
|
111
|
+
let high = offsets.value.length - 1;
|
|
112
|
+
let mid: number;
|
|
113
|
+
|
|
114
|
+
while( low <= high )
|
|
115
|
+
{
|
|
116
|
+
mid = floor(( low + high ) / 2 );
|
|
117
|
+
|
|
118
|
+
if( offsets.value[ mid ] === scrollPos.y.value )
|
|
119
|
+
{
|
|
120
|
+
return mid;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if( offsets.value[ mid ] < scrollPos.y.value )
|
|
124
|
+
{
|
|
125
|
+
low = mid + 1;
|
|
126
|
+
}
|
|
127
|
+
else
|
|
128
|
+
{
|
|
129
|
+
high = mid - 1;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return low - 1 < 0
|
|
134
|
+
? 0
|
|
135
|
+
: low - 1;
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const endIndex = computed(() =>
|
|
139
|
+
{
|
|
140
|
+
let total = 0;
|
|
141
|
+
let idx = startIndex.value;
|
|
142
|
+
|
|
143
|
+
while( idx < heights.value.length && total < window.innerHeight )
|
|
144
|
+
{
|
|
145
|
+
total += heights.value[ idx ];
|
|
146
|
+
idx++;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return max( 0, idx - 1 );
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const startIndexWithMargin = computed(() =>
|
|
153
|
+
max( 0, startIndex.value - props.buffer )
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const endIndexWithMargin = computed(() =>
|
|
157
|
+
clamp( endIndex.value + props.buffer, 0, heights.value.length )
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const visibleItems = computed(() =>
|
|
161
|
+
identifiedItems.slice( startIndexWithMargin.value, endIndexWithMargin.value )
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
const { observe } = useResizeObserver(( entries ) =>
|
|
165
|
+
{
|
|
166
|
+
entries.forEach( entry =>
|
|
167
|
+
{
|
|
168
|
+
const index = parseInt(( entry.target as HTMLElement ).dataset.index );
|
|
169
|
+
const newHeight = entry.contentRect.height;
|
|
170
|
+
const oldHeight = heights.value[ index ] || 0;
|
|
171
|
+
|
|
172
|
+
heights.value[ index ] = index in dirtyItems
|
|
173
|
+
// replace old height
|
|
174
|
+
? newHeight + metrics.value.gap
|
|
175
|
+
: max( newHeight + metrics.value.gap, oldHeight );
|
|
176
|
+
|
|
177
|
+
delete dirtyItems[ index ];
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
watch( scrollPos.y, () => scrollTop.value = scrollPos.y.value );
|
|
182
|
+
|
|
183
|
+
watch( scrollTop, async () =>
|
|
184
|
+
{
|
|
185
|
+
await nextTick();
|
|
186
|
+
|
|
187
|
+
if( scrollableElement.value instanceof HTMLElement )
|
|
188
|
+
{
|
|
189
|
+
scrollableElement.value.scrollTop = scrollTop.value;
|
|
190
|
+
}
|
|
191
|
+
else if( scrollableElement.value === window )
|
|
192
|
+
{
|
|
193
|
+
window.scrollTo( 0, scrollTop.value );
|
|
194
|
+
}
|
|
195
|
+
}, { immediate: true });
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
watch( endIndex, () =>
|
|
199
|
+
{
|
|
200
|
+
if(( identifiedItems.length - 1 ) - endIndex.value <= props.threshold )
|
|
201
|
+
{
|
|
202
|
+
emit( "threshold-reached" );
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
watch(() => props.items, items =>
|
|
207
|
+
{
|
|
208
|
+
const normalizedItems = items.map(( item, i ) =>
|
|
209
|
+
({
|
|
210
|
+
item,
|
|
211
|
+
index: i
|
|
212
|
+
}));
|
|
213
|
+
|
|
214
|
+
identifiedItems.length = 0;
|
|
215
|
+
identifiedItems.push( ...normalizedItems );
|
|
216
|
+
|
|
217
|
+
heights.value = items.map(( _, i ) =>
|
|
218
|
+
heights.value[ i ] ?? props.minHeight
|
|
219
|
+
);
|
|
220
|
+
}, { immediate: true, deep: true });
|
|
221
|
+
|
|
222
|
+
watch( isHeightsDirty, () =>
|
|
223
|
+
{
|
|
224
|
+
if( isHeightsDirty.value === false )
|
|
225
|
+
{
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
visibleItems.value.forEach(({ index }) =>
|
|
230
|
+
{
|
|
231
|
+
const itemEl = container.value.querySelector( "[data-index='" + index + "']" );
|
|
232
|
+
|
|
233
|
+
if( itemEl === null )
|
|
234
|
+
{
|
|
235
|
+
return dirtyItems[ index ] = true;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
heights.value[ index ] = itemEl.getBoundingClientRect().height + metrics.value.gap;
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
isHeightsDirty.value = false;
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
if( props.dontWatchResizing === false )
|
|
245
|
+
{
|
|
246
|
+
useEventListener( window, "resize", debounce( resetHeights, 100 ), { passive: true });
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function resetHeights()
|
|
250
|
+
{
|
|
251
|
+
heights.value = identifiedItems.map(() => props.minHeight );
|
|
252
|
+
isHeightsDirty.value = true;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
</script>
|
package/composables/index.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export { default as useMediaQuery } from "./use-media-query.js";
|
|
2
|
+
export { default as useCssMetrics } from "./use-css-metrics.ts";
|
|
2
3
|
export { default as useScrollEvent } from "./use-scroll-event.js";
|
|
4
|
+
export { default as usePointerSwipe } from "./use-pointer-swipe.js";
|
|
5
|
+
export { default as useOutsideClicks } from "./use-outside-clicks.js";
|
|
3
6
|
export { default as useEventListener } from "./use-event-listener.js";
|
|
4
7
|
export { default as useResponsiveness } from "./use-responsiveness.js";
|
|
8
|
+
export { default as useResizeObserver } from "./use-resize-observer.js";
|
|
9
|
+
export { default as useScrollPosition } from "./use-scroll-position.js";
|
|
10
|
+
export { default as useSwipeableDrawer } from "./use-swipeable-drawer.ts";
|
|
5
11
|
export { default as useIntersectionObserver } from "./use-intersection-observer.js";
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { ref, onMounted, onUnmounted, Ref } from "vue";
|
|
2
|
+
import { useResizeObserver } from ".";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a reactive reference containing numeric values of specified CSS properties
|
|
6
|
+
* for elements with given classes. It observes size changes and updates the metrics
|
|
7
|
+
* accordingly.
|
|
8
|
+
*
|
|
9
|
+
* @template P
|
|
10
|
+
* @param classes - CSS classes to apply to the ghost element used for measurement.
|
|
11
|
+
* @param properties - Array of CSS property names to track.
|
|
12
|
+
* @param options - Optional configuration object.
|
|
13
|
+
* @returns A reactive reference with the current values of the specified CSS properties.
|
|
14
|
+
*/
|
|
15
|
+
export default function useCssMetrics<P extends readonly string[]>(
|
|
16
|
+
classes: string|string[],
|
|
17
|
+
properties: P, { throttle = 0 } = {}
|
|
18
|
+
): Ref<{[ K in typeof properties[ number ]]: number }>
|
|
19
|
+
{
|
|
20
|
+
const metrics = ref({});
|
|
21
|
+
let ghostEl = null;
|
|
22
|
+
let widthEl = null;
|
|
23
|
+
let observer = null;
|
|
24
|
+
const { observe, unobserve, disconnect } = useResizeObserver( updateMetrics, { throttle });
|
|
25
|
+
|
|
26
|
+
metrics.value = Object.fromEntries( properties.map( prop =>
|
|
27
|
+
[ prop, 0 ]
|
|
28
|
+
));
|
|
29
|
+
|
|
30
|
+
if( classes === undefined || ( Array.isArray( classes ) && classes.length === 0 ))
|
|
31
|
+
{
|
|
32
|
+
return metrics;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
onMounted( init );
|
|
36
|
+
onUnmounted( destroy );
|
|
37
|
+
|
|
38
|
+
function init()
|
|
39
|
+
{
|
|
40
|
+
if( ghostEl )
|
|
41
|
+
{
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
ghostEl = document.createElement( "div" );
|
|
46
|
+
widthEl = document.createElement( "div" );
|
|
47
|
+
|
|
48
|
+
const ghostStyle = ghostEl.style;
|
|
49
|
+
const widthStyle = widthEl.style;
|
|
50
|
+
|
|
51
|
+
ghostStyle.position = "absolute";
|
|
52
|
+
ghostStyle.top = "0";
|
|
53
|
+
ghostStyle.left = "0";
|
|
54
|
+
ghostStyle.width = "100%";
|
|
55
|
+
ghostStyle.height = "100%";
|
|
56
|
+
ghostStyle.visibility = "hidden";
|
|
57
|
+
ghostStyle.pointerEvents = "none";
|
|
58
|
+
|
|
59
|
+
widthStyle.position = "absolute";
|
|
60
|
+
widthStyle.visibility = "hidden";
|
|
61
|
+
widthStyle.pointerEvents = "none";
|
|
62
|
+
|
|
63
|
+
ghostEl.appendChild( widthEl );
|
|
64
|
+
|
|
65
|
+
const cls = Array.isArray( classes )
|
|
66
|
+
? classes
|
|
67
|
+
: classes.split( " " );
|
|
68
|
+
|
|
69
|
+
ghostEl.classList.add( ...cls );
|
|
70
|
+
widthEl.classList.add( ...cls );
|
|
71
|
+
|
|
72
|
+
document.body.appendChild( ghostEl );
|
|
73
|
+
|
|
74
|
+
observe( ghostEl );
|
|
75
|
+
updateMetrics();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function updateMetrics()
|
|
79
|
+
{
|
|
80
|
+
if( ! ghostEl )
|
|
81
|
+
{
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const computed = getComputedStyle( ghostEl );
|
|
86
|
+
const computed2 = getComputedStyle( widthEl );
|
|
87
|
+
|
|
88
|
+
properties.forEach( prop =>
|
|
89
|
+
{
|
|
90
|
+
const source = [ "width", "height" ].includes( prop )
|
|
91
|
+
? computed2
|
|
92
|
+
: computed;
|
|
93
|
+
|
|
94
|
+
metrics.value[ prop ] = parseFloat( source.getPropertyValue( prop )) || 0;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function destroy()
|
|
99
|
+
{
|
|
100
|
+
if( observer && ghostEl )
|
|
101
|
+
{
|
|
102
|
+
unobserve( ghostEl );
|
|
103
|
+
disconnect();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if( ghostEl && ghostEl.parentElement )
|
|
107
|
+
{
|
|
108
|
+
ghostEl.parentElement.removeChild( ghostEl );
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
ghostEl = null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return metrics;
|
|
115
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { onMounted, onUnmounted, getCurrentInstance } from "vue";
|
|
1
|
+
import { isRef, unref, onMounted, onUnmounted, getCurrentInstance } from "vue";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A composition function that adds a DOM event listener to the given target with the
|
|
@@ -8,15 +8,15 @@ import { onMounted, onUnmounted, getCurrentInstance } from "vue";
|
|
|
8
8
|
* composition function is called outside of a component, then the event listener is
|
|
9
9
|
* added immediately and must be removed manually by calling the returned function.
|
|
10
10
|
*
|
|
11
|
-
* @param {
|
|
11
|
+
* @param {EventTarget|Ref<EventTarget>} maybeRefTarget - The target element to add the event listener to.
|
|
12
12
|
* @param {string} eventName - The name of the event to add a listener for.
|
|
13
13
|
* @param {function} callBack - The callback function to call when the event happens.
|
|
14
|
-
* @param {
|
|
14
|
+
* @param {EventListenerOptions} [options] - The options object to pass to addEventListener.
|
|
15
15
|
* @returns {function} - A function that can be called to remove the event listener.
|
|
16
16
|
*/
|
|
17
|
-
export default function useEventListener(
|
|
17
|
+
export default function useEventListener( maybeRefTarget, eventName, callBack, options )
|
|
18
18
|
{
|
|
19
|
-
if( getCurrentInstance())
|
|
19
|
+
if( getCurrentInstance() && isRef( maybeRefTarget ))
|
|
20
20
|
{
|
|
21
21
|
onMounted( listen );
|
|
22
22
|
onUnmounted( stop );
|
|
@@ -28,13 +28,35 @@ export default function useEventListener( target, eventName, callBack, options )
|
|
|
28
28
|
|
|
29
29
|
function listen()
|
|
30
30
|
{
|
|
31
|
-
|
|
31
|
+
const el = unref( maybeRefTarget );
|
|
32
|
+
|
|
33
|
+
if( el && el.addEventListener )
|
|
34
|
+
{
|
|
35
|
+
el.addEventListener( eventName, callBack, options );
|
|
36
|
+
}
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
function stop()
|
|
35
40
|
{
|
|
36
|
-
|
|
41
|
+
const el = unref( maybeRefTarget );
|
|
42
|
+
|
|
43
|
+
if( el && el.addEventListener )
|
|
44
|
+
{
|
|
45
|
+
el.removeEventListener( eventName, callBack );
|
|
46
|
+
}
|
|
37
47
|
}
|
|
38
48
|
|
|
39
49
|
return stop;
|
|
40
50
|
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @template T
|
|
54
|
+
* @typedef {import("vue").Ref<T>} Ref
|
|
55
|
+
*/
|
|
56
|
+
/**
|
|
57
|
+
* @typedef EventListenerOptions
|
|
58
|
+
* @type {object}
|
|
59
|
+
* @property {boolean} [capture=false] true for capturing, false for bubbling
|
|
60
|
+
* @property {boolean} [once=false] true for run the event once or false for keep it persistent
|
|
61
|
+
* @property {boolean} [passive=false] true for passive, false for not passive
|
|
62
|
+
*/
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useEventListener } from ".";
|
|
2
|
+
import { isArray } from "../helpers/types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Watches for clicks outside of the given element and calls the
|
|
6
|
+
* callback when it happens.
|
|
7
|
+
*
|
|
8
|
+
* This is useful for things like dropdowns, where you want to close
|
|
9
|
+
* the dropdown when the user clicks outside of it.
|
|
10
|
+
*
|
|
11
|
+
* This is a composition function that uses useEventListener under the
|
|
12
|
+
* hood. So, it will automatically remove the event listener when the
|
|
13
|
+
* component is unmounted.
|
|
14
|
+
*
|
|
15
|
+
* It also returns a function that can be used to remove the event
|
|
16
|
+
* listener. This is useful for when you want to remove the event
|
|
17
|
+
* listener when the extraordinary circumstances arise.
|
|
18
|
+
*
|
|
19
|
+
* @typedef {import('vue').Ref<Element>} ElementRef
|
|
20
|
+
* @param {ElementRef|ElementRef[]} elRefs - The elements to watch for clicks outside of.
|
|
21
|
+
* @param {function} callback - The callback to call when a click is outside of the element.
|
|
22
|
+
* @param {object} options - The options object.
|
|
23
|
+
* @property {string} options.on - The event to listen for.
|
|
24
|
+
* @returns {function}
|
|
25
|
+
* @example
|
|
26
|
+
* const elRef = ref();
|
|
27
|
+
*
|
|
28
|
+
* useOutsideClick( elRef, () =>
|
|
29
|
+
* console.log( "Clicked outside!" )
|
|
30
|
+
* );
|
|
31
|
+
* @example
|
|
32
|
+
* const elRef = ref();
|
|
33
|
+
*
|
|
34
|
+
* useOutsideClick(
|
|
35
|
+
* elRef,
|
|
36
|
+
* () => console.log( "Clicked outside!" ),
|
|
37
|
+
* {
|
|
38
|
+
* on: "mousedown"
|
|
39
|
+
* }
|
|
40
|
+
* );
|
|
41
|
+
*/
|
|
42
|
+
export default function useOutsideClick( elRefs, callback, { on = "click" } = {})
|
|
43
|
+
{
|
|
44
|
+
if( ! isArray( elRefs ))
|
|
45
|
+
{
|
|
46
|
+
elRefs = [ elRefs ];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return useEventListener( document, on, e =>
|
|
50
|
+
{
|
|
51
|
+
if( elRefs.length === 0 )
|
|
52
|
+
{
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
for( let el of elRefs )
|
|
57
|
+
{
|
|
58
|
+
el = el.value;
|
|
59
|
+
|
|
60
|
+
if( el?.contains( e.target ))
|
|
61
|
+
{
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
callback( e );
|
|
67
|
+
});
|
|
68
|
+
}
|