Skip to content

Commit

Permalink
Scrolling to the active file
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikmayer committed Jan 20, 2025
1 parent b795753 commit 7afa329
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,31 @@ class VirtualList {
if (file) {
this.activeFilePath = file.path;
this.updateActiveHighlight();
this.scrollToActiveFile();
}
}

private scrollToActiveFile(): void {
if (!this.activeFilePath) return;

const activeIndex = this.items.findIndex((item) => item.file.path === this.activeFilePath);
if (activeIndex === -1) return;

const scrollToPosition = activeIndex * this.itemHeight;
const containerHeight = this.rootEl.clientHeight;

// Check if the active file is already in view
if (
scrollToPosition >= this.rootEl.scrollTop &&
scrollToPosition < this.rootEl.scrollTop + containerHeight
) {
return;
}

// Scroll to the position of the active file
this.rootEl.scrollTop = scrollToPosition - containerHeight / 2 + this.itemHeight / 2;
}

private updateContainerHeight(): void {
const totalHeight = this.items.length * this.itemHeight;
this.spacerEl.style.height = totalHeight + 'px';
Expand Down

0 comments on commit 7afa329

Please sign in to comment.