Skip to content

Commit 1244d0a

Browse files
committed
Disable hero/timeline animations on small screens
1 parent 7bdc721 commit 1244d0a

2 files changed

Lines changed: 56 additions & 16 deletions

File tree

src/pages/Home.jsx

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ import profilePicture1200 from '../assets/profile_picture_1200.jpeg';
44
import profilePicture2000 from '../assets/profile_picture_2000.jpeg';
55

66
export 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">

src/styles.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,14 @@ p {
428428
}
429429

430430
.hero-content {
431+
animation: none;
431432
padding: 0 1.25rem 2rem;
432433
}
433434

435+
.hero-command .prompt {
436+
transition: none;
437+
}
438+
434439
.section-body {
435440
padding: 1.5rem;
436441
}
@@ -440,7 +445,9 @@ p {
440445
}
441446

442447
.timeline-item {
443-
animation-delay: 0ms !important;
448+
opacity: 1;
449+
transform: none;
450+
animation: none;
444451
}
445452
}
446453

0 commit comments

Comments
 (0)