-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance(client): visualize scroll depth
Adds scroll depth visualization.
- Loading branch information
Showing
3 changed files
with
89 additions
and
2 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
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,23 @@ | ||
.debug { | ||
.scroll-depth { | ||
--color: #f00; | ||
|
||
.depth { | ||
background-color: var(--color); | ||
height: 1px; | ||
left: 0; | ||
position: absolute; | ||
width: 100%; | ||
|
||
&::after { | ||
background-color: var(--background-primary); | ||
border: 1px solid var(--color); | ||
content: attr(data-text); | ||
left: 50%; | ||
padding: 0 5px; | ||
position: absolute; | ||
transform: translateX(-50%) translateY(-50%); | ||
} | ||
} | ||
} | ||
} |
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,55 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
import "./index.scss"; | ||
|
||
export default function Debug() { | ||
return ( | ||
<div className="debug"> | ||
<DebugScrollDepth /> | ||
</div> | ||
); | ||
} | ||
|
||
function DebugScrollDepth() { | ||
const ref = useRef<HTMLDivElement>(null); | ||
const [scrollHeight, setScrollHeight] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
const root = ref.current; | ||
|
||
if (!root || !scrollHeight) { | ||
return; | ||
} | ||
|
||
const depths = [25, 50, 75, 90]; | ||
depths.forEach((depth) => { | ||
const div = document.createElement("div"); | ||
div.className = "depth"; | ||
div.dataset.text = `${depth}%`; | ||
div.style.top = `${(depth / 100) * scrollHeight}px`; | ||
root.appendChild(div); | ||
}); | ||
|
||
return () => | ||
Array.from(root.children).forEach((child) => root.removeChild(child)); | ||
}, [ref, scrollHeight]); | ||
|
||
useEffect(() => { | ||
const handler = () => { | ||
setScrollHeight(document.documentElement.scrollHeight); | ||
}; | ||
|
||
const observer = new MutationObserver(handler); | ||
|
||
observer.observe(document.body, { | ||
childList: true, // Monitor for added/removed elements | ||
subtree: true, // Monitor all descendants of body | ||
attributes: true, // Monitor attribute changes (can affect size) | ||
characterData: true, // Monitor text content changes | ||
}); | ||
|
||
return () => observer.disconnect(); | ||
}, []); | ||
|
||
return <div ref={ref} className="scroll-depth" />; | ||
} |