Skip to content

Commit a826289

Browse files
authored
Merge pull request algoscope-hq#270 from tanushkat96/fix/visualizer-layout-scroll
fix: improve visualizer layout and scroll behavior
2 parents 129b921 + 231ca12 commit a826289

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/components/AlgoCard.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default function AlgoCard({
3131
return (
3232
<MotionLink
3333
to={link}
34+
onClick={() => window.scrollTo(0, 0)}
3435
className={`group relative block w-full rounded-3xl p-8 backdrop-blur-xl transition-all duration-500 ease-out text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-cyan-500/50 overflow-hidden ${colorClasses} border hover:-translate-y-2 hover:shadow-[0_30px_60px_-15px_rgba(0,0,0,0.8)]`}
3536
variants={cardVariants}
3637
whileHover={{ y: -10 }}

src/components/visualizer/CodePanel.jsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,30 @@ const CodePanel = memo(function CodePanel({
1717
const activeTheme = themes[theme] ?? themes.vscDarkPlus
1818

1919
useEffect(() => {
20-
if (!activeLine || !scrollContainerRef.current) {
21-
return
22-
}
20+
if (!activeLine || !scrollContainerRef.current) return
2321

2422
const activeNode = scrollContainerRef.current.querySelector(
2523
`[data-line-number="${activeLine}"]`
2624
)
2725

28-
activeNode?.scrollIntoView({
29-
block: 'nearest',
30-
behavior: 'smooth',
31-
})
26+
if (activeNode) {
27+
const container = scrollContainerRef.current
28+
29+
const containerTop = container.scrollTop
30+
const containerBottom = containerTop + container.clientHeight
31+
32+
const nodeTop = activeNode.offsetTop
33+
const nodeBottom = nodeTop + activeNode.clientHeight
34+
35+
if (nodeTop < containerTop) {
36+
container.scrollTo({ top: nodeTop - 20, behavior: 'smooth' })
37+
} else if (nodeBottom > containerBottom) {
38+
container.scrollTo({
39+
top: nodeBottom - container.clientHeight + 20,
40+
behavior: 'smooth',
41+
})
42+
}
43+
}
3244
}, [activeLine])
3345

3446
useEffect(() => {

0 commit comments

Comments
 (0)