@amaui/ui-react
Version:
UI for React
209 lines (208 loc) • 15.5 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 IconMaterialTimerW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialTimerW100"));
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 IconMaterialFlagW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialFlagW100"));
const Fade_1 = __importDefault(require("../Fade"));
const Expand_1 = __importDefault(require("../Expand"));
const Type_1 = __importDefault(require("../Type"));
const Tooltip_1 = __importDefault(require("../Tooltip"));
const Tree_1 = __importDefault(require("../Tree"));
const Surface_1 = __importDefault(require("../Surface"));
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: '300px',
padding: `${theme.methods.space.value(3, 'px')} ${theme.methods.space.value(5, 'px')}`,
borderRadius: theme.methods.shape.radius.value('rg', 'px')
},
value: {
marginTop: '8px'
},
flags_wrapper: {
width: '100%',
paddingTop: theme.methods.space.value(1, 'px')
},
flags: {
width: '100%',
maxHeight: '170px',
overflowY: 'auto'
},
flag: {
width: '100%',
paddingInlineStart: theme.methods.space.value(0.5, 'px')
},
actions: {
marginTop: '8px'
}
}), { name: 'amaui-Timer' });
const Timer = 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.amauiTimer) === 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 Expand = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Expand) || Expand_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 Tree = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Tree) || Tree_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 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', renderValue, icon = true, onStart: onStart_, onFlag: onFlag_, onPause: onPause_, onStop: onStop_, onResume: onResume_, Icon: Icon_ = IconMaterialTimerW100_1.default, IconStart = IconMaterialPlayArrowW100_1.default, IconPause = IconMaterialPauseW100_1.default, IconFlag = IconMaterialFlagW100_1.default, IconStop = IconMaterialStopW100_1.default, TreeProps: TreeProps_, TooltipProps: TooltipProps_, IconButtonProps: IconButtonProps_, IconProps: IconProps_, Component = 'div', className } = props, other = __rest(props, ["tonal", "color", "renderValue", "icon", "onStart", "onFlag", "onPause", "onStop", "onResume", "Icon", "IconStart", "IconPause", "IconFlag", "IconStop", "TreeProps", "TooltipProps", "IconButtonProps", "IconProps", "Component", "className"]);
const { classes } = useStyle();
const [status, setStatus] = react_1.default.useState('initial');
const [flags, setFlags] = react_1.default.useState([]);
const [expand, setExpand] = react_1.default.useState();
const [value, setValue] = react_1.default.useState(0);
const refs = {
root: react_1.default.useRef(undefined),
start: react_1.default.useRef(0),
valuePaused: react_1.default.useRef(0),
value: react_1.default.useRef(undefined),
animationFrame: react_1.default.useRef(undefined)
};
refs.value.current = value;
const clear = () => {
cancelAnimationFrame(refs.animationFrame.current);
};
react_1.default.useEffect(() => {
return () => {
// Clean up
clear();
};
}, []);
const update = () => {
setValue(refs.valuePaused.current + (date_1.AmauiDate.milliseconds - refs.start.current));
refs.animationFrame.current = requestAnimationFrame(update);
};
const onStart = react_1.default.useCallback((event) => {
refs.start.current = date_1.AmauiDate.milliseconds;
// ~60+ fps
refs.animationFrame.current = requestAnimationFrame(update);
setStatus('running');
if ((0, utils_1.is)('function', onStart_))
onStart_(event);
}, []);
const onFlag = react_1.default.useCallback((event) => {
if (!expand)
setExpand(true);
setFlags(values => [...values, refs.value.current]);
if ((0, utils_1.is)('function', onFlag_))
onFlag_(event);
}, [expand]);
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();
setStatus('initial');
setExpand(false);
setValue(0);
refs.start.current = 0;
refs.valuePaused.current = 0;
refs.value.current = 0;
if ((0, utils_1.is)('function', onStop_))
onStop_(event);
}, []);
const onResume = react_1.default.useCallback((event) => {
// ~60+ fps
refs.animationFrame.current = requestAnimationFrame(update);
// Update start, valuePaused value
refs.start.current = date_1.AmauiDate.milliseconds;
setStatus('running');
if ((0, utils_1.is)('function', onResume_))
onResume_(event);
}, []);
const onExited = react_1.default.useCallback(() => {
setFlags([]);
}, []);
const IconProps = Object.assign({}, IconProps_);
const TooltipProps = Object.assign({ portal: true, interactive: false }, TooltipProps_);
const TreeProps = Object.assign({ tonal, color: 'inherit' }, TreeProps_);
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', 'millisecond']);
if (valueDuration.hour > 0)
valueNew += `${(0, utils_1.getLeadingZerosNumber)(valueDuration.hour)}:`;
valueNew += `${(0, utils_1.getLeadingZerosNumber)(valueDuration.minute || 0)}:`;
valueNew += `${(0, utils_1.getLeadingZerosNumber)(valueDuration.second || 0)}.`;
valueNew += `${(0, utils_1.getLeadingZerosNumber)(Math.floor((valueDuration.millisecond || 0) / 10))}`;
return valueNew;
};
const value_ = status === 'initial' ? '00:00.00' : valueFormat(value);
return ((0, jsx_runtime_1.jsxs)(Surface, Object.assign({ ref: item => {
if (ref) {
if ((0, utils_1.is)('function', ref))
ref(item);
else if (ref === null || ref === void 0 ? void 0 : ref.current)
ref.current = item;
}
refs.root.current = item;
}, tonal: tonal, color: color, gap: 0, direction: 'column', align: 'center', Component: Line, AdditionalProps: {
Component
}, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Timer', theme) && [
'amaui-Timer-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) }))), (0, utils_1.is)('function', renderValue) ? renderValue(value_) : ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'h1', className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Timer', theme) && [
'amaui-Timer-value'
],
classes.value
]) }, { children: value_ }))), (0, jsx_runtime_1.jsx)(Expand, Object.assign({ in: expand, parent: refs.root.current, onExited: onExited, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Timer', theme) && [
'amaui-Timer-flags-wrapper'
],
classes.flags_wrapper
]) }, { children: (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(Surface, Object.assign({ tonal: tonal, color: color }, { children: ({ palette }) => {
return ((0, jsx_runtime_1.jsx)(Tree, Object.assign({ openDefault: true, middle: 'Flags', indicator: true, indicatorPosition: 'end', parent: refs.root.current, TreeProps: {
className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Timer', theme) && [
'amaui-Timer-flags'
],
classes.flags
])
} }, TreeProps, { children: flags.map((item, index) => ((0, jsx_runtime_1.jsx)(Tree, Object.assign({ icon: ((0, jsx_runtime_1.jsx)(IconFlag, { size: 'small', color: palette[40] })), middle: ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ direction: 'row', align: 'center', justify: 'space-between', className: (0, style_react_1.classNames)([
classes.flag
]) }, { children: [(0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'b2' }, { children: (0, utils_1.getLeadingZerosNumber)(index + 1) })), (0, jsx_runtime_1.jsxs)(Type, Object.assign({ version: 'b2', style: {
opacity: '0.7'
} }, { children: ["+", valueFormat(item - (flags[index - 1] || 0))] })), (0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'b2' }, { children: valueFormat(item) }))] }))), noPadding: true, indicator: true, indicatorPosition: 'end' }, TreeProps), index))) })));
} })) }) })), (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1, direction: 'row', align: 'center', justify: 'center', className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('Timer', theme) && [
'amaui-Timer-actions'
],
classes.actions
]) }, { 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 }, 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: 'Flag' }, TooltipProps, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: onFlag }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconFlag, {}) })) })), (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, {}) })) }))] }) })))] }))] })));
});
Timer.displayName = 'amaui-Timer';
exports.default = Timer;