Skip to content

Commit

Permalink
feat: fade error notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardocasares committed Oct 31, 2020
1 parent b823bf6 commit 4389bea
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/components/Player/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export const Player = () => {
const { t } = useTranslation();
const { station, play, pause, stop, error, playing, loading } = usePlayer();

const closeNotification = () => stop();

if (!station) return null;

if (error) {
return (
<ErrorNotification>
<ErrorNotification onAnimationEnd={closeNotification}>
<AlertCircle /> <p>{t.player.error}</p>
</ErrorNotification>
);
Expand Down
2 changes: 2 additions & 0 deletions src/components/Player/style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import styled from "@emotion/styled";
import { fadeInAndOut } from "@/css/animations";

export const Container = styled.footer`
display: grid;
Expand Down Expand Up @@ -30,6 +31,7 @@ export const ErrorNotification = styled.div`
background: var(--error);
font-size: var(--sz4);
padding: var(--sz5) var(--sz4);
animation: ${fadeInAndOut} linear 5s;
`;

export const Controls = styled.section`
Expand Down
19 changes: 19 additions & 0 deletions src/css/animations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { keyframes } from "@emotion/core";

export const fadeInAndOut = keyframes`
0% {
opacity: 0;
}
10% {
opacity: 1;
}
90% {
opacity: 1;
}
100% {
opacity: 0
}
`;
18 changes: 6 additions & 12 deletions src/lib/hooks/usePlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,65 +38,59 @@ export function usePlayer() {
});
};

const onPlaying = () => {
const onPlaying = () =>
setState((state) => ({
...state,
error: false,
paused: false,
loading: false,
playing: true,
}));
};

const onPause = () => {
const onPause = () =>
setState((state) => ({
...state,
error: false,
paused: true,
loading: false,
playing: false,
}));
};

const onCanPlayThrough = () => {
const onCanPlayThrough = () =>
setState((state) => ({
...state,
error: false,
paused: false,
loading: false,
playing: true,
}));
};

const onLoadStart = () => {
const onLoadStart = () =>
setState((state) => ({
...state,
error: false,
paused: false,
loading: true,
playing: false,
}));
};

const onError = () => {
const onError = () =>
setState((state) => ({
...state,
error: true,
paused: false,
loading: false,
playing: false,
}));
};

const onEnded = () => {
const onEnded = () =>
setState((state) => ({
...state,
error: false,
paused: false,
playing: false,
station: null,
}));
};

useEffect(() => {
audio.current.addEventListener("error", onError);
Expand Down

0 comments on commit 4389bea

Please sign in to comment.