Skip to content

Commit

Permalink
Show loading indicator on first load while fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
jungomi committed Feb 9, 2020
1 parent ac61c5c commit 519e0c2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 30 deletions.
24 changes: 15 additions & 9 deletions js/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Markdown } from "./Markdown";
import { Overlay, OverlayContext, OverlayFn } from "./Overlay";
import { Scalars } from "./Scalars";
import { Names, Sidebar } from "./Sidebar";
import { Loading } from "./Spinner";
import {
retrieveColours,
retrieveNames,
Expand Down Expand Up @@ -134,17 +135,22 @@ export const App = () => {
setNames={setNames}
colours={colours}
setColour={setNewColour}
loading={!hasFetched}
/>
<main className={styles.main}>
{Content && (
<div className={styles.content}>
<Content
data={data}
colours={colours}
names={names.active}
hideName={hideName}
/>
</div>
{hasFetched ? (
Content && (
<div className={styles.content}>
<Content
data={data}
colours={colours}
names={names.active}
hideName={hideName}
/>
</div>
)
) : (
<Loading />
)}
</main>
</div>
Expand Down
8 changes: 8 additions & 0 deletions js/Empty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ export const SmallEmpty: React.FC<Props> = ({ text }) => {
export const EmptyDash: React.FC = () => {
return <div className={baseClass}></div>;
};

export const EmptyLoading: React.FC = () => {
return <div className={emptyClass}>Loading</div>;
};

export const SmallEmptyLoading: React.FC = () => {
return <div className={smallEmptyClass}>Loading</div>;
};
23 changes: 15 additions & 8 deletions js/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState, useRef } from "react";
import React, { useEffect, useRef, useState } from "react";
import { ColourPicker } from "./colour/ColourPicker";
import { fieldHeight } from "./colour/ColourPicker.styles";
import {
Expand All @@ -9,6 +9,7 @@ import {
} from "./colour/definition";
import { EmptyDash, SmallEmpty } from "./Empty";
import * as styles from "./Sidebar.styles";
import { SmallLoading } from "./Spinner";

export type Names = { active: Array<string>; inactive: Array<string> };

Expand Down Expand Up @@ -37,13 +38,15 @@ type Props = {
setNames: (names: Names) => void;
colours: ColourMap;
setColour: (name: string, colour: Colour) => void;
loading: boolean;
};

export const Sidebar: React.FC<Props> = ({
names,
setNames,
colours,
setColour
setColour,
loading
}) => {
const listRef = useRef<HTMLDivElement>(null);
const [shownColourPicker, setShownColourPicker] = useState<
Expand Down Expand Up @@ -174,12 +177,16 @@ export const Sidebar: React.FC<Props> = ({
>
<path d="M11.727 26.71l9.977-9.999a1.012 1.012 0 000-1.429l-9.97-9.991c-.634-.66-1.748-.162-1.723.734v19.943c-.023.893 1.083 1.377 1.716.742zm7.84-10.713l-7.55 7.566V8.431l7.55 7.566z" />
</svg>
<div
className={shown ? styles.nameListContainer : styles.nameListHidden}
ref={listRef}
>
{hasData ? nameLists : <SmallEmpty text="data" />}
</div>
{loading ? (
<SmallLoading />
) : (
<div
className={shown ? styles.nameListContainer : styles.nameListHidden}
ref={listRef}
>
{hasData ? nameLists : <SmallEmpty text="data" />}
</div>
)}
</div>
);
};
24 changes: 24 additions & 0 deletions js/Spinner.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { css, keyframes } from "emotion";

const animation = keyframes({
"0%": { transform: "scale(0)" },
"100%": { transform: "scale(1.0)", opacity: 0 }
});

export const spinner = css({
width: "2rem",
height: "2rem",
background: "grey",
borderRadius: "50%",
animation: `${animation} 1.0s infinite ease-in-out`
});

export const loading = css({
display: "flex",
flexDirection: "column",
alignItems: "center"
});

export const loadingSpinner = css({
marginTop: "0.8rem"
});
32 changes: 19 additions & 13 deletions js/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { css, keyframes } from "emotion";
import React from "react";
import { EmptyLoading, SmallEmptyLoading } from "./Empty";
import * as styles from "./Spinner.styles";

const animation = keyframes({
"0%": { transform: "scale(0)" },
"100%": { transform: "scale(1.0)", opacity: 0 }
});
export const Spinner: React.FC = () => <div className={styles.spinner} />;

const spinnerClass = css({
width: "2rem",
height: "2rem",
background: "grey",
borderRadius: "50%",
animation: `${animation} 1.0s infinite ease-in-out`
});
export const Loading: React.FC = () => (
<div className={styles.loading}>
<EmptyLoading />
<div className={styles.loadingSpinner}>
<Spinner />
</div>
</div>
);

export const Spinner: React.FC = () => <div className={spinnerClass} />;
export const SmallLoading: React.FC = () => (
<div className={styles.loading}>
<SmallEmptyLoading />
<div className={styles.loadingSpinner}>
<Spinner />
</div>
</div>
);

0 comments on commit 519e0c2

Please sign in to comment.