Skip to content

Commit 81d4d63

Browse files
committed
inView判定の修正
1 parent c9d64b5 commit 81d4d63

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

app/[docs_id]/pageContent.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface PageContentProps {
2121
}
2222
export function PageContent(props: PageContentProps) {
2323
const { setSidebarMdContent } = useSidebarMdContext();
24-
24+
2525
// SSR用のローカルstate
2626
const [dynamicMdContent, setDynamicMdContent] = useState<
2727
DynamicMarkdownSection[]
@@ -52,18 +52,22 @@ export function PageContent(props: PageContentProps) {
5252

5353
useEffect(() => {
5454
const handleScroll = () => {
55-
const updateContent = (prevDynamicMdContent: DynamicMarkdownSection[]) => {
55+
const updateContent = (
56+
prevDynamicMdContent: DynamicMarkdownSection[]
57+
) => {
5658
const dynMdContent = prevDynamicMdContent.slice(); // Reactの変更検知のために新しい配列を作成
5759
for (let i = 0; i < sectionRefs.current.length; i++) {
5860
if (sectionRefs.current.at(i) && dynMdContent.at(i)) {
5961
const rect = sectionRefs.current.at(i)!.getBoundingClientRect();
62+
console.log(i, rect);
6063
dynMdContent.at(i)!.inView =
61-
rect.top < window.innerHeight && rect.bottom >= 0;
64+
rect.top < window.innerHeight * 0.9 &&
65+
rect.bottom >= window.innerHeight * 0.1;
6266
}
6367
}
6468
return dynMdContent;
6569
};
66-
70+
6771
// ローカルstateとcontextの両方を更新
6872
setDynamicMdContent(updateContent);
6973
setSidebarMdContent(props.docs_id, updateContent);
@@ -102,8 +106,9 @@ export function PageContent(props: PageContentProps) {
102106
</div>
103107
<div key={`${index}-chat`}>
104108
{/* 右側に表示するチャット履歴欄 */}
105-
{chatHistories.filter((c) => c.sectionId === section.sectionId).map(
106-
({chatId, messages}) => (
109+
{chatHistories
110+
.filter((c) => c.sectionId === section.sectionId)
111+
.map(({ chatId, messages }) => (
107112
<div
108113
key={chatId}
109114
className="max-w-xs mb-2 p-2 text-sm border border-base-content/10 rounded-sm shadow-sm bg-base-100"
@@ -131,8 +136,7 @@ export function PageContent(props: PageContentProps) {
131136
))}
132137
</div>
133138
</div>
134-
)
135-
)}
139+
))}
136140
</div>
137141
</>
138142
))}

app/sidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function Sidebar() {
8383

8484
// 現在表示中のセクション(最初にinViewがtrueのもの)を見つける
8585
const currentSectionIndex = sidebarMdContent.findIndex(
86-
(section) => section.inView
86+
(section, i) => i >= 1 && section.inView
8787
);
8888

8989
// 目次の開閉状態

0 commit comments

Comments
 (0)