|
| 1 | +import op from 'object-path'; |
| 2 | +import ReactDom from 'react-dom'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | + |
| 5 | +import React, { |
| 6 | + forwardRef, |
| 7 | + useEffect, |
| 8 | + useImperativeHandle, |
| 9 | + useRef, |
| 10 | + useState, |
| 11 | +} from 'react'; |
| 12 | + |
| 13 | +/** |
| 14 | + * ----------------------------------------------------------------------------- |
| 15 | + * Functional Component: Loading |
| 16 | + * ----------------------------------------------------------------------------- |
| 17 | + */ |
| 18 | +let Loading = ( |
| 19 | + { controlled, visible: initVisible, style: initStyle, ...props }, |
| 20 | + ref, |
| 21 | +) => { |
| 22 | + const containerRef = useRef(); |
| 23 | + const ival = useRef(); |
| 24 | + const opacity = useRef(initVisible === true ? 1 : 0); |
| 25 | + |
| 26 | + const [visible, setVisible] = useState(initVisible); |
| 27 | + |
| 28 | + const [init, setInit] = useState(false); |
| 29 | + |
| 30 | + const [style, setStyle] = useState({ |
| 31 | + ...initStyle, |
| 32 | + display: opacity.current === 1 ? null : 'none', |
| 33 | + opacity: opacity.current, |
| 34 | + }); |
| 35 | + |
| 36 | + const show = () => { |
| 37 | + let i = ival.current; |
| 38 | + if (i) clearInterval(i); |
| 39 | + |
| 40 | + const cont = containerRef.current; |
| 41 | + if (cont) { |
| 42 | + cont.style.display = null; |
| 43 | + if (init !== true) { |
| 44 | + cont.style.opacity = 0; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + i = setInterval(() => { |
| 49 | + opacity.current = opacity.current + 0.0125; |
| 50 | + opacity.current = Math.min(opacity.current, 1); |
| 51 | + |
| 52 | + if (cont) cont.style.opacity = opacity.current; |
| 53 | + |
| 54 | + if (opacity.current === 1) { |
| 55 | + clearInterval(i); |
| 56 | + setStyle({ ...style, display: null }); |
| 57 | + } |
| 58 | + }, 1); |
| 59 | + |
| 60 | + ival.current = i; |
| 61 | + }; |
| 62 | + |
| 63 | + const hide = () => { |
| 64 | + let i = ival.current; |
| 65 | + if (i) clearInterval(i); |
| 66 | + const cont = containerRef.current; |
| 67 | + |
| 68 | + i = setInterval(() => { |
| 69 | + opacity.current = opacity.current - 0.0125; |
| 70 | + opacity.current = Math.max(opacity.current, 0); |
| 71 | + |
| 72 | + if (cont) cont.style.opacity = opacity.current; |
| 73 | + |
| 74 | + if (opacity.current === 0) { |
| 75 | + cont.style.display = 'none'; |
| 76 | + clearInterval(i); |
| 77 | + setStyle({ ...style, opacity: null }); |
| 78 | + } |
| 79 | + }, 1); |
| 80 | + |
| 81 | + ival.current = i; |
| 82 | + }; |
| 83 | + |
| 84 | + useImperativeHandle( |
| 85 | + ref, |
| 86 | + () => ({ |
| 87 | + ...props, |
| 88 | + hide, |
| 89 | + show, |
| 90 | + containerRef, |
| 91 | + visible, |
| 92 | + setStyle, |
| 93 | + setVisible, |
| 94 | + }), |
| 95 | + [visible], |
| 96 | + ); |
| 97 | + |
| 98 | + useEffect(() => { |
| 99 | + if (controlled === true) { |
| 100 | + const val = op.get(props, 'visible'); |
| 101 | + if (val !== visible) setVisible(val); |
| 102 | + } |
| 103 | + }, [controlled, initVisible]); |
| 104 | + |
| 105 | + useEffect(() => { |
| 106 | + if (controlled === true) { |
| 107 | + const val = op.get(props, 'style'); |
| 108 | + if (val !== style) setStyle(val); |
| 109 | + } |
| 110 | + }, [controlled, initStyle]); |
| 111 | + |
| 112 | + useEffect(() => { |
| 113 | + if (visible === true) { |
| 114 | + show(); |
| 115 | + } else { |
| 116 | + hide(); |
| 117 | + } |
| 118 | + }, [visible]); |
| 119 | + |
| 120 | + useEffect(() => { |
| 121 | + setInit(true); |
| 122 | + }, []); |
| 123 | + |
| 124 | + return ReactDom.createPortal( |
| 125 | + <div ref={containerRef} style={style} {...props} />, |
| 126 | + document.body, |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +Loading = forwardRef(Loading); |
| 131 | + |
| 132 | +Loading.propTypes = { |
| 133 | + className: PropTypes.string, |
| 134 | + controlled: PropTypes.bool, |
| 135 | + style: PropTypes.object, |
| 136 | + visible: PropTypes.bool, |
| 137 | +}; |
| 138 | + |
| 139 | +Loading.defaultProps = { |
| 140 | + className: 'reactium-loading', |
| 141 | + controlled: false, |
| 142 | + style: {}, |
| 143 | + visible: true, |
| 144 | +}; |
| 145 | + |
| 146 | +export { Loading }; |
0 commit comments