@carbon/react
Version:
React components for the Carbon Design System
93 lines (91 loc) • 2.97 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { usePrefix } from "../../internal/usePrefix.js";
import Loading_default from "../Loading/index.js";
import classNames from "classnames";
import { useEffect, useRef } from "react";
import PropTypes from "prop-types";
import { jsx, jsxs } from "react/jsx-runtime";
import { CheckmarkFilled, ErrorFilled } from "@carbon/icons-react";
//#region src/components/InlineLoading/InlineLoading.tsx
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const InlineLoading = ({ className, status = "active", iconDescription, description, onSuccess, successDelay = 1500, ...rest }) => {
const prefix = usePrefix();
const loadingClasses = classNames(`${prefix}--inline-loading`, className);
const timerRef = useRef(null);
useEffect(() => {
if (status === "finished") timerRef.current = setTimeout(() => {
if (onSuccess) onSuccess();
}, successDelay);
return () => {
if (timerRef.current) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
};
}, [
status,
onSuccess,
successDelay
]);
const getLoading = () => {
let iconLabel = iconDescription ? iconDescription : status;
if (status === "error") return /* @__PURE__ */ jsx(ErrorFilled, {
className: `${prefix}--inline-loading--error`,
children: /* @__PURE__ */ jsx("title", { children: iconLabel })
});
if (status === "finished") return /* @__PURE__ */ jsx(CheckmarkFilled, {
className: `${prefix}--inline-loading__checkmark-container`,
children: /* @__PURE__ */ jsx("title", { children: iconLabel })
});
if (status === "inactive" || status === "active") {
if (status === "inactive") return;
if (!iconDescription) iconLabel = "loading";
return /* @__PURE__ */ jsx(Loading_default, {
small: true,
description: iconLabel,
withOverlay: false,
active: status === "active"
});
}
};
const loadingText = description && /* @__PURE__ */ jsx("div", {
className: `${prefix}--inline-loading__text`,
children: description
});
const loading = getLoading();
const loadingAnimation = loading && /* @__PURE__ */ jsx("div", {
className: `${prefix}--inline-loading__animation`,
children: loading
});
return /* @__PURE__ */ jsxs("div", {
className: loadingClasses,
...rest,
"aria-live": rest["aria-live"] ?? (status === "inactive" ? "off" : "assertive"),
children: [loadingAnimation, loadingText]
});
};
InlineLoading.propTypes = {
className: PropTypes.string,
description: PropTypes.node,
iconDescription: PropTypes.string,
onSuccess: PropTypes.func,
status: PropTypes.oneOf([
"inactive",
"active",
"finished",
"error"
]),
successDelay: PropTypes.number
};
//#endregion
export { InlineLoading as default };