From 8d8ea5617bedf06044e1b4fb8c776562ae7ffda6 Mon Sep 17 00:00:00 2001 From: Sendou Date: Wed, 22 Jan 2020 18:28:43 +0200 Subject: [PATCH] custom theme started using context --- .../src/components/builds/BuildCard.tsx | 8 ++- .../src/components/builds/ViewAP.tsx | 6 +- .../src/components/common/Loading.tsx | 6 +- frontend-react/src/components/root/App.tsx | 68 +++++++++++++++---- .../src/components/root/ColorPicker.tsx | 6 +- .../src/components/root/NavItem.tsx | 6 +- .../src/components/root/SideNav.tsx | 6 +- frontend-react/src/components/ui/Button.tsx | 6 +- .../src/components/ui/DividingBox.tsx | 6 +- .../src/components/user/UserPage.tsx | 8 ++- .../src/components/user/WeaponPool.tsx | 6 +- frontend-react/src/hooks/useTheme.ts | 55 --------------- frontend-react/src/themeContext.ts | 17 +++++ frontend-react/src/types.ts | 14 ++++ 14 files changed, 118 insertions(+), 100 deletions(-) delete mode 100644 frontend-react/src/hooks/useTheme.ts create mode 100644 frontend-react/src/themeContext.ts diff --git a/frontend-react/src/components/builds/BuildCard.tsx b/frontend-react/src/components/builds/BuildCard.tsx index 17f890573..7c6ef569b 100644 --- a/frontend-react/src/components/builds/BuildCard.tsx +++ b/frontend-react/src/components/builds/BuildCard.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react" +import React, { useState, useContext } from "react" import { Build } from "../../types" import { Box, @@ -10,12 +10,12 @@ import { PopoverContent, PopoverBody, } from "@chakra-ui/core" -import useTheme from "../../hooks/useTheme" import WeaponImage from "../common/WeaponImage" import { top500 } from "../../assets/imageImports" import { FaInfo, FaPlus, FaMinus } from "react-icons/fa" import ViewGear from "./ViewGear" import ViewAP from "./ViewAP" +import MyThemeContext from "../../themeContext" interface BuildCardProps { build: Build @@ -24,7 +24,9 @@ interface BuildCardProps { const BuildCard: React.FC = ({ build, defaultToAPView }) => { const [apView, setApView] = useState(defaultToAPView) - const { borderStyle, themeColor, darkerBgColor, grayWithShade } = useTheme() + const { borderStyle, themeColor, darkerBgColor, grayWithShade } = useContext( + MyThemeContext + ) return ( = ({ build }) => { - const { grayWithShade } = useTheme() + const { grayWithShade } = useContext(MyThemeContext) const abilityArrays: Ability[][] = [ build.headgear, build.clothing, diff --git a/frontend-react/src/components/common/Loading.tsx b/frontend-react/src/components/common/Loading.tsx index 98df4f5ee..3fb86299b 100644 --- a/frontend-react/src/components/common/Loading.tsx +++ b/frontend-react/src/components/common/Loading.tsx @@ -1,11 +1,11 @@ -import React from "react" +import React, { useContext } from "react" import { Spinner, Box } from "@chakra-ui/core" -import useTheme from "../../hooks/useTheme" +import MyThemeContext from "../../themeContext" interface LoadingProps {} const Loading: React.FC = () => { - const { themeColorWithShade } = useTheme() + const { themeColorWithShade } = useContext(MyThemeContext) return ( { - const { bgColor, textColor } = useTheme() + const chakraTheme = useChakraTheme() + const { colorMode } = useColorMode() + const [themeColorFromStorage] = useLocalStorage("colorPreference") + + const themeColor = themeColorFromStorage + ? themeColorFromStorage + : colorMode === "light" + ? "pink" + : "orange" + + const theme = { + light: { + colorMode: "light", + bgColor: "#eff0f3", + darkerBgColor: "#D6D7DA", + textColor: "#0d0d0d", + borderStyle: "1px solid rgba(0, 0, 0, .2)", + themeColorWithShade: `${themeColor}.500`, + grayWithShade: "gray.600", + themeColorHex: chakraTheme.colors[themeColor]["500"], + themeColor, + } as Theme, + dark: { + colorMode: "dark", + bgColor: "#232946", + darkerBgColor: "#0A102D", + textColor: "#fffffe", + borderStyle: "1px solid rgba(255, 255, 255, .2)", + themeColorWithShade: `${themeColor}.200`, + grayWithShade: "gray.300", + themeColorHex: chakraTheme.colors[themeColor]["500"], + themeColor, + } as Theme, + } return ( - - - - - + + + + + + + - + ) } diff --git a/frontend-react/src/components/root/ColorPicker.tsx b/frontend-react/src/components/root/ColorPicker.tsx index 770f3d0cd..c313d6020 100644 --- a/frontend-react/src/components/root/ColorPicker.tsx +++ b/frontend-react/src/components/root/ColorPicker.tsx @@ -1,4 +1,4 @@ -import React from "react" +import React, { useContext } from "react" import { Box, PopoverTrigger, @@ -10,12 +10,12 @@ import { import { CirclePicker, ColorResult } from "react-color" import { writeStorage } from "@rehooks/local-storage" import { themeColors as choices } from "../../utils/lists" -import useTheme from "../../hooks/useTheme" +import MyThemeContext from "../../themeContext" const size = "20px" as const const ColorPicker: React.FC = () => { - const { themeColorHex, bgColor } = useTheme() + const { themeColorHex, bgColor } = useContext(MyThemeContext) const theme = useChakraTheme() const hexCodes = choices.map(color => diff --git a/frontend-react/src/components/root/NavItem.tsx b/frontend-react/src/components/root/NavItem.tsx index 889a231ef..e32af7052 100644 --- a/frontend-react/src/components/root/NavItem.tsx +++ b/frontend-react/src/components/root/NavItem.tsx @@ -1,8 +1,8 @@ -import React from "react" +import React, { useContext } from "react" import { PseudoBox, ListIcon, ListItem } from "@chakra-ui/core" import { Link } from "@reach/router" import { IconType } from "react-icons/lib/cjs" -import useTheme from "../../hooks/useTheme" +import MyThemeContext from "../../themeContext" /*const hoverColor = { light: "gray.900", dark: "whiteAlpha.900" } const activeColor = { light: "orange.800", dark: "pink.100" } @@ -15,7 +15,7 @@ interface NavItemProps { } const NavItem: React.FC = ({ to, Icon, title }) => { - const { themeColorWithShade } = useTheme() + const { themeColorWithShade } = useContext(MyThemeContext) return ( diff --git a/frontend-react/src/components/root/SideNav.tsx b/frontend-react/src/components/root/SideNav.tsx index 5af5121bf..c7c2610df 100644 --- a/frontend-react/src/components/root/SideNav.tsx +++ b/frontend-react/src/components/root/SideNav.tsx @@ -1,7 +1,7 @@ import { Box } from "@chakra-ui/core" -import React from "react" +import React, { useContext } from "react" import { SideNavContent } from "./SideNavContent" -import useTheme from "../../hooks/useTheme" +import MyThemeContext from "../../themeContext" const shadow = { light: "0px 1px 10px 8px rgba(0,0,0,0.15)", @@ -9,7 +9,7 @@ const shadow = { } as const const SideNav = () => { - const { darkerBgColor, colorMode } = useTheme() + const { darkerBgColor, colorMode } = useContext(MyThemeContext) return ( = ({ children, onClick }) => { - const { themeColor } = useTheme() + const { themeColor } = useContext(MyThemeContext) return ( {children} diff --git a/frontend-react/src/components/ui/DividingBox.tsx b/frontend-react/src/components/ui/DividingBox.tsx index 6a5073f1b..ea728bad1 100644 --- a/frontend-react/src/components/ui/DividingBox.tsx +++ b/frontend-react/src/components/ui/DividingBox.tsx @@ -1,6 +1,6 @@ -import React from "react" +import React, { useContext } from "react" import { Box } from "@chakra-ui/core" -import useTheme from "../../hooks/useTheme" +import MyThemeContext from "../../themeContext" interface DividingBoxProps { children: JSX.Element | JSX.Element[] | string @@ -15,7 +15,7 @@ const DividingBox: React.FC = ({ width = undefined, margin = "0.4em", }) => { - const { borderStyle } = useTheme() + const { borderStyle } = useContext(MyThemeContext) return ( = ({ id }) => { skip: !data || !data.searchForUser, }) - const { textColor, themeColor, themeColorWithShade } = useTheme() + const { textColor, themeColor, themeColorWithShade } = useContext( + MyThemeContext + ) if (loading || userLoading || buildsLoading) return if (error) return diff --git a/frontend-react/src/components/user/WeaponPool.tsx b/frontend-react/src/components/user/WeaponPool.tsx index 515fb2270..12a47ac84 100644 --- a/frontend-react/src/components/user/WeaponPool.tsx +++ b/frontend-react/src/components/user/WeaponPool.tsx @@ -1,8 +1,8 @@ -import React from "react" +import React, { useContext } from "react" import { Weapon } from "../../types" import { Box, Flex } from "@chakra-ui/core" import WeaponImage from "../common/WeaponImage" -import useTheme from "../../hooks/useTheme" +import MyThemeContext from "../../themeContext" interface WeaponPoolProps { weapons: Weapon[] @@ -14,7 +14,7 @@ const styles = { } as const const WeaponPool: React.FC = ({ weapons }) => { - const { colorMode, grayWithShade } = useTheme() + const { colorMode, grayWithShade } = useContext(MyThemeContext) const borderStyle: string = styles[colorMode] return ( diff --git a/frontend-react/src/hooks/useTheme.ts b/frontend-react/src/hooks/useTheme.ts deleted file mode 100644 index 3b9a1b97b..000000000 --- a/frontend-react/src/hooks/useTheme.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { useColorMode, useTheme as useChakraTheme } from "@chakra-ui/core" -import { useLocalStorage } from "@rehooks/local-storage" -import { ThemeColor } from "../types" - -interface Theme { - colorMode: "light" | "dark" - bgColor: "#eff0f3" | "#232946" - darkerBgColor: "#D6D7DA" | "#0A102D" - textColor: "#0d0d0d" | "#fffffe" - borderStyle: - | "1px solid rgba(0, 0, 0, .2)" - | "1px solid rgba(255, 255, 255, .2)" - grayWithShade: "gray.300" | "gray.600" - themeColorWithShade: string - themeColorHex: string - themeColor: ThemeColor -} - -function useTheme(): Theme { - const { colorMode } = useColorMode() - const chakraTheme = useChakraTheme() - const light = colorMode === "light" - const bgColor = light ? "#eff0f3" : "#232946" - const darkerBgColor = light ? "#D6D7DA" : "#0A102D" - const textColor = light ? "#0d0d0d" : "#fffffe" - const grayWithShade = light ? "gray.600" : "gray.300" - const borderStyle = light - ? "1px solid rgba(0, 0, 0, .2)" - : "1px solid rgba(255, 255, 255, .2)" - const [themeColorFromStorage] = useLocalStorage("colorPreference") - - const themeColor = themeColorFromStorage - ? themeColorFromStorage - : light - ? "pink" - : "orange" - - const themeColorWithShade = light ? `${themeColor}.500` : `${themeColor}.200` - - const themeColorHex = chakraTheme.colors[themeColor]["500"] - - return { - colorMode, - bgColor, - darkerBgColor, - textColor, - borderStyle, - themeColorWithShade, - grayWithShade, - themeColorHex, - themeColor, - } -} - -export default useTheme diff --git a/frontend-react/src/themeContext.ts b/frontend-react/src/themeContext.ts new file mode 100644 index 000000000..4c93bf4d0 --- /dev/null +++ b/frontend-react/src/themeContext.ts @@ -0,0 +1,17 @@ +import React from "react" +import { Theme } from "./types" + +const MyThemeContext = React.createContext({ + colorMode: "dark", + bgColor: "#232946", + darkerBgColor: "#0A102D", + textColor: "#fffffe", + borderStyle: "1px solid rgba(255, 255, 255, .2)", + themeColorWithShade: "pink.200", + grayWithShade: "gray.300", + themeColorHex: "#D53F8C", + themeColor: "pink", +}) + +export const MyThemeProvider = MyThemeContext.Provider +export default MyThemeContext diff --git a/frontend-react/src/types.ts b/frontend-react/src/types.ts index e88ce55ca..42eb36dd2 100644 --- a/frontend-react/src/types.ts +++ b/frontend-react/src/types.ts @@ -36,6 +36,20 @@ export type HeadGear = ElementType export type ClothingGear = ElementType export type ShoesGear = ElementType +export interface Theme { + colorMode: "dark" | "light" + bgColor: "#eff0f3" | "#232946" + darkerBgColor: "#0A102D" | "#D6D7DA" + textColor: "#fffffe" | "#0d0d0d" + borderStyle: + | "1px solid rgba(0, 0, 0, .2)" + | "1px solid rgba(255, 255, 255, .2)" + grayWithShade: "gray.300" | "gray.600" + themeColorWithShade: string + themeColorHex: string + themeColor: ThemeColor +} + export interface UserLean { id: string username: string