Created
November 8, 2019 16:57
-
-
Save ryanwilsonperkin/8bd74ef5fc0078e9aa85a9db2e9d3ac2 to your computer and use it in GitHub Desktop.
Revisions
-
ryanwilsonperkin renamed this gist
Nov 8, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ryanwilsonperkin created this gist
Nov 8, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ import React from "react"; import { ThemeProvider } from "./Theme"; import ToggleTheme from "./ToggleTheme"; import ThemedText from "./ThemedText"; const App: React.FC = () => ( <ThemeProvider> <ToggleTheme /> <ThemedText>Hello World</ThemedText> </ThemeProvider> ) export default App; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ import React from "react"; type Theme = "light" | "dark"; interface IThemeContext { theme: Theme; setTheme: (theme: Theme) => void; } // Here we create the ThemeContext which our components will reference // We provide the default values that will be used if there's no <ThemeProvider> in the tree export const ThemeContext = React.createContext<IThemeContext>({ // By default, we'll use the light theme theme: "light", // If the <ThemeProvider> isn't in the tree, then calling setTheme will be a no-op setTheme: () => {} }); export const ThemeProvider: React.FC = ({ children }) => { const [theme, setTheme] = React.useState<Theme>("light"); return ( <ThemeContext.Provider value={{ theme, setTheme }}> {children} </ThemeContext.Provider> ); }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ import React from "react"; import { ThemeContext } from "./Theme"; const ThemedText: React.FC = ({ children }) => { const { theme } = React.useContext(ThemeContext); const textColor = theme === "light" ? "black" : "white"; return <p style={{color: textColor}}>{children}</p> }; export default ThemedText; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ import React from "react"; import { ThemeContext } from "./Theme"; const ToggleTheme: React.FC = () => { const { theme, setTheme } = React.useContext(ThemeContext); const toggle = () => { if (theme === "light") setTheme("dark"); else if (theme === "dark") setTheme("light"); }; return <button onClick={toggle}>Change themes</button>; }; export default ToggleTheme;