-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.client.tsx
191 lines (166 loc) · 5.03 KB
/
page.client.tsx
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use client';
import {
Fragment,
type HTMLAttributes,
useEffect,
useMemo,
useState,
} from 'react';
import { ChevronLeft, ChevronRight } from 'lucide-react';
import Link from 'next/link';
import { cva } from 'class-variance-authority';
import { cn } from './lib/cn';
import { useI18n } from 'fumadocs-ui/provider';
import { useTreeContext, useTreePath } from 'fumadocs-ui/provider';
import { useSidebar } from 'fumadocs-ui/provider';
import type { PageTree } from 'fumadocs-core/server';
import { usePathname } from 'next/navigation';
import { useNav } from './components/layout/nav';
import {
type BreadcrumbOptions,
getBreadcrumbItemsFromPath,
} from 'fumadocs-core/breadcrumb';
export function PageHeader(props: HTMLAttributes<HTMLDivElement>) {
const { open } = useSidebar();
const { isTransparent } = useNav();
return (
<header
{...props}
className={cn(
'sticky top-fd-layout-top z-10 flex flex-row items-center border-b border-fd-foreground/10 text-sm backdrop-blur-md transition-colors',
!isTransparent && 'bg-fd-background/80',
open && 'opacity-0',
props.className,
)}
style={
{
...props.style,
'--fd-toc-top-with-offset':
'calc(4px + var(--fd-banner-height) + var(--fd-nav-height))',
} as object
}
>
{props.children}
</header>
);
}
export function LastUpdate(props: { date: Date }) {
const { text } = useI18n();
const [date, setDate] = useState('');
useEffect(() => {
// to the timezone of client
setDate(props.date.toLocaleDateString());
}, [props.date]);
return (
<p className="text-sm text-fd-muted-foreground">
{text.lastUpdate} {date}
</p>
);
}
export interface FooterProps {
/**
* Items including information for the next and previous page
*/
items?: {
previous?: { name: string; url: string };
next?: { name: string; url: string };
};
}
const itemVariants = cva(
'flex w-full flex-col gap-2 rounded-lg border bg-fd-card p-4 text-sm transition-colors hover:bg-fd-accent/80 hover:text-fd-accent-foreground',
);
const itemLabel = cva(
'inline-flex items-center gap-0.5 text-fd-muted-foreground',
);
function scanNavigationList(tree: PageTree.Node[]) {
const list: PageTree.Item[] = [];
tree.forEach((node) => {
if (node.type === 'folder') {
if (node.index) {
list.push(node.index);
}
list.push(...scanNavigationList(node.children));
return;
}
if (node.type === 'page' && !node.external) {
list.push(node);
}
});
return list;
}
const listCache = new WeakMap<PageTree.Root, PageTree.Item[]>();
export function Footer({ items }: FooterProps) {
const { root } = useTreeContext();
const { text } = useI18n();
const pathname = usePathname();
const { previous, next } = useMemo(() => {
if (items) return items;
const cached = listCache.get(root);
const list = cached ?? scanNavigationList(root.children);
listCache.set(root, list);
const idx = list.findIndex((item) => item.url === pathname);
if (idx === -1) return {};
return {
previous: list[idx - 1],
next: list[idx + 1],
};
}, [items, pathname, root]);
return (
<div className="grid grid-cols-2 gap-4 pb-6">
{previous ? (
<Link href={previous.url} className={cn(itemVariants())}>
<div className={cn(itemLabel())}>
<ChevronLeft className="-ms-1 size-4 shrink-0 rtl:rotate-180" />
<p>{text.previousPage}</p>
</div>
<p className="font-medium">{previous.name}</p>
</Link>
) : null}
{next ? (
<Link
href={next.url}
className={cn(itemVariants({ className: 'col-start-2 text-end' }))}
>
<div className={cn(itemLabel({ className: 'flex-row-reverse' }))}>
<ChevronRight className="-me-1 size-4 shrink-0 rtl:rotate-180" />
<p>{text.nextPage}</p>
</div>
<p className="font-medium">{next.name}</p>
</Link>
) : null}
</div>
);
}
export type BreadcrumbProps = BreadcrumbOptions;
export function Breadcrumb(options: BreadcrumbProps) {
const path = useTreePath();
const { root } = useTreeContext();
const items = useMemo(() => {
return getBreadcrumbItemsFromPath(root, path, {
includePage: options.includePage ?? false,
...options,
});
}, [options, path, root]);
if (items.length === 0) return null;
return (
<div className="-mb-3 flex flex-row items-center gap-1 text-sm font-medium text-fd-muted-foreground">
{items.map((item, i) => (
<Fragment key={i}>
{i !== 0 && (
<ChevronRight className="size-4 shrink-0 rtl:rotate-180" />
)}
{item.url ? (
<Link
href={item.url}
className="truncate hover:text-fd-accent-foreground"
>
{item.name}
</Link>
) : (
<span className="truncate">{item.name}</span>
)}
</Fragment>
))}
</div>
);
}