File size: 1,866 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import clsx from 'clsx';
import type { ReactNode } from 'react';

import './style.scss';

const CircularProgressBar = ( {
	currentStep,
	numberOfSteps,
	size,
	enableDesktopScaling = false,
	strokeColor,
	strokeWidth = 4,
	showProgressText = true,
	customText,
	variant,
}: {
	currentStep: number | null;
	numberOfSteps: number | null;
	size: number;
	enableDesktopScaling?: boolean;
	strokeColor?: string;
	strokeWidth?: number;
	showProgressText?: boolean;
	customText?: ReactNode;
	variant?: 'success';
} ) => {
	const SIZE = size;
	const RADIUS = SIZE / 2 - strokeWidth / 2;
	const FULL_ARC = 2 * Math.PI * RADIUS;

	if ( currentStep === null || ! numberOfSteps ) {
		return null;
	}

	return (
		<div
			role="progressbar"
			className={ clsx( 'circular__progress-bar', {
				'desktop-scaling': enableDesktopScaling,
				'is-success': variant === 'success',
			} ) }
			style={ { width: SIZE, height: SIZE } }
		>
			<svg
				viewBox={ `0 0 ${ SIZE } ${ SIZE }` }
				style={ { width: SIZE, height: SIZE } }
				fill="none"
				xmlns="http://www.w3.org/2000/svg"
			>
				<circle
					className="circular__progress-bar-empty-circle"
					fill="none"
					cx={ SIZE / 2 }
					cy={ SIZE / 2 }
					r={ RADIUS }
					strokeWidth={ strokeWidth }
				/>
				<circle
					style={ {
						display: currentStep === 0 ? 'none' : 'block',
						stroke: strokeColor,
						strokeDasharray: `${ FULL_ARC * ( currentStep / numberOfSteps ) }, ${ FULL_ARC }`,
					} }
					className="circular__progress-bar-fill-circle"
					fill="none"
					cx={ SIZE / 2 }
					cy={ SIZE / 2 }
					r={ RADIUS }
					strokeWidth={ strokeWidth }
				/>
			</svg>
			{ ( customText || showProgressText ) && (
				<div className="circular__progress-bar-text">
					{ customText || `${ currentStep }/${ numberOfSteps }` }
				</div>
			) }
		</div>
	);
};

export default CircularProgressBar;