Skip to content

feat: Add onLinkClick callback to TableOfContents component#437

Merged
ainsleyclark merged 1 commit intomainfrom
claude/add-toc-click-handler-uoHvz
Mar 16, 2026
Merged

feat: Add onLinkClick callback to TableOfContents component#437
ainsleyclark merged 1 commit intomainfrom
claude/add-toc-click-handler-uoHvz

Conversation

@ainsleyclark
Copy link
Contributor

Summary

Added an optional onLinkClick callback prop to the TableOfContents component, allowing consumers to respond to TOC link clicks with custom logic.

Changes

  • Added onLinkClick?: (event: MouseEvent, item: TOCItem) => void prop to TableOfContentsProps type
  • Destructured onLinkClick from component props
  • Attached click handler to TOC links that invokes the callback with the click event and corresponding TOC item
  • Added changeset entry documenting the new feature

Implementation Details

The callback is invoked via optional chaining (onLinkClick?.(e, item)) on the onclick handler of each TOC link, making it safe to use even when the prop is not provided. This enables use cases like closing a sidebar or drawer when navigating via TOC links.

https://claude.ai/code/session_01TaD4iZDnbNnQqNr9TJGdRX

Adds an optional onLinkClick callback to TableOfContentsProps that is
invoked when a TOC link is clicked, allowing consumers to close a
sidebar or drawer on navigation.

https://claude.ai/code/session_01TaD4iZDnbNnQqNr9TJGdRX
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@ainsleyclark ainsleyclark merged commit b748144 into main Mar 16, 2026
4 checks passed
@ainsleyclark ainsleyclark deleted the claude/add-toc-click-handler-uoHvz branch March 16, 2026 13:45
@claude
Copy link

claude bot commented Mar 16, 2026

Review summary

  • Overall score: 7/10
  • Critical issues: 0
  • Warnings: 2
  • Suggestions: 3
  • Recommendation: ⚠️ Approve with comments

Small, well-scoped change that is idiomatic Svelte 5 and fully backward-compatible. The main gaps are the absence of component tests and a minor handler-attachment inefficiency.

Critical issues 🔴

None

Warnings 🟡

1. No tests for new behaviour
No test files exist for this component, and the new callback introduces testable behaviour: that onLinkClick is called with the correct MouseEvent and TOCItem, and that omitting the prop does not throw. Per AGENTS.md, new features should include tests using Vitest/Testing Library.

2. No-op handler always attached to every link
Inline onclick={(e) => onLinkClick?.(e, item)} attaches an arrow function to every link even when no callback is provided. Prefer:

onclick={onLinkClick ? (e) => onLinkClick(e, item) : undefined}

This avoids attaching unnecessary DOM event handlers when the prop is absent.

Suggestions 🟢

1. Add @param tags to JSDoc
The onLinkClick JSDoc comment is missing @param tags per AGENTS.md conventions:

/**
 * Optional callback invoked when a TOC link is clicked.
 * Useful for closing a sidebar or drawer on navigation.
 * @param event - The native mouse event from the anchor click.
 * @param item - The TOC item corresponding to the clicked link.
 */
onLinkClick?: (event: MouseEvent, item: TOCItem) => void;

2. Document in the @component usage examples
The component JSDoc block does not show onLinkClick in use (e.g. closing a sidebar). Adding one would aid discoverability.

3. Clarify preventDefault responsibility
A brief note that consumers must call e.preventDefault() themselves if they want to suppress default scroll behaviour would prevent confusion.

@codecov
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.26%. Comparing base (7f6b060) to head (6f0b800).
⚠️ Report is 523 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #437      +/-   ##
==========================================
+ Coverage   64.59%   70.26%   +5.67%     
==========================================
  Files         154      187      +33     
  Lines        6064     7439    +1375     
==========================================
+ Hits         3917     5227    +1310     
+ Misses       2064     2012      -52     
- Partials       83      200     +117     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants