Skip to content

Commit 8eb88cf

Browse files
committed
fix: 2nd bug
1 parent 1fb7b49 commit 8eb88cf

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

KNOWN-ISSUES.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
### Using @sveltejs/package > 2.3.0 will generate "ts" rather than transpiled outputs, resulting in ts files being pushed to npm.
2-
32
For now it breaks Svelte preview REPL as it can't compile ts on the fly
3+
4+
5+
6+
[ ] Problem with positioning
7+
[ ] Problem with visible index range events
8+
[ ] Problem with offset scrolling
9+
[ ] Problem with focus positioning

README.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ The component accepts the following properties
9999
| width | `number` or `string`\* || Width of List. This property will determine the number of rendered items when scrollDirection is `'horizontal'`. |
100100
| height | `number` or `string`\* || Height of List. This property will determine the number of rendered items when scrollDirection is `'vertical'`. |
101101
| model | any[] || the model, the data for the items to display in the list. |
102-
| modelCount | `number` || The number of items you want to render. |
103-
| itemSize | `number | number[] | (index: number) => number` || Either a fixed height/width (depending on the scrollDirection), an array containing the heights of all the items in your list, or a function that returns the height of an item given its index: `(index: number): number` |
102+
| modelCount | `number` || The number of items you want to render. |
103+
| sizeCalculator | `(index: number, item:any) => number or SizingCalculatorFn` || A function that returns the size (height or width) of the row/column being rendered. the output of this function is used for scrollbar positioning and is passed to the `vl_slot` |
104104
| row | (r:RowAttributes) => SnippetResult || Snippet called to render every item, see description below. |
105105
| scrollDirection | `string` | | Whether the list should scroll vertically or horizontally. One of `'vertical'` (default) or `'horizontal'`. |
106-
| scrollOffset | `number` | | Can be used to control the scroll offset; Also useful for setting an initial scroll offset. |
106+
| scrollToOffset | `number` | | Can be used to control the scroll offset; Also useful for setting an initial scroll offset. |
107107
| scrollToIndex | `number` | | Item index to scroll to (by forcefully scrolling if necessary). |
108108
| scrollToAlignment | `string` | | Used in combination with `scrollToIndex`, this prop controls the alignment of the scrolled to item. One of: `'start'`, `'center'`, `'end'` or `'auto'`. Use `'start'` to always align items to the top of the container and `'end'` to align them bottom. Use `'center`' to align them in the middle of the container. `'auto'` scrolls the least amount possible to ensure that the specified `scrollToIndex` item is fully visible. |
109109
| scrollToBehaviour | `string` | | Used in combination with `scrollToIndex`, this prop controls the behaviour of the scrolling. One of: `'auto'`, `'smooth'` or `'instant'` (default). |
110-
| windowOverPadding | `number` | | Number of extra buffer items to render above/below the visible items. Tweaking this can help reduce scroll flickering on certain browsers/devices. |
110+
| windowOverPadding | `number` | | Number of extra items to render above/below the visible items. Tweaking this can help reduce scroll flickering on certain browsers and devices. |
111111
| estimatedItemSize | `number` | | Used to estimate the total size of the list before all of its items have actually been measured. The estimated total height is progressively adjusted as items are rendered. |
112112
| getKey | `(index: number) => any` | | Function that returns the key of an item in the list, which is used to uniquely identify an item. This is useful for dynamic data coming from a database or similar. By default, it's using the item's index. |
113113

@@ -116,28 +116,22 @@ _\** `model` is stored, `items` are rendered
116116

117117
### Snippets
118118

119-
- `header` - a snippet for the elements that should appear at the top of the list
120-
- `footer` - a snippet for the elements that should appear at the bottom of the list
121-
- `slot` - a required snipper property called to render each row of the list with the signature `slot(r:RowAttributes<T>)`
119+
- `header` - an optional snippet for the elements that should appear at the top of the list, typically used in table mode
120+
- `footer` - an optional snippet for the elements that should appear at the bottom of the list, typically used in table mode
121+
- `vl_slot` - a required snipper property called to render each row of the list with the signature `vl_slot({index, item, size?})`. `size` is only present if a custom **sizeCalculator** is in place.
122+
122123

123-
```typescript
124-
export interface RowAttributes<T> {
125-
item: T // the element from the model array to be rendered
126-
index: number; // Item index
127-
style: string; // Item style, must be applied to the slot (look above for example)
128-
}
129-
```
130124

131125
for instance,
132126

133127
```svelte
134-
<VirtualList ...>
135-
{#snippet slot({ item, style, index }:RowAttributes)}
136-
<div class="row" {style}>
137-
Item: {item}, Row: #{index}
128+
<VirtualList items={myModel} style="height:600px">
129+
{#snippet vl_slot({ index, item }: VLSlotSignature)}
130+
<div style="border: 1px solid rgb(204, 204, 204);">
131+
Index:{index} Content:{item.text}
138132
</div>
139133
{/snippet}
140-
</VirtualList>
134+
</VirtualList>
141135
```
142136

143137
### Events

TODO.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[x] Add class and style attributes - implemeted via {...attributes}
33
[x] store the model inside the component
44
[x] Rename the row snippet to something else -> slot
5-
[ ] add disabled
6-
[ ] move the css style logic out of the component row({ item, style, index })
5+
[x] add disabled
6+
[x] move the css style logic out of the component row({ item, style, index })
77
[x] considering, items={data} itemCount={data.length} itemSize={50}; model is data, items is the view should be transformed into model={data} modelCount={data.length} itemSize={50}, {@render item(...)}
88
[x] implement minimization for the npm package

src/lib/new/VirtualListNew.svelte

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,13 @@
172172
173173
// this is index -> height or width
174174
const sizes: number[] = $derived.by(() => {
175-
const p = items.map((item, index) => {
175+
return items.map((item, index) => {
176176
let s = sizingCalculator?.(index, item);
177177
if (s !== undefined) return s;
178178
s = runtimeSizes[index];
179179
if (s !== undefined) return s;
180180
return avgSizeInPx;
181181
});
182-
183-
return p;
184182
});
185183
186184
// this is index -> offset
@@ -201,10 +199,10 @@
201199
for (let index = startIdx; index <= end2; index++) {
202200
// console.log(index);
203201
const item = items[index];
204-
if (!item) {
205-
break;
202+
if (item) {
203+
// console.log(sizes[index])
204+
r.push({ item, index: index, size: sizes[index] });
206205
}
207-
r.push({ item, index: startIdx + index });
208206
}
209207
return r;
210208
});

src/routes/examples/variablesizing/code.svelte

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
<script lang="ts">
2-
import { type VLSlotSignature } from 'svelte-virtuallists';
2+
import { type SizingCalculatorFn, type VLSlotSignature } from 'svelte-virtuallists';
33
import VirtualList from 'svelte-virtuallists/new/VirtualListNew.svelte';
44
55
const myModel = new Array(10000).fill(1).map((v, i) => {
66
return {
7-
text: 'Item ' + i + ' item ' + i
7+
text: 'Item ' + i
88
};
99
});
1010
11-
let rowHeights = $state();
12-
1311
function randomize() {
14-
rowHeights = (index: number, item: any) => Math.round(Math.random() * (155 - 30) + 30);
12+
calculator = (index: number, item: any) => Math.round(Math.random() * (155 - 30) + 30);
1513
}
1614
1715
function sameSize() {
18-
rowHeights = (index: number, item: any) => 25;
16+
calculator = (index: number, item: any) => 25;
1917
}
2018
19+
// @ts-expect-error undefined
20+
let calculator: SizingCalculatorFn = $state();
21+
2122
randomize();
2223
</script>
2324

2425
<h2>Horizontal</h2>
2526

26-
<VirtualList items={myModel} style="width:100%" isHorizontal={true}>
27+
<VirtualList items={myModel} style="width:100%" isHorizontal={true} sizingCalculator={calculator}>
2728
{#snippet vl_slot({ index, item, size }: VLSlotSignature)}
2829
<div style="border: 1px solid rgb(204, 204, 204); width: {size}px;">
2930
{item.text}
@@ -33,10 +34,10 @@
3334

3435
<h2>Vertical</h2>
3536

36-
<VirtualList items={myModel} style="height:600px">
37+
<VirtualList items={myModel} style="height:600px" sizingCalculator={calculator}>
3738
{#snippet vl_slot({ index, item, size }: VLSlotSignature)}
3839
<div style="border: 1px solid rgb(204, 204, 204); line-height: {size}px;">
39-
{item.text}
40+
Index:{index} Content:{item.text}
4041
</div>
4142
{/snippet}
4243
</VirtualList>

0 commit comments

Comments
 (0)