@jump-section/core
v1.0.1
Published
Core scroll management library for jump-section
Readme
@jump-section/core
Core scroll management library for jump-section. Framework-agnostic scroll navigation with intersection observer.
Installation
npm install @jump-section/core
# or
pnpm add @jump-section/core
# or
yarn add @jump-section/coreUsage
import { ScrollManager } from '@jump-section/core';
// Create a scroll manager instance
const manager = new ScrollManager({
offset: -80, // Offset for fixed headers (optional)
behavior: 'smooth', // Scroll behavior (optional)
});
// Register sections
const section1 = document.getElementById('section-1');
const section2 = document.getElementById('section-2');
manager.registerSection('section-1', section1);
manager.registerSection('section-2', section2);
// Scroll to a section
manager.scrollTo('section-1');
// Listen to active section changes
const unsubscribe = manager.onActiveChange((activeId) => {
console.log('Active section:', activeId);
});
// Cleanup
manager.unregisterSection('section-1');
manager.destroy();
unsubscribe();API
ScrollManager
Constructor
new ScrollManager(options?: ScrollOptions)Options:
offset?: number- Vertical offset in pixels (useful for fixed headers)behavior?: ScrollBehavior- Scroll behavior:'smooth'|'instant'|'auto'
Methods
registerSection(id: string, element: HTMLElement): void
Registers a section element to be tracked by the scroll manager.
unregisterSection(id: string): void
Unregisters a section from tracking.
scrollTo(id: string): void
Scrolls to the specified section.
onActiveChange(callback: (id: string | null) => void): () => void
Subscribes to active section changes. Returns an unsubscribe function.
destroy(): void
Cleans up the scroll manager and removes all observers.
License
ISC
