Poll results with bars animating to each option's share
A developer survey shows a poll question with a Show results button. When the results appear, each option’s bar should grow from empty to its share of the votes, instead of jumping straight to its final width. Every bar grows at the same speed, so options with fewer votes finish first. Build the poll.
The starter code includes the poll and the fill speed:
const POLL = {
question: 'Which build tool do you reach for first?',
options: [
{ label: 'Vite', votes: 58 },
{ label: 'Webpack', votes: 31 },
{ label: 'esbuild', votes: 17 },
{ label: 'Parcel', votes: 14 },
],
};
const STEP_MS = 20;
<button> labelled Show results. No bars, percentages or vote counts are shown yet.Total: 120 votes (the sum of all votes),POLL.options order, with the label, the bar’s current percentage as N%, the bar itself, and {votes} votes.role="progressbar", aria-valuemin="0", aria-valuemax="100", aria-valuenow set to its current percentage, and an accessible name equal to the option label (aria-label or aria-labelledby). Its visual width matches the current percentage.Math.round(votes / total * 100), which gives 48, 26, 14 and 12.STEP_MS milliseconds until it reaches its own target, then stops. In other words, after t ms its value is min(target, floor(t / STEP_MS)).Click "Show results" Vite 0% Webpack 0% esbuild 0% Parcel 0%
after 200 ms Vite 10% Webpack 10% esbuild 10% Parcel 10%
after 300 ms Vite 15% Webpack 15% esbuild 14% Parcel 12% (two bars are done)
after 1 s Vite 48% Webpack 26% esbuild 14% Parcel 12% (all done)
Click "Hide results" bars removed
Click "Show results" Vite 0% Webpack 0% esbuild 0% Parcel 0% (fills again)
Math.round on its own. Don’t make the targets add up to 100 by adjusting one of them.The tests replace the browser clock (Date, performance.now, setTimeout, setInterval and requestAnimationFrame) with a fake one they move forward by hand, click your buttons, and read the page like a user would. You don’t need test ids, specific class names, or an exact HTML structure. They check that:
role="progressbar" elements, in option order, named by their option label, with the right aria-valuemin, aria-valuemax and aria-valuenow. Values are read at chosen times, allowing one point either way while a bar is still filling.N%, {votes} votes and Total: 120 votes appear as described.Capitalization and extra spacing don’t matter.