import React, { useEffect, useState } from "react"; import { Overlay2 as BlueprintOverlay, OverlayProps as BlueprintOverlayProps, Spinner as BlueprintSpinner, SpinnerProps as BlueprintSpinnerProps, } from "@blueprintjs/core"; import Color, { ColorLike } from "color"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; import { ButtonProps } from "./../Button/Button"; type SpinnerPosition = "local" | "inline" | "global"; type SpinnerSize = "tiny" | "small" | "medium" | "large" | "xlarge" | "inherit"; type SpinnerStroke = "thin" | "medium" | "bold"; /** A spinner that is either displayed globally or locally. */ export interface SpinnerProps extends Omit { /** * Must be a valid css color definition. * `intent` property will always overwrite this setting. */ color?: ColorLike | "inherit"; /** * Intent state of the spinner. * When used the spinner is colored. * Property overwrites `elevated` setting. */ intent?: ButtonProps["intent"]; /** * Highlight the spinner. * It is displayed with accent color intent. */ elevated?: boolean; /** * Additional CSS class names. */ className?: string; /** * Position where and how the spinner is displayed: * * `local`: the spinner is displayed as centered overlay to the neareast parent with relative (or equivalent) positioning * * `inline`: the spinner is displayed as inline element * * `global`: the spinner is displayed including backdrop centered over the full viewport */ position?: SpinnerPosition; /** * The size of the spinner. * The default size relates to the `position`. */ size?: SpinnerSize; /** * The stroke width that is used to visualize the spinner. * The default size relates to the `position`. * There are only rare cases to set this property, */ stroke?: SpinnerStroke; /** * Delay when to show the spinner in ms. */ delay?: number; /** * Includes a backdrop behind the spinner that narrows visibility of the area behind the spinner. * This option only works with "local" spinners, for "inline" spinners there is no backdrop, "global" spinners always have backdrops. * The backdrop and the spinner are located over the nearest parent element that is styled by `position: relative` or some other CSS rule with an equivalent outcome. */ showLocalBackdrop?: boolean; /** * Use this property to alter the display of the backdrop used for the global spinner */ overlayProps?: BlueprintOverlayProps; } export const Spinner = ({ className = "", color = "inherit", intent, elevated, position = "local", size, stroke, showLocalBackdrop = false, delay = 0, overlayProps, ...otherProps }: SpinnerProps) => { const [showSpinner, setShowSpinner] = useState(!delay || delay <= 0); useEffect(() => { if (!showSpinner) { const timeoutId = setTimeout(() => setShowSpinner(true), delay); return () => clearTimeout(timeoutId); } return; }, [showSpinner, delay]); const internSizes = { thin: 100, medium: 50, bold: 10, }; const spinnerElement = position === "inline" ? "span" : "div"; let spinnerSize; let spinnerStroke; switch (position) { case "local": spinnerSize = size ?? "medium"; spinnerStroke = stroke ?? "medium"; break; case "global": spinnerSize = size ?? "large"; spinnerStroke = stroke ?? "thin"; break; case "inline": spinnerSize = size ?? "inherit"; spinnerStroke = stroke ?? "bold"; break; default: spinnerSize = size ?? "medium"; spinnerStroke = stroke ?? "medium"; } let spinner = ( ); if (!intent) { try { const spinnerColor = color === "inherit" ? color : Color(color).rgb().toString(); spinner = {spinner}; } catch { spinner = {spinner}; // eslint-disable-next-line no-console console.warn("Spinner received invalid color property: " + color); } } return position === "global" ? ( {spinner} ) : showSpinner ? ( spinner ) : null; }; export default Spinner;