forked from aws-samples/aws-sdk-js-notes-app
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: lukqw <[email protected]>
- Loading branch information
Showing
6 changed files
with
114 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
import React from "react"; | ||
import { Card } from "react-bootstrap"; | ||
import { ThemeSwitch } from "../content/ThemeSwitch"; | ||
|
||
const PageContainer = (props: { | ||
header: React.ReactNode; | ||
children: React.ReactNode; | ||
}) => ( | ||
<Card> | ||
<Card.Header>{props.header}</Card.Header> | ||
<Card.Body>{props.children}</Card.Body> | ||
</Card> | ||
); | ||
}) => { | ||
return ( | ||
<> | ||
<Card> | ||
<Card.Header>{props.header}</Card.Header> | ||
<Card.Body>{props.children}</Card.Body> | ||
</Card> | ||
<ThemeSwitch /> | ||
</> | ||
) | ||
}; | ||
|
||
export { PageContainer }; |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, { useState } from "react"; | ||
import { useThemeContext } from "../hooks/useThemeContext"; | ||
import { Form } from "react-bootstrap"; | ||
|
||
|
||
export const ThemeSwitch = () => { | ||
const { darkMode, setDarkMode } = useThemeContext(); | ||
|
||
const toggleTheme = () => { | ||
setDarkMode(!darkMode); | ||
}; | ||
|
||
return ( | ||
<Form style={{paddingTop: '1rem', display: 'flex', justifyContent: 'flex-end'}}> | ||
<Form.Check // prettier-ignore | ||
type="switch" | ||
onClick={toggleTheme} | ||
id="custom-switch" | ||
defaultChecked={darkMode} | ||
style={{color: darkMode ? 'white' : ''}} | ||
label="Dark Mode" | ||
/> | ||
</Form> | ||
); | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
// to store the state to local storage | ||
export default function usePersistedState(key: string, defaultValue: any) { | ||
const [state, setState] = useState(() => { | ||
return JSON.parse(localStorage.getItem(key) ?? '{}') || defaultValue; | ||
}); | ||
|
||
useEffect(() => { | ||
localStorage.setItem(key, JSON.stringify(state)); | ||
}, [key, state]); | ||
|
||
return [state, setState]; | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from "react"; | ||
import { createContext, useContext, ReactElement } from "react"; | ||
import usePersistedState from "./usePersistedState"; | ||
|
||
type ThemeContextValue = { | ||
darkMode: boolean; | ||
setDarkMode: React.Dispatch<React.SetStateAction<boolean>>; | ||
}; | ||
|
||
// register the context | ||
const ThemeContext = createContext<ThemeContextValue>(undefined as any); | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export const ThemeProvider = ({ children }: Props): ReactElement => { | ||
/** usePersistedState for storing state in local store */ | ||
const [darkMode, setDarkMode] = usePersistedState("darkmode", false); | ||
|
||
return ( | ||
<ThemeContext.Provider value={{ darkMode, setDarkMode }}> | ||
{children} | ||
</ThemeContext.Provider> | ||
); | ||
} | ||
|
||
// export a custom hook to use this specific context | ||
export const useThemeContext = (): ThemeContextValue => { | ||
return useContext(ThemeContext); | ||
} | ||
|
This file contains 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 characters