UNPKG

@amaui/ui-react

Version:
182 lines (175 loc) 7.21 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["min", "max", "firstIncrement", "incrementMin", "incrementMax", "stepMin", "stepMax", "stepInterval", "onStart", "onIncrement", "onUpdate", "onDone", "fixed", "position", "TransitionComponent", "TransitionComponentProps", "className", "children"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } import React from 'react'; import { is, wait, random, clamp } from '@amaui/utils'; import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react'; import FadeElement from '../Fade'; import LinearProgressElement from '../LinearProgress'; import MainProgressContext from './Context'; import { staticClassName } from '../utils'; const useStyle = styleMethod(theme => ({ root: { zIndex: theme.z_index.tooltip - 14, '&.amaui-LinearProgress-root': { height: '2px' }, '& .amaui-LinearProgress-buffer': { background: 'transparent' } }, fixed: { insetInline: '0', '&.amaui-LinearProgress-root': { position: 'fixed' } }, position_top: { top: '0' }, position_bottom: { bottom: '0' } }), { name: 'amaui-MainProgress' }); const MainProgress = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useAmauiTheme(); const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiMainProgress?.props?.default), props_), [props_]); const Fade = React.useMemo(() => theme?.elements?.Fade || FadeElement, [theme]); const LinearProgress = React.useMemo(() => theme?.elements?.LinearProgress || LinearProgressElement, [theme]); const { min = 0, max = 99, firstIncrement = true, incrementMin = 4, incrementMax = 14, stepMin = 1, stepMax = 4, stepInterval = 700, onStart, onIncrement, onUpdate, onDone, fixed = true, position = 'top', TransitionComponent = Fade, TransitionComponentProps = {}, className, children } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const [inProp, setInProp] = React.useState(false); const [value, setValue] = React.useState(min); const refs = { value_: React.useRef({}), value: React.useRef(), min: React.useRef(), max: React.useRef(), firstIncrement: React.useRef(), incrementMin: React.useRef(), incrementMax: React.useRef(), stepInterval: React.useRef(), stepMin: React.useRef(), stepMax: React.useRef(), TransitionComponentProps: React.useRef(undefined), linearProgress: React.useRef(undefined), interval: React.useRef(undefined), props: React.useRef(undefined) }; refs.value.current = value; refs.props.current = props; refs.min.current = min; refs.max.current = max; refs.firstIncrement.current = firstIncrement; refs.incrementMin.current = incrementMin; refs.incrementMax.current = incrementMax; refs.stepInterval.current = stepInterval; refs.stepMin.current = stepMin; refs.stepMax.current = stepMax; refs.TransitionComponentProps.current = TransitionComponentProps; const clear = () => { if (refs.interval.current) { clearInterval(refs.interval.current); refs.interval.current = undefined; } }; const start = React.useCallback(value_ => { // Reset clear(); // Update refs.linearProgress.current = value_; // Open setInProp(true); // Set initial value to min const min_ = refs.min.current !== undefined ? refs.min.current : 0; const max_ = refs.max.current !== undefined ? refs.max.current : 0; setValue(min_); // start inc interval for steps // in refs.interval // in interval, remove interval if value is >= max if (min_ < max_) { const stepInterval_ = refs.stepInterval.current !== undefined ? refs.stepInterval.current : 700; refs.interval.current = setInterval(() => { if (refs.value.current >= max_) clearInterval(refs.interval.current); setValue(previousValue => clamp(previousValue + random(refs.stepMin.current, refs.stepMax.current), refs.min.current, refs.max.current)); }, stepInterval_); } if (is('function', onStart)) onStart(); }, []); const increment = React.useCallback(() => { // Inc value random between incrementMin, incrementMax, with clam to min, max let valueNew = refs.value.current; valueNew += clamp(random(refs.incrementMin.current, refs.incrementMax.current), refs.min.current, refs.max.current); setValue(valueNew); if (is('function', onIncrement)) onIncrement(valueNew); }, []); const update = React.useCallback(value_ => { const valueNew = value_; setValue(valueNew); if (is('function', onUpdate)) onUpdate(valueNew); }, []); const done = React.useCallback(async () => { clear(); // Update value to 100 setValue(100); // Update inProp to false with the timeout value const timeout = theme.transitions.duration.rg + 44; await wait(timeout); setInProp(false); if (is('function', onDone)) onDone(); }, []); const onExited = () => { // Reset setValue(0); }; React.useEffect(() => { if (inProp) { if (refs.firstIncrement.current) increment(); } }, [inProp]); TransitionComponentProps.in = inProp; refs.value_.current.start = start; refs.value_.current.increment = increment; refs.value_.current.update = update; refs.value_.current.done = done; return /*#__PURE__*/React.createElement(MainProgressContext.Provider, { value: refs.value_.current }, /*#__PURE__*/React.createElement(TransitionComponent, _extends({ in: inProp, onExited: onExited }, TransitionComponentProps), /*#__PURE__*/React.createElement(LinearProgress, _extends({ ref: ref, value: value, version: "determinate", className: classNames([staticClassName('MainProgress', theme) && [`amaui-MainProgress-root`], className, classes.root, classes[`position_${position}`], fixed && classes.fixed]) }, other, refs.linearProgress.current))), children); }); MainProgress.displayName = 'amaui-MainProgress'; export default MainProgress;