|
| 1 | +import * as React from "react"; |
| 2 | +import { |
| 3 | + StyleProp, |
| 4 | + ViewStyle, |
| 5 | + ActivityIndicator as ActivityIndicatorRN, |
| 6 | + View, |
| 7 | + Platform, |
| 8 | +} from "react-native"; |
| 9 | +import { withTheme } from "@draftbit/theme"; |
| 10 | +import type { ReadTheme } from "@draftbit/theme"; |
| 11 | +import { |
| 12 | + Bounce, |
| 13 | + Chase, |
| 14 | + Circle, |
| 15 | + CircleFade, |
| 16 | + Flow, |
| 17 | + Fold, |
| 18 | + Grid, |
| 19 | + Plane, |
| 20 | + Pulse, |
| 21 | + Swing, |
| 22 | + Wander, |
| 23 | + Wave, |
| 24 | +} from "react-native-animated-spinkit"; |
| 25 | + |
| 26 | +export enum ActivityIndicatorType { |
| 27 | + default = "default", |
| 28 | + plane = "plane", |
| 29 | + chase = "chase", |
| 30 | + bounce = "bounce", |
| 31 | + wave = "wave", |
| 32 | + pulse = "pulse", |
| 33 | + flow = "flow", |
| 34 | + swing = "swing", |
| 35 | + circle = "circle", |
| 36 | + circleFade = "circleFade", |
| 37 | + grid = "grid", |
| 38 | + fold = "fold", |
| 39 | + wander = "wander", |
| 40 | +} |
| 41 | + |
| 42 | +type Props = { |
| 43 | + style?: StyleProp<ViewStyle>; |
| 44 | + color?: string; |
| 45 | + theme: ReadTheme; |
| 46 | + type?: ActivityIndicatorType; |
| 47 | + size?: number | "small" | "large"; |
| 48 | +}; |
| 49 | + |
| 50 | +const SPINNER_COMPONENTS = { |
| 51 | + [ActivityIndicatorType.plane]: Plane, |
| 52 | + [ActivityIndicatorType.chase]: Chase, |
| 53 | + [ActivityIndicatorType.bounce]: Bounce, |
| 54 | + [ActivityIndicatorType.wave]: Wave, |
| 55 | + [ActivityIndicatorType.pulse]: Pulse, |
| 56 | + [ActivityIndicatorType.flow]: Flow, |
| 57 | + [ActivityIndicatorType.swing]: Swing, |
| 58 | + [ActivityIndicatorType.circle]: Circle, |
| 59 | + [ActivityIndicatorType.circleFade]: CircleFade, |
| 60 | + [ActivityIndicatorType.grid]: Grid, |
| 61 | + [ActivityIndicatorType.fold]: Fold, |
| 62 | + [ActivityIndicatorType.wander]: Wander, |
| 63 | +}; |
| 64 | + |
| 65 | +const smallActivityIndicatorSize = 20; |
| 66 | +const largeActivityIndicatorSize = 40; |
| 67 | + |
| 68 | +const getLoadingSizeNumber = (size?: number | "small" | "large"): number => { |
| 69 | + if (typeof size === "number") return size; |
| 70 | + return size === "large" |
| 71 | + ? largeActivityIndicatorSize |
| 72 | + : smallActivityIndicatorSize; |
| 73 | +}; |
| 74 | + |
| 75 | +const getScaleLevel = (size?: number | "small" | "large"): number => { |
| 76 | + if (typeof size === "number") return size / smallActivityIndicatorSize; |
| 77 | + return size === "large" ? 2 : 1; |
| 78 | +}; |
| 79 | + |
| 80 | +const ActivityIndicator: React.FC<React.PropsWithChildren<Props>> = ({ |
| 81 | + theme, |
| 82 | + color = theme.colors.branding.primary, |
| 83 | + type = ActivityIndicatorType.default, |
| 84 | + size = "small", |
| 85 | + style, |
| 86 | + ...rest |
| 87 | +}) => { |
| 88 | + const sizeNumber = getLoadingSizeNumber(size); |
| 89 | + const spinnerColor = color ?? theme.colors.branding.primary; |
| 90 | + |
| 91 | + // Handle display spinner type |
| 92 | + if (type !== ActivityIndicatorType.default) { |
| 93 | + if (type && SPINNER_COMPONENTS[type]) { |
| 94 | + const SpinnerComponent = SPINNER_COMPONENTS[type]; |
| 95 | + return ( |
| 96 | + <SpinnerComponent |
| 97 | + size={sizeNumber} |
| 98 | + color={spinnerColor} |
| 99 | + style={style} |
| 100 | + /> |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // This is due to limitations with iOS UIActivityIndicator |
| 106 | + // Refer: https://github.com/facebook/react-native/issues/12250 |
| 107 | + const scaleLevel = getScaleLevel(size); |
| 108 | + if (Platform.OS === "ios") { |
| 109 | + return ( |
| 110 | + <View |
| 111 | + style={[ |
| 112 | + style, |
| 113 | + { |
| 114 | + transform: [{ scale: scaleLevel }], |
| 115 | + }, |
| 116 | + ]} |
| 117 | + > |
| 118 | + <ActivityIndicatorRN |
| 119 | + animating={true} |
| 120 | + hidesWhenStopped={true} |
| 121 | + size={"small"} |
| 122 | + color={spinnerColor} |
| 123 | + {...rest} |
| 124 | + /> |
| 125 | + </View> |
| 126 | + ); |
| 127 | + } |
| 128 | + return ( |
| 129 | + <ActivityIndicatorRN |
| 130 | + animating={true} |
| 131 | + hidesWhenStopped={true} |
| 132 | + size={size} |
| 133 | + color={spinnerColor} |
| 134 | + style={style} |
| 135 | + {...rest} |
| 136 | + /> |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +export default withTheme(ActivityIndicator); |
0 commit comments