@aniruddha1806/progress-tracker
Version:
A customizable progress tracker component for React applications
148 lines (138 loc) • 8.69 kB
JavaScript
'use strict';
var React = require('react');
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var ProgressTracker = function (_a) {
var steps = _a.steps, currentStep = _a.currentStep, _b = _a.orientation, orientation = _b === void 0 ? "horizontal" : _b, _c = _a.className, className = _c === void 0 ? "" : _c, _d = _a.stepClassName, stepClassName = _d === void 0 ? "" : _d, _e = _a.labelClassName, labelClassName = _e === void 0 ? "" : _e, _f = _a.sublabelClassName, sublabelClassName = _f === void 0 ? "" : _f, _g = _a.iconClassName, iconClassName = _g === void 0 ? "" : _g, _h = _a.connectorClassName, connectorClassName = _h === void 0 ? "" : _h, _j = _a.activeConnectorClassName, activeConnectorClassName = _j === void 0 ? "" : _j, _k = _a.inactiveConnectorClassName, inactiveConnectorClassName = _k === void 0 ? "" : _k, _l = _a.activeColor, activeColor = _l === void 0 ? "#0074D9" : _l, _m = _a.inactiveColor, inactiveColor = _m === void 0 ? "#E1E8ED" : _m, _o = _a.showLabels, showLabels = _o === void 0 ? true : _o, _p = _a.showConnectors, showConnectors = _p === void 0 ? true : _p, _q = _a.connectorStyle, connectorStyle = _q === void 0 ? "solid" : _q, _r = _a.size, size = _r === void 0 ? "medium" : _r, renderCustomStep = _a.renderCustomStep;
// Size mappings
var sizeMap = {
small: { icon: 24, fontSize: 12, subFontSize: 10, spacing: 8 },
medium: { icon: 32, fontSize: 14, subFontSize: 12, spacing: 12 },
large: { icon: 48, fontSize: 16, subFontSize: 14, spacing: 16 },
};
var currentSize = sizeMap[size];
var verticalStepSpacing = 32; // Space between vertical steps
// Base styles
var baseStyles = {
container: {
display: "flex",
flexDirection: orientation === "horizontal" ? "column" : "row",
width: "100%",
padding: "16px",
boxSizing: "border-box",
},
stepsWrapper: {
display: "flex",
flexDirection: orientation === "horizontal" ? "row" : "column",
width: "100%",
position: "relative",
},
step: {
display: "flex",
flexDirection: orientation === "horizontal" ? "column" : "row",
alignItems: "center",
justifyContent: orientation === "horizontal" ? "flex-start" : "flex-start",
position: "relative",
zIndex: 2,
},
stepIcon: {
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "".concat(currentSize.icon, "px"),
height: "".concat(currentSize.icon, "px"),
borderRadius: "50%",
backgroundColor: inactiveColor,
color: "#FFFFFF",
transition: "all 0.3s ease",
},
stepIconActive: {
backgroundColor: activeColor,
},
stepIconCompleted: {
backgroundColor: activeColor,
},
labelContainer: {
display: "flex",
flexDirection: "column",
alignItems: orientation === "horizontal" ? "center" : "flex-start",
marginTop: orientation === "horizontal" ? "".concat(currentSize.spacing, "px") : "0",
marginLeft: orientation === "horizontal" ? "0" : "".concat(currentSize.spacing, "px"),
},
label: {
fontSize: "".concat(currentSize.fontSize, "px"),
fontWeight: 500,
textAlign: orientation === "horizontal" ? "center" : "left",
color: "#333333",
},
sublabel: {
fontSize: "".concat(currentSize.subFontSize, "px"),
color: "#666666",
marginTop: "2px",
},
connector: __assign(__assign({ position: "absolute", transition: "all 0.3s ease" }, (connectorStyle === "dashed" && { borderStyle: "dashed" })), (connectorStyle === "dotted" && { borderStyle: "dotted" })),
};
// Render the connectors separately for better control
var renderConnectors = function () {
if (!showConnectors)
return null;
return steps.map(function (_, index) {
if (index === steps.length - 1)
return null; // No connector after last step
var isCompleted = index < currentStep;
if (orientation === "horizontal") {
return (React.createElement("div", { key: "connector-".concat(index), style: __assign(__assign({}, baseStyles.connector), { height: "2px", left: "calc(".concat((index + 0.5) * (100 / steps.length), "%)"), right: "calc(".concat(100 - (index + 1.5) * (100 / steps.length), "%)"), top: "".concat(currentSize.icon / 2, "px"), backgroundColor: isCompleted ? activeColor : inactiveColor }), className: "".concat(connectorClassName, " ").concat(isCompleted ? activeConnectorClassName : inactiveConnectorClassName) }));
}
else {
// Vertical connector
return (React.createElement("div", { key: "connector-".concat(index), style: __assign(__assign({}, baseStyles.connector), { width: "2px", top: "".concat(index * (verticalStepSpacing + currentSize.icon) + currentSize.icon, "px"), height: "".concat(verticalStepSpacing, "px"), left: "".concat(currentSize.icon / 2 - 1, "px"), backgroundColor: isCompleted ? activeColor : inactiveColor }), className: "".concat(connectorClassName, " ").concat(isCompleted ? activeConnectorClassName : inactiveConnectorClassName) }));
}
});
};
return (React.createElement("div", { style: baseStyles.container, className: className },
React.createElement("div", { style: baseStyles.stepsWrapper },
renderConnectors(),
steps.map(function (step, index) {
var isCompleted = index < currentStep;
var isActive = index === currentStep;
var isLast = index === steps.length - 1;
// If custom render function is provided, use it
if (renderCustomStep) {
return renderCustomStep(step, index, isCompleted, isActive);
}
return (React.createElement("div", { key: "step-".concat(index), style: __assign(__assign({}, baseStyles.step), (orientation === "horizontal"
? { flex: 1 }
: { marginBottom: isLast ? "0" : "".concat(verticalStepSpacing, "px") })), className: stepClassName },
React.createElement("div", { style: __assign(__assign(__assign({}, baseStyles.stepIcon), (isActive ? baseStyles.stepIconActive : {})), (isCompleted ? baseStyles.stepIconCompleted : {})), className: iconClassName }, step.icon),
showLabels && (React.createElement("div", { style: baseStyles.labelContainer },
React.createElement("div", { style: baseStyles.label, className: labelClassName }, step.label),
step.sublabel && (React.createElement("div", { style: baseStyles.sublabel, className: sublabelClassName }, step.sublabel))))));
}))));
};
exports.ProgressTracker = ProgressTracker;
//# sourceMappingURL=index.js.map