forked from nodejs/doc-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
136 lines (120 loc) · 3.88 KB
/
index.jsx
File metadata and controls
136 lines (120 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* eslint-disable react-x/no-use-context, react-x/no-context-provider -- preact/compat does not export `use`, so React 19 patterns are unavailable at runtime */
import { CodeBracketIcon, DocumentIcon } from '@heroicons/react/24/outline';
import Badge from '@node-core/ui-components/Common/Badge';
import MetaBar from '@node-core/ui-components/Containers/MetaBar';
import GitHubIcon from '@node-core/ui-components/Icons/Social/GitHub';
import { createContext, useContext } from 'react';
import styles from './index.module.css';
import { useScrollSpy } from '../../hooks/useScrollSpy.mjs';
const iconMap = {
JSON: CodeBracketIcon,
MD: DocumentIcon,
};
/**
* @typedef MetaBarProps
* @property {Array<import('@vcarl/remark-headings').Heading & { stability: string }>} headings - Array of page headings for table of contents
* @property {string} addedIn - Version or date when feature was added
* @property {string} readingTime - Estimated reading time for the page
* @property {Array<[string, string]>} viewAs - Array of [title, path] tuples for view options
* @property {string} editThisPage - URL for editing the current page
*/
const STABILITY_KINDS = ['error', 'warning', null, 'info'];
const STABILITY_LABELS = ['D', 'E', null, 'L'];
const STABILITY_TOOLTIPS = ['Deprecated', 'Experimental', null, 'Legacy'];
const ActiveSlugContext = createContext(null);
/**
* Renders a heading value with an optional stability badge
* @param {{ value: string, stability: number }} props
*/
const HeadingValue = ({ value, stability }) => {
if (stability === 2) {
return value;
}
const ariaLabel = STABILITY_TOOLTIPS[stability]
? `Stability: ${STABILITY_TOOLTIPS[stability]}`
: undefined;
return (
<>
{value}
<Badge
size="small"
className={styles.badge}
kind={STABILITY_KINDS[stability]}
data-tooltip={STABILITY_TOOLTIPS[stability]}
aria-label={ariaLabel}
tabIndex={0}
>
{STABILITY_LABELS[stability]}
</Badge>
</>
);
};
/**
* Anchor element that highlights itself when it matches the active TOC slug.
* @param {{ href: string, className?: string }} props
*/
const ActiveLink = ({ href, className, ...props }) => {
const activeSlug = useContext(ActiveSlugContext);
return (
<a
href={href}
aria-current={href === `#${activeSlug}` ? 'true' : undefined}
className={[className, href === `#${activeSlug}` ? styles.tocActive : '']
.filter(Boolean)
.join(' ')}
{...props}
/>
);
};
/**
* MetaBar component that displays table of contents and page metadata
* @param {MetaBarProps} props - Component props
*/
export default ({
headings = [],
addedIn,
readingTime,
viewAs = [],
editThisPage,
}) => {
const activeSlug = useScrollSpy(headings);
return (
<ActiveSlugContext.Provider value={activeSlug}>
<MetaBar
heading="Table of Contents"
as={ActiveLink}
headings={{
items: headings.map(({ value, stability, ...heading }) => ({
...heading,
value: <HeadingValue value={value} stability={stability} />,
})),
}}
items={{
'Reading Time': readingTime,
'Added In': addedIn,
'View As': (
<ol>
{viewAs.map(([title, path]) => {
const Icon = iconMap[title];
return (
<li key={title}>
<a href={path}>
{Icon && <Icon className={styles.icon} />}
{title}
</a>
</li>
);
})}
</ol>
),
Contribute: (
<>
<GitHubIcon className="fill-neutral-700 dark:fill-neutral-100" />
<a href={editThisPage}>Edit this page</a>
</>
),
}}
/>
</ActiveSlugContext.Provider>
);
};