TeaWeb/shared/js/ui/react-elements/LoadingDots.tsx

23 lines
729 B
TypeScript
Raw Normal View History

import {useEffect, useState} from "react";
import * as React from "react";
export const LoadingDots = (props: { maxDots?: number, speed?: number, textOnly?: boolean }) => {
2020-07-17 23:56:20 +02:00
let { maxDots, speed } = props;
if(!maxDots || maxDots < 1)
maxDots = 3;
const [dots, setDots] = useState(0);
useEffect(() => {
2020-07-17 23:56:20 +02:00
const timeout = setTimeout(() => setDots(dots + 1), speed || 500);
return () => clearTimeout(timeout);
});
let result = ".";
2020-07-17 23:56:20 +02:00
for(let index = 0; index < dots % maxDots; index++)
result += ".";
if(props.textOnly)
return <>{result}</>;
2020-07-17 23:56:20 +02:00
return <div style={{ width: (maxDots / 3) + "em", display: "inline-block", textAlign: "left" }}>{result}</div>;
};