@carbon/react
Version:
React components for the Carbon Design System
97 lines (95 loc) • 3.52 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.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_usePrefix = require("../../internal/usePrefix.js");
const require_index = require("../Loading/index.js");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let react_jsx_runtime = require("react/jsx-runtime");
let _carbon_icons_react = require("@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 = require_usePrefix.usePrefix();
const loadingClasses = (0, classnames.default)(`${prefix}--inline-loading`, className);
const timerRef = (0, react.useRef)(null);
(0, react.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__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.ErrorFilled, {
className: `${prefix}--inline-loading--error`,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("title", { children: iconLabel })
});
if (status === "finished") return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.CheckmarkFilled, {
className: `${prefix}--inline-loading__checkmark-container`,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("title", { children: iconLabel })
});
if (status === "inactive" || status === "active") {
if (status === "inactive") return;
if (!iconDescription) iconLabel = "loading";
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_index.default, {
small: true,
description: iconLabel,
withOverlay: false,
active: status === "active"
});
}
};
const loadingText = description && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: `${prefix}--inline-loading__text`,
children: description
});
const loading = getLoading();
const loadingAnimation = loading && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: `${prefix}--inline-loading__animation`,
children: loading
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
className: loadingClasses,
...rest,
"aria-live": rest["aria-live"] ?? (status === "inactive" ? "off" : "assertive"),
children: [loadingAnimation, loadingText]
});
};
InlineLoading.propTypes = {
className: prop_types.default.string,
description: prop_types.default.node,
iconDescription: prop_types.default.string,
onSuccess: prop_types.default.func,
status: prop_types.default.oneOf([
"inactive",
"active",
"finished",
"error"
]),
successDelay: prop_types.default.number
};
//#endregion
exports.default = InlineLoading;