Skip to content

Commit

Permalink
optimization and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gregrickaby committed Feb 13, 2024
1 parent cd2926e commit 8e1533a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
24 changes: 6 additions & 18 deletions components/BackToTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,28 @@ export default function BackToTop() {
const [showButton, setShowButton] = useState(false)
const buttonText = 'Go back to the top of the page'

// Effect to handle scroll events.
useEffect(() => {
// Handle scroll event.
// Scroll event handler.
const scrollHandler = () => {
// If the user has scrolled down 200px, show the button.
if (window.scrollY > 200) {
setShowButton(true)
} else {
setShowButton(false)
}
setShowButton(window.scrollY > 200)
}

// Add event listener.
window.addEventListener('scroll', scrollHandler)

// Clean up event listener.
// Cleanup event listener.
return () => window.removeEventListener('scroll', scrollHandler)
}, [])

/**
* Scroll to the top of the page.
*/
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
})
}

// No button? Bail.
if (!showButton) {
return null
}

return (
return showButton ? (
<button
aria-label={buttonText}
className="button fixed bottom-8 right-6"
Expand All @@ -53,5 +41,5 @@ export default function BackToTop() {
<IconArrowUp height="32" width="32" />
<span className="sr-only">{buttonText}</span>
</button>
)
) : null
}
9 changes: 3 additions & 6 deletions components/BossButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function BossButton() {
const buttonText =
'The boss button. Click or press Escape to quickly navigate to DuckDuckGo.'

// Effect for the boss button.
useEffect(() => {
// On initial load, show the button if the viewport is wider than 768px.
if (window.innerWidth > 768) {
Expand Down Expand Up @@ -46,11 +47,7 @@ export default function BossButton() {
}
}, [router])

if (!showButton) {
return null
}

return (
return showButton ? (
<button
aria-label={buttonText}
className="fixed right-6 top-8 z-10"
Expand All @@ -59,5 +56,5 @@ export default function BossButton() {
>
<IconDoorExit aria-hidden="true" height="32" width="32" />
</button>
)
) : null
}
4 changes: 2 additions & 2 deletions components/HlsPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function HlsPlayer(props: HlsPlayerProps) {
const videoRef = useRef<HTMLMediaElement>(null)

/**
* Initialize Hls.js.
* Effect for initializing Hls.js.
*
* @see https://github.com/video-dev/hls.js/#embedding-hlsjs
*/
Expand All @@ -36,7 +36,7 @@ export default function HlsPlayer(props: HlsPlayerProps) {
}, [props.src, videoRef])

/**
* Pause this video if another starts playing.
* Effect for pausing.
*/
useEffect(() => {
const video = videoRef.current
Expand Down

0 comments on commit 8e1533a

Please sign in to comment.