|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow strict-local |
| 9 | + */ |
| 10 | + |
| 11 | +import type {Node} from 'react'; |
| 12 | + |
| 13 | +import React, {useCallback, useEffect, useRef, useState} from 'react'; |
| 14 | +import {ActivityIndicator, StyleSheet, View} from 'react-native'; |
| 15 | + |
| 16 | +function ToggleAnimatingActivityIndicator() { |
| 17 | + const timer = useRef<void | TimeoutID>(); |
| 18 | + |
| 19 | + const [animating, setAnimating] = useState(true); |
| 20 | + |
| 21 | + const setToggleTimeout: () => void = useCallback(() => { |
| 22 | + timer.current = setTimeout(() => { |
| 23 | + setAnimating(currentState => !currentState); |
| 24 | + setToggleTimeout(); |
| 25 | + }, 2000); |
| 26 | + }, []); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + setToggleTimeout(); |
| 30 | + |
| 31 | + return () => { |
| 32 | + clearTimeout(timer.current); |
| 33 | + }; |
| 34 | + }, [timer, setToggleTimeout]); |
| 35 | + |
| 36 | + return ( |
| 37 | + <ActivityIndicator |
| 38 | + animating={animating} |
| 39 | + style={[styles.centering, {height: 80}]} |
| 40 | + size="large" |
| 41 | + testID="activity-toggle" |
| 42 | + accessible |
| 43 | + /> |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +const styles = StyleSheet.create({ |
| 48 | + centering: { |
| 49 | + alignItems: 'center', |
| 50 | + justifyContent: 'center', |
| 51 | + padding: 8, |
| 52 | + }, |
| 53 | + gray: { |
| 54 | + backgroundColor: '#cccccc', |
| 55 | + }, |
| 56 | + horizontal: { |
| 57 | + flexDirection: 'row', |
| 58 | + justifyContent: 'space-around', |
| 59 | + padding: 8, |
| 60 | + }, |
| 61 | +}); |
| 62 | + |
| 63 | +exports.displayName = (undefined: ?string); |
| 64 | +exports.category = 'UI'; |
| 65 | +exports.framework = 'React'; |
| 66 | +exports.title = 'ActivityIndicator'; |
| 67 | +exports.documentationURL = 'https://reactnative.dev/docs/activityindicator'; |
| 68 | +exports.description = 'Animated loading indicators.'; |
| 69 | + |
| 70 | +exports.examples = [ |
| 71 | + { |
| 72 | + title: 'Default (small, white)', |
| 73 | + render(): Node { |
| 74 | + return ( |
| 75 | + <ActivityIndicator |
| 76 | + style={[styles.centering, styles.gray]} |
| 77 | + color="white" |
| 78 | + testID="default_activity_indicator" |
| 79 | + accessibilityLabel="Wait for content to load!" |
| 80 | + accessible |
| 81 | + /> |
| 82 | + ); |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + title: 'Gray', |
| 87 | + render(): Node { |
| 88 | + return ( |
| 89 | + <View> |
| 90 | + <ActivityIndicator style={[styles.centering]} /> |
| 91 | + <ActivityIndicator |
| 92 | + style={[styles.centering, styles.gray]} |
| 93 | + testID="activity-gray" |
| 94 | + accessible |
| 95 | + /> |
| 96 | + </View> |
| 97 | + ); |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + title: 'Custom colors', |
| 102 | + render(): Node { |
| 103 | + return ( |
| 104 | + <View style={styles.horizontal} testID="activity-color" accessible> |
| 105 | + <ActivityIndicator color="#0000ff" accessible /> |
| 106 | + <ActivityIndicator color="#aa00aa" accessible /> |
| 107 | + <ActivityIndicator color="#aa3300" accessible /> |
| 108 | + <ActivityIndicator color="#00aa00" accessible /> |
| 109 | + </View> |
| 110 | + ); |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + title: 'Large', |
| 115 | + render(): Node { |
| 116 | + return ( |
| 117 | + <ActivityIndicator |
| 118 | + style={[styles.centering, styles.gray]} |
| 119 | + size="large" |
| 120 | + color="white" |
| 121 | + testID="activity-large" |
| 122 | + accessible |
| 123 | + /> |
| 124 | + ); |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + title: 'Large, custom colors', |
| 129 | + render(): Node { |
| 130 | + return ( |
| 131 | + <View |
| 132 | + style={styles.horizontal} |
| 133 | + testID="activity-large-color" |
| 134 | + accessible> |
| 135 | + <ActivityIndicator size="large" color="#0000ff" accessible /> |
| 136 | + <ActivityIndicator size="large" color="#aa00aa" accessible /> |
| 137 | + <ActivityIndicator size="large" color="#aa3300" accessible /> |
| 138 | + <ActivityIndicator size="large" color="#00aa00" accessible /> |
| 139 | + </View> |
| 140 | + ); |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + title: 'Start/stop', |
| 145 | + render(): Node { |
| 146 | + return <ToggleAnimatingActivityIndicator />; |
| 147 | + }, |
| 148 | + }, |
| 149 | + { |
| 150 | + title: 'Custom size', |
| 151 | + render(): Node { |
| 152 | + return ( |
| 153 | + <ActivityIndicator |
| 154 | + style={[styles.centering, {transform: [{scale: 1.5}]}]} |
| 155 | + size="large" |
| 156 | + testID="activity-size" |
| 157 | + accessible |
| 158 | + /> |
| 159 | + ); |
| 160 | + }, |
| 161 | + }, |
| 162 | + { |
| 163 | + platform: 'android', |
| 164 | + title: 'Custom size (size: 75)', |
| 165 | + render(): Node { |
| 166 | + return <ActivityIndicator style={styles.centering} size={75} />; |
| 167 | + }, |
| 168 | + }, |
| 169 | +]; |
0 commit comments