@onesy/ui-react
Version:
UI for React
335 lines (332 loc) • 12.9 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["ref", "tonal", "color", "renderValue", "icon", "onStart", "onFlag", "onPause", "onStop", "onResume", "Icon", "IconStart", "IconPause", "IconFlag", "IconStop", "TreeProps", "TooltipProps", "IconButtonProps", "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) { _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; }
import React from 'react';
import { is, getLeadingZerosNumber } from '@onesy/utils';
import { OnesyDate, duration } from '@onesy/date';
import { classNames, style as styleMethod, useOnesyTheme } from '@onesy/style-react';
import IconMaterialTimer from '@onesy/icons-material-rounded-react/IconMaterialTimerW100';
import IconMaterialPlayArrow from '@onesy/icons-material-rounded-react/IconMaterialPlayArrowW100';
import IconMaterialPause from '@onesy/icons-material-rounded-react/IconMaterialPauseW100';
import IconMaterialStop from '@onesy/icons-material-rounded-react/IconMaterialStopW100';
import IconMaterialFlag from '@onesy/icons-material-rounded-react/IconMaterialFlagW100';
import FadeElement from '../Fade';
import ExpandElement from '../Expand';
import TypeElement from '../Type';
import TooltipElement from '../Tooltip';
import TreeElement from '../Tree';
import SurfaceElement from '../Surface';
import LineElement from '../Line';
import IconButtonElement from '../IconButton';
import { staticClassName } from '../utils';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const useStyle = styleMethod(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: 'onesy-Timer'
});
const Timer = props_ => {
const theme = useOnesyTheme();
const l = theme.l;
const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyTimer?.props?.default), props_);
const Line = theme?.elements?.Line || LineElement;
const Fade = theme?.elements?.Fade || FadeElement;
const Type = theme?.elements?.Type || TypeElement;
const Expand = theme?.elements?.Expand || ExpandElement;
const Tooltip = theme?.elements?.Tooltip || TooltipElement;
const Tree = theme?.elements?.Tree || TreeElement;
const Surface = theme?.elements?.Surface || SurfaceElement;
const IconButton = theme?.elements?.IconButton || IconButtonElement;
const {
ref,
tonal = true,
color = 'primary',
renderValue,
icon = true,
onStart: onStart_,
onFlag: onFlag_,
onPause: onPause_,
onStop: onStop_,
onResume: onResume_,
Icon: Icon_ = IconMaterialTimer,
IconStart = IconMaterialPlayArrow,
IconPause = IconMaterialPause,
IconFlag = IconMaterialFlag,
IconStop = IconMaterialStop,
TreeProps: TreeProps_,
TooltipProps: TooltipProps_,
IconButtonProps: IconButtonProps_,
IconProps: IconProps_,
Component = 'div',
className
} = props,
other = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const [status, setStatus] = React.useState('initial');
const [flags, setFlags] = React.useState([]);
const [expand, setExpand] = React.useState();
const [value, setValue] = React.useState(0);
const refs = {
root: React.useRef(undefined),
start: React.useRef(0),
valuePaused: React.useRef(0),
value: React.useRef(undefined),
animationFrame: React.useRef(undefined)
};
refs.value.current = value;
const clear = () => {
cancelAnimationFrame(refs.animationFrame.current);
};
React.useEffect(() => {
return () => {
// Clean up
clear();
};
}, []);
const update = () => {
setValue(refs.valuePaused.current + (OnesyDate.milliseconds - refs.start.current));
refs.animationFrame.current = requestAnimationFrame(update);
};
const onStart = event => {
refs.start.current = OnesyDate.milliseconds;
// ~60+ fps
refs.animationFrame.current = requestAnimationFrame(update);
setStatus('running');
if (is('function', onStart_)) onStart_(event);
};
const onFlag = event_0 => {
if (!expand) setExpand(true);
setFlags(values => [...values, refs.value.current]);
if (is('function', onFlag_)) onFlag_(event_0);
};
const onPause = event_1 => {
clear();
// Remember previous value
refs.valuePaused.current = refs.value.current;
setStatus('paused');
if (is('function', onPause_)) onPause_(event_1);
};
const onStop = event_2 => {
clear();
setStatus('initial');
setExpand(false);
setValue(0);
refs.start.current = 0;
refs.valuePaused.current = 0;
refs.value.current = 0;
if (is('function', onStop_)) onStop_(event_2);
};
const onResume = event_3 => {
// ~60+ fps
refs.animationFrame.current = requestAnimationFrame(update);
// Update start, valuePaused value
refs.start.current = OnesyDate.milliseconds;
setStatus('running');
if (is('function', onResume_)) onResume_(event_3);
};
const onExited = () => {
setFlags([]);
};
const IconProps = _objectSpread({}, IconProps_);
const TooltipProps = _objectSpread({
portal: true,
interactive: false
}, TooltipProps_);
const TreeProps = _objectSpread({
tonal,
color: 'inherit'
}, TreeProps_);
const IconButtonProps = _objectSpread({
tonal,
color: 'inherit',
version: 'text'
}, IconButtonProps_);
const valueFormat = valueNew_ => {
let valueNew = '';
const valueDuration = duration(valueNew_, undefined, true, undefined, ['hour', 'minute', 'second', 'millisecond']);
if (valueDuration.hour > 0) valueNew += `${getLeadingZerosNumber(valueDuration.hour)}:`;
valueNew += `${getLeadingZerosNumber(valueDuration.minute || 0)}:`;
valueNew += `${getLeadingZerosNumber(valueDuration.second || 0)}.`;
valueNew += `${getLeadingZerosNumber(Math.floor((valueDuration.millisecond || 0) / 10))}`;
return valueNew;
};
const value_ = status === 'initial' ? '00:00.00' : valueFormat(value);
return /*#__PURE__*/_jsxs(Surface, _objectSpread(_objectSpread({
ref: item => {
if (ref) {
if (is('function', ref)) ref(item);else if (ref?.current) ref.current = item;
}
refs.root.current = item;
},
tonal: tonal,
color: color,
gap: 0,
direction: "column",
align: "center",
Component: Line,
AdditionalProps: {
Component
},
className: classNames([staticClassName('Timer', theme) && ['onesy-Timer-root'], className, classes.root])
}, other), {}, {
children: [icon && /*#__PURE__*/_jsx(Icon_, _objectSpread(_objectSpread({
size: "medium"
}, IconProps), {}, {
style: _objectSpread({
marginBottom: 4
}, IconProps?.style)
})), is('function', renderValue) ? renderValue(value_) : /*#__PURE__*/_jsx(Type, {
version: "h1",
className: classNames([staticClassName('Timer', theme) && ['onesy-Timer-value'], classes.value]),
children: value_
}), /*#__PURE__*/_jsx(Expand, {
in: expand,
parent: refs.root.current,
onExited: onExited,
className: classNames([staticClassName('Timer', theme) && ['onesy-Timer-flags-wrapper'], classes.flags_wrapper]),
children: /*#__PURE__*/_jsx("div", {
children: /*#__PURE__*/_jsx(Surface, {
tonal: tonal,
color: color,
children: ({
palette
}) => {
return /*#__PURE__*/_jsx(Tree, _objectSpread(_objectSpread({
openDefault: true,
middle: l('Flags'),
indicator: true,
indicatorPosition: "end",
parent: refs.root.current,
TreeProps: {
className: classNames([staticClassName('Timer', theme) && ['onesy-Timer-flags'], classes.flags])
}
}, TreeProps), {}, {
children: flags.map((item_0, index) => /*#__PURE__*/_jsx(Tree, _objectSpread({
icon: /*#__PURE__*/_jsx(IconFlag, {
size: "small",
color: palette[40]
}),
middle: /*#__PURE__*/_jsxs(Line, {
direction: "row",
align: "center",
justify: "space-between",
className: classNames([classes.flag]),
children: [/*#__PURE__*/_jsx(Type, {
version: "b2",
children: getLeadingZerosNumber(index + 1)
}), /*#__PURE__*/_jsxs(Type, {
version: "b2",
style: {
opacity: '0.7'
},
children: ["+", valueFormat(item_0 - (flags[index - 1] || 0))]
}), /*#__PURE__*/_jsx(Type, {
version: "b2",
children: valueFormat(item_0)
})]
}),
noPadding: true,
indicator: true,
indicatorPosition: "end"
}, TreeProps), index))
}));
}
})
})
}), /*#__PURE__*/_jsxs(Line, {
gap: 1,
direction: "row",
align: "center",
justify: "center",
className: classNames([staticClassName('Timer', theme) && ['onesy-Timer-actions'], classes.actions]),
children: [status === 'initial' && /*#__PURE__*/_jsx(Fade, {
in: true,
add: true,
children: /*#__PURE__*/_jsx("span", {
children: /*#__PURE__*/_jsx(Tooltip, _objectSpread(_objectSpread({
name: l('Start')
}, TooltipProps), {}, {
children: /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({
onClick: onStart
}, IconButtonProps), {}, {
children: /*#__PURE__*/_jsx(IconStart, {})
}))
}))
})
}), status === 'running' && /*#__PURE__*/_jsx(Fade, {
in: true,
add: true,
children: /*#__PURE__*/_jsxs("span", {
children: [/*#__PURE__*/_jsx(Tooltip, _objectSpread(_objectSpread({
name: l('Flag')
}, TooltipProps), {}, {
children: /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({
onClick: onFlag
}, IconButtonProps), {}, {
children: /*#__PURE__*/_jsx(IconFlag, {})
}))
})), /*#__PURE__*/_jsx(Tooltip, _objectSpread(_objectSpread({
name: l('Pause')
}, TooltipProps), {}, {
children: /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({
onClick: onPause
}, IconButtonProps), {}, {
children: /*#__PURE__*/_jsx(IconPause, {})
}))
}))]
})
}), status === 'paused' && /*#__PURE__*/_jsx(Fade, {
in: true,
add: true,
children: /*#__PURE__*/_jsxs("span", {
children: [/*#__PURE__*/_jsx(Tooltip, _objectSpread(_objectSpread({
name: l('Stop')
}, TooltipProps), {}, {
children: /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({
onClick: onStop
}, IconButtonProps), {}, {
children: /*#__PURE__*/_jsx(IconStop, {})
}))
})), /*#__PURE__*/_jsx(Tooltip, _objectSpread(_objectSpread({
name: l('Resume')
}, TooltipProps), {}, {
children: /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({
onClick: onResume
}, IconButtonProps), {}, {
children: /*#__PURE__*/_jsx(IconStart, {})
}))
}))]
})
})]
})]
}));
};
Timer.displayName = 'onesy-Timer';
export default Timer;