# All files / progress CircularProgress.tsx

63.44% Statements 151/238
100% Branches 0/0
0% Functions 0/1
63.44% Lines 151/238

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    2391x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

     import { cnb } from "cnbuilder";
    import {
      forwardRef,
      useMemo,
      type CSSProperties,
      type HTMLAttributes,
    } from "react";
    import { cssUtils } from "../cssUtils.js";
    import { type LabelRequiredForA11y } from "../types.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { bem } from "../utils/bem.js";
    import { getPercentage } from "../utils/getPercentage.js";
    import { type ProgressProps } from "./types.js";

    /**
     * @remarks \@since 6.0.0
     * Removed the `determinateRotateDegrees` prop since the determinate state no
     * longer rotates while increasing value.
     * Added the `disableShrink` prop.
     * Renamed the `small` prop to `dense` to match other components.
     */
    export interface CircularProgressProps
      extends Omit<HTMLAttributes<HTMLSpanElement>, "id">,
        ProgressProps {
      /**
       * An optional style to apply to the svg within the circular progress. The
       * values of this style object will be merged with the current determinate
       * style (if it exists).
       */
      svgStyle?: CSSProperties;

      /**
       * An optional className to apply to the svg within the circular progress.
       */
      svgClassName?: string;

      /**
       * An optional style to apply to the circle within the circular progress. The
       * values of this style object will be merged with the current determinate
       * style (if it exists).
       */
      circleStyle?: CSSProperties;

      /**
       * An optional className to apply to the circle within the circular progress.
       */
      circleClassName?: string;

      /**
       * The radius for the circle. It is generally recommended to have the radius
       * be 1/2 of the viewbox and minus a few more pixels so that there is some
       * surrounding padding. You probably shouldn't really be changing this prop
       * though.
       *
       * @defaultValue `30`
       */
      radius?: number;

      /**
       * The center point for the circle. This should be half of the `viewBox` prop
       * 99% of the time and probably won't be changed.
       *
       * @defaultValue `33`
       */
      center?: number;

      /**
       * The viewbox for the child svg. I wouldn't recommend changing this value as
       * you will also need to update the `dashoffset` in both Sass and this prop to
       * get the animation to look nice again.
       *
       * @defaultValue `0 0 66 66`
       */
      viewBox?: string;

      /**
       * The `stroke-dashoffset` for the circle within the SVG. You probably won't
       * be changing this value that much as it should match the
       * `$rmd-progress-circle-dashoffset` Sass variable. This is really just used
       * to help to create the determinate progress animation.
       *
       * @defaultValue `187`
       */
      dashoffset?: number;

      /**
       * Boolean if the circular progress should be centered using left and right
       * margins.
       *
       * @defaultValue `false`
       */
      disableCentered?: boolean;

      /**
       * Set to `true` to render as a smaller size.
       *
       * @defaultValue `false`
       * @remarks
       * \@since 2.3.0
       * \@since 6.0.0 Renamed from `small`
       */
      dense?: boolean;

      /**
       * Set this to `true` to update the indeterminate behavior to only rotate
       * which will increase performance during CPU-intensive tasks or when many
       * loading spinners are displayed at once on the page.
       *
       * @defaultValue `false`
       * @remarks \@since 6.0.0
       */
      disableShrink?: boolean;
    }

    const styles = bem("rmd-circular-progress");

    /**
     * **Server Component**
     *
     * @example
     * Indeterminate Example
     * ```tsx
     * import { CircularProgress } from "@react-md/core":
     * import { type ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return <CircularProgress aria-label="Loading" />;
     * }
     * ```
     *
     * @example
     * Determinate Example
     * ```tsx
     * import { CircularProgress } from "@react-md/core":
     * import { useState, type ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   // a number from 0 - 100
     *   const [progress, setProgress] = useState(0);
     *
     *   return <CircularProgress aria-label="File upload" value={progress} />;
     * }
     * ```
     *
     * @remarks \@since 6.0.0 Updated the determinate circular progress to no longer
     * rotate while increasing the value and require a label for accessibility.
     */
    export const CircularProgress = forwardRef<
      HTMLSpanElement,
      LabelRequiredForA11y<CircularProgressProps>
    >(function CircularProgress(props, ref) {
      const {
        id: propId,
        className,
        svgStyle,
        svgClassName,
        circleStyle: propCircleStyle,
        circleClassName,
        value,
        min = 0,
        max = 100,
        radius = 30,
        center = 33,
        viewBox = "0 0 66 66",
        theme = "primary",
        dense = false,
        dashoffset = 187,
        disableShrink = false,
        disableCentered = false,
        disableTransition = false,
        ...remaining
      } = props;

      const id = useEnsuredId(propId, "circular-progress");
      let progress: number | undefined;
      if (typeof value === "number") {
        progress = getPercentage({ min, max, value, validate: true });
      }

      const circleStyle = useMemo(() => {
        if (typeof progress !== "number") {
          return propCircleStyle;
        }

        return {
          ...propCircleStyle,
          strokeDashoffset: dashoffset - dashoffset * progress,
        };
      }, [progress, propCircleStyle, dashoffset]);

      const determinate = typeof progress === "number";
      const indeterminate = !determinate;
      return (
        <span
          {...remaining}
          id={id}
          ref={ref}
          role="progressbar"
          aria-valuemin={min}
          aria-valuemax={max}
          aria-valuenow={value}
          className={cnb(
            styles({ dense, centered: !disableCentered }),
            theme !== "current-color" && cssUtils({ textColor: theme }),
            className
          )}
        >
          <svg
            style={svgStyle}
            className={cnb(
              styles("svg", {
                determinate,
                indeterminate: indeterminate && !disableShrink,
                "rotate-only": indeterminate && disableShrink,
              }),
              svgClassName
            )}
            viewBox={viewBox}
          >
            <circle
              style={circleStyle}
              className={cnb(
                styles("circle", {
                  animate: !disableTransition && determinate,
                  determinate,
                  indeterminate: indeterminate && !disableShrink,
                  "rotate-only": indeterminate && disableShrink,
                }),
                circleClassName
              )}
              r={radius}
              cx={center}
              cy={center}
            />
          </svg>
        </span>
      );
    });