From 78a69c9f3bc4db3ddf61753e6172380580a95954 Mon Sep 17 00:00:00 2001 From: rektdeckard Date: Mon, 14 Sep 2020 17:43:01 -0400 Subject: [PATCH] IconGrid: extract empty state into Warn component The empty state now renders a Warn component, which can be used to show empty queries, or to show an arbitrary error message. --- src/components/IconGrid/IconGrid.tsx | 28 ++++------------------ src/components/Warn/Warn.tsx | 36 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 src/components/Warn/Warn.tsx diff --git a/src/components/IconGrid/IconGrid.tsx b/src/components/IconGrid/IconGrid.tsx index 4deb3d0..f167662 100644 --- a/src/components/IconGrid/IconGrid.tsx +++ b/src/components/IconGrid/IconGrid.tsx @@ -2,19 +2,15 @@ import React, { useRef, useEffect } from "react"; import { useRecoilValue } from "recoil"; import { motion, useAnimation } from "framer-motion"; import { useWindowSize } from "react-use"; -import { IconContext, Warning } from "phosphor-react"; +import { IconContext } from "phosphor-react"; -import { - iconStyleAtom, - iconSizeAtom, - iconColorAtom, - searchQueryAtom, -} from "../../state/atoms"; +import { iconStyleAtom, iconSizeAtom, iconColorAtom } from "../../state/atoms"; import { filteredQueryResultsSelector, isDarkThemeSelector, } from "../../state/selectors"; import GridItem from "./IconGridItem"; +import Warn from "../Warn/Warn"; import "./IconGrid.css"; type IconGridProps = {}; @@ -23,7 +19,6 @@ const IconGrid: React.FC = () => { const weight = useRecoilValue(iconStyleAtom); const size = useRecoilValue(iconSizeAtom); const color = useRecoilValue(iconColorAtom); - const query = useRecoilValue(searchQueryAtom); const isDark = useRecoilValue(isDarkThemeSelector); const { width } = useWindowSize(); @@ -38,22 +33,7 @@ const IconGrid: React.FC = () => { controls.start("visible"); }, [controls, filteredQueryResults]); - if (!filteredQueryResults.length) - return ( -
- - -

- No results for '{query}' -

-
-
- ); + if (!filteredQueryResults.length) return ; return ( diff --git a/src/components/Warn/Warn.tsx b/src/components/Warn/Warn.tsx new file mode 100644 index 0000000..d9fa146 --- /dev/null +++ b/src/components/Warn/Warn.tsx @@ -0,0 +1,36 @@ +import React from "react"; +import { motion } from "framer-motion"; +import { useRecoilValue } from "recoil"; + +import { isDarkThemeSelector } from "../../state/selectors"; +import { searchQueryAtom } from "../../state/atoms"; +import { Warning } from "phosphor-react"; + +interface WarnProps { + message?: string; +} + +const Warn: React.FC = ({ message }) => { + const isDark = useRecoilValue(isDarkThemeSelector); + const query = useRecoilValue(searchQueryAtom); + + return ( +
+ + + {message ?? ( +

+ No results for '{query}' +

+ )} +
+
+ ); +}; + +export default Warn;