File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { useState , useEffect } from "react" ;
2+
3+ /**
4+ * Returns true while the mouse is moving, false after it's been
5+ * idle for `idleMs` milliseconds (default 1000ms).
6+ */
7+ export function useMouseMoving ( idleMs = 1000 ) {
8+ const [ isMoving , setIsMoving ] = useState ( false ) ;
9+
10+ useEffect ( ( ) => {
11+ let timeout : ReturnType < typeof setTimeout > ;
12+
13+ const handleMove = ( ) => {
14+ setIsMoving ( true ) ;
15+ clearTimeout ( timeout ) ;
16+ timeout = setTimeout ( ( ) => setIsMoving ( false ) , idleMs ) ;
17+ } ;
18+
19+ window . addEventListener ( "mousemove" , handleMove ) ;
20+ return ( ) => {
21+ window . removeEventListener ( "mousemove" , handleMove ) ;
22+ clearTimeout ( timeout ) ;
23+ } ;
24+ } , [ idleMs ] ) ;
25+
26+ return isMoving ;
27+ }
Original file line number Diff line number Diff line change @@ -2,14 +2,15 @@ import styles from "./Home.module.css";
22import { useState , useEffect , useRef } from "react" ;
33import LevelComponent from "../../components/LevelComponent" ;
44import { main } from "./solarsystem.js" ;
5-
5+ import { useMouseMoving } from "../../hooks/useMouseMoving.js" ;
66export default function Home ( ) {
77 const lvlNum = 4 ;
88 const canvasRef = useRef < HTMLCanvasElement | null > ( null ) ;
99 const zoomOutRef = useRef < ( ( ) => void ) | null > ( null ) ;
1010
1111 const [ isVisible , setIsVisible ] = useState ( false ) ;
1212 const [ mountKey , setMountKey ] = useState ( 0 ) ;
13+ const isMouseMoving = useMouseMoving ( 800 ) ; // fade back in after 800ms of no movement
1314
1415 const handleOpen = ( ) => {
1516 setMountKey ( k => k + 1 ) ;
@@ -29,6 +30,17 @@ export default function Home() {
2930
3031 return (
3132 < div >
33+ < header style = { {
34+ transition : "opacity 0.6s ease" ,
35+ opacity : isMouseMoving ? 0 : 1 ,
36+ pointerEvents : isMouseMoving ? "none" : "auto" ,
37+ } }
38+ >
39+ < div className = { styles [ "header-brand" ] } >
40+
41+ < h1 > glorpython</ h1 >
42+ </ div >
43+ </ header >
3244 < section className = "relative w-full h-screen" >
3345 { isVisible && (
3446
You can’t perform that action at this time.
0 commit comments