-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from OpenMOSS/ui
Add the feature of side navigation and preview.
- Loading branch information
Showing
7 changed files
with
180 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { cn } from "@/lib/utils"; | ||
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export const SectionNavigator = ({ sections }: { sections: { title: string; id: string }[] }) => { | ||
const [activeSection, setActiveSection] = useState<{ title: string; id: string } | null>(null); | ||
|
||
const handleScroll = () => { | ||
// Use reduce instead of find for obtaining the last section that is in view | ||
const currentSection = sections.reduce((result: { title: string; id: string } | null, section) => { | ||
const secElement = document.getElementById(section.id); | ||
if (!secElement) return result; | ||
const rect = secElement.getBoundingClientRect(); | ||
if (rect.top <= window.innerHeight / 2) { | ||
return section; | ||
} | ||
return result; | ||
}, null); | ||
|
||
setActiveSection(currentSection); | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener("scroll", handleScroll); | ||
|
||
// Run the handler to set the initial active section | ||
handleScroll(); | ||
|
||
return () => { | ||
window.removeEventListener("scroll", handleScroll); | ||
}; | ||
}); | ||
|
||
return ( | ||
<Card className="py-4 sticky top-0 w-60 h-full bg-transparent"> | ||
<CardHeader className="py-0"> | ||
<CardTitle className="flex justify-between items-center text-xs p-2"> | ||
<span className="font-bold">CONTENTS</span> | ||
</CardTitle> | ||
</CardHeader> | ||
<CardContent className="py-0"> | ||
<div className="flex flex-col"> | ||
<ul> | ||
{sections.map((section) => ( | ||
<li key={section.id} className="relative"> | ||
<a | ||
href={"#" + section.id} | ||
className={cn("p-2 block text-neutral-700", activeSection === section && "text-[blue]")} | ||
> | ||
{section.title} | ||
</a> | ||
{activeSection === section && <div className="absolute -left-1.5 top-0 bottom-0 w-0.5 bg-[blue]"></div>} | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
</CardContent> | ||
</Card> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,7 @@ | |
@apply bg-background text-foreground; | ||
} | ||
} | ||
|
||
html { | ||
scroll-behavior: smooth; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters