mirror of
https://github.com/outline/outline.git
synced 2026-11-22 14:01:41 +03:00
91 lines
2.2 KiB
TypeScript
91 lines
2.2 KiB
TypeScript
import styled, { useTheme } from "styled-components";
|
||
|
||
const cleanPercentage = (percentage: number) => {
|
||
const tooLow = !Number.isFinite(+percentage) || percentage < 0;
|
||
const tooHigh = percentage > 100;
|
||
return tooLow ? 0 : tooHigh ? 100 : +percentage;
|
||
};
|
||
|
||
const Circle = ({
|
||
color,
|
||
percentage,
|
||
offset,
|
||
}: {
|
||
color: string;
|
||
percentage?: number;
|
||
offset: number;
|
||
}) => {
|
||
const radius = offset * 0.7;
|
||
const circumference = 2 * Math.PI * radius;
|
||
let strokeDashoffset = 0;
|
||
|
||
if (percentage !== undefined) {
|
||
// because the circle is so small, anything greater than 85% appears like 100%
|
||
const visible = percentage > 85 && percentage < 100 ? 85 : percentage;
|
||
strokeDashoffset = ((100 - visible) * circumference) / 100;
|
||
}
|
||
|
||
return (
|
||
<circle
|
||
r={radius}
|
||
cx={offset}
|
||
cy={offset}
|
||
fill="none"
|
||
stroke={color}
|
||
strokeWidth={2.5}
|
||
strokeDasharray={circumference}
|
||
strokeLinecap="round"
|
||
style={{
|
||
strokeDashoffset: `${strokeDashoffset}px`,
|
||
opacity: percentage === 0 ? 0 : 1,
|
||
transition: "stroke-dashoffset 0.6s ease, opacity 0.6s ease",
|
||
}}
|
||
/>
|
||
);
|
||
};
|
||
|
||
/**
|
||
* Renders a small circular progress indicator.
|
||
*
|
||
* @param percentage - the progress to display, clamped to the range 0–100.
|
||
* @param size - the width and height of the indicator in pixels.
|
||
* @param label - an accessible name announced by assistive technology.
|
||
* @returns the rendered progress bar element.
|
||
*/
|
||
const CircularProgressBar = ({
|
||
percentage,
|
||
size = 16,
|
||
label,
|
||
}: {
|
||
percentage: number;
|
||
size?: number;
|
||
label?: string;
|
||
}) => {
|
||
const theme = useTheme();
|
||
percentage = cleanPercentage(percentage);
|
||
const offset = Math.floor(size / 2);
|
||
|
||
return (
|
||
<SVG
|
||
width={size}
|
||
height={size}
|
||
role="progressbar"
|
||
aria-label={label}
|
||
aria-valuenow={Math.round(percentage)}
|
||
aria-valuemin={0}
|
||
aria-valuemax={100}
|
||
>
|
||
<g transform={`rotate(-90 ${offset} ${offset})`}>
|
||
<Circle color={theme.progressBarBackground} offset={offset} />
|
||
<Circle color={theme.accent} percentage={percentage} offset={offset} />
|
||
</g>
|
||
</SVG>
|
||
);
|
||
};
|
||
|
||
const SVG = styled.svg`
|
||
flex-shrink: 0;
|
||
`;
|
||
|
||
export default CircularProgressBar;
|