Skip to content

Commit 8aa5ee2

Browse files
committed
fix: bug fix for cellCount !== 1 in InfiniteGrid + additional exports
1 parent c2e12c5 commit 8aa5ee2

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/lib/components/generic/InfiniteGrid.svelte

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<script>
2+
import { tick } from 'svelte';
3+
24
import { spring } from 'svelte/motion';
35
import { derived, writable } from 'svelte/store';
46
@@ -9,23 +11,33 @@
911
export let get;
1012
export let stiffness = 0.065;
1113
export let damping = 0.9;
14+
export let useCache = true;
15+
export let idKey = undefined;
1216
1317
export const move = (amount) => {
1418
index = Math.max(0, Math.min(itemCount - 1, index + amount));
1519
};
1620
21+
const forceUpdate = writable(false);
22+
export const triggerUpdate = async () => {
23+
await tick();
24+
forceUpdate.set(true);
25+
await tick();
26+
forceUpdate.set(false);
27+
};
28+
1729
const getCached = (index) => $visibleData.find(({ index: i }) => i === index)?.data || get(index);
1830
1931
let inRange = [-Infinity, Infinity];
2032
const initialized = writable(false);
2133
const dim = writable({ w: 0, h: 0 });
2234
const offset = spring(0, { stiffness, damping });
23-
2435
export const visibleData = derived(
25-
[dim, offset, initialized],
26-
([{ w, h }, $o, $initialized]) => {
36+
[dim, offset, initialized, forceUpdate],
37+
([{ w, h }, $o, $initialized, $force]) => {
2738
if (!w || !h || !$initialized) return [];
2839
if ($o < inRange[0] || $o > inRange[1]) return $visibleData;
40+
const divisibleHeight = cellCount > 1 ? h + (cellCount - (h % cellCount)) : h;
2941
const cellHeight = h / cellCount;
3042
const start = Math.max(-1, Math.floor((-1 * $o) / cellHeight) - 1);
3143
const baseOffset = $o % cellHeight;
@@ -35,7 +47,8 @@
3547
const index = i + start;
3648
const pos = baseOffset + (i - 1) * cellHeight;
3749
if (index < 0 || index >= itemCount) return undefined;
38-
return { data: getCached(index), pos, index };
50+
const data = $force || !useCache ? get(index) : getCached(index);
51+
return { data, pos, index };
3952
})
4053
.filter(Boolean);
4154
},
@@ -51,15 +64,14 @@
5164
$: gridStyle = `grid-template-${type}: repeat(${cellCount}, 1fr);`;
5265
$: {
5366
if ($dim.w && $dim.h) {
54-
const newOffset = ($dim.h / cellCount) * index * -1;
55-
updateOffset(+newOffset.toFixed(3));
67+
updateOffset(($dim.h / cellCount) * index * -1);
5668
if (!$initialized) initialized.set(true);
5769
}
5870
}
5971
</script>
6072

6173
<div class="grid" style={gridStyle} bind:clientHeight={$dim.h} bind:clientWidth={$dim.w}>
62-
{#each $visibleData as obj (obj.index)}
74+
{#each $visibleData as obj (obj.data?.[idKey] || obj.index)}
6375
<div style="transform: translateY({obj.pos}px)">
6476
<slot {...obj.data} index={obj.index} />
6577
</div>

src/lib/directives/scrollable.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { scrollStep } from '$lib/config/scroll';
1+
import { scrollStep } from '../config/scroll';
22

3-
export default (node, { y: yi = 0, step = scrollStep, maxSteps = Infinity }) => {
3+
export default (node, opts) => {
4+
let { y: yi = 0, step = scrollStep } = opts;
45
let lastTouch = 0;
56
let y = yi;
67

78
const updateY = (val) => {
9+
const { maxSteps = Infinity } = opts;
810
y = Math.max(0, Math.min(maxSteps * step, val));
911
};
1012

src/lib/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Swappable from './components/generic/Swappable.svelte';
88
import Scrollable from './components/generic/Scrollable.svelte';
99
import InfiniteGrid from './components/generic/InfiniteGrid.svelte';
1010
import FiniteGrid from './components/generic/FiniteGrid.svelte';
11+
import scrollable from './directives/scrollable';
12+
import Theme from './components/generic/Theme.svelte';
1113
import * as themes from './config/theme';
1214

1315
export {
@@ -21,5 +23,7 @@ export {
2123
CrossfadeProvider,
2224
Scrollable,
2325
Swappable,
24-
themes
26+
Theme,
27+
themes,
28+
scrollable
2529
};

0 commit comments

Comments
 (0)