Marquee
Measured-overflow horizontal scrolling strip with pause/reset/direction controls, reduced-motion static fallback, and AppState lifecycle stop.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add marqueeSource files
Expand a file to read the source the installer copies into your app.
components/ui/marquee/Marquee.tsx
import * as React from 'react'
import { AppState, ScrollView, View, type AppStateStatus, type LayoutChangeEvent, type ViewProps } from 'react-native'
import Animated, {
cancelAnimation,
Easing,
useAnimatedStyle,
useReducedMotion,
useSharedValue,
withRepeat,
withTiming,
} from 'react-native-reanimated'
import { motionDurations } from '@/lib/motion'
import { cn } from '@/lib/utils'
const DEFAULT_SPEED = 48
const DEFAULT_GAP = 24
const OVERFLOW_THRESHOLD = 1
export type MarqueeDirection = 'left' | 'right'
export interface MarqueeHandle {
/** Reset the marquee scroll offset to `0` and restart the loop if eligible. */
reset: () => void
}
export interface MarqueeProps extends ViewProps {
children?: React.ReactNode
/** Scroll speed in pixels per second. Must be finite and `> 0` (defaults to `48`). */
speed?: number
/** Horizontal gap in pixels between seamless loop copies. Clamped to `>= 0` (defaults to `24`). */
gap?: number
/** Travel direction of the scrolling strip (defaults to `'left'`). */
direction?: MarqueeDirection
/** Pause scrolling and fall back to a static single copy. */
paused?: boolean
/** Classes applied to the outer bounded viewport `View`. */
className?: string
}
export const Marquee = React.forwardRef<MarqueeHandle, MarqueeProps>(
(
{
children,
speed = DEFAULT_SPEED,
gap = DEFAULT_GAP,
direction = 'left',
paused = false,
className,
onLayout,
...props
},
ref,
) => {
const reducedMotion = useReducedMotion()
const [appActive, setAppActive] = React.useState(() => AppState.currentState === 'active')
const [viewportWidth, setViewportWidth] = React.useState(0)
const [contentWidth, setContentWidth] = React.useState(0)
const [resetCount, setResetCount] = React.useState(0)
const translateX = useSharedValue(0)
const isAnimatingRef = React.useRef(false)
React.useEffect(() => {
const subscription = AppState.addEventListener('change', (nextState: AppStateStatus) => {
setAppActive(nextState === 'active')
})
return () => subscription.remove()
}, [])
const safeSpeed = Number.isFinite(speed) && speed > 0 ? speed : DEFAULT_SPEED
const safeGap = Number.isFinite(gap) ? Math.max(0, gap) : DEFAULT_GAP
const isOverflowing = viewportWidth > 0 && contentWidth > viewportWidth + OVERFLOW_THRESHOLD
const shouldAnimate = isOverflowing && !paused && !reducedMotion && appActive
React.useImperativeHandle(
ref,
() => ({
reset: () => {
cancelAnimation(translateX)
isAnimatingRef.current = false
translateX.value = 0
setResetCount((count) => count + 1)
},
}),
[translateX],
)
React.useEffect(() => {
if (!shouldAnimate) {
if (isAnimatingRef.current) {
cancelAnimation(translateX)
isAnimatingRef.current = false
}
translateX.value = 0
return
}
const cycleDistance = contentWidth + safeGap
const duration = Math.max(motionDurations.fast, Math.round((cycleDistance / safeSpeed) * 1000))
isAnimatingRef.current = true
if (direction === 'right') {
translateX.value = -cycleDistance
translateX.value = withRepeat(withTiming(0, { duration, easing: Easing.linear }), -1, false)
} else {
translateX.value = 0
translateX.value = withRepeat(withTiming(-cycleDistance, { duration, easing: Easing.linear }), -1, false)
}
return () => {
cancelAnimation(translateX)
isAnimatingRef.current = false
}
}, [shouldAnimate, contentWidth, safeGap, safeSpeed, direction, resetCount, translateX])
const trackStyle = useAnimatedStyle(() => ({
transform: [{ translateX: translateX.value }],
}))
const handleViewportLayout = (event: LayoutChangeEvent) => {
const nextWidth = Math.max(0, event.nativeEvent.layout.width)
setViewportWidth((prev) => (Math.abs(prev - nextWidth) < 0.5 ? prev : nextWidth))
onLayout?.(event)
}
const handleContentLayout = (event: LayoutChangeEvent) => {
const nextWidth = Math.max(0, event.nativeEvent.layout.width)
setContentWidth((prev) => (Math.abs(prev - nextWidth) < 0.5 ? prev : nextWidth))
}
return (
<View {...props} className={cn('overflow-hidden', className)} onLayout={handleViewportLayout}>
<ScrollView horizontal scrollEnabled={false} showsHorizontalScrollIndicator={false}>
<Animated.View style={[{ flexDirection: 'row', alignItems: 'center' }, trackStyle]}>
<View onLayout={handleContentLayout} style={{ flexDirection: 'row', alignItems: 'center', flexShrink: 0 }}>
{children}
</View>
{shouldAnimate ? (
<View
accessible={false}
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
style={{ flexDirection: 'row', alignItems: 'center', flexShrink: 0, marginLeft: safeGap }}
>
{children}
</View>
) : null}
</Animated.View>
</ScrollView>
</View>
)
},
)
Marquee.displayName = 'Marquee'
components/ui/marquee/index.ts
export { Marquee, type MarqueeDirection, type MarqueeHandle, type MarqueeProps } from './Marquee'