@stonecrop/utilities 0.2.9 → 0.2.10
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stonecrop/utilities",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.10",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"@typescript-eslint/eslint-plugin": "^7.6.0",
|
|
38
38
|
"@typescript-eslint/parser": "^7.6.0",
|
|
39
39
|
"@vitejs/plugin-vue": "^5.0.4",
|
|
40
|
-
"@vueuse/core": "^9.
|
|
40
|
+
"@vueuse/core": "^10.9.0",
|
|
41
41
|
"cypress": "^12.11.0",
|
|
42
42
|
"eslint": "^8.40.0",
|
|
43
43
|
"eslint-config-prettier": "^8.8.0",
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ConfigurableWindow,
|
|
3
|
+
type MaybeComputedElementRef,
|
|
4
|
+
type MaybeRefOrGetter,
|
|
5
|
+
defaultWindow,
|
|
6
|
+
unrefElement,
|
|
7
|
+
useEventListener,
|
|
8
|
+
} from '@vueuse/core'
|
|
9
|
+
import { ref, watch } from 'vue'
|
|
10
|
+
|
|
11
|
+
export interface UseElementVisibilityOptions extends ConfigurableWindow {
|
|
12
|
+
scrollTarget?: MaybeRefOrGetter<HTMLElement | undefined | null>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Tracks the visibility of an element within the viewport.
|
|
17
|
+
*
|
|
18
|
+
* Compatibility version to get Stonecrop keyboard navigation to work. This function is a copy of the
|
|
19
|
+
* `useElementVisibility` function from VueUse v9.13.0, with the `IntersectionObserver` API removed.
|
|
20
|
+
*
|
|
21
|
+
* This version uses the `getBoundingClientRect` method to determine if an element is visible
|
|
22
|
+
* in the viewport. This is less performant than the `IntersectionObserver` API, but it is
|
|
23
|
+
* more compatible.
|
|
24
|
+
*
|
|
25
|
+
* Note: the newer versions of the VueUse dependencies imported here are sufficient for this composable.
|
|
26
|
+
* (Last verified: v10.9.0 on May 2, 2024)
|
|
27
|
+
*
|
|
28
|
+
* @see https://v9-13-0.vueuse.org/core/useElementVisibility
|
|
29
|
+
* @param element
|
|
30
|
+
* @param options
|
|
31
|
+
*/
|
|
32
|
+
export function useElementVisibility(
|
|
33
|
+
element: MaybeComputedElementRef,
|
|
34
|
+
{ window = defaultWindow, scrollTarget }: UseElementVisibilityOptions = {}
|
|
35
|
+
) {
|
|
36
|
+
const elementIsVisible = ref(false)
|
|
37
|
+
|
|
38
|
+
const testBounding = () => {
|
|
39
|
+
if (!window) return
|
|
40
|
+
|
|
41
|
+
const document = window.document
|
|
42
|
+
const el = unrefElement(element)
|
|
43
|
+
if (!el) {
|
|
44
|
+
elementIsVisible.value = false
|
|
45
|
+
} else {
|
|
46
|
+
const rect = el.getBoundingClientRect()
|
|
47
|
+
elementIsVisible.value =
|
|
48
|
+
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
|
|
49
|
+
rect.left <= (window.innerWidth || document.documentElement.clientWidth) &&
|
|
50
|
+
rect.bottom >= 0 &&
|
|
51
|
+
rect.right >= 0
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
watch(
|
|
56
|
+
() => unrefElement(element),
|
|
57
|
+
() => testBounding(),
|
|
58
|
+
{ immediate: true, flush: 'post' }
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
if (window) {
|
|
62
|
+
useEventListener(scrollTarget || window, 'scroll', testBounding, {
|
|
63
|
+
capture: false,
|
|
64
|
+
passive: true,
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return elementIsVisible
|
|
69
|
+
}
|