@grucloud/bau-ui 0.302.0 → 0.303.0
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 +4 -4
- package/virtualList/index.d.ts +27 -0
- package/virtualList/index.js +1 -0
- package/virtualList/virtualList.js +111 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grucloud/bau-ui",
|
|
3
3
|
"description": "Components library built with Bau",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.303.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "BSD",
|
|
7
7
|
"keywords": [
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"directory": "bau-ui"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@grucloud/bau": "^0.
|
|
26
|
-
"@grucloud/bau-css": "^0.
|
|
25
|
+
"@grucloud/bau": "^0.303.0",
|
|
26
|
+
"@grucloud/bau-css": "^0.303.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@vitest/browser-playwright": "^5.1.2",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"vite": "^8.4.1",
|
|
35
35
|
"vitest": "^5.1.2"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "6cf5d24a9f48cb5f382cc8077d424299f441949f"
|
|
38
38
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare module "@grucloud/bau-ui/virtualList" {
|
|
2
|
+
type DefaultDesignProps = import("../constants").DefaultDesignProps;
|
|
3
|
+
type ComponentOption = import("../bau-ui").ComponentOption;
|
|
4
|
+
type ReadonlyState<T> = import("@grucloud/bau").ReadonlyState<T>;
|
|
5
|
+
|
|
6
|
+
export type VirtualListProps<T = any> = {
|
|
7
|
+
// The items, a state of an array
|
|
8
|
+
items: ReadonlyState<T[]>;
|
|
9
|
+
// The element of an item
|
|
10
|
+
Item: (item: T, index: number) => HTMLElement;
|
|
11
|
+
// The height of a row in px, the same for all; 40 by default
|
|
12
|
+
itemHeight?: number;
|
|
13
|
+
// The rows rendered before and after those in view; 5 by default
|
|
14
|
+
overscan?: number;
|
|
15
|
+
} & DefaultDesignProps &
|
|
16
|
+
import("@grucloud/bau").PropsAll<HTMLDivElement>;
|
|
17
|
+
|
|
18
|
+
type Option = {
|
|
19
|
+
itemHeight?: number;
|
|
20
|
+
overscan?: number;
|
|
21
|
+
} & ComponentOption;
|
|
22
|
+
|
|
23
|
+
export default function (
|
|
24
|
+
context: any,
|
|
25
|
+
option?: Option
|
|
26
|
+
): <T>(props: VirtualListProps<T>) => HTMLDivElement;
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./virtualList";
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export default function (context, options = {}) {
|
|
2
|
+
const { bau, css, window } = context;
|
|
3
|
+
const { div, ul, li } = bau.tags;
|
|
4
|
+
|
|
5
|
+
const className = css`
|
|
6
|
+
display: block;
|
|
7
|
+
height: 20rem;
|
|
8
|
+
overflow-y: auto;
|
|
9
|
+
position: relative;
|
|
10
|
+
box-sizing: border-box;
|
|
11
|
+
border-radius: var(--global-radius);
|
|
12
|
+
&.outline {
|
|
13
|
+
border: 1px solid var(--color-border-subtle);
|
|
14
|
+
}
|
|
15
|
+
&.solid {
|
|
16
|
+
background-color: var(--accent-lightest);
|
|
17
|
+
}
|
|
18
|
+
& > ul {
|
|
19
|
+
position: relative;
|
|
20
|
+
list-style: none;
|
|
21
|
+
margin: 0;
|
|
22
|
+
padding: 0;
|
|
23
|
+
}
|
|
24
|
+
& > ul > li {
|
|
25
|
+
position: absolute;
|
|
26
|
+
left: 0;
|
|
27
|
+
right: 0;
|
|
28
|
+
box-sizing: border-box;
|
|
29
|
+
display: flex;
|
|
30
|
+
align-items: center;
|
|
31
|
+
overflow: hidden;
|
|
32
|
+
}
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
return function VirtualList({
|
|
36
|
+
size = options.size ?? "md",
|
|
37
|
+
variant = options.variant ?? "outline",
|
|
38
|
+
color = options.color ?? "neutral",
|
|
39
|
+
// The items, a state of an array
|
|
40
|
+
items,
|
|
41
|
+
// The element of an item: Item(item, index)
|
|
42
|
+
Item,
|
|
43
|
+
// The height of a row in px, the same for all
|
|
44
|
+
itemHeight = options.itemHeight ?? 40,
|
|
45
|
+
// The rows rendered before and after those in view
|
|
46
|
+
overscan = options.overscan ?? 5,
|
|
47
|
+
...props
|
|
48
|
+
}) {
|
|
49
|
+
// The rows rendered, from start to end, excluded. A scroll renders them
|
|
50
|
+
// again only when the rows in view are not all rendered.
|
|
51
|
+
const start = bau.state(0);
|
|
52
|
+
const end = bau.state(0);
|
|
53
|
+
let observer;
|
|
54
|
+
|
|
55
|
+
const update = (element) => {
|
|
56
|
+
const first = Math.floor(element.scrollTop / itemHeight);
|
|
57
|
+
const last = Math.ceil(
|
|
58
|
+
(element.scrollTop + element.clientHeight) / itemHeight
|
|
59
|
+
);
|
|
60
|
+
if (first < start.val || last > end.val) {
|
|
61
|
+
start.val = Math.max(0, first - overscan);
|
|
62
|
+
end.val = last + overscan;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return div(
|
|
67
|
+
{
|
|
68
|
+
// Scrolled by the keyboard: arrows, page up and down, home, end
|
|
69
|
+
tabindex: 0,
|
|
70
|
+
...props,
|
|
71
|
+
class: [
|
|
72
|
+
"virtualList",
|
|
73
|
+
className,
|
|
74
|
+
color,
|
|
75
|
+
variant,
|
|
76
|
+
size,
|
|
77
|
+
options.class,
|
|
78
|
+
props.class,
|
|
79
|
+
],
|
|
80
|
+
onscroll: (event) => update(event.currentTarget),
|
|
81
|
+
bauMounted: ({ element }) => {
|
|
82
|
+
// The height of the list, known once in the document, and when it
|
|
83
|
+
// changes
|
|
84
|
+
observer = new window.ResizeObserver(() => update(element));
|
|
85
|
+
observer.observe(element);
|
|
86
|
+
},
|
|
87
|
+
bauUnmounted: () => observer.disconnect(),
|
|
88
|
+
},
|
|
89
|
+
() => {
|
|
90
|
+
const all = items.val;
|
|
91
|
+
const last = Math.min(end.val, all.length);
|
|
92
|
+
const rows = [];
|
|
93
|
+
for (let index = start.val; index < last; index++) {
|
|
94
|
+
rows.push(
|
|
95
|
+
li(
|
|
96
|
+
{
|
|
97
|
+
style: `top: ${index * itemHeight}px; height: ${itemHeight}px`,
|
|
98
|
+
// The position of the row in the whole list, for the screen
|
|
99
|
+
// readers
|
|
100
|
+
"aria-setsize": all.length,
|
|
101
|
+
"aria-posinset": index + 1,
|
|
102
|
+
},
|
|
103
|
+
Item(all[index], index)
|
|
104
|
+
)
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
return ul({ style: `height: ${all.length * itemHeight}px` }, rows);
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
}
|