Skip to content

Commit 35da24d

Browse files
committed
a11y: conditional autoRotate based on motion preference
1 parent 86e182d commit 35da24d

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

app/[locale]/10years/_components/TenYearGlobe.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client"
22

3-
import { useEffect, useMemo, useRef, useState } from "react"
3+
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
44
import { useTheme } from "next-themes"
55
import Globe from "react-globe.gl"
66
import { type GlobeMethods } from "react-globe.gl"
@@ -12,6 +12,7 @@ import Link from "@/components/ui/Link"
1212
import countries from "./countries.json"
1313

1414
import { useBreakpointValue } from "@/hooks/useBreakpointValue"
15+
import { usePrefersReducedMotion } from "@/hooks/usePrefersReducedMotion"
1516
import EthLogo from "@/public/images/assets/eth-glyph-colored.png"
1617

1718
// Define a type for event data
@@ -42,6 +43,7 @@ const TenYearGlobe = ({ events }: { events: EventData[] }) => {
4243
const globeRef = useRef<GlobeMethods>()
4344
const globeContainerRef = useRef<HTMLDivElement>(null)
4445
const { resolvedTheme } = useTheme()
46+
const { prefersReducedMotion } = usePrefersReducedMotion()
4547

4648
const atmosphereColor = resolvedTheme === "dark" ? "#B38DF0" : "#945AF4"
4749

@@ -61,7 +63,7 @@ const TenYearGlobe = ({ events }: { events: EventData[] }) => {
6163
const b = Math.min(255, Math.floor((basePurple & 0xff) * 1.3))
6264
return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`
6365
})
64-
}, [countries.features, resolvedTheme])
66+
}, [resolvedTheme])
6567

6668
const hexPolygonColor = (feature: object) => {
6769
const idx = countries.features.indexOf(
@@ -78,10 +80,18 @@ const TenYearGlobe = ({ events }: { events: EventData[] }) => {
7880
return hexPolygonColors[idx]
7981
}
8082

83+
// Function to safely set auto-rotate based on motion preferences
84+
const setAutoRotate = useCallback(
85+
(controls: ExtendedOrbitControls, value: boolean) => {
86+
controls.autoRotate = value && !prefersReducedMotion
87+
},
88+
[prefersReducedMotion]
89+
)
90+
8191
useEffect(() => {
8292
if (globeRef.current) {
8393
const controls = globeRef.current.controls() as ExtendedOrbitControls
84-
controls.autoRotate = true
94+
setAutoRotate(controls, true)
8595
controls.enablePan = false
8696
controls.enableZoom = false
8797
controls.autoRotateSpeed = 2.0
@@ -90,7 +100,7 @@ const TenYearGlobe = ({ events }: { events: EventData[] }) => {
90100
const ambientLight = new THREE.AmbientLight(0xffffff, 1)
91101
globeRef.current.scene().add(ambientLight)
92102
}
93-
}, [])
103+
}, [setAutoRotate])
94104

95105
// Prepare htmlElementsData for EthLogo
96106
const htmlElementsData = events.map((event) => ({
@@ -176,7 +186,7 @@ const TenYearGlobe = ({ events }: { events: EventData[] }) => {
176186
}
177187
// Stop rotation immediately
178188
const controls = globeRef.current.controls() as ExtendedOrbitControls
179-
controls.autoRotate = false
189+
setAutoRotate(controls, false)
180190
if ("autoRotateSpeed" in controls) controls.autoRotateSpeed = 0
181191
if (controls._sphericalDelta) {
182192
controls._sphericalDelta.theta = 0
@@ -193,15 +203,15 @@ const TenYearGlobe = ({ events }: { events: EventData[] }) => {
193203
if (globeRef.current) {
194204
const controls =
195205
globeRef.current.controls() as ExtendedOrbitControls
196-
controls.autoRotate = true
206+
setAutoRotate(controls, true)
197207
if ("autoRotateSpeed" in controls) controls.autoRotateSpeed = 2.0
198208
}
199209
}
200210
}}
201211
onPointClick={() => {
202212
if (globeRef.current) {
203213
const controls = globeRef.current.controls() as ExtendedOrbitControls
204-
controls.autoRotate = false
214+
setAutoRotate(controls, false)
205215
if ("autoRotateSpeed" in controls) controls.autoRotateSpeed = 0
206216
if (controls._sphericalDelta) {
207217
controls._sphericalDelta.theta = 0

0 commit comments

Comments
 (0)