feat(player): scroll chapter list to current chapter on open
Use a Svelte action on each chapter button that calls scrollIntoView with behavior:'instant' so the list opens centred on the active chapter with no visible scroll animation.
This commit is contained in:
@@ -34,6 +34,14 @@
|
|||||||
|
|
||||||
// ── Chapter search ────────────────────────────────────────────────────────
|
// ── Chapter search ────────────────────────────────────────────────────────
|
||||||
let chapterSearch = $state('');
|
let chapterSearch = $state('');
|
||||||
|
|
||||||
|
// Scroll the current chapter into view instantly (no animation) when the
|
||||||
|
// chapter modal opens. Applied to every chapter button; only scrolls when
|
||||||
|
// the chapter number matches the currently playing one. Runs once on mount
|
||||||
|
// before the browser paints so no scroll animation is ever visible.
|
||||||
|
function scrollIfActive(node: HTMLElement, isActive: boolean) {
|
||||||
|
if (isActive) node.scrollIntoView({ block: 'center', behavior: 'instant' });
|
||||||
|
}
|
||||||
const filteredChapters = $derived(
|
const filteredChapters = $derived(
|
||||||
chapterSearch.trim() === ''
|
chapterSearch.trim() === ''
|
||||||
? audioStore.chapters
|
? audioStore.chapters
|
||||||
@@ -386,11 +394,12 @@
|
|||||||
</div>
|
</div>
|
||||||
<!-- Chapter list -->
|
<!-- Chapter list -->
|
||||||
<div class="flex-1 overflow-y-auto">
|
<div class="flex-1 overflow-y-auto">
|
||||||
{#each filteredChapters as ch (ch.number)}
|
{#each filteredChapters as ch (ch.number)}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onclick={() => playChapter(ch.number)}
|
onclick={() => playChapter(ch.number)}
|
||||||
class={cn(
|
use:scrollIfActive={ch.number === audioStore.chapter}
|
||||||
|
class={cn(
|
||||||
'w-full flex items-center gap-3 px-4 py-3 border-b border-(--color-border)/40 transition-colors text-left',
|
'w-full flex items-center gap-3 px-4 py-3 border-b border-(--color-border)/40 transition-colors text-left',
|
||||||
ch.number === audioStore.chapter ? 'bg-(--color-brand)/8' : 'hover:bg-(--color-surface-2)'
|
ch.number === audioStore.chapter ? 'bg-(--color-brand)/8' : 'hover:bg-(--color-surface-2)'
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user