Skip to content

Commit 8242e63

Browse files
committed
chore(toc): comprehensive typedef for toc data attributes
1 parent 3f6983e commit 8242e63

3 files changed

Lines changed: 83 additions & 3 deletions

File tree

.changeset/blue-flowers-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@svelte-put/toc": patch
3+
---
4+
5+
add comprehensive type definitions for data attributes

packages/toc/src/attributes/attributes.d.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { TocObserveConfig } from '../parameter/parameter.d.ts';
55
* Data attributes to override `toc` behavior per matching element
66
* @public
77
*/
8-
export interface TocDataAttributes {
8+
export interface TocElementDataAttributes {
99
/** whether to ignore this element when searching for matching elements */
1010
'data-toc-ignore'?: boolean;
1111
/**
@@ -28,6 +28,81 @@ export interface TocDataAttributes {
2828
'data-toc-threshold'?: number;
2929
}
3030

31+
/**
32+
* Data attributes used by the `observe` operation when enabled
33+
* @public
34+
*/
35+
export interface TocObserveDataAttributes {
36+
/**
37+
* added to the element where IntersectionObserver is used when observe is
38+
* turned on and references the associated toc element
39+
*/
40+
readonly 'data-toc-observe-for'?: string;
41+
/**
42+
* added to toc root (the element where toc action is placed on) and
43+
* references the id of the active matching element
44+
*
45+
* @remarks
46+
*
47+
* This attribute is reactive. When changed (either by toc or manually),
48+
* it will trigger events and store update accordingly
49+
*/
50+
'data-toc-observe-active-id'?: string;
51+
/**
52+
* added to toc root (the element where toc action is placed on) and
53+
* indicate whether observe is being throttled, typically seen in conjunction
54+
* with usage of the complementary toclink action
55+
*/
56+
readonly 'data-toc-observe-throttled'?: boolean;
57+
/**
58+
* added to the element where toclink is used and
59+
* set to true when the linked toc element is active
60+
*/
61+
readonly 'data-toc-link-active'?: boolean;
62+
}
63+
64+
/**
65+
* Readonly reference markers added by toc
66+
* @public
67+
*/
68+
export interface TocReferenceMarkerDataAttributes {
69+
/**
70+
* marking this element that it's been processed by toc
71+
*
72+
* @remarks
73+
*
74+
* If this is already preprocessed by {@link https://svelte-put.vnphanquang.com/docs/preprocess-auto-slug | @svelte-put/preprocess-auto-slug},
75+
* there will also be a `data-auto-slug` attribute.
76+
*/
77+
readonly 'data-toc'?: '';
78+
/**
79+
* if the anchor option is enabled in toc parameters, this attribute is present on the injected anchor element.
80+
*
81+
* @remarks
82+
*
83+
* If the element is already added by {@link https://svelte-put.vnphanquang.com/docs/preprocess-auto-slug | @svelte-put/preprocess-auto-slug},
84+
* there `data-auto-slug-anchor` attribute is found instead.
85+
*/
86+
readonly 'data-toc-anchor'?: '';
87+
/**
88+
* added to the element where toc action is used for internal reference
89+
*/
90+
readonly 'data-toc-root'?: '';
91+
/**
92+
* added to the element where toclink action is used and references the linked toc element
93+
*/
94+
readonly 'data-toc-link-for'?: '';
95+
96+
/**
97+
* from {@link https://svelte-put.vnphanquang.com/docs/preprocess-auto-slug | @svelte-put/preprocess-auto-slug}
98+
*/
99+
'data-auto-slug'?: '';
100+
'data-auto-slug-anchor'?: '';
101+
'data-auto-slug-anchor-position'?: '';
102+
}
103+
104+
export interface TocDataAttributes extends TocElementDataAttributes, TocObserveDataAttributes, TocReferenceMarkerDataAttributes {}
105+
31106
/**
32107
* ambient typing for `toc` event handlers
33108
* @public

packages/toc/src/attributes/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* all relevant data attribute name literals
33
* @internal
44
*/
5-
export const ATTRIBUTES = {
5+
export const ATTRIBUTES = /** @type {Record<string, keyof (import('./attributes.d.ts').TocDataAttributes)>} */({
66
// markers from `@svelte-put/preprocess-auo-slug`
77
autoslug: 'data-auto-slug',
88
autoSlugAnchor: 'data-auto-slug-anchor',
@@ -23,4 +23,4 @@ export const ATTRIBUTES = {
2323
// for elements that `use:toclink`
2424
linkFor: 'data-toc-link-for',
2525
linkActive: 'data-toc-link-active',
26-
};
26+
});

0 commit comments

Comments
 (0)