|
| 1 | +// All credit goes to https://github.com/davidhu2000/react-spinners |
| 2 | +// This is copied from the react-spinners package directly |
| 3 | + |
| 4 | +import { CSSProperties, DetailedHTMLProps, HTMLAttributes } from 'react' |
| 5 | + |
| 6 | +interface CommonProps |
| 7 | + extends DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement> { |
| 8 | + color?: string |
| 9 | + loading?: boolean |
| 10 | + cssOverride?: CSSProperties |
| 11 | + speedMultiplier?: number |
| 12 | +} |
| 13 | +export type LengthType = number | string |
| 14 | +export interface LoaderSizeMarginProps extends CommonProps { |
| 15 | + size?: LengthType |
| 16 | + margin?: LengthType |
| 17 | +} |
| 18 | + |
| 19 | +export function cssValue(value: number | string): string { |
| 20 | + const lengthWithunit = parseLengthAndUnit(value) |
| 21 | + |
| 22 | + return `${lengthWithunit.value}${lengthWithunit.unit}` |
| 23 | +} |
| 24 | + |
| 25 | +export const createAnimation = ( |
| 26 | + loaderName: string, |
| 27 | + frames: string, |
| 28 | + suffix: string, |
| 29 | +): string => { |
| 30 | + const animationName = `react-spinners-${loaderName}-${suffix}` |
| 31 | + |
| 32 | + if (typeof window == 'undefined' || !window.document) { |
| 33 | + return animationName |
| 34 | + } |
| 35 | + |
| 36 | + const styleEl = document.createElement('style') |
| 37 | + document.head.appendChild(styleEl) |
| 38 | + const styleSheet = styleEl.sheet |
| 39 | + |
| 40 | + const keyFrames = ` |
| 41 | + @keyframes ${animationName} { |
| 42 | + ${frames} |
| 43 | + } |
| 44 | + ` |
| 45 | + |
| 46 | + if (styleSheet) { |
| 47 | + styleSheet.insertRule(keyFrames, 0) |
| 48 | + } |
| 49 | + |
| 50 | + return animationName |
| 51 | +} |
| 52 | +const cssUnit: { [unit: string]: boolean } = { |
| 53 | + cm: true, |
| 54 | + mm: true, |
| 55 | + in: true, |
| 56 | + px: true, |
| 57 | + pt: true, |
| 58 | + pc: true, |
| 59 | + em: true, |
| 60 | + ex: true, |
| 61 | + ch: true, |
| 62 | + rem: true, |
| 63 | + vw: true, |
| 64 | + vh: true, |
| 65 | + vmin: true, |
| 66 | + vmax: true, |
| 67 | + '%': true, |
| 68 | +} |
| 69 | +interface LengthObject { |
| 70 | + value: number |
| 71 | + unit: string |
| 72 | +} |
| 73 | +export function parseLengthAndUnit(size: number | string): LengthObject { |
| 74 | + if (typeof size === 'number') { |
| 75 | + return { |
| 76 | + value: size, |
| 77 | + unit: 'px', |
| 78 | + } |
| 79 | + } |
| 80 | + let value: number |
| 81 | + const valueString: string = (size.match(/^[0-9.]*/) || '').toString() |
| 82 | + if (valueString.includes('.')) { |
| 83 | + value = parseFloat(valueString) |
| 84 | + } else { |
| 85 | + value = parseInt(valueString, 10) |
| 86 | + } |
| 87 | + |
| 88 | + const unit: string = (size.match(/[^0-9]*$/) || '').toString() |
| 89 | + |
| 90 | + if (cssUnit[unit]) { |
| 91 | + return { |
| 92 | + value, |
| 93 | + unit, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + console.warn( |
| 98 | + `React Spinners: ${size} is not a valid css value. Defaulting to ${value}px.`, |
| 99 | + ) |
| 100 | + |
| 101 | + return { |
| 102 | + value, |
| 103 | + unit: 'px', |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +const sync = createAnimation( |
| 108 | + 'SyncLoader', |
| 109 | + `33% {transform: translateY(10px)} |
| 110 | + 66% {transform: translateY(-10px)} |
| 111 | + 100% {transform: translateY(0)}`, |
| 112 | + 'sync', |
| 113 | +) |
| 114 | + |
| 115 | +function SyncLoader({ |
| 116 | + loading = true, |
| 117 | + color = '#000000', |
| 118 | + speedMultiplier = 1, |
| 119 | + cssOverride = {}, |
| 120 | + size = 15, |
| 121 | + margin = 2, |
| 122 | + ...additionalprops |
| 123 | +}: LoaderSizeMarginProps): JSX.Element | null { |
| 124 | + const wrapper: React.CSSProperties = { |
| 125 | + display: 'inherit', |
| 126 | + ...cssOverride, |
| 127 | + } |
| 128 | + |
| 129 | + const style = (i: number): React.CSSProperties => { |
| 130 | + return { |
| 131 | + backgroundColor: color, |
| 132 | + width: cssValue(size), |
| 133 | + height: cssValue(size), |
| 134 | + margin: cssValue(margin), |
| 135 | + borderRadius: '100%', |
| 136 | + display: 'inline-block', |
| 137 | + animation: `${sync} ${0.6 / speedMultiplier}s ${ |
| 138 | + i * 0.07 |
| 139 | + }s infinite ease-in-out`, |
| 140 | + animationFillMode: 'both', |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (!loading) { |
| 145 | + return null |
| 146 | + } |
| 147 | + |
| 148 | + return ( |
| 149 | + <span style={wrapper} {...additionalprops}> |
| 150 | + <span style={style(1)} /> |
| 151 | + <span style={style(2)} /> |
| 152 | + <span style={style(3)} /> |
| 153 | + </span> |
| 154 | + ) |
| 155 | +} |
| 156 | + |
| 157 | +export default SyncLoader |
0 commit comments