Building a Looping News Ticker in React
To keep news text scrolling, place two copies of the same news list side by side and repeat a CSS animation that moves the whole row to the left by half its width. The part that has moved half way looks the same as the starting position, so the loop seam does not jump. React handles measuring the width of the news to set the scroll speed, and the state of the pause button.
The code in this article was tested by opening a production build in Chromium and checking the scroll speed, the seam, how it stops, Tab key navigation, and the reduced-motion setting.
The test environment was Ubuntu 24.04, Node.js 22.22.0, and Chromium 153. I used React 19.3.1, TypeScript 7.0.2, esbuild 0.28.2, and Playwright 1.63.0. I type-checked the .tsx files with tsc, converted them with esbuild, and operated them with Playwright on a page 400px wide. I also confirmed that the development build with StrictMode enabled showed no React warnings during operation. I did not test Firefox, Safari, mobile browsers, screen reader announcements, or frameworks such as Next.js.

- 1. Place Two Copies of the List Side by Side and Move Half the Width to Hide the Seam
- 2. Measure the Width and Scroll Only When the Text Does Not Fit
- 3. Provide a Button for Pausing, Not Only Hover
- 4. Hide the Duplicate List from Screen Readers and Tab Navigation with aria-hidden and inert
- 5. When Keyboard Focus Moves to a Link, Reset to the Starting Position
- 6. When Reduced Motion Is Enabled, Stop the Scrolling and Allow Horizontal Scrolling
Place Two Copies of the List Side by Side and Move Half the Width to Hide the Seam
The CSS for the scrolling part is shown below. .ticker-viewport is the visible window that hides whatever overflows. Inside it, .ticker-track holds two <ul> elements with the same content side by side. @keyframes moves them from translateX(0) to translateX(-50%).
.ticker { display: flex; align-items: center; gap: 8px; padding: 6px 8px;
background: #eefafd; border: 1px solid #00c8e8; border-radius: 6px; }
.ticker-viewport { flex: 1; min-width: 0; overflow: hidden; }
.ticker-track { display: flex; width: max-content; }
.ticker-track[data-scrolling="true"] { animation: ticker var(--ticker-duration) linear infinite; }
/* Stop at the current position while the pause button is pressed and while the mouse is over the ticker */
.ticker-track[data-paused="true"],
.ticker-viewport:hover .ticker-track { animation-play-state: paused; }
/* While a link inside has focus, return to the starting position and show the link by scrolling the viewport */
.ticker-viewport:focus-within .ticker-track { animation: none; }
/* Two identical lists are placed side by side, so moving by half (one list's width) looks the same as the start */
@keyframes ticker { from { transform: translateX(0); } to { transform: translateX(-50%); } }
.ticker-list { display: flex; flex: none; margin: 0; padding: 0; list-style: none; }
.ticker-list li { padding-right: 3em; white-space: nowrap; }
.ticker-toggle { flex: none; font: inherit; padding: 2px 10px; }
@media (prefers-reduced-motion: reduce) {
.ticker-track[data-scrolling="true"] { animation: none; }
.ticker-viewport { overflow-x: auto; }
.ticker-list[aria-hidden="true"], .ticker-toggle { display: none; }
}
The gaps between news items are made with the right padding on li. By not adding any other spacing between the two lists, the width of .ticker-track is exactly two lists, so -50% is exactly one list's distance.
In the test environment, one list was 1091.2px wide and .ticker-track was 2182.4px wide. On the last frame of the animation, the copied list was at the same position as the original list at the start: 25px from the left edge of the page, which is exactly the left edge of the viewport. A screenshot of the viewport also matched between the first frame and the last frame.
Measure the Width and Scroll Only When the Text Does Not Fit
Usage is as follows. News items are passed as an array of { id, text, href }, and if href is present the item becomes a link. Below is the part with the long news items, taken from the App.tsx used in testing.
import { NewsTicker, type News } from './NewsTicker';
const LONG: News[] = [
{ id: '1', text: '11月3日(火)はサポート窓口をお休みします', href: '#holiday' },
{ id: '2', text: '新しい料金プランの受付を始めました', href: '#plan' },
{ id: '3', text: '11月10日 2:00〜4:00 にメンテナンスを行います', href: '#maintenance' },
];
export function App() {
return <NewsTicker items={LONG} />;
}
The component itself is below.
import { useLayoutEffect, useRef, useState, type CSSProperties, type FocusEvent } from 'react';
export type News = { id: string; text: string; href?: string };
type Props = {
items: News[];
// Distance in px moved per second. The speed stays the same even if the news text length changes
speed?: number;
};
export function NewsTicker({ items, speed = 60 }: Props) {
const viewportRef = useRef<HTMLDivElement>(null);
const listRef = useRef<HTMLUListElement>(null);
const [listWidth, setListWidth] = useState(0);
const [overflowing, setOverflowing] = useState(false);
const [paused, setPaused] = useState(false);
// Measure the list and viewport widths; only scroll when the list does not fit
useLayoutEffect(() => {
const viewport = viewportRef.current!;
const list = listRef.current!;
const measure = () => {
const w = list.getBoundingClientRect().width;
setListWidth(w);
setOverflowing(w > viewport.clientWidth);
};
measure();
const observer = new ResizeObserver(measure);
observer.observe(viewport);
observer.observe(list);
return () => observer.disconnect();
}, []);
// Scroll the viewport horizontally so the focused link's left edge is at the viewport's left edge
const handleFocus = (e: FocusEvent<HTMLDivElement>) => {
const viewport = e.currentTarget;
viewport.scrollLeft += e.target.getBoundingClientRect().left - viewport.getBoundingClientRect().left;
};
// When focus leaves the viewport, reset the scroll position
const handleBlur = (e: FocusEvent<HTMLDivElement>) => {
if (!e.currentTarget.contains(e.relatedTarget)) e.currentTarget.scrollLeft = 0;
};
const renderItems = () =>
items.map((item) => (
<li key={item.id}>{item.href ? <a href={item.href}>{item.text}</a> : item.text}</li>
));
return (
<section className="ticker" aria-label="お知らせ">
<div ref={viewportRef} className="ticker-viewport" onFocus={handleFocus} onBlur={handleBlur}>
<div
className="ticker-track"
data-scrolling={overflowing}
data-paused={paused}
style={{ '--ticker-duration': `${listWidth / speed}s` } as CSSProperties}
>
<ul ref={listRef} className="ticker-list">
{renderItems()}
</ul>
{/* Duplicate for a seamless loop. Hidden from screen readers and keyboard focus */}
{overflowing && (
<ul className="ticker-list" aria-hidden="true" inert>
{renderItems()}
</ul>
)}
</div>
</div>
{overflowing && (
<button type="button" className="ticker-toggle" onClick={() => setPaused((p) => !p)}>
{paused ? '再生' : '一時停止'}
</button>
)}
</section>
);
}
The animation length (in seconds) is set by dividing the width of one list by speed (how many px to move per second). A fixed length would make longer news scroll faster. In the test environment, a 1091.2px list gave an animation length of 18.1867s, moving 61.0px per second. This includes some error from the one-second wait. To change the speed, pass a value such as <NewsTicker items={LONG} speed={40} />. However, testing was done only with the default value of 60.
The width is measured inside useLayoutEffect. As the official React reference for useLayoutEffect explains, if you measure here and update state, React re-renders before the browser paints the screen. After that, changes in width are detected with ResizeObserver. Both the viewport and the list are observed, so changes to the list width are also picked up. I only tested changes to the viewport width. Replacing the news items was not tested.
When the list fits inside the viewport, it is shown as is without scrolling. Moving short text that does not need to scroll only makes it harder to read. On a page 400px wide, a short news item (287.5px wide in a 350px viewport) did not scroll, and no duplicate or pause button appeared. For the long news, widening the page to 1300px made the viewport 1250px wide, which stopped the scrolling. Returning the page to 400px made it scroll again.
Provide a Button for Pausing, Not Only Hover
While the mouse is over the ticker, animation-play-state: paused stops it at its current position. As described on MDN's animation-play-state page, a paused animation resumes from the position where it stopped. In the test environment, while hovered, the text moved 0.0px in 0.5 seconds. After the mouse left, it moved 30.0px in 0.5 seconds.
However, not everyone can hover with a mouse. People who operate only with a keyboard are one example. WCAG 2.2 Success Criterion 2.2.2 requires that content which starts automatically, lasts longer than 5 seconds, and is presented alongside other content have a way for the user to pause, stop, or hide it. The explanation, Understanding Pause, Stop, Hide, says that a design which stops only while focus is on the content and resumes when focus leaves does not count as such a way.
So I placed a "Pause" button to the right of the list. Pressing it sets data-paused to "true", which stops the scrolling and changes the button label to "Play". Pressing it again resumes the scrolling.
Stopping on hover alone is not enough. Place a button that keeps the ticker stopped outside the scrolling area.
Hide the Duplicate List from Screen Readers and Tab Navigation with aria-hidden and inert
The duplicate list added for the seam exists only for the visuals. Without changes, screen readers would read the same news items twice, and Tab would stop on the same links twice.
So the duplicate <ul> gets aria-hidden="true" and inert. MDN's aria-hidden page advises against putting aria-hidden="true" on elements that can receive focus or on their ancestors. The links inside the duplicate can receive focus, so I used the inert attribute to prevent both focus and clicks. In React 19.3.1, which I tested, inert can be written as a boolean attribute in JSX, and the type check passed.
When I dumped the accessibility tree with Playwright, the links inside region "お知らせ" were three, and each appeared once. When I moved through them with Tab, focus went from the three links to the pause button. It never stopped on a link inside the duplicate.
When Keyboard Focus Moves to a Link, Reset to the Starting Position
If text is scrolling while a link is selected with Tab, you cannot tell which item is selected. So while focus is inside the viewport, :focus-within sets animation: none and returns the list to its starting position. Then, in onFocus, the viewport's scrollLeft is changed so the left edge of the focused link lines up with the left edge of the viewport. Even with overflow: hidden, changing scrollLeft shifts the content sideways.
In the test environment, moving with Tab to the second and third links set scrollLeft to 379 and 699. In both cases, the left edge of the link was at the left edge of the viewport (the 0px position). Moving on to the pause button set animation-name back to ticker, and the text started moving again. After moving to the next link, I measured and found that onBlur had reset the viewport's scrollLeft to 0.
When Reduced Motion Is Enabled, Stop the Scrolling and Allow Horizontal Scrolling
People who have turned on the reduced-motion setting in their OS match prefers-reduced-motion with reduce. In the last @media block of the CSS above, this setting stops the animation, sets overflow-x: auto on the viewport, and hides the duplicate and the pause button.
When I set reducedMotion: 'reduce' in Playwright, animation-name was none, and the duplicate and the button were display: none. The viewport was 350px wide and its content was 1091px wide, so it could be scrolled horizontally by 150px.





