Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add-toc-click-handler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ainsleydev/sveltekit-helper": patch
---

feat(TableOfContents): Adding onLinkClick callback prop for click events on TOC links.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export type TableOfContentsProps = {
* @default 80
*/
scrollOffset?: number;
/**
* Optional callback invoked when a TOC link is clicked.
* Useful for closing a sidebar or drawer on navigation.
*/
onLinkClick?: (event: MouseEvent, item: TOCItem) => void;
};
</script>

Expand All @@ -49,6 +54,7 @@ export type TableOfContentsProps = {
contentSelector = '[data-sidebar-content="true"]',
headingSelector,
scrollOffset = 80,
onLinkClick,
}: TableOfContentsProps = $props()

let activeId = $state<string | null>(null)
Expand Down Expand Up @@ -146,6 +152,7 @@ export type TableOfContentsProps = {
class:toc__link--active={activeId ===
(item.href.startsWith('#') ? item.href.slice(1) : item.href)}
href="#{item.href}"
onclick={(e) => onLinkClick?.(e, item)}
>
<small>{item.label}</small>
</a>
Expand Down
Loading