@onesy/ui-react
Version:
UI for React
425 lines (422 loc) • 20.6 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _utils = require("@onesy/utils");
var _date = require("@onesy/date");
var _styleReact = require("@onesy/style-react");
var _IconMaterialAvTimerW = _interopRequireDefault(require("@onesy/icons-material-rounded-react/IconMaterialAvTimerW100"));
var _IconMaterialPlayArrowW = _interopRequireDefault(require("@onesy/icons-material-rounded-react/IconMaterialPlayArrowW100"));
var _IconMaterialPauseW = _interopRequireDefault(require("@onesy/icons-material-rounded-react/IconMaterialPauseW100"));
var _IconMaterialStopW = _interopRequireDefault(require("@onesy/icons-material-rounded-react/IconMaterialStopW100"));
var _Fade = _interopRequireDefault(require("../Fade"));
var _Type = _interopRequireDefault(require("../Type"));
var _Tooltip = _interopRequireDefault(require("../Tooltip"));
var _Surface = _interopRequireDefault(require("../Surface"));
var _NumericTextField = _interopRequireDefault(require("../NumericTextField"));
var _RoundProgress = _interopRequireDefault(require("../RoundProgress"));
var _LinearProgress = _interopRequireDefault(require("../LinearProgress"));
var _Line = _interopRequireDefault(require("../Line"));
var _IconButton = _interopRequireDefault(require("../IconButton"));
var _utils2 = require("../utils");
var _jsxRuntime = require("react/jsx-runtime");
const _excluded = ["tonal", "color", "version", "renderValue", "icon", "onStart", "onPause", "onStop", "onResume", "Icon", "IconStart", "IconPause", "IconStop", "TreeProps", "TooltipProps", "RoundProgressProps", "NumericTextFieldProps", "IconButtonProps", "LinearProgressProps", "IconProps", "Component", "className"];
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) { (0, _defineProperty2.default)(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; }
const useStyle = (0, _styleReact.style)(theme => ({
root: {
minWidth: '314px',
padding: `${theme.methods.space.value(3, 'px')} ${theme.methods.space.value(5, 'px')}`,
borderRadius: theme.methods.shape.radius.value('rg', 'px')
},
roundWrapper: {
position: 'relative',
width: '240px',
height: '240px'
},
linearProgress: {
borderRadius: theme.methods.shape.radius.value(40, 'px'),
'&.onesy-LinearProgress-root': {
height: '6px'
},
'& .onesy-LinearProgress-line, & .onesy-LinearProgress-buffer': {
borderRadius: theme.methods.shape.radius.value(40, 'px')
}
},
roundProgress: {
'&.onesy-RoundedProgress-root': {
position: 'absolute',
inset: '0'
},
'& .onesy-RoundedProgress-svg': {
width: '100%',
height: '100%',
margin: '0'
},
'& .onesy-RoundedProgress-path-background': {
stroke: 'currentColor',
opacity: '0.24'
},
'& .onesy-RoundedProgress-path, & .onesy-RoundedProgress-path-background': {
strokeLinecap: 'round'
}
},
meta: {
maxWidth: '114px',
textAlign: 'center'
},
numericTextField: {
width: '70px',
'& .onesy-TextField-input-wrapper': {
paddingInline: '0px',
paddingBlock: theme.methods.space.value(1, 'px'),
height: 'auto'
},
'&.onesy-TextField-value .onesy-TextField-label, &.onesy-TextField-focus .onesy-TextField-label': {
insetInlineStart: '60%',
transform: `translate(${theme.direction === 'ltr' ? '-' : ''}50%, -16px) scale(0.667)`
},
'& .onesy-TextField-input': _objectSpread(_objectSpread({}, theme.typography.values.h2), {}, {
textAlign: 'center'
}),
'& .onesy-TextField-border': {
boxShadow: 'none'
}
}
}), {
name: 'onesy-Countdown'
});
const Countdown = props_ => {
var _theme$ui, _theme$ui2, _theme$elements, _theme$elements2, _theme$elements3, _theme$elements4, _theme$elements5, _theme$elements6, _theme$elements7, _theme$elements8, _theme$elements9;
const theme = (0, _styleReact.useOnesyTheme)();
const l = theme.l;
const props = _objectSpread(_objectSpread(_objectSpread({}, theme === null || theme === void 0 || (_theme$ui = theme.ui) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.elements) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.all) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.props) === null || _theme$ui === void 0 ? void 0 : _theme$ui.default), theme === null || theme === void 0 || (_theme$ui2 = theme.ui) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.elements) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.onesyCountdown) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_);
const Line = (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.Line) || _Line.default;
const Fade = (theme === null || theme === void 0 || (_theme$elements2 = theme.elements) === null || _theme$elements2 === void 0 ? void 0 : _theme$elements2.Fade) || _Fade.default;
const Type = (theme === null || theme === void 0 || (_theme$elements3 = theme.elements) === null || _theme$elements3 === void 0 ? void 0 : _theme$elements3.Type) || _Type.default;
const Tooltip = (theme === null || theme === void 0 || (_theme$elements4 = theme.elements) === null || _theme$elements4 === void 0 ? void 0 : _theme$elements4.Tooltip) || _Tooltip.default;
const Surface = (theme === null || theme === void 0 || (_theme$elements5 = theme.elements) === null || _theme$elements5 === void 0 ? void 0 : _theme$elements5.Surface) || _Surface.default;
const NumericTextField = (theme === null || theme === void 0 || (_theme$elements6 = theme.elements) === null || _theme$elements6 === void 0 ? void 0 : _theme$elements6.NumericTextField) || _NumericTextField.default;
const RoundProgress = (theme === null || theme === void 0 || (_theme$elements7 = theme.elements) === null || _theme$elements7 === void 0 ? void 0 : _theme$elements7.RoundProgress) || _RoundProgress.default;
const LinearProgress = (theme === null || theme === void 0 || (_theme$elements8 = theme.elements) === null || _theme$elements8 === void 0 ? void 0 : _theme$elements8.LinearProgress) || _LinearProgress.default;
const IconButton = (theme === null || theme === void 0 || (_theme$elements9 = theme.elements) === null || _theme$elements9 === void 0 ? void 0 : _theme$elements9.IconButton) || _IconButton.default;
const {
tonal = true,
color = 'primary',
version = 'linear',
renderValue,
icon = true,
onStart: onStart_,
onPause: onPause_,
onStop: onStop_,
onResume: onResume_,
Icon: Icon_ = _IconMaterialAvTimerW.default,
IconStart = _IconMaterialPlayArrowW.default,
IconPause = _IconMaterialPauseW.default,
IconStop = _IconMaterialStopW.default,
TreeProps: TreeProps_,
TooltipProps: TooltipProps_,
RoundProgressProps: RoundProgressProps_,
NumericTextFieldProps: NumericTextFieldProps_,
IconButtonProps: IconButtonProps_,
LinearProgressProps: LinearProgressProps_,
IconProps: IconProps_,
Component = 'div',
className
} = props,
other = (0, _objectWithoutProperties2.default)(props, _excluded);
const {
classes
} = useStyle();
const [status, setStatus] = _react.default.useState('initial');
const [value, setValue] = _react.default.useState(0);
const [values, setValues] = _react.default.useState({
hours: 0,
minutes: 0,
seconds: 0
});
const refs = {
start: _react.default.useRef(0),
valuePaused: _react.default.useRef(0),
value: _react.default.useRef(undefined),
total: _react.default.useRef(undefined),
values: _react.default.useRef(undefined),
animationFrame: _react.default.useRef(undefined)
};
refs.value.current = value;
refs.values.current = values;
const updateValues = (property, valueItem) => {
setValues(values_ => _objectSpread(_objectSpread({}, values_), {}, {
[property]: valueItem
}));
};
const clear = () => {
cancelAnimationFrame(refs.animationFrame.current);
};
_react.default.useEffect(() => {
return () => {
// Clean up
clear();
};
}, []);
const update = () => {
if (refs.value.current <= 0) {
// Wait for linear, round progress
// transitions to end
setTimeout(() => {
setStatus('initial');
}, 1400);
return clear();
}
setValue(refs.valuePaused.current - (_date.OnesyDate.milliseconds - refs.start.current));
refs.animationFrame.current = requestAnimationFrame(update);
};
const onStart = event => {
refs.total.current = refs.valuePaused.current = (refs.values.current.hours || 0) * 60 ** 2 * 1e3 + (refs.values.current.minutes || 0) * 60 ** 1 * 1e3 + (refs.values.current.seconds || 0) * 1e3;
setValue(refs.valuePaused.current);
setStatus('running');
setTimeout(() => {
refs.start.current = _date.OnesyDate.milliseconds;
refs.animationFrame.current = requestAnimationFrame(update);
}, 14);
if ((0, _utils.is)('function', onStart_)) onStart_(event);
};
const onPause = event_0 => {
clear();
// Remember previous value
refs.valuePaused.current = refs.value.current;
setStatus('paused');
if ((0, _utils.is)('function', onPause_)) onPause_(event_0);
};
const onStop = event_1 => {
clear();
refs.start.current = 0;
refs.valuePaused.current = 0;
refs.value.current = 0;
setStatus('initial');
setValue(0);
if ((0, _utils.is)('function', onStop_)) onStop_(event_1);
};
const onResume = event_2 => {
// Update start, valuePaused value
refs.start.current = _date.OnesyDate.milliseconds;
// ~60+ fps
refs.animationFrame.current = requestAnimationFrame(update);
setStatus('running');
if ((0, _utils.is)('function', onResume_)) onResume_(event_2);
};
const IconProps = _objectSpread({}, IconProps_);
const TooltipProps = _objectSpread({
portal: true,
interactive: false
}, TooltipProps_);
const NumericTextFieldProps = _objectSpread({
tonal,
color: 'inherit',
size: 'small',
version: 'text',
increment: false,
decrement: false
}, NumericTextFieldProps_);
const RoundProgressProps = _objectSpread({
tonal,
color,
rounded: true,
thickness: 1
}, RoundProgressProps_);
const LinearProgressProps = _objectSpread({
tonal,
color
}, LinearProgressProps_);
const IconButtonProps = _objectSpread({
tonal,
color: 'inherit',
version: 'text'
}, IconButtonProps_);
const valueFormat = valueNew_ => {
let valueNew = '';
const valueDuration = (0, _date.duration)(valueNew_, undefined, true, undefined, ['hour', 'minute', 'second']);
valueNew += `${(0, _utils.getLeadingZerosNumber)(valueDuration.hour || 0)}:`;
valueNew += `${(0, _utils.getLeadingZerosNumber)(valueDuration.minute || 0)}:`;
valueNew += `${(0, _utils.getLeadingZerosNumber)(valueDuration.second || 0)}`;
return valueNew;
};
const value_ = status === 'initial' ? '00:00:00' : valueFormat(value);
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Surface, _objectSpread(_objectSpread({
tonal: tonal,
color: color,
gap: 1,
direction: "column",
align: "center",
Component: Line,
AdditionalProps: {
Component
},
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Countdown', theme) && ['onesy-Countdown-root'], className, classes.root])
}, other), {}, {
children: [icon && /*#__PURE__*/(0, _jsxRuntime.jsx)(Icon_, _objectSpread(_objectSpread({
size: "medium"
}, IconProps), {}, {
style: _objectSpread({
marginBottom: 4
}, IconProps === null || IconProps === void 0 ? void 0 : IconProps.style)
})), status === 'initial' && /*#__PURE__*/(0, _jsxRuntime.jsx)(Fade, {
in: true,
add: true,
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(Line, {
gap: 1,
direction: "row",
align: "flex-end",
style: {
marginTop: 12,
marginBottom: -8
},
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(NumericTextField, _objectSpread(_objectSpread({
name: l('Hours'),
min: 0,
max: 23,
value: values.hours,
onChange: valueNew_0 => updateValues('hours', !valueNew_0 ? 0 : valueNew_0)
}, NumericTextFieldProps), {}, {
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Countdown', theme) && ['onesy-Countdown-numeric-text-field'], NumericTextFieldProps === null || NumericTextFieldProps === void 0 ? void 0 : NumericTextFieldProps.className, classes.numericTextField])
})), /*#__PURE__*/(0, _jsxRuntime.jsx)(NumericTextField, _objectSpread(_objectSpread({
name: l('Minutes'),
min: 0,
max: 59,
value: values.minutes,
onChange: valueNew_1 => updateValues('minutes', !valueNew_1 ? 0 : valueNew_1)
}, NumericTextFieldProps), {}, {
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Countdown', theme) && ['onesy-Countdown-numeric-text-field'], NumericTextFieldProps === null || NumericTextFieldProps === void 0 ? void 0 : NumericTextFieldProps.className, classes.numericTextField])
})), /*#__PURE__*/(0, _jsxRuntime.jsx)(NumericTextField, _objectSpread(_objectSpread({
name: l('Seconds'),
min: 0,
max: 59,
value: values.seconds,
onChange: valueNew_2 => updateValues('seconds', !valueNew_2 ? 0 : valueNew_2)
}, NumericTextFieldProps), {}, {
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Countdown', theme) && ['onesy-Countdown-numeric-text-field'], NumericTextFieldProps === null || NumericTextFieldProps === void 0 ? void 0 : NumericTextFieldProps.className, classes.numericTextField])
}))]
})
}), status !== 'initial' && ['regular', 'linear'].includes(version) && /*#__PURE__*/(0, _jsxRuntime.jsx)(Fade, {
in: true,
add: true,
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(Line, {
gap: 1,
direction: "column",
align: "center",
justify: "center",
style: {
width: '100%'
},
children: [(0, _utils.is)('function', renderValue) ? renderValue(value_) : /*#__PURE__*/(0, _jsxRuntime.jsx)(Type, {
version: "h1",
children: value_
}), version === 'linear' && /*#__PURE__*/(0, _jsxRuntime.jsx)(LinearProgress, _objectSpread(_objectSpread({
version: "determinate",
value: (0, _utils.clamp)(Math.round(value / 1000 / (refs.total.current / 1000) * 100), 0, 100),
reverse: true,
style: {
margin: '4px 0 8px'
}
}, LinearProgressProps), {}, {
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Countdown', theme) && ['onesy-Countdown-linear-progress'], LinearProgressProps === null || LinearProgressProps === void 0 ? void 0 : LinearProgressProps.className, classes.linearProgress])
}))]
})
}), status !== 'initial' && ['round'].includes(version) && /*#__PURE__*/(0, _jsxRuntime.jsx)(Fade, {
in: true,
add: true,
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(Line, {
gap: 0.5,
direction: "column",
align: "center",
justify: "center",
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Countdown', theme) && ['onesy-Countdown-round-wrapper'], classes.roundWrapper]),
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(RoundProgress, _objectSpread(_objectSpread({
version: "determinate",
value: (0, _utils.clamp)(Math.round(value / 1000 / (refs.total.current / 1000) * 100), 0, 100)
}, RoundProgressProps), {}, {
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Countdown', theme) && ['onesy-Countdown-round-progress'], RoundProgressProps === null || RoundProgressProps === void 0 ? void 0 : RoundProgressProps.className, classes.roundProgress])
})), (0, _utils.is)('function', renderValue) ? renderValue(value_) : /*#__PURE__*/(0, _jsxRuntime.jsx)(Type, {
version: "h1",
children: value_
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)(Type, {
version: "b3",
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Countdown', theme) && ['onesy-Countdown-meta'], classes.meta]),
children: [l('Total'), " ", (0, _date.duration)(refs.total.current, false)]
})]
})
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)(Line, {
gap: 1,
direction: "row",
align: "center",
justify: "center",
children: [status === 'initial' && /*#__PURE__*/(0, _jsxRuntime.jsx)(Fade, {
in: true,
add: true,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Tooltip, _objectSpread(_objectSpread({
name: l('Start')
}, TooltipProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconButton, _objectSpread(_objectSpread({
onClick: onStart,
disabled: !((values.hours || 0) + (values.minutes || 0) + (values.seconds || 0))
}, IconButtonProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconStart, {})
}))
}))
})
}), status === 'running' && /*#__PURE__*/(0, _jsxRuntime.jsx)(Fade, {
in: true,
add: true,
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Tooltip, _objectSpread(_objectSpread({
name: l('Stop')
}, TooltipProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconButton, _objectSpread(_objectSpread({
onClick: onStop
}, IconButtonProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconStop, {})
}))
})), /*#__PURE__*/(0, _jsxRuntime.jsx)(Tooltip, _objectSpread(_objectSpread({
name: l('Pause')
}, TooltipProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconButton, _objectSpread(_objectSpread({
onClick: onPause
}, IconButtonProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconPause, {})
}))
}))]
})
}), status === 'paused' && /*#__PURE__*/(0, _jsxRuntime.jsx)(Fade, {
in: true,
add: true,
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Tooltip, _objectSpread(_objectSpread({
name: l('Stop')
}, TooltipProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconButton, _objectSpread(_objectSpread({
onClick: onStop
}, IconButtonProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconStop, {})
}))
})), /*#__PURE__*/(0, _jsxRuntime.jsx)(Tooltip, _objectSpread(_objectSpread({
name: l('Resume')
}, TooltipProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconButton, _objectSpread(_objectSpread({
onClick: onResume
}, IconButtonProps), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(IconStart, {})
}))
}))]
})
})]
})]
}));
};
Countdown.displayName = 'onesy-Countdown';
var _default = exports.default = Countdown;