# All files AsyncButton.tsx

94.14% Statements 177/188
7.14% Branches 1/14
50% Functions 1/2
94.14% Lines 177/188

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
    1891x
    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
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x

    2x
    2x
    2x
    2x
    2x

    2x

    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    2x
    2x
    2x
    2x
    2x
    1x
     "use client";
    import { cnb } from "cnbuilder";
    import { forwardRef, type MouseEvent, type ReactNode } from "react";
    import { type BoxAlignItems } from "../box/styles.js";
    import { overlay } from "../overlay/overlayStyles.js";
    import { CircularProgress } from "../progress/CircularProgress.js";
    import { LinearProgress } from "../progress/LinearProgress.js";
    import { type ProgressTheme } from "../progress/types.js";
    import { useAsyncAction } from "../useAsyncAction.js";
    import { Button, type ButtonProps } from "./Button.js";

    const noop = (): void => {
      // do nothing
    };

    /**
     * @remarks \@since 6.0.0
     */
    export type AsyncButtonLoadingType =
      | "circular-before"
      | "circular-after"
      | "circular-overlay"
      | "linear-above"
      | "linear-below";

    /**
     * @remarks \@since 6.0.0
     */
    export interface AsyncButtonProps extends ButtonProps {
      /**
       * When this is defined and returns a `Promise`, the loading indicator will
       * display until the promise has resolved.
       *
       * @defaultValue `() => {}`
       */
      onClick?(event: MouseEvent<HTMLButtonElement>): Promise<void> | void;

      /**
       * Set this to `true` to manually display a loading spinner.
       *
       * @defaultValue `false`
       */
      loading?: boolean;

      /**
       * - `"circular-overlay"` - Covers and hides the button content with a
       *   centered circular progress
       * - `"circular-before"` - Renders a circular progress bar before the button
       *   content which is useful when rendering an icon before the button text.
       *   See {@link beforeAddon} as well.
       * - `"circular-after"` - Renders a circular progress bar before the button
       *   content which is useful when rendering an icon after the button text.
       *   See {@link afterAddon} as well.
       * - `"linear-above"` - Renders a linear progress bar at the top of the button
       *   while still displaying the button contents. Usually looks good for
       *   outlined buttons.
       * - `"linear-below"` - Renders a linear progress bar at the bottom of the
       *   button while still displaying the button contents. Usually looks good for
       *   outlined buttons.
       *
       * @defaultValue `"circular-overlay"`
       */
      loadingType?: AsyncButtonLoadingType;

      /**
       * Optional content to display instead of the default `children` while
       * loading.
       */
      loadingChildren?: ReactNode;

      /**
       * Set this to `true` to use the `disabled` theme while loading.
       *
       * @defaultValue `false`
       */
      loadingDisabledTheme?: boolean;

      /**
       * This should be used when the {@link loadingType} is set to
       * `"circular-before"`, an icon should appear before the other content in
       * the button, and the loading indicator should replace the icon.
       */
      beforeAddon?: ReactNode;

      /**
       * This should be used when the {@link loadingType} is set to
       * `"circular-after"`, an icon should appear before the other content in
       * the button, and the loading indicator should replace the icon.
       */
      afterAddon?: ReactNode;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export const AsyncButton = forwardRef<HTMLButtonElement, AsyncButtonProps>(
      function AsyncButton(props, ref) {
        const {
          onClick = noop,
          children,
          theme,
          className,
          disabled,
          loading: propLoading = false,
          loadingType = "circular-overlay",
          loadingChildren: pendingChildren,
          loadingDisabledTheme = false,
          afterAddon: propAfterAddon,
          beforeAddon: propBeforeAddon,
          ...remaining
        } = props;
        const { handleAsync, pending } = useAsyncAction({ disabled });
        const loading = pending || propLoading;

        let progressTheme: ProgressTheme = "current-color";
        if (!theme || theme === "clear" || theme === "disabled") {
          progressTheme = "primary";
        }

        const progress = loadingType.includes("linear") ? (
          <LinearProgress theme={progressTheme} />
        ) : (
          <CircularProgress theme={progressTheme} />
        );

        let afterAddon = propAfterAddon;
        let beforeAddon = propBeforeAddon;
        let overlayElement: ReactNode;
        let isOverlayCover = false;
        switch (loadingType) {
          case "circular-before":
            beforeAddon = loading ? progress : propBeforeAddon;
            break;
          case "circular-after":
            afterAddon = loading ? progress : propAfterAddon;
            break;
          case "circular-overlay":
          case "linear-above":
          case "linear-below": {
            let alignItems: BoxAlignItems = "center";
            if (loadingType === "linear-above") {
              alignItems = "start";
            } else if (loadingType === "linear-below") {
              alignItems = "end";
            } else {
              isOverlayCover = true;
            }

            overlayElement = loading && (
              <span
                className={overlay({
                  active: true,
                  visible: true,
                  absolute: true,
                  align: alignItems,
                })}
              >
                {progress}
              </span>
            );
            break;
          }
        }

        return (
          <Button
            {...remaining}
            aria-disabled={loading || undefined}
            ref={ref}
            disabled={disabled}
            className={cnb(
              "rmd-button--async",
              loading && isOverlayCover && "rmd-button--async-overlay",
              className
            )}
            theme={loading && loadingDisabledTheme ? "disabled" : theme}
            onClick={handleAsync((event) => Promise.resolve(onClick(event)))}
          >
            {beforeAddon}
            {loading && typeof pendingChildren !== "undefined"
              ? pendingChildren
              : children}
            {afterAddon}
            {overlayElement}
          </Button>
        );
      }
    );