Skip to content

Commit

Permalink
refactor: cleanup property names for clarity, type code properly
Browse files Browse the repository at this point in the history
  • Loading branch information
orefalo committed Jun 17, 2024
1 parent da0f018 commit 0225567
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/lib/SizeAndPositionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ export default class SizeAndPositionManager {
return Math.max(0, Math.min(totalSize - containerSize, idealOffset));
}

getVisibleRange(containerSize: number = 0, offset: number, overscanCount: number): VirtualRange {
getVisibleRange(containerSize: number = 0, offset: number, windowOverPadding: number): VirtualRange {
const totalSize = this.getTotalSize();

if (totalSize === 0) {
return {};
return { start: 0, end: 0 };
}

const maxOffset = offset + containerSize;
Expand All @@ -226,9 +226,9 @@ export default class SizeAndPositionManager {
offset += this.getSizeAndPositionForIndex(end).size;
}

if (overscanCount) {
start = Math.max(0, start - overscanCount);
end = Math.min(end + overscanCount, this.itemCount - 1);
if (windowOverPadding) {
start = Math.max(0, start - windowOverPadding);
end = Math.min(end + windowOverPadding, this.itemCount - 1);
}

return {
Expand Down
24 changes: 11 additions & 13 deletions src/lib/VirtualList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
SCROLL_CHANGE_REASON,
type AfterScrollEvent,
type RowAttributes,
type VirtualRange,
type VirtualRangeEvent,
type VirtualRowSize
} from '.';
Expand All @@ -62,16 +62,16 @@
// positioning
scrollToIndex?: number | undefined;
scrollOffset?: number | undefined;
overscanCount?: number;
windowOverPadding?: number;
scrollDirection?: DIRECTION;
scrollToAlignment?: ALIGNMENT;
scrollToBehaviour?: SCROLL_BEHAVIOR;
//snippets
// snippets
header?: Snippet;
row: Snippet<[RowAttributes]>;
footer?: Snippet;
// callbacks
onVisibleRangeUpdate?: (range: VirtualRange) => void;
// events
onVisibleRangeUpdate?: (range: VirtualRangeEvent) => void;
onAfterScroll?: (event: AfterScrollEvent) => void;
}
Expand All @@ -86,7 +86,7 @@
// positioning
scrollToIndex,
scrollOffset,
overscanCount = 3,
windowOverPadding = 3,
scrollDirection = DIRECTION.VERTICAL,
scrollToAlignment = ALIGNMENT.AUTO,
scrollToBehaviour = SCROLL_BEHAVIOR.INSTANT,
Expand All @@ -96,9 +96,8 @@
row,
header,
// callback
//TODO rename to visibleRangeUpdated
onVisibleRangeUpdate: onVisibleRangeUpdate = () => null,
// events
onVisibleRangeUpdate = () => null,
onAfterScroll = () => null
}: Props = $props();
Expand Down Expand Up @@ -261,7 +260,7 @@
//@ts-expect-error wrong type assignment
scrollDirection === DIRECTION.VERTICAL ? height : width,
offset,
overscanCount
windowOverPadding
);
let updatedItems = [];
Expand All @@ -285,8 +284,7 @@
});
}
// TODO: carefull, stop is not named the same as end, maybe rename stop->end
onVisibleRangeUpdate({ start, end });
onVisibleRangeUpdate({ type: 'range.update', start, end });
}
items = updatedItems;
Expand Down Expand Up @@ -341,7 +339,7 @@
scrollChangeReason: SCROLL_CHANGE_REASON.OBSERVED
};
onAfterScroll({ offset, event });
onAfterScroll({ type: 'scroll.update', offset, event });
};
function getEstimatedItemSize(): number {
Expand Down
17 changes: 14 additions & 3 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ export type VirtualRowSize = number | number[] | ((index: number) => number);

export { default as VirtualList } from './VirtualList.svelte';

// use by the row() snippet
export interface RowAttributes {
// the index of the row being rendered, from the original dataset
index: number;
// the calculated style for this row
style: string;
}

Expand All @@ -13,13 +16,21 @@ export interface VirtualPosition {
}

export interface VirtualRange {
start?: number;
end?: number;
// index of the first visible item
start: number;
// index of the last visible item
end: number;
}

export interface VirtualRangeEvent extends VirtualRange {
type: 'range.update';
}

export interface AfterScrollEvent {
type: 'scroll.update';
// either the value of `wrapper.scrollTop` or `wrapper.scrollLeft`
offset: number | string;
// original event
// the original event
event: Event;
}

Expand Down

0 comments on commit 0225567

Please sign in to comment.