@@ -4,16 +4,26 @@ import profilePicture1200 from '../assets/profile_picture_1200.jpeg';
44import profilePicture2000 from '../assets/profile_picture_2000.jpeg' ;
55
66export default function Home ( ) {
7- const [ typedCount , setTypedCount ] = useState ( 0 ) ;
8- const [ hidePrompt , setHidePrompt ] = useState ( false ) ;
9- const [ reduceMotion , setReduceMotion ] = useState ( false ) ;
10- const promptRef = useRef ( null ) ;
11- const [ promptWidth , setPromptWidth ] = useState ( 0 ) ;
12-
137 const fullPrompt = '~ cd' ;
148 const fullPath = 'davidbingmann/.' ;
159 const totalLength = fullPrompt . length + fullPath . length ;
1610
11+ const [ reduceMotion , setReduceMotion ] = useState ( ( ) =>
12+ typeof window !== 'undefined' &&
13+ window . matchMedia ( '(prefers-reduced-motion: reduce)' ) . matches
14+ ) ;
15+ const [ isSmallScreen , setIsSmallScreen ] = useState ( ( ) =>
16+ typeof window !== 'undefined' && window . matchMedia ( '(max-width: 720px)' ) . matches
17+ ) ;
18+ const disableHeroAnimation = reduceMotion || isSmallScreen ;
19+
20+ const [ typedCount , setTypedCount ] = useState ( ( ) =>
21+ disableHeroAnimation ? totalLength : 0
22+ ) ;
23+ const [ hidePrompt , setHidePrompt ] = useState ( ( ) => disableHeroAnimation ) ;
24+ const promptRef = useRef ( null ) ;
25+ const [ promptWidth , setPromptWidth ] = useState ( 0 ) ;
26+
1727 const promptText = fullPrompt . slice ( 0 , Math . min ( typedCount , fullPrompt . length ) ) ;
1828 const pathText =
1929 typedCount > fullPrompt . length
@@ -26,16 +36,34 @@ export default function Home() {
2636
2737 useEffect ( ( ) => {
2838 const motionQuery = window . matchMedia ( '(prefers-reduced-motion: reduce)' ) ;
29- const handleMotionChange = ( ) => setReduceMotion ( motionQuery . matches ) ;
39+ const handleMotionChange = ( event ) => setReduceMotion ( event . matches ) ;
3040
31- handleMotionChange ( ) ;
32- motionQuery . addEventListener ( 'change' , handleMotionChange ) ;
41+ setReduceMotion ( motionQuery . matches ) ;
42+ if ( motionQuery . addEventListener ) {
43+ motionQuery . addEventListener ( 'change' , handleMotionChange ) ;
44+ return ( ) => motionQuery . removeEventListener ( 'change' , handleMotionChange ) ;
45+ }
3346
34- return ( ) => motionQuery . removeEventListener ( 'change' , handleMotionChange ) ;
47+ motionQuery . addListener ( handleMotionChange ) ;
48+ return ( ) => motionQuery . removeListener ( handleMotionChange ) ;
3549 } , [ ] ) ;
3650
3751 useEffect ( ( ) => {
38- if ( reduceMotion ) {
52+ const screenQuery = window . matchMedia ( '(max-width: 720px)' ) ;
53+ const handleScreenChange = ( event ) => setIsSmallScreen ( event . matches ) ;
54+
55+ setIsSmallScreen ( screenQuery . matches ) ;
56+ if ( screenQuery . addEventListener ) {
57+ screenQuery . addEventListener ( 'change' , handleScreenChange ) ;
58+ return ( ) => screenQuery . removeEventListener ( 'change' , handleScreenChange ) ;
59+ }
60+
61+ screenQuery . addListener ( handleScreenChange ) ;
62+ return ( ) => screenQuery . removeListener ( handleScreenChange ) ;
63+ } , [ ] ) ;
64+
65+ useEffect ( ( ) => {
66+ if ( disableHeroAnimation ) {
3967 setTypedCount ( totalLength ) ;
4068 setHidePrompt ( true ) ;
4169 return undefined ;
@@ -53,10 +81,10 @@ export default function Home() {
5381 return ( ) => {
5482 clearInterval ( timer ) ;
5583 } ;
56- } , [ reduceMotion , totalLength ] ) ;
84+ } , [ disableHeroAnimation , totalLength ] ) ;
5785
5886 useEffect ( ( ) => {
59- if ( reduceMotion ) {
87+ if ( disableHeroAnimation ) {
6088 return undefined ;
6189 }
6290
@@ -65,13 +93,18 @@ export default function Home() {
6593 return ( ) => clearTimeout ( timer ) ;
6694 }
6795 return undefined ;
68- } , [ reduceMotion , typedCount , totalLength ] ) ;
96+ } , [ disableHeroAnimation , typedCount , totalLength ] ) ;
6997
7098 useEffect ( ( ) => {
99+ if ( disableHeroAnimation ) {
100+ return undefined ;
101+ }
102+
71103 if ( promptText . length === fullPrompt . length && promptRef . current ) {
72104 setPromptWidth ( promptRef . current . getBoundingClientRect ( ) . width ) ;
73105 }
74- } , [ promptText , fullPrompt . length ] ) ;
106+ return undefined ;
107+ } , [ disableHeroAnimation , promptText , fullPrompt . length ] ) ;
75108
76109 return (
77110 < div className = "home" >
0 commit comments