Flip words
motionFreeMITOne word in a sentence swaps for the next with a soft lift, pauses on hover and stops after a few rounds. Still under reduced motion.
npx shadcn@latest add https://mikkelmanniche.dk/lab/r/flip-words.jsonPreview
Live demo in the dark theme.
Code
Install
npx shadcn@latest add https://mikkelmanniche.dk/lab/r/flip-words.jsonWith the registry added (components.json)
npx shadcn@latest add @manniche/flip-wordsUsage
import { FlipWords } from '@/components/flip-words'
export default function FlipWordsDemo() {
return (
<p className="text-center font-serif text-3xl leading-tight">
Websites that are <FlipWords words={['fast', 'honest', 'yours', 'found']} className="text-primary" />
</p>
)
}Source
import { AnimatePresence, MotionConfig, motion } from 'motion/react'
import { useEffect, useState } from 'react'
import { useReducedMotion } from '@/registry/manniche/hooks/use-reduced-motion'
import { cn } from '@/lib/utils'
export type FlipWordsProps = {
/** The words to cycle through. The first one is shown under reduced motion. */
words: string[]
/** Milliseconds each word stays. */
interval?: number
/** How many rounds before it stops on the last word, so it never moves forever. 0 keeps going. */
rounds?: number
/** Classes for the outer inline element. */
className?: string
}
/**
* One word in a sentence that swaps for the next with a soft lift and blur.
* It pauses while the pointer or focus is on it. Screen readers hear all the words once, not each swap.
* Under reduced motion it shows the first word and stays still.
*/
export function FlipWords({ words, interval = 2600, rounds = 3, className }: FlipWordsProps) {
const [i, setI] = useState(0)
const [paused, setPaused] = useState(false)
// Follows the system setting live; motion's own hook reads it once at mount and never updates.
const still = useReducedMotion()
const done = rounds > 0 && i >= words.length * rounds - 1
useEffect(() => {
if (still || paused || done || words.length < 2) return
const t = setTimeout(() => setI((n) => n + 1), interval)
return () => clearTimeout(t)
}, [i, still, paused, done, interval, words.length])
const word = words[i % words.length] ?? ''
return (
<MotionConfig reducedMotion="user">
<span
className={cn('relative inline-grid align-baseline', className)}
onPointerEnter={() => setPaused(true)}
onPointerLeave={() => setPaused(false)}
onFocus={() => setPaused(true)}
onBlur={() => setPaused(false)}
>
<span className="sr-only">{words.join(', ')}</span>
<AnimatePresence mode="popLayout" initial={false}>
<motion.span
key={i}
aria-hidden
initial={{ opacity: 0, y: '0.4em', filter: 'blur(6px)' }}
animate={{ opacity: 1, y: 0, filter: 'blur(0px)' }}
exit={{ opacity: 0, y: '-0.4em', filter: 'blur(6px)' }}
transition={{ duration: 0.35, ease: [0.23, 1, 0.32, 1] }}
className="col-start-1 row-start-1 whitespace-nowrap"
>
{word}
</motion.span>
</AnimatePresence>
</span>
</MotionConfig>
)
}import { useSyncExternalStore } from 'react'
const QUERY = '(prefers-reduced-motion: reduce)'
function subscribe(onChange: () => void) {
const mq = window.matchMedia(QUERY)
mq.addEventListener('change', onChange)
return () => mq.removeEventListener('change', onChange)
}
/** True when the visitor has asked the system for less motion. */
export function useReducedMotion() {
return useSyncExternalStore(
subscribe,
() => window.matchMedia(QUERY).matches,
() => false,
)
}Props
FlipWordsProps
| Prop | Type | Default | What it does |
|---|---|---|---|
words required | string[] | – | The words to cycle through. The first one is shown under reduced motion. |
interval | number | 2600 | Milliseconds each word stays. |
rounds | number | 3 | How many rounds before it stops on the last word, so it never moves forever. 0 keeps going. |
className | string | – | Classes for the outer inline element. |
A11y
Screen reader
A screen-reader-only span lists all the words joined with commas, and the visible swapping word is aria-hidden, so the words are heard once and not on each swap.
Keyboard
The cycling pauses while the pointer is over it, and while focus is inside it (focus and blur on the wrapper).
Reduced motion
Under reduced motion it shows the first word and never cycles. The setting is followed live, so switching it on or off while the page is open takes effect at once. With rounds above 0 it stops on the last word after that many rounds, and with rounds set to 0 it keeps going.