Skip to content

Commit 67577dc

Browse files
committed
docs: change Toc style
1 parent 11cd8eb commit 67577dc

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

docs/src/components/Aside/Aside.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const Aside = component$(() => {
2121
{title}
2222
</a>
2323
) : (
24-
<span class="mb-2 block rounded bg-blue-700 px-4 py-1 text-base font-bold uppercase text-white no-underline">
24+
<span class="mb-2 block rounded bg-blue-700 dark:bg-blue-600 px-4 py-1 text-base font-bold uppercase text-white no-underline">
2525
{text}
2626
</span>
2727
)}

docs/src/components/Toc/Toc.tsx

+37-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { cn } from "~/utils/cn";
2-
import { component$, useSignal, $, useOnWindow } from '@qwik.dev/core';
3-
import type { ContentHeading } from '@qwik.dev/router';
2+
import { component$, useSignal, $, useOnWindow } from "@qwik.dev/core";
3+
import type { ContentHeading } from "@qwik.dev/router";
44

55
export const Toc = component$(
66
({ headings }: { headings: ContentHeading[] }) => {
77
if (headings.length === 0) {
88
return null;
99
}
1010
return (
11-
<div class="space-y-2 sticky top-24 max-h-[calc(80vh)] p-1 dark:text-white text-black hidden xl:block">
12-
<div class="font-medium">On This Page</div>
11+
<div class="sticky top-24 hidden max-h-[calc(80vh)] space-y-2 p-1 text-black dark:text-white xl:block">
12+
<div class="mb-2 block rounded bg-blue-700 px-4 py-1 text-base font-bold uppercase text-white no-underline dark:bg-blue-600">
13+
On this page
14+
</div>
1315
<TableOfContents headings={headings} />
1416
</div>
1517
);
@@ -25,30 +27,39 @@ interface Node extends ContentHeading {
2527
type Tree = Array<Node>;
2628

2729
const TableOfContents = component$<TableOfContentsProps>(({ headings }) => {
28-
const sanitizedHeadings = headings.map(({ text, id, level }) => ({ text, id, level }));
30+
const sanitizedHeadings = headings.map(({ text, id, level }) => ({
31+
text,
32+
id,
33+
level,
34+
}));
2935
const itemIds = headings.map(({ id }) => id);
3036
const activeHeading = useActiveItem(itemIds);
3137
const tree = buildTree(sanitizedHeadings);
3238
const fixStartingBug: Node = { ...tree, children: [tree] };
33-
return <RecursiveList tree={fixStartingBug} activeItem={activeHeading.value ?? ''} />;
39+
return (
40+
<RecursiveList
41+
tree={fixStartingBug}
42+
activeItem={activeHeading.value ?? ""}
43+
/>
44+
);
3445
});
3546

3647
function deltaToStrg(
3748
currNode: Node,
3849
nextNode: Node,
39-
): 'same level' | 'down one level' | 'up one level' | 'upwards discontinuous' {
50+
): "same level" | "down one level" | "up one level" | "upwards discontinuous" {
4051
const delta = currNode.level - nextNode.level;
4152
if (delta > 1) {
42-
return 'upwards discontinuous';
53+
return "upwards discontinuous";
4354
}
4455
if (delta === 1) {
45-
return 'up one level';
56+
return "up one level";
4657
}
4758
if (delta === 0) {
48-
return 'same level';
59+
return "same level";
4960
}
5061
if (delta === -1) {
51-
return 'down one level';
62+
return "down one level";
5263
}
5364

5465
throw new Error(
@@ -68,25 +79,25 @@ function buildTree(nodes: ContentHeading[]) {
6879
childrenMap.set(nextNode.level, nextNode.children);
6980
const deltaStrg = deltaToStrg(currNode, nextNode);
7081
switch (deltaStrg) {
71-
case 'upwards discontinuous': {
82+
case "upwards discontinuous": {
7283
const delta = currNode.level - nextNode.level;
7384
if (childrenMap.has(delta - 1)) {
7485
const nthParent = childrenMap.get(delta - 1);
7586
nthParent?.push(nextNode);
7687
}
7788
break;
7889
}
79-
case 'up one level': {
90+
case "up one level": {
8091
const grandParent = childrenMap.get(currNode.level - 2);
8192
grandParent?.push(nextNode);
8293
break;
8394
}
84-
case 'same level': {
95+
case "same level": {
8596
const parent = childrenMap.get(currNode.level - 1);
8697
parent?.push(nextNode);
8798
break;
8899
}
89-
case 'down one level': {
100+
case "down one level": {
90101
currNode.children.push(nextNode);
91102
break;
92103
}
@@ -107,7 +118,7 @@ type RecursiveListProps = {
107118
const RecursiveList = component$<RecursiveListProps>(
108119
({ tree, activeItem, limit = 3 }) => {
109120
return tree.children.length && tree.level < limit ? (
110-
<ul class={cn('m-0 list-none', { 'pl-4': tree.level !== 1 })}>
121+
<ul class={cn("m-0 list-none", { "pl-4": tree.level !== 1 })}>
111122
{tree.children.map((childNode) => (
112123
<li key={childNode.id} class="mt-0 list-none pt-2">
113124
<Anchor node={childNode} activeItem={activeItem} />
@@ -125,7 +136,7 @@ const useActiveItem = (itemIds: string[]) => {
125136
const activeId = useSignal<string>();
126137

127138
useOnWindow(
128-
'scroll',
139+
"scroll",
129140
$(() => {
130141
const observer = new IntersectionObserver(
131142
(entries) => {
@@ -135,7 +146,7 @@ const useActiveItem = (itemIds: string[]) => {
135146
}
136147
});
137148
},
138-
{ rootMargin: '0% 0% -85% 0%' },
149+
{ rootMargin: "0% 0% -85% 0%" },
139150
);
140151

141152
itemIds.forEach((id) => {
@@ -175,18 +186,20 @@ const Anchor = component$<AnchorProps>(({ node, activeItem }) => {
175186
if (element) {
176187
const navbarHeight = 90;
177188
const position =
178-
element.getBoundingClientRect().top + window.scrollY - navbarHeight;
179-
window.scrollTo({ top: position, behavior: 'auto' });
189+
element.getBoundingClientRect().top +
190+
window.scrollY -
191+
navbarHeight;
192+
window.scrollTo({ top: position, behavior: "auto" });
180193
}
181194
}),
182195
]}
183196
class={cn(
184-
node.level > 2 && 'ml-2',
185-
'inline-block no-underline transition-colors',
186-
isActive ? 'text-blue-500 dark:text-blue-300' : 'text-muted-foreground',
197+
node.level > 2 && "ml-2",
198+
"inline-block no-underline transition-colors",
199+
isActive ? "text-blue-500 dark:text-blue-300" : "text-muted-foreground",
187200
)}
188201
>
189202
{node.text}
190203
</a>
191204
);
192-
});
205+
});

0 commit comments

Comments
 (0)