|
| 1 | +// Copyright (C) 2022-2025 Intel Corporation |
| 2 | +// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE |
| 3 | + |
| 4 | +import { useMemo } from 'react'; |
| 5 | + |
| 6 | +import { ColorValue } from '@react-types/shared/src/dna'; |
| 7 | + |
| 8 | +interface CircularProgressProps { |
| 9 | + percentage: number; |
| 10 | + size?: number; |
| 11 | + labelFontSize?: number; |
| 12 | + strokeWidth?: number; |
| 13 | + labelFontColor?: ColorValue; |
| 14 | + backStrokeColor?: ColorValue; |
| 15 | + color?: ColorValue; |
| 16 | + hasError?: boolean; |
| 17 | + checkMarkOnComplete?: boolean; |
| 18 | + checkMarkSize?: number; |
| 19 | + checkMarkColor?: ColorValue | string; |
| 20 | +} |
| 21 | + |
| 22 | +export const CircularProgress = ({ |
| 23 | + percentage, |
| 24 | + size = 50, |
| 25 | + strokeWidth = 4, |
| 26 | + labelFontSize = 10, |
| 27 | + labelFontColor = 'gray-600', |
| 28 | + backStrokeColor = 'gray-100', |
| 29 | + color = 'blue-400', |
| 30 | + hasError = false, |
| 31 | + checkMarkSize = 50, |
| 32 | + checkMarkOnComplete = true, |
| 33 | + checkMarkColor = '--energy-blue-shade', |
| 34 | +}: CircularProgressProps): JSX.Element => { |
| 35 | + const progress = Math.max(0, Math.min(100, percentage)) | 0; |
| 36 | + |
| 37 | + const viewBox = useMemo<string>((): string => `0 0 ${size} ${size}`, [size]); |
| 38 | + const radius = useMemo<number>((): number => (size - strokeWidth) / 2, [size, strokeWidth]); |
| 39 | + const circumference = useMemo<number>((): number => radius * Math.PI * 2, [radius]); |
| 40 | + const dash = useMemo<number>((): number => (progress * circumference) / 100, [progress, circumference]); |
| 41 | + const getCheckMarkColor = useMemo<string>( |
| 42 | + (): string => |
| 43 | + checkMarkColor.startsWith('--') ? `var(${checkMarkColor})` : `var(--spectrum-global-color-${color})`, |
| 44 | + [checkMarkColor, color] |
| 45 | + ); |
| 46 | + |
| 47 | + return ( |
| 48 | + <svg width={size} height={size} viewBox={viewBox} aria-label='progress-circular-loader'> |
| 49 | + <circle |
| 50 | + fill='none' |
| 51 | + stroke={`var(--spectrum-global-color-${backStrokeColor})`} |
| 52 | + cx={size / 2} |
| 53 | + cy={size / 2} |
| 54 | + r={radius} |
| 55 | + strokeWidth={`${strokeWidth}px`} |
| 56 | + /> |
| 57 | + <circle |
| 58 | + fill='none' |
| 59 | + stroke={ |
| 60 | + checkMarkOnComplete |
| 61 | + ? progress < 100 |
| 62 | + ? `var(--spectrum-global-color-${color})` |
| 63 | + : getCheckMarkColor |
| 64 | + : `var(--spectrum-global-color-${color})` |
| 65 | + } |
| 66 | + cx={size / 2} |
| 67 | + cy={size / 2} |
| 68 | + r={radius} |
| 69 | + strokeWidth={`${strokeWidth}px`} |
| 70 | + transform={`rotate(-90 ${size / 2} ${size / 2})`} |
| 71 | + strokeDasharray={`${[dash, circumference - dash]}`} |
| 72 | + strokeLinecap='square' |
| 73 | + style={{ transition: 'all 0.5s' }} |
| 74 | + /> |
| 75 | + <text |
| 76 | + fill={ |
| 77 | + checkMarkOnComplete |
| 78 | + ? progress < 100 |
| 79 | + ? `var(--spectrum-global-color-${labelFontColor})` |
| 80 | + : getCheckMarkColor |
| 81 | + : `var(--spectrum-global-color-${labelFontColor})` |
| 82 | + } |
| 83 | + fontSize={ |
| 84 | + checkMarkOnComplete |
| 85 | + ? progress < 100 |
| 86 | + ? `${labelFontSize}px` |
| 87 | + : `${checkMarkSize}px` |
| 88 | + : `${labelFontSize}px` |
| 89 | + } |
| 90 | + dy={ |
| 91 | + checkMarkOnComplete |
| 92 | + ? progress < 100 |
| 93 | + ? `${labelFontSize / 2}px` |
| 94 | + : `${checkMarkSize / 2.5}px` |
| 95 | + : `${labelFontSize / 2}px` |
| 96 | + } |
| 97 | + textAnchor='middle' |
| 98 | + x='50%' |
| 99 | + y='50%' |
| 100 | + > |
| 101 | + {hasError ? 'N/A' : checkMarkOnComplete ? (progress < 100 ? `${progress}%` : '✓') : `${progress}%`} |
| 102 | + </text> |
| 103 | + </svg> |
| 104 | + ); |
| 105 | +}; |
0 commit comments