@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
193 lines (192 loc) • 9.86 kB
JavaScript
;
"use client";
require("core-js/modules/web.dom-collections.iterator.js");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _componentHelper = require("../../shared/component-helper");
var _helpers = require("../../shared/helpers");
const _excluded = ["size", "visible", "progress", "onComplete", "callOnCompleteHandler", "title", "customColors", "customCircleWidth", "counterClockwise"],
_excluded2 = ["customColor", "customWidth", "className"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 && {}.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() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function ProgressIndicatorCircular(props) {
const {
size,
visible,
progress,
onComplete,
callOnCompleteHandler,
title,
customColors,
customCircleWidth,
counterClockwise = false
} = props,
rest = _objectWithoutProperties(props, _excluded);
const keepAnimatingRef = (0, _react.useRef)(true);
const visibleRef = (0, _react.useRef)(false);
const useAnimationFrame = typeof onComplete === 'function' || _helpers.IS_EDGE;
const _refDark = (0, _react.useRef)(null);
const _refLight = (0, _react.useRef)(null);
const _startupTimeout = (0, _react.useRef)();
(0, _react.useEffect)(() => {
if (useAnimationFrame) {
_startupTimeout.current = setTimeout(() => {
if (_refDark.current) {
doAnimation(_refDark.current, true, callOnCompleteHandler);
}
if (_refLight.current) {
doAnimation(_refLight.current, false);
}
}, 300);
}
return () => {
keepAnimatingRef.current = false;
if (_startupTimeout.current) {
clearTimeout(_startupTimeout.current);
}
};
}, []);
(0, _react.useEffect)(() => {
visibleRef.current = visible;
}, [visible]);
const doAnimation = function (element) {
let animateOnStart = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
let callback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
const min = 1;
const max = Math.PI * 100;
let start = 0,
ms = 0,
prog = max,
setProg = animateOnStart,
animate1 = true,
completeCalled = false,
stopNextRound = false;
const step = timestamp => {
if (!start) {
start = timestamp;
}
ms = timestamp - start;
if (animate1) {
if (!visibleRef.current && prog < 20) {
prog = min;
}
if (setProg) {
element.style['stroke-dashoffset'] = `${prog}%`;
} else if (!animateOnStart) {
element.style['stroke-dashoffset'] = `${max}%`;
}
}
if (stopNextRound) {
animate1 = false;
if (!completeCalled) {
completeCalled = true;
if (animateOnStart && typeof callback === 'function') {
callback();
}
} else if (visibleRef.current && ms % 1e3 > 950) {
stopNextRound = false;
}
} else {
stopNextRound = !visibleRef.current && prog === min;
animate1 = true;
completeCalled = false;
}
prog = Math.round(max - max / 1e3 * (ms % 1e3));
setProg = animateOnStart ? Math.ceil(ms / 1e3) % 2 === 1 || ms === 0 : Math.ceil(ms / 1e3) % 2 === 0 && ms !== 0;
if (keepAnimatingRef.current) {
window.requestAnimationFrame(step);
}
};
if (typeof window !== 'undefined' && window.requestAnimationFrame) {
window.requestAnimationFrame(step);
}
};
const progressIsControlled = progress > -1;
if (progressIsControlled) {
rest.role = 'progressbar';
rest['aria-label'] = title;
rest['title'] = title;
} else {
rest.role = 'alert';
rest['aria-busy'] = true;
}
const remainingDOMAttributes = (0, _componentHelper.validateDOMAttributes)(props, _objectSpread({}, rest));
return _react.default.createElement("span", _extends({
className: (0, _classnames.default)('dnb-progress-indicator__circular', size && `dnb-progress-indicator__circular--${size}`, progressIsControlled && 'dnb-progress-indicator__circular--has-progress-value')
}, remainingDOMAttributes), _react.default.createElement("span", {
className: "dnb-progress-indicator__circular__background-padding"
}, _react.default.createElement("span", {
className: "dnb-progress-indicator__circular__background",
style: {
backgroundColor: customColors === null || customColors === void 0 ? void 0 : customColors.background
}
})), _react.default.createElement(Circle, {
className: "light paused",
customColor: customColors === null || customColors === void 0 ? void 0 : customColors.shaft,
customWidth: customCircleWidth
}), _react.default.createElement(Circle, {
className: "dark dark" + (progressIsControlled || useAnimationFrame ? " paused" : ""),
style: progressIsControlled ? {
strokeDashoffset: getOffset(progress, counterClockwise)
} : {},
customColor: customColors === null || customColors === void 0 ? void 0 : customColors.line,
customWidth: customCircleWidth,
ref: _refDark
}), !progressIsControlled && _react.default.createElement(Circle, {
className: 'light' + (useAnimationFrame ? " paused" : ""),
customColor: customColors === null || customColors === void 0 ? void 0 : customColors.shaft,
customWidth: customCircleWidth,
ref: _refLight
}));
}
const Circle = (0, _react.forwardRef)(function Circle(_ref, ref) {
let {
customColor,
customWidth,
className
} = _ref,
rest = _objectWithoutProperties(_ref, _excluded2);
const correctedCustomWidth = correctPercentageStrokeWidth(customWidth);
return _react.default.createElement("svg", _extends({
className: (0, _classnames.default)('dnb-progress-indicator__circular__line', className),
shapeRendering: "geometricPrecision",
ref: ref
}, rest), _react.default.createElement("circle", {
className: "dnb-progress-indicator__circular__circle",
fill: "none",
cx: "50%",
cy: "50%",
r: "50%",
style: _objectSpread({
stroke: customColor
}, correctedCustomWidth ? {
'--progress-indicator-circular-stroke-width': correctedCustomWidth
} : undefined)
}));
});
function getOffset(progress) {
let counterClockwise = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
const offset = Math.PI * (100 - progress);
return `${counterClockwise ? -offset : offset}%`;
}
function correctPercentageStrokeWidth(strokeWidth) {
if (typeof strokeWidth === 'string' && strokeWidth.endsWith('%')) {
const number = parseFloat(strokeWidth.slice(0, strokeWidth.length - 1));
return `${100 * number / (100 - number)}%`;
}
}
var _default = exports.default = ProgressIndicatorCircular;
//# sourceMappingURL=ProgressIndicatorCircular.js.map