@razorpay/blade
Version:
The Design System that powers Razorpay
379 lines (371 loc) • 14.4 kB
JavaScript
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { useState, useEffect } from 'react';
import { m, AnimatePresence } from 'framer-motion';
import Rotate from './Rotate.web.js';
import '../BladeProvider/index.js';
import { msToSeconds } from '../../utils/msToSeconds.js';
import { cssBezierToArray } from '../../utils/cssBezierToArray.js';
import '../../utils/index.js';
import '../Icons/index.js';
import '../Typography/index.js';
import '../Box/BaseBox/index.js';
import { Collapsible } from '../Collapsible/Collapsible.js';
import { CollapsibleBody } from '../Collapsible/CollapsibleBody.js';
import '../Icons/_Svg/index.js';
import '../Icons/useIconProps/index.js';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import useIconProps from '../Icons/useIconProps/useIconProps.js';
import Svg from '../Icons/_Svg/Svg/Svg.web.js';
import Path from '../Icons/_Svg/Path/Path.web.js';
import { BaseBox } from '../Box/BaseBox/BaseBox.web.js';
import useTheme from '../BladeProvider/useTheme.js';
import { castWebType } from '../../utils/platform/castUtils.js';
import { Text } from '../Typography/Text/Text.js';
import ChevronUpIcon from '../Icons/ChevronUpIcon/ChevronUpIcon.js';
import ChevronDownIcon from '../Icons/ChevronDownIcon/ChevronDownIcon.js';
var _excluded = ["size", "color"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var SparkleIcon = function SparkleIcon(_ref) {
var size = _ref.size,
color = _ref.color,
styledProps = _objectWithoutProperties(_ref, _excluded);
var _useIconProps = useIconProps({
size: size,
color: color
}),
height = _useIconProps.height,
width = _useIconProps.width;
return /*#__PURE__*/jsx(Svg, _objectSpread(_objectSpread({}, styledProps), {}, {
width: width,
height: height,
viewBox: "0 0 9 9",
fill: "none",
children: /*#__PURE__*/jsx(Path, {
d: "M4.7169 8.67519C4.62025 8.93638 4.25084 8.93638 4.15419 8.67519L3.13296 5.91537C3.10258 5.83326 3.03784 5.76851 2.95572 5.73813L0.195898 4.7169C-0.0652872 4.62025 -0.0652871 4.25084 0.195899 4.15419L2.95572 3.13296C3.03784 3.10258 3.10258 3.03784 3.13297 2.95572L4.15419 0.1959C4.25084 -0.0652861 4.62026 -0.0652861 4.7169 0.195899L5.73813 2.95572C5.76851 3.03784 5.83326 3.10258 5.91537 3.13297L8.6752 4.15419C8.93638 4.25084 8.93638 4.62026 8.6752 4.7169L5.91537 5.73813C5.83326 5.76851 5.76851 5.83326 5.73813 5.91537L4.7169 8.67519Z",
fill: "#009E5C"
})
}));
};
// Spinning sparkle for the active step row (smaller)
var ActiveStepIcon = function ActiveStepIcon() {
return /*#__PURE__*/jsx(Rotate, {
animate: true,
children: /*#__PURE__*/jsx(SparkleIcon, {
size: "xsmall",
color: "feedback.icon.positive.intense"
})
});
};
var StepDot = function StepDot(_ref2) {
var status = _ref2.status;
return /*#__PURE__*/jsx(BaseBox, {
style: {
opacity: status === 'pending' ? 0.3 : 1
},
width: "6px",
height: "6px",
borderRadius: "max",
backgroundColor: status === 'completed' ? 'surface.icon.onSea.onSubtle' : 'surface.icon.gray.muted',
flexShrink: 0
});
};
// Connector color follows the step above it: green after completed, gray otherwise
var StepConnector = function StepConnector(_ref3) {
var fromStatus = _ref3.fromStatus;
return /*#__PURE__*/jsx(BaseBox, {
style: {
flex: 1,
minHeight: '6px'
},
width: "1px",
backgroundColor: fromStatus === 'completed' ? 'surface.icon.onSea.onSubtle' : 'surface.border.gray.muted'
});
};
var ShimmerOverlay = function ShimmerOverlay(_ref4) {
var children = _ref4.children,
isActive = _ref4.isActive;
var _useTheme = useTheme(),
theme = _useTheme.theme;
var shimmerColor = theme.colors.surface.text.staticWhite.muted;
var shimmerDuration = msToSeconds(theme.motion.duration['2xgentle']);
var shimmerDelay = msToSeconds(theme.motion.delay.gentle);
var shimmerEase = cssBezierToArray(castWebType(theme.motion.easing.standard));
return /*#__PURE__*/jsxs(BaseBox, {
position: "relative",
overflow: "hidden",
children: [children, isActive && /*#__PURE__*/jsx(m.span, {
"aria-hidden": true,
style: {
position: 'absolute',
inset: 0,
background: "linear-gradient(90deg, transparent 0%, ".concat(shimmerColor, " 50%, transparent 100%)"),
pointerEvents: 'none'
},
initial: {
x: '-100%'
},
animate: {
x: '100%'
},
transition: {
duration: shimmerDuration,
ease: shimmerEase,
repeat: Infinity,
repeatDelay: shimmerDelay
}
})]
});
};
var TraceRow = function TraceRow(_ref5) {
var text = _ref5.text,
isLast = _ref5.isLast,
stepStatus = _ref5.stepStatus;
return /*#__PURE__*/jsxs(BaseBox, {
display: "flex",
flexDirection: "row",
alignItems: "stretch",
children: [/*#__PURE__*/jsxs(BaseBox, {
display: "flex",
flexDirection: "column",
alignItems: "center",
flexShrink: 0,
width: "16px",
marginRight: "spacing.3",
children: [/*#__PURE__*/jsx(BaseBox, {
display: "flex",
alignItems: "center",
justifyContent: "center",
height: "17px",
flexShrink: 0,
children: stepStatus === 'active' ? /*#__PURE__*/jsx(ActiveStepIcon, {}) : /*#__PURE__*/jsx(StepDot, {
status: stepStatus
})
}), !isLast && /*#__PURE__*/jsx(StepConnector, {
fromStatus: stepStatus
})]
}), /*#__PURE__*/jsx(BaseBox, {
paddingBottom: isLast ? 'spacing.0' : 'spacing.2',
style: stepStatus === 'pending' ? {
opacity: 0.5
} : undefined,
children: /*#__PURE__*/jsx(ShimmerOverlay, {
isActive: stepStatus === 'active',
children: /*#__PURE__*/jsx(Text, {
color: stepStatus === 'active' ? 'surface.text.onSea.onSubtle' : 'surface.text.gray.muted',
size: "small",
weight: "regular",
children: text
})
})
})]
});
};
var ReasoningTraces = function ReasoningTraces(_ref6) {
var traces = _ref6.traces,
_ref6$status = _ref6.status,
status = _ref6$status === void 0 ? 'loading' : _ref6$status,
_ref6$title = _ref6.title,
title = _ref6$title === void 0 ? 'Explored' : _ref6$title,
activeStepIndex = _ref6.activeStepIndex;
// Upfront mode: all steps are known; activeStepIndex controls which is active
var isUpfrontMode = activeStepIndex !== undefined;
// Start collapsed in upfront mode so the open animation plays on mount
var _useState = useState(!isUpfrontMode),
_useState2 = _slicedToArray(_useState, 2),
isExpanded = _useState2[0],
setIsExpanded = _useState2[1];
var _useTheme2 = useTheme(),
theme = _useTheme2.theme;
var isLoading = status === 'loading';
var rowTransition = {
duration: msToSeconds(theme.motion.duration.moderate),
ease: cssBezierToArray(castWebType(theme.motion.easing.entrance))
};
var slideTransition = {
duration: msToSeconds(theme.motion.duration.xmoderate),
ease: cssBezierToArray(castWebType(theme.motion.easing.emphasized))
};
// Reset to expanded when status changes back to loading (for replay scenarios)
useEffect(function () {
if (status === 'loading') {
setIsExpanded(true);
}
}, [status]);
// Auto-collapse once all steps complete
useEffect(function () {
if (status === 'complete') {
var timer = setTimeout(function () {
return setIsExpanded(false);
}, 600);
return function () {
return clearTimeout(timer);
};
}
return undefined;
}, [status]);
var handleExpandChange = function handleExpandChange(_ref7) {
var next = _ref7.isExpanded;
setIsExpanded(next);
};
var getStepStatus = function getStepStatus(index) {
if (status === 'complete') return 'completed';
if (isUpfrontMode) {
if (index < activeStepIndex) return 'completed';
if (index === activeStepIndex) return 'active';
return 'pending';
}
// Streaming mode: last trace is active, all before are completed
return index < traces.length - 1 ? 'completed' : 'active';
};
var getTraceLabel = function getTraceLabel(trace, stepStatus) {
return stepStatus === 'completed' && trace.completedLabel ? trace.completedLabel : trace.label;
};
return /*#__PURE__*/jsxs(BaseBox, {
marginTop: "spacing.2",
marginBottom: "spacing.3",
children: [status === 'complete' && /*#__PURE__*/jsxs(BaseBox, {
as: "button",
display: "flex",
flexDirection: "row",
alignItems: "center",
gap: "spacing.2",
onClick: function onClick() {
return setIsExpanded(function (prev) {
return !prev;
});
},
cursor: "pointer",
style: {
background: 'none',
border: 'none'
},
padding: "spacing.0",
textAlign: "left",
marginBottom: "spacing.3",
children: [/*#__PURE__*/jsx(Text, {
color: "surface.text.gray.muted",
weight: "regular",
variant: "body",
size: "medium",
children: title
}), /*#__PURE__*/jsx(BaseBox, {
display: "flex",
alignItems: "center",
children: isExpanded ? /*#__PURE__*/jsx(ChevronUpIcon, {
size: "small",
color: "surface.icon.gray.muted"
}) : /*#__PURE__*/jsx(ChevronDownIcon, {
size: "small",
color: "surface.icon.gray.muted"
})
})]
}), /*#__PURE__*/jsx(Collapsible, {
isExpanded: isExpanded,
onExpandChange: handleExpandChange,
_shouldApplyWidthRestrictions: false,
children: /*#__PURE__*/jsx(CollapsibleBody, {
_hasMargin: false,
children: /*#__PURE__*/jsx(BaseBox, {
paddingTop: "spacing.2",
children: isUpfrontMode || !isLoading ?
// Upfront mode OR complete: render all traces with computed step status
traces.map(function (trace, index) {
var stepStatus = getStepStatus(index);
return /*#__PURE__*/jsx(TraceRow, {
text: getTraceLabel(trace, stepStatus),
isLast: index === traces.length - 1,
stepStatus: stepStatus
}, index);
}) :
/*#__PURE__*/
// Streaming mode (loading, steps added one by one):
// completed rows + active RollingText row
jsxs(Fragment, {
children: [traces.slice(0, -1).map(function (trace, index) {
// The last completed row just transitioned from the active position —
// it was already visible, so skip the entry animation for it.
var isJustCompleted = index === traces.length - 2;
return /*#__PURE__*/jsx(m.div, {
initial: isJustCompleted ? false : {
opacity: 0,
y: 8
},
animate: {
opacity: 1,
y: 0
},
transition: rowTransition,
children: /*#__PURE__*/jsx(TraceRow, {
text: getTraceLabel(trace, 'completed'),
isLast: false,
stepStatus: "completed"
})
}, trace.label);
}), traces.length > 0 && /*#__PURE__*/jsx(m.div, {
initial: {
opacity: 0,
y: 8
},
animate: {
opacity: 1,
y: 0
},
transition: rowTransition,
children: /*#__PURE__*/jsxs(BaseBox, {
display: "flex",
flexDirection: "row",
alignItems: "center",
gap: "spacing.3",
children: [/*#__PURE__*/jsx(BaseBox, {
display: "flex",
alignItems: "center",
justifyContent: "center",
flexShrink: 0,
width: "16px",
children: /*#__PURE__*/jsx(ActiveStepIcon, {})
}), /*#__PURE__*/jsx(BaseBox, {
position: "relative",
overflow: "hidden",
children: /*#__PURE__*/jsx(AnimatePresence, {
mode: "popLayout",
initial: false,
children: /*#__PURE__*/jsx(m.div, {
initial: {
y: 12,
opacity: 0
},
animate: {
y: 0,
opacity: 1
},
exit: {
y: -12,
opacity: 0,
position: 'absolute'
},
transition: slideTransition,
children: /*#__PURE__*/jsx(ShimmerOverlay, {
isActive: true,
children: /*#__PURE__*/jsx(Text, {
color: "surface.text.onSea.onSubtle",
size: "small",
weight: "regular",
children: getTraceLabel(traces[traces.length - 1], 'active')
})
})
}, traces.length)
})
})]
})
}, "active-row")]
})
})
})
})]
});
};
export { ReasoningTraces };
//# sourceMappingURL=ReasoningTraces.web.js.map