1+ export interface ScrollToAnchorOptions {
2+ /** Custom scroll function (e.g., from useActiveTocItem) */
3+ scrollFn ?: ( id : string ) => void
4+ /** Whether to update the URL hash (default: true) */
5+ updateUrl ?: boolean
6+ }
7+
18/**
29 * Scroll to an element by ID, using a custom scroll function if provided,
310 * otherwise falling back to default scroll behavior with header offset.
411 *
512 * @param id - The element ID to scroll to
6- * @param scrollFn - Optional custom scroll function (e.g., from useActiveTocItem)
13+ * @param options - Optional configuration for scroll behavior
714 */
8- export function scrollToAnchor ( id : string , scrollFn ?: ( id : string ) => void ) : void {
15+ export function scrollToAnchor ( id : string , options ?: ScrollToAnchorOptions ) : void {
16+ const { scrollFn, updateUrl = true } = options ?? { }
17+
918 // Use custom scroll function if provided
1019 if ( scrollFn ) {
1120 scrollFn ( id )
@@ -18,8 +27,9 @@ export function scrollToAnchor(id: string, scrollFn?: (id: string) => void): voi
1827
1928 // Calculate scroll position with header offset (matches scroll-padding-top in main.css)
2029 const HEADER_OFFSET = 80
30+ const PKG_STICKY_HEADER_OFFSET = 52
2131 const elementTop = element . getBoundingClientRect ( ) . top + window . scrollY
22- const targetScrollY = elementTop - HEADER_OFFSET
32+ const targetScrollY = elementTop - ( HEADER_OFFSET + PKG_STICKY_HEADER_OFFSET )
2333
2434 // Use scrollTo for precise control
2535 window . scrollTo ( {
@@ -29,5 +39,7 @@ export function scrollToAnchor(id: string, scrollFn?: (id: string) => void): voi
2939
3040 // Update URL hash after initiating scroll
3141 // Use replaceState to avoid triggering native scroll-to-anchor behavior
32- history . replaceState ( null , '' , `#${ id } ` )
42+ if ( updateUrl ) {
43+ history . replaceState ( null , '' , `#${ id } ` )
44+ }
3345}
0 commit comments