fix(blog): improve table of contents navigation
This commit is contained in:
@@ -462,7 +462,11 @@ export function BlogDetailContent({ post, section = "blog" }: BlogDetailContentP
|
|||||||
<Icon icon="mdi:close" width={18} height={18} />
|
<Icon icon="mdi:close" width={18} height={18} />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<BlogToc markdown={post.markdown} onNavigate={closeToc} />
|
<BlogToc
|
||||||
|
markdown={post.markdown}
|
||||||
|
onNavigate={closeToc}
|
||||||
|
showTitle={false}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
+49
-9
@@ -1,7 +1,14 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { Typography } from "poyraz-ui/atoms";
|
import { Icon } from "@iconify/react";
|
||||||
|
import { useTranslations } from "next-intl";
|
||||||
|
import {
|
||||||
|
Button,
|
||||||
|
ButtonIcon,
|
||||||
|
ButtonLabel,
|
||||||
|
Typography,
|
||||||
|
} from "poyraz-ui/atoms";
|
||||||
|
|
||||||
type TocHeading = {
|
type TocHeading = {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -37,9 +44,15 @@ function parseHeadings(markdown: string): TocHeading[] {
|
|||||||
type BlogTocProps = {
|
type BlogTocProps = {
|
||||||
markdown: string;
|
markdown: string;
|
||||||
onNavigate?: () => void;
|
onNavigate?: () => void;
|
||||||
|
showTitle?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function BlogToc({ markdown, onNavigate }: BlogTocProps) {
|
export function BlogToc({
|
||||||
|
markdown,
|
||||||
|
onNavigate,
|
||||||
|
showTitle = true,
|
||||||
|
}: BlogTocProps) {
|
||||||
|
const t = useTranslations("Blog");
|
||||||
const headings = useMemo(() => parseHeadings(markdown), [markdown]);
|
const headings = useMemo(() => parseHeadings(markdown), [markdown]);
|
||||||
const [activeId, setActiveId] = useState("");
|
const [activeId, setActiveId] = useState("");
|
||||||
const rafRef = useRef(0);
|
const rafRef = useRef(0);
|
||||||
@@ -56,6 +69,11 @@ export function BlogToc({ markdown, onNavigate }: BlogTocProps) {
|
|||||||
[onNavigate],
|
[onNavigate],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const handleBackToTop = useCallback(() => {
|
||||||
|
window.scrollTo({ top: 0, behavior: "smooth" });
|
||||||
|
onNavigate?.();
|
||||||
|
}, [onNavigate]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (headings.length === 0) return;
|
if (headings.length === 0) return;
|
||||||
|
|
||||||
@@ -112,13 +130,18 @@ export function BlogToc({ markdown, onNavigate }: BlogTocProps) {
|
|||||||
if (headings.length < 2) return null;
|
if (headings.length < 2) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<nav aria-label="İçindekiler" className="space-y-1">
|
<nav
|
||||||
<Typography
|
aria-label={t("toc")}
|
||||||
variant="small"
|
className="max-h-[calc(100dvh-7rem)] space-y-1 overflow-y-auto overscroll-contain pr-1 [scrollbar-width:thin]"
|
||||||
className="mb-2 font-semibold text-foreground"
|
>
|
||||||
>
|
{showTitle ? (
|
||||||
İçindekiler
|
<Typography
|
||||||
</Typography>
|
variant="small"
|
||||||
|
className="mb-2 font-semibold text-foreground"
|
||||||
|
>
|
||||||
|
{t("toc")}
|
||||||
|
</Typography>
|
||||||
|
) : null}
|
||||||
{headings.map((heading, index) => (
|
{headings.map((heading, index) => (
|
||||||
<button
|
<button
|
||||||
key={`${heading.id}-${index}`}
|
key={`${heading.id}-${index}`}
|
||||||
@@ -135,6 +158,23 @@ export function BlogToc({ markdown, onNavigate }: BlogTocProps) {
|
|||||||
{heading.text}
|
{heading.text}
|
||||||
</button>
|
</button>
|
||||||
))}
|
))}
|
||||||
|
<div className="mt-3 border-t border-border pt-3">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
size="xs"
|
||||||
|
radius="sm"
|
||||||
|
effect="swap"
|
||||||
|
swapTarget="both"
|
||||||
|
onClick={handleBackToTop}
|
||||||
|
className="w-full justify-between"
|
||||||
|
>
|
||||||
|
<ButtonLabel>{t("backToTop")}</ButtonLabel>
|
||||||
|
<ButtonIcon>
|
||||||
|
<Icon icon="mdi:arrow-up" width={14} height={14} />
|
||||||
|
</ButtonIcon>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user