split stuff up

This commit is contained in:
MapleLeaf
2021-12-29 17:12:42 -06:00
parent 401fcd2f16
commit eef9d3a0bc
5 changed files with 45 additions and 38 deletions

View File

@@ -0,0 +1,8 @@
import { useState } from "react"
import { useWindowEvent } from "~/hooks/dom/use-window-event"
export function useScrolled() {
const [scrolled, setScrolled] = useState(false)
useWindowEvent("scroll", () => setScrolled(window.scrollY > 0))
return scrolled
}

View File

@@ -0,0 +1,11 @@
import { useEffect } from "react"
export function useWindowEvent<EventType extends keyof WindowEventMap>(
type: EventType,
handler: (event: WindowEventMap[EventType]) => void,
) {
useEffect(() => {
window.addEventListener(type, handler)
return () => window.removeEventListener(type, handler)
})
}