@v1nt1248/3nclient-lib 0.0.11 → 0.0.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.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "private": false,
4
4
  "license": "AGPL-3.0-or-later",
5
5
  "author": "v1nt1248",
6
- "version": "0.0.11",
6
+ "version": "0.0.12",
7
7
  "description": "Library for 3NWeb clients",
8
8
  "type": "module",
9
9
  "files": [
@@ -21,6 +21,8 @@ import Ui3nMenu from './ui3n-menu.vue'
21
21
  import type { Ui3nMenuProps, Ui3nMenuEmits, Ui3nMenuSlots } from './ui3n-menu.vue'
22
22
  import Ui3nList from './ui3n-list.vue'
23
23
  import type { Ui3nListProps, Ui3nListEmits, Ui3nListSlots } from './ui3n-list.vue'
24
+ import Ui3nVirtualScroll from './ui3n-virtual-scroll.vue'
25
+ import type { Ui3nVirtualScrollProps, Ui3nVirtualScrollSlots } from './ui3n-virtual-scroll.vue'
24
26
 
25
27
  export {
26
28
  Ui3nIcon,
@@ -60,4 +62,7 @@ export {
60
62
  Ui3nListProps,
61
63
  Ui3nListEmits,
62
64
  Ui3nListSlots,
65
+ Ui3nVirtualScroll,
66
+ Ui3nVirtualScrollProps,
67
+ Ui3nVirtualScrollSlots,
63
68
  }
@@ -0,0 +1,108 @@
1
+ <script lang="ts" setup generic="T extends { id?: string }">
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ import { computed, onMounted, ref } from 'vue'
4
+
5
+ export interface Ui3nVirtualScrollProps<T> {
6
+ items: T[];
7
+ minChildHeight: number;
8
+ renderAhread?: number;
9
+ }
10
+
11
+ export interface Ui3nVirtualScrollSlots<T> {
12
+ item: (item: T, index: number) => any;
13
+ }
14
+
15
+ const props = withDefaults(defineProps<Ui3nVirtualScrollProps<T>>(), {
16
+ renderAhread: 20,
17
+ })
18
+ defineSlots<Ui3nVirtualScrollSlots<T>>()
19
+
20
+ const scrollTop = ref(0)
21
+ const animationFrame = ref<number | undefined>()
22
+ const wrapElement = ref<HTMLDivElement | null>(null)
23
+ const wrapElementHeight = computed(() => wrapElement.value
24
+ ? wrapElement.value.clientHeight
25
+ : 0
26
+ )
27
+ const itemCount = computed(() => props.items.length)
28
+ const totalHeight = computed(() => itemCount.value * props.minChildHeight)
29
+ const startNode = computed(() => {
30
+ const val = Math.floor(scrollTop.value / props.minChildHeight) - props.renderAhread
31
+ return Math.max(0, val)
32
+ })
33
+ const visibleNodeCount = computed(() => {
34
+ const val = Math.ceil(wrapElementHeight.value / props.minChildHeight) +2 * props.renderAhread
35
+ return Math.min(itemCount.value - startNode.value, val)
36
+ })
37
+ const offsetY = computed(() => startNode.value * props.minChildHeight)
38
+ const visibleChildren = computed(() => props.items.slice(startNode.value, startNode.value + visibleNodeCount.value))
39
+
40
+ const onScroll = (event: Event) => {
41
+ if (animationFrame.value) {
42
+ cancelAnimationFrame(animationFrame.value)
43
+ }
44
+
45
+ animationFrame.value = requestAnimationFrame(() => scrollTop.value = (event.target as HTMLDivElement).scrollTop)
46
+ }
47
+
48
+ onMounted(() => {
49
+ if (wrapElement.value) {
50
+ scrollTop.value = wrapElement.value.scrollTop
51
+ }
52
+ })
53
+ </script>
54
+
55
+ <template>
56
+ <div
57
+ ref="wrapElement"
58
+ class="ui3n-virtual-scroll"
59
+ @scroll="onScroll"
60
+ >
61
+ <div
62
+ class="ui3n-virtual-scroll__viewport"
63
+ :style="{ height: `${totalHeight}px` }"
64
+ >
65
+ <div
66
+ class="ui3n-virtual-scroll__content"
67
+ :style="{ transform: `translateY(${offsetY}px)` }"
68
+ >
69
+ <div
70
+ v-for="(item, index) in visibleChildren"
71
+ :key="item.id"
72
+ :data-sid="item.id ?? index"
73
+ class="ui3n-virtual-scroll__item"
74
+ >
75
+ <slot
76
+ name="item"
77
+ :item="item"
78
+ :index="startNode + index"
79
+ />
80
+ </div>
81
+ </div>
82
+ </div>
83
+ </div>
84
+ </template>
85
+
86
+ <style lang="scss" scoped>
87
+ .ui3n-virtual-scroll {
88
+ position: relative;
89
+ width: 100%;
90
+ height: 100%;
91
+ overflow-y: auto;
92
+
93
+ &__viewport {
94
+ position: relative;
95
+ overflow: hidden;
96
+ will-change: transform;
97
+ }
98
+
99
+ &__content {
100
+ will-change: transform;
101
+ }
102
+
103
+ &__item {
104
+ position: relative;
105
+ min-height: 16px;
106
+ }
107
+ }
108
+ </style>