Skip to content

Commit 36693dd

Browse files
committed
fix(dots): fix loading indicator
1 parent 1dd8113 commit 36693dd

7 files changed

Lines changed: 32 additions & 17 deletions

File tree

src/components/Button/Button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { DeepPartial } from 'ts-essentials';
1010

1111
import { ButtonColor, ControlSize, useTheme } from '../../theme';
1212
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
13-
import { LoadingDots } from '../Loading';
13+
import { Dots } from '../LoadingIndicators';
1414
import { Text } from '../Typography';
1515
import {
1616
ButtonAppearance,
@@ -178,7 +178,7 @@ export interface ButtonContentProps extends ButtonProps {
178178
const ButtonContent = (props: ButtonContentProps) => {
179179
const { isLoading, iconLoading, icon, title, textStyle, size } = props;
180180

181-
if (isLoading) return iconLoading || <LoadingDots color={textStyle.color} />;
181+
if (isLoading) return iconLoading || <Dots color={textStyle.color} />;
182182
if (icon) return icon;
183183
if (title) {
184184
return (

src/components/Loading/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@ import * as React from 'react';
22
import { View } from 'react-native';
33
import { animated, useTrail } from 'react-spring/native.cjs';
44

5+
import { useForceUpdate } from '../../hooks';
6+
import { useTheme } from '../../theme';
7+
58
const AnimatedView = animated(View);
69

7-
export interface LoadingDotsProps {
10+
export interface DotsProps {
811
color?: string;
912
size?: number;
1013
}
1114

12-
const items = ['1', '2', '3'];
13-
14-
export const LoadingDots = (props: LoadingDotsProps) => {
15-
const { size = 10, color = '#aaa' } = props;
15+
export const Dots = (props: DotsProps) => {
16+
const theme = useTheme();
17+
const { size = 10, color = theme.colors.text.primary } = props;
18+
const forceUpdate = useForceUpdate();
1619

17-
const trail = useTrail(items.length, {
20+
const trail = useTrail(3, {
1821
config: { duration: 800 },
1922
from: { opacity: 0 },
23+
onRest: forceUpdate,
2024
reset: true,
2125
to: async next => {
22-
while (true) {
23-
// tslint:disable-next-line
24-
await next({ opacity: 1 });
25-
// tslint:disable-next-line
26-
await next({ opacity: 0 });
27-
}
26+
// tslint:disable-next-line
27+
await next({ opacity: 1 });
28+
// tslint:disable-next-line
29+
await next({ opacity: 0 });
2830
},
2931
});
3032

@@ -38,7 +40,7 @@ export const LoadingDots = (props: LoadingDotsProps) => {
3840
>
3941
{trail.map((style, index) => (
4042
<AnimatedView
41-
key={items[index]}
43+
key={index}
4244
// @ts-ignore
4345
style={{
4446
backgroundColor: color,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Dots';

src/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export * from './Icon';
1616
export * from './Inputs';
1717
export * from './KitchenSink';
1818
export * from './ListItem';
19-
export * from './Loading';
19+
export * from './LoadingIndicators';
2020
export * from './Modal';
2121
export * from './Overlay';
2222
export * from './Pickers';

src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './usePrevious';
22
export * from './useMeasure';
3+
export * from './useForceUpdate';
34
export * from './useElement';
45
export * from './useLockBodyScroll';

src/hooks/useForceUpdate.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
3+
const reducer = (state: boolean, action: null): boolean => !state;
4+
5+
export const useForceUpdate = () => {
6+
const [, dispatch] = React.useReducer(reducer, true);
7+
8+
// Turn dispatch(required_parameter) into dispatch().
9+
return React.useCallback(() => {
10+
dispatch(null);
11+
}, [dispatch]);
12+
};

0 commit comments

Comments
 (0)