@amaui/ui-react
Version:
UI for React
266 lines (265 loc) • 20.3 kB
JavaScript
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const utils_1 = require("@amaui/utils");
const date_1 = require("@amaui/date");
const style_react_1 = require("@amaui/style-react");
const IconMaterialAvTimerW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialAvTimerW100"));
const IconMaterialPlayArrowW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialPlayArrowW100"));
const IconMaterialPauseW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialPauseW100"));
const IconMaterialStopW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialStopW100"));
const Fade_1 = __importDefault(require("../Fade"));
const Type_1 = __importDefault(require("../Type"));
const Tooltip_1 = __importDefault(require("../Tooltip"));
const Surface_1 = __importDefault(require("../Surface"));
const NumericTextField_1 = __importDefault(require("../NumericTextField"));
const RoundProgress_1 = __importDefault(require("../RoundProgress"));
const LinearProgress_1 = __importDefault(require("../LinearProgress"));
const Line_1 = __importDefault(require("../Line"));
const IconButton_1 = __importDefault(require("../IconButton"));
const utils_2 = require("../utils");
const useStyle = (0, style_react_1.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'),
'&.amaui-LinearProgress-root': {
height: '6px'
},
'& .amaui-LinearProgress-line, & .amaui-LinearProgress-buffer': {
borderRadius: theme.methods.shape.radius.value(40, 'px')
}
},
roundProgress: {
'&.amaui-RoundedProgress-root': {
position: 'absolute',
inset: '0'
},
'& .amaui-RoundedProgress-svg': {
width: '100%',
height: '100%',
margin: '0'
},
'& .amaui-RoundedProgress-path-background': {
stroke: 'currentColor',
opacity: '0.24'
},
'& .amaui-RoundedProgress-path, & .amaui-RoundedProgress-path-background': {
strokeLinecap: 'round'
}
},
meta: {
maxWidth: '114px',
textAlign: 'center'
},
numericTextField: {
width: '70px',
'& .amaui-TextField-input-wrapper': {
paddingInline: '0px',
paddingBlock: theme.methods.space.value(1, 'px'),
height: 'auto'
},
'&.amaui-TextField-value .amaui-TextField-label, &.amaui-TextField-focus .amaui-TextField-label': {
insetInlineStart: '60%',
transform: `translate(${theme.direction === 'ltr' ? '-' : ''}50%, -16px) scale(0.667)`
},
'& .amaui-TextField-input': Object.assign(Object.assign({}, theme.typography.values.h2), { textAlign: 'center' }),
'& .amaui-TextField-border': {
boxShadow: 'none'
}
}
}), { name: 'amaui-Countdown' });
const Countdown = react_1.default.forwardRef((props_, ref) => {
const theme = (0, style_react_1.useAmauiTheme)();
const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiCountdown) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]);
const Fade = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Fade) || Fade_1.default; }, [theme]);
const Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_1.default; }, [theme]);
const Tooltip = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Tooltip) || Tooltip_1.default; }, [theme]);
const Surface = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Surface) || Surface_1.default; }, [theme]);
const NumericTextField = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.NumericTextField) || NumericTextField_1.default; }, [theme]);
const RoundProgress = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.RoundProgress) || RoundProgress_1.default; }, [theme]);
const LinearProgress = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.LinearProgress) || LinearProgress_1.default; }, [theme]);
const IconButton = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.IconButton) || IconButton_1.default; }, [theme]);
const { tonal = true, color = 'primary', version = 'linear', renderValue, icon = true, onStart: onStart_, onPause: onPause_, onStop: onStop_, onResume: onResume_, Icon: Icon_ = IconMaterialAvTimerW100_1.default, IconStart = IconMaterialPlayArrowW100_1.default, IconPause = IconMaterialPauseW100_1.default, IconStop = IconMaterialStopW100_1.default, TreeProps: TreeProps_, TooltipProps: TooltipProps_, RoundProgressProps: RoundProgressProps_, NumericTextFieldProps: NumericTextFieldProps_, IconButtonProps: IconButtonProps_, LinearProgressProps: LinearProgressProps_, IconProps: IconProps_, Component = 'div', className } = props, other = __rest(props, ["tonal", "color", "version", "renderValue", "icon", "onStart", "onPause", "onStop", "onResume", "Icon", "IconStart", "IconPause", "IconStop", "TreeProps", "TooltipProps", "RoundProgressProps", "NumericTextFieldProps", "IconButtonProps", "LinearProgressProps", "IconProps", "Component", "className"]);
const { classes } = useStyle();
const [status, setStatus] = react_1.default.useState('initial');
const [value, setValue] = react_1.default.useState(0);
const [values, setValues] = react_1.default.useState({
hours: 0,
minutes: 0,
seconds: 0
});
const refs = {
start: react_1.default.useRef(0),
valuePaused: react_1.default.useRef(0),
value: react_1.default.useRef(undefined),
total: react_1.default.useRef(undefined),
values: react_1.default.useRef(undefined),
animationFrame: react_1.default.useRef(undefined)
};
refs.value.current = value;
refs.values.current = values;
const updateValues = (property, valueItem) => {
setValues(values_ => (Object.assign(Object.assign({}, values_), { [property]: valueItem })));
};
const clear = () => {
cancelAnimationFrame(refs.animationFrame.current);
};
react_1.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_1.AmauiDate.milliseconds - refs.start.current));
refs.animationFrame.current = requestAnimationFrame(update);
};
const onStart = react_1.default.useCallback((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_1.AmauiDate.milliseconds;
refs.animationFrame.current = requestAnimationFrame(update);
}, 14);
if ((0, utils_1.is)('function', onStart_))
onStart_(event);
}, []);
const onPause = react_1.default.useCallback((event) => {
clear();
// Remember previous value
refs.valuePaused.current = refs.value.current;
setStatus('paused');
if ((0, utils_1.is)('function', onPause_))
onPause_(event);
}, []);
const onStop = react_1.default.useCallback((event) => {
clear();
refs.start.current = 0;
refs.valuePaused.current = 0;
refs.value.current = 0;
setStatus('initial');
setValue(0);
if ((0, utils_1.is)('function', onStop_))
onStop_(event);
}, []);
const onResume = react_1.default.useCallback((event) => {
// Update start, valuePaused value
refs.start.current = date_1.AmauiDate.milliseconds;
// ~60+ fps
refs.animationFrame.current = requestAnimationFrame(update);
setStatus('running');
if ((0, utils_1.is)('function', onResume_))
onResume_(event);
}, []);
const IconProps = Object.assign({}, IconProps_);
const TooltipProps = Object.assign({ portal: true, interactive: false }, TooltipProps_);
const NumericTextFieldProps = Object.assign({ tonal, color: 'inherit', size: 'small', version: 'text', increment: false, decrement: false }, NumericTextFieldProps_);
const RoundProgressProps = Object.assign({ tonal,
color, rounded: true, thickness: 1 }, RoundProgressProps_);
const LinearProgressProps = Object.assign({ tonal,
color }, LinearProgressProps_);
const IconButtonProps = Object.assign({ tonal, color: 'inherit', version: 'text' }, IconButtonProps_);
const valueFormat = (valueNew_) => {
let valueNew = '';
const valueDuration = (0, date_1.duration)(valueNew_, undefined, true, undefined, ['hour', 'minute', 'second']);
valueNew += `${(0, utils_1.getLeadingZerosNumber)(valueDuration.hour || 0)}:`;
valueNew += `${(0, utils_1.getLeadingZerosNumber)(valueDuration.minute || 0)}:`;
valueNew += `${(0, utils_1.getLeadingZerosNumber)(valueDuration.second || 0)}`;
return valueNew;
};
const value_ = status === 'initial' ? '00:00:00' : valueFormat(value);
return ((0, jsx_runtime_1.jsxs)(Surface, Object.assign({ ref: ref, tonal: tonal, color: color, gap: 1, direction: 'column', align: 'center', Component: Line, AdditionalProps: {
Component
}, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Countdown', theme) && [
'amaui-Countdown-root'
],
className,
classes.root
]) }, other, { children: [icon && ((0, jsx_runtime_1.jsx)(Icon_, Object.assign({ size: 'medium' }, IconProps, { style: Object.assign({ marginBottom: 4 }, IconProps === null || IconProps === void 0 ? void 0 : IconProps.style) }))), status === 'initial' && ((0, jsx_runtime_1.jsx)(Fade, Object.assign({ in: true, add: true }, { children: (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1, direction: 'row', align: 'flex-end', style: {
marginTop: 12,
marginBottom: -8
} }, { children: [(0, jsx_runtime_1.jsx)(NumericTextField, Object.assign({ label: 'Hours', min: 0, max: 23, value: values.hours, onChange: valueNew => updateValues('hours', !valueNew ? 0 : valueNew) }, NumericTextFieldProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Countdown', theme) && [
'amaui-Countdown-numeric-text-field'
],
NumericTextFieldProps === null || NumericTextFieldProps === void 0 ? void 0 : NumericTextFieldProps.className,
classes.numericTextField
]) })), (0, jsx_runtime_1.jsx)(NumericTextField, Object.assign({ label: 'Minutes', min: 0, max: 59, value: values.minutes, onChange: valueNew => updateValues('minutes', !valueNew ? 0 : valueNew) }, NumericTextFieldProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Countdown', theme) && [
'amaui-Countdown-numeric-text-field'
],
NumericTextFieldProps === null || NumericTextFieldProps === void 0 ? void 0 : NumericTextFieldProps.className,
classes.numericTextField
]) })), (0, jsx_runtime_1.jsx)(NumericTextField, Object.assign({ label: 'Seconds', min: 0, max: 59, value: values.seconds, onChange: valueNew => updateValues('seconds', !valueNew ? 0 : valueNew) }, NumericTextFieldProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Countdown', theme) && [
'amaui-Countdown-numeric-text-field'
],
NumericTextFieldProps === null || NumericTextFieldProps === void 0 ? void 0 : NumericTextFieldProps.className,
classes.numericTextField
]) }))] })) }))), status !== 'initial' && ['regular', 'linear'].includes(version) && ((0, jsx_runtime_1.jsx)(Fade, Object.assign({ in: true, add: true }, { children: (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1, direction: 'column', align: 'center', justify: 'center', style: {
width: '100%'
} }, { children: [(0, utils_1.is)('function', renderValue) ? renderValue(value_) : ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'h1' }, { children: value_ }))), version === 'linear' && ((0, jsx_runtime_1.jsx)(LinearProgress, Object.assign({ version: 'determinate', value: (0, utils_1.clamp)(Math.round(((value / 1000) / (refs.total.current / 1000)) * 100), 0, 100), reverse: true, style: {
margin: '4px 0 8px'
} }, LinearProgressProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Countdown', theme) && [
'amaui-Countdown-linear-progress'
],
LinearProgressProps === null || LinearProgressProps === void 0 ? void 0 : LinearProgressProps.className,
classes.linearProgress
]) })))] })) }))), status !== 'initial' && ['round'].includes(version) && ((0, jsx_runtime_1.jsx)(Fade, Object.assign({ in: true, add: true }, { children: (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 0.5, direction: 'column', align: 'center', justify: 'center', className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Countdown', theme) && [
'amaui-Countdown-round-wrapper'
],
classes.roundWrapper
]) }, { children: [(0, jsx_runtime_1.jsx)(RoundProgress, Object.assign({ version: 'determinate', value: (0, utils_1.clamp)(Math.round(((value / 1000) / (refs.total.current / 1000)) * 100), 0, 100) }, RoundProgressProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Countdown', theme) && [
'amaui-Countdown-round-progress'
],
RoundProgressProps === null || RoundProgressProps === void 0 ? void 0 : RoundProgressProps.className,
classes.roundProgress
]) })), (0, utils_1.is)('function', renderValue) ? renderValue(value_) : ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'h1' }, { children: value_ }))), (0, jsx_runtime_1.jsxs)(Type, Object.assign({ version: 'b3', className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Countdown', theme) && [
'amaui-Countdown-meta'
],
classes.meta
]) }, { children: ["Total ", (0, date_1.duration)(refs.total.current, false)] }))] })) }))), (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1, direction: 'row', align: 'center', justify: 'center' }, { children: [status === 'initial' && ((0, jsx_runtime_1.jsx)(Fade, Object.assign({ in: true, add: true }, { children: (0, jsx_runtime_1.jsx)("span", { children: (0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ label: 'Start' }, TooltipProps, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: onStart, disabled: !((values.hours || 0) + (values.minutes || 0) + (values.seconds || 0)) }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconStart, {}) })) })) }) }))), status === 'running' && ((0, jsx_runtime_1.jsx)(Fade, Object.assign({ in: true, add: true }, { children: (0, jsx_runtime_1.jsxs)("span", { children: [(0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ label: 'Stop' }, TooltipProps, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: onStop }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconStop, {}) })) })), (0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ label: 'Pause' }, TooltipProps, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: onPause }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconPause, {}) })) }))] }) }))), status === 'paused' && ((0, jsx_runtime_1.jsx)(Fade, Object.assign({ in: true, add: true }, { children: (0, jsx_runtime_1.jsxs)("span", { children: [(0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ label: 'Stop' }, TooltipProps, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: onStop }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconStop, {}) })) })), (0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ label: 'Resume' }, TooltipProps, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: onResume }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconStart, {}) })) }))] }) })))] }))] })));
});
Countdown.displayName = 'amaui-Countdown';
exports.default = Countdown;