Skip to content

stop marquees in prefers-reduced-motion #2001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/app/conf/_design-system/marquee.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use client"

import { clsx } from "clsx"
import { useMotionValue, animate, motion } from "motion/react"
import { useState, useEffect, Fragment } from "react"
import { useMotionValue, animate, motion, useReducedMotion } from "motion/react"
import { useState, useEffect, Fragment, useId } from "react"
import useMeasure from "react-use-measure"

export interface MarqueeProps extends React.HTMLAttributes<HTMLElement> {
Expand All @@ -29,13 +29,23 @@ export function Marquee({
separator,
...rest
}: MarqueeProps) {
const shouldReduceMotion = useReducedMotion()
const [currentSpeed, setCurrentSpeed] = useState(speed)
const [ref, { width, height }] = useMeasure()
const translation = useMotionValue(0)

// ensure the marquees don't start in the same place.
const initialShiftPx = useSomeValue() * -64

const translation = useMotionValue(initialShiftPx)
const [isTransitioning, setIsTransitioning] = useState(false)
const [key, setKey] = useState(0)

useEffect(() => {
if (shouldReduceMotion) {
setCurrentSpeed(0)
return
}

let controls
const size = direction === "horizontal" ? width : height
const contentSize = size + gap
Expand Down Expand Up @@ -81,6 +91,7 @@ export function Marquee({
isTransitioning,
direction,
reverse,
shouldReduceMotion,
])

const hoverProps =
Expand Down Expand Up @@ -135,6 +146,7 @@ export function Marquee({
: {}

const multiples = 2

return (
<div className={clsx("overflow-hidden", className)} {...rest}>
<motion.div
Expand All @@ -155,7 +167,7 @@ export function Marquee({
hoverProps.onPointerUp?.(event)
}}
>
{Array.from({ length: 2 }).map((_, i) => (
{Array.from({ length: multiples }).map((_, i) => (
<Fragment key={i}>
{children}
{i < multiples - 1 && separator}
Expand All @@ -165,3 +177,11 @@ export function Marquee({
</div>
)
}

function useSomeValue() {
const id = useId()
const num =
id.split("").reduce((acc, char) => acc + char.charCodeAt(0), 0) - 950

return Math.abs(Math.sin(num))
}