Skip to content

Commit

Permalink
feat: add rootMargin to TableOfContents builder (#1210)
Browse files Browse the repository at this point in the history
Co-authored-by: Jiewei Cheong <[email protected]>
Co-authored-by: Thomas G. Lopes <26071571 [email protected]>
  • Loading branch information
3 people committed Jun 28, 2024
1 parent 23129c1 commit 13275cf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-pugs-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
---
"@melt-ui/svelte": minor
---

feat: add rootMargin to TableOfContents builder
6 changes: 6 additions & 0 deletions src/docs/data/builders/table-of-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 35,12 @@ const builder: APISchema = {
`'highest'` means the opposite.\n\
`'lowest-parents'` means that parents of the heading with the lowest visible content are also considered active, and the same goes for `'highest-parents'`.",
},
{
name: 'rootMargin',
type: 'string',
description:
'The root margin for the intersection observer. Refer to the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin) for more information.',
},
{
name: 'scrollOffset',
type: 'number',
Expand Down
2 changes: 2 additions & 0 deletions src/lib/builders/table-of-contents/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 32,7 @@ export function createTableOfContents(args: CreateTableOfContentsArgs) {
selector,
exclude,
activeType,
rootMargin,
scrollBehaviour,
scrollOffset,
headingFilterFn,
Expand Down Expand Up @@ -301,6 302,7 @@ export function createTableOfContents(args: CreateTableOfContentsArgs) {
observer = new IntersectionObserver(handleElementObservation, {
root: null,
threshold: observer_threshold,
rootMargin,
});
elementsList.forEach((el) => observer?.observe(el));
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/builders/table-of-contents/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 48,10 @@ export type CreateTableOfContentsArgs = {
* - 'highest-parents': Parents of the heading with the highest visible content are also considered active.
*/
activeType?: ActiveType;
/**
* The root margin for the intersection observer. Refer to the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin) for more information.
*/
rootMargin?: string;
/**
* A custom filter function for headings.
*/
Expand Down

0 comments on commit 13275cf

Please sign in to comment.