-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathuseFileListWidth.ts
More file actions
64 lines (54 loc) · 1.53 KB
/
useFileListWidth.ts
File metadata and controls
64 lines (54 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*!
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { computed, readonly, ref } from '@vue/reactivity'
/** The element we observe */
let element: HTMLElement | undefined
/** The current width of the element */
const width = ref<number>(0)
const isWide = computed(() => width.value >= 1024)
const isMedium = computed(() => width.value >= 512 && width.value < 1024)
const isNarrow = computed(() => width.value < 512)
const observer = new ResizeObserver(([el]) => {
if (!el) {
return
}
const contentBoxSize = el.contentBoxSize?.[0]
if (contentBoxSize) {
// use the newer `contentBoxSize` property if available
width.value = contentBoxSize.inlineSize
} else {
// fall back to `contentRect`
width.value = el.contentRect.width
}
})
/**
* Update the observed element if needed and reconfigure the observer
*/
function updateObserver() {
const el = document.querySelector<HTMLElement>('#app-content-vue') ?? document.body
if (el !== element) {
// if already observing: stop observing the old element
if (element) {
observer.unobserve(element)
}
// observe the new element
observer.observe(el)
element = el
}
}
/**
* Get the reactive width of the file list
*/
export function useFileListWidth() {
// Update the observer in setup context so we already have an initial value
updateObserver()
return {
width: readonly(width),
isWide,
isMedium,
isNarrow,
}
}