Forked from eYinka/tailwind-radial-progress-bar.txt
Last active
November 7, 2024 08:45
-
-
Save maxotuteye/86dc828106847c471a0abbb11a631550 to your computer and use it in GitHub Desktop.
Tailwind CSS Radial Progress Bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This is a react-based fork of tailwind-radial-progress-bar. | |
* The size is the size of the containing div, given as a tailwind dimension value | |
* The radius is provided as a prop, and circumference is calculated based on it. | |
* The strokeDashOffset is dynamically calculated from the circumference. | |
* The progress is a number as a percentage, such as 50, for 50%. | |
*/ | |
export const RadialProgressBar = ({size, radius, progress}: { size: number, radius: number, progress: number }) => { | |
const width = `w-${size}` | |
const height = `h-${size}` | |
const circumference = Number((2 * Math.PI * radius).toFixed(1)) | |
const strokeDashOffset = (circumference - (circumference * progress) / 100) | |
return ( | |
<div className={`relative ${width} ${height}`}> | |
<svg className="w-full h-full" viewBox="0 0 100 100"> | |
<circle | |
className="text-gray-200 stroke-current" | |
strokeWidth="10" | |
cx="50" | |
cy="50" | |
r={radius} | |
fill="transparent" | |
/> | |
<circle | |
className="text-indigo-500 progress-ring__circle stroke-current" | |
strokeWidth="10" | |
strokeLinecap="round" | |
cx="50" | |
cy="50" | |
r={radius} | |
fill="transparent" | |
strokeDasharray={circumference} | |
strokeDashoffset={`${strokeDashOffset}px`}/> | |
<text | |
x="50" | |
y="50" | |
fontSize="12" | |
textAnchor="middle" | |
alignmentBaseline="middle">{progress}% | |
</text> | |
</svg> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great work!
I made some improvements / set some defaults over here, take a look if you'd like :)