Skip to content

Commit 5f37a78

Browse files
loading animation which can be displayed while data is fetched from an api
0 parents  commit 5f37a78

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Fetch Loading
2+
3+
Loading animation which can be displayed while data is fetched from an API.

package-lock.json

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "fetch-loading",
3+
"version": "1.0.0",
4+
"description": "Loading animation which can be displayed while data is fetched from an API",
5+
"main": "src/index.ts",
6+
"files": [
7+
"src/*"
8+
],
9+
"keywords": [
10+
"loading",
11+
"animation",
12+
"fetch",
13+
"api",
14+
"spinner",
15+
"react",
16+
"typescript"
17+
],
18+
"prettier": "prettier-es5",
19+
"author": "Michael Münzenhofer",
20+
"license": "MIT",
21+
"devDependencies": {
22+
"@tsconfig/recommended": "^1.0.8",
23+
"@types/react": "^19.0.8"
24+
},
25+
"dependencies": {
26+
"react": "^19.0.0",
27+
"typescript": "^5.7.3"
28+
}
29+
}

src/FetchLoading.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { FC } from 'react'
2+
import './index.css'
3+
4+
type FetchLoadingProps = {
5+
theme?: string
6+
}
7+
type DotProps = {
8+
delay?: number
9+
}
10+
11+
const isValidHex = (color: string): boolean =>
12+
/^#([0-9a-f]{3}|[0-9a-f]{6})$/i.test(color)
13+
14+
const Dot: FC<DotProps & FetchLoadingProps> = ({ delay = 0, theme }) => {
15+
const background = theme && isValidHex(theme) ? theme : '#52525c'
16+
return (
17+
<div
18+
style={{
19+
animation: 'fetch-loading 2s ease-in-out infinite',
20+
animationDelay: `${delay}s`,
21+
height: '12px',
22+
width: '12px',
23+
backgroundColor: background,
24+
}}
25+
></div>
26+
)
27+
}
28+
29+
export const FetchLoading: FC<FetchLoadingProps> = ({ theme }) => {
30+
return (
31+
<>
32+
<div style={{ display: 'flex', gap: '8px', padding: '6px 2px' }}>
33+
<Dot delay={0} theme={theme} />
34+
<Dot delay={-1.33} theme={theme} />
35+
<Dot delay={-0.67} theme={theme} />
36+
</div>
37+
</>
38+
)
39+
}

src/index.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@keyframes fetch-loading {
2+
0% {
3+
opacity: 0;
4+
scale: 0.5;
5+
border-radius: 0%;
6+
transform: translate(0px);
7+
}
8+
33% {
9+
opacity: 1;
10+
scale: 1;
11+
border-radius: 80%;
12+
transform: translate(0px, -6px);
13+
}
14+
60% {
15+
opacity: 1;
16+
scale: 1;
17+
border-radius: 80% 80% 60% 60%;
18+
transform: translate(0px, 4px);
19+
}
20+
100% {
21+
opacity: 0;
22+
scale: 0.5;
23+
border-radius: 0%;
24+
transform: translate(0px, -2px);
25+
}
26+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './FetchLoading'

tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "@tsconfig/recommended/tsconfig.json",
3+
"compilerOptions": {
4+
"jsx": "preserve"
5+
}
6+
}

0 commit comments

Comments
 (0)