Files
tech-log-frontend/src/features/tech-log/presentation/shared/public-render/document-toc.tsx
T
DongHyeonkaandClaude Opus 5 db4450fe00 feat: 본문을 읽는 동안에도 조사 안의 위치가 보인다
레일에는 「이 글에서」만 있었다. 이 글 안의 위치는 알아도 이 조사 안의 위치는 알 수
없었고, 제목 아래의 맥락 덩어리는 화면을 몇 번 넘기면 사라진다. 레일 아래 칸에
프로젝트·주제·종류를 짧게 붙여 따라오게 한다. 관계 목록은 두지 않는다 — 제목 아래에
이미 있고, 같은 목록이 한 화면에 두 번 있으면 목록이 아니라 소음이다.

레일이 둘을 담게 되었으므로 이름도 갈랐다. 「이 글에서」와 「맥락」은 서로 다른 질문에
답한다.

작성자가 확대를 끈 도식도 좁은 화면에서는 확대할 수 있게 했다. 390px 화면에서 폭
1,200px 짜리 도식은 3분의 1 로 눌려 덩어리만 보이고 글자는 읽히지 않는다 — 설명하는
일을 하지 못한다. 기준은 치수다: 본문 폭(720px)보다 넓은 그림은 어디선가 반드시 줄어든다.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015c8cBFUaVr7KLVUx6ujz7D
2026-09-15 20:20:43 +09:00

118 lines
4.0 KiB
TypeScript

import { useEffect, useRef, useState, type ReactNode } from "react";
type HeadingItem = {
id: string;
label: string;
};
type DocumentTocProps = {
headings: ReadonlyArray<HeadingItem>;
variant: "desktop" | "mobile";
/**
* 데스크톱 레일의 아래 칸. 「이 글에서」와 「맥락」은 서로 다른 질문에 답한다 — 앞은 이 글의
* 어디인지, 뒤는 이 조사의 어디인지다. 한 레일에 두되 마디를 갈라 둔다.
*/
context?: ReactNode;
};
export function DocumentToc({ headings, variant, context }: DocumentTocProps) {
const [currentId, setCurrentId] = useState(headings[0]?.id ?? "");
const empty = headings.length === 0;
const detailsRef = useRef<HTMLDetailsElement>(null);
useEffect(() => {
/*
`IntersectionObserver` 가 없는 환경이 있다 — jsdom 이 그렇고, 오래된 브라우저도 그렇다.
없으면 "지금 읽는 절" 표시만 못 할 뿐 목차 자체는 쓸 수 있으므로, 없다고 문서를 통째로
못 그리게 두지 않는다.
한동안 이 컴포넌트는 픽스처 Case 하나에서만 쓰여 그 환경을 만난 적이 없었다. 모든 Case 가
목차를 받게 되면서 드러났다.
*/
if (typeof IntersectionObserver === "undefined") return undefined;
const elements = headings
.map((heading) => document.getElementById(heading.id))
.filter((element): element is HTMLElement => Boolean(element));
const observer = new IntersectionObserver(
(entries) => {
const visible = entries
.filter((entry) => entry.isIntersecting)
.sort(
(left, right) =>
left.boundingClientRect.top - right.boundingClientRect.top,
);
if (visible[0]) setCurrentId(visible[0].target.id);
},
{ rootMargin: "-12% 0px -72% 0px", threshold: [0, 1] },
);
elements.forEach((element) => observer.observe(element));
return () => observer.disconnect();
}, [headings]);
function select(id: string) {
setCurrentId(id);
if (detailsRef.current) detailsRef.current.open = false;
}
/*
제목이 없는 문서에는 목차를 그리지 않는다. 예전에는 이 컴포넌트가 픽스처 문서 하나에서만
쓰여 빈 경우를 만날 일이 없었지만, 이제 모든 Case 가 지나므로 빈 레일이 남을 수 있다.
*/
if (empty && !(variant === "desktop" && context)) return null;
if (variant === "mobile") {
const current =
headings.find((heading) => heading.id === currentId) ?? headings[0];
return (
<div className="shell mobile-toc-wrap">
<details className="mobile-toc" ref={detailsRef}>
<summary>목차 · {current?.label}</summary>
<nav aria-label="문서 목차">
{headings.map((heading) => (
<a
href={`#${heading.id}`}
key={heading.id}
aria-current={
heading.id === currentId ? "location" : undefined
}
onClick={() => select(heading.id)}
>
{heading.label}
</a>
))}
</nav>
</details>
</div>
);
}
return (
/*
레일은 이제 둘을 담는다 — 이 글의 어디인지(「이 글에서」)와 이 조사의 어디인지(「맥락」).
서로 다른 질문에 답하므로 이름도 따로 준다. 레일 전체의 이름은 그 둘을 아우른다.
*/
<aside className="document-toc" aria-label="문서 안내">
{empty ? null : (
<nav aria-label="이 글에서">
<p>이 글에서</p>
{headings.map((heading) => (
<a
href={`#${heading.id}`}
key={heading.id}
aria-current={heading.id === currentId ? "location" : undefined}
onClick={() => select(heading.id)}
>
{heading.label}
</a>
))}
</nav>
)}
{context}
</aside>
);
}