baseui
Version:
A React Component library implementing the Base design language
117 lines (111 loc) • 5.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _constants = require("./constants");
var _styledComponents = require("./styled-components");
var _overrides = require("../helpers/overrides");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } /*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/ /* global window */
const defaults = {
Root: _styledComponents.StyledProgressBarRoundedRoot,
Svg: _styledComponents.StyledProgressBarRoundedSvg,
TrackBackground: _styledComponents.StyledProgressBarRoundedTrackBackground,
TrackForeground: _styledComponents.StyledProgressBarRoundedTrackForeground,
Text: _styledComponents.StyledProgressBarRoundedText
};
// @ts-ignore
function roundTo(n, digits) {
if (digits === undefined) {
digits = 0;
}
const multiplicator = Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
const test = Math.round(n) / multiplicator;
return +test.toFixed(digits);
}
function ProgressBarRounded({
progress = 0,
size = _constants.SIZE.medium,
animate = true,
inline = false,
overrides = {},
...restProps
}) {
const {
Root: [Root, rootProps],
Svg: [Svg, svgProps],
TrackBackground: [TrackBackground, trackBackgroundProps],
TrackForeground: [TrackForeground, trackForegroundProps],
Text: [Text, textProps]
} = (0, _overrides.useOverrides)(defaults, overrides);
// Get path length after initial render
const [pathLength, setPathLength] = React.useState(0);
const pathRef = React.useRef();
React.useEffect(() => {
if (pathRef.current && pathRef.current.getTotalLength) {
setPathLength(pathRef.current.getTotalLength());
}
}, []);
// Animation
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const animationFrameRef = React.useRef();
const [pathProgress, setPathProgress] = React.useState(0);
React.useEffect(() => {
if (!animate) {
setPathProgress(progress);
return;
}
if (window && animationFrameRef.current) {
window.cancelAnimationFrame(animationFrameRef.current);
}
let animationDuration = Math.max(1000 * (progress - pathProgress), 250);
// @ts-ignore
let animationTimeStarted;
function loop(now = 0) {
// @ts-ignore
if (!animationTimeStarted) {
animationTimeStarted = now;
}
// @ts-ignore
let animationTimeElapsed = now - animationTimeStarted;
// Move out of state - might need to reverse calculate the path progress for interruped animations
let currentPathProgress = Math.min((progress - pathProgress) * (animationTimeElapsed / animationDuration) + pathProgress, 1);
currentPathProgress = Math.max(currentPathProgress, 0);
setPathProgress(currentPathProgress);
if (animationTimeElapsed <= animationDuration) {
animationFrameRef.current = window.requestAnimationFrame(loop);
}
}
loop();
}, [progress]); // We want *only* `progress` to trigger this effect
return /*#__PURE__*/React.createElement(Root, _extends({
"data-baseweb": "progressbar-rounded",
role: "progressbar",
"aria-valuenow": progress.toFixed(2).replace(/\.?0+$/, ''),
"aria-valuemin": 0,
"aria-valuemax": 1,
$size: size,
$inline: inline
}, restProps, rootProps), /*#__PURE__*/React.createElement(Svg, _extends({
$size: size
}, restProps, svgProps), /*#__PURE__*/React.createElement(TrackBackground, _extends({
$size: size
}, trackBackgroundProps)), /*#__PURE__*/React.createElement(TrackForeground, _extends({
ref: pathRef,
$size: size,
$visible: !!pathRef.current,
$pathLength: pathLength,
$pathProgress: pathProgress
}, trackForegroundProps))), /*#__PURE__*/React.createElement(Text, _extends({
$size: size
}, textProps), roundTo(Math.min(progress * 100, 100)), "%"));
}
var _default = exports.default = ProgressBarRounded;