-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from jonyw4/feat/add-theme-switch
Feat/add theme switch
- Loading branch information
Showing
25 changed files
with
409 additions
and
27 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,5 +1,12 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
transform: { | ||
".(ts|tsx)": "ts-jest" | ||
}, | ||
globals: { | ||
"ts-jest": { | ||
compiler: "ttypescript", | ||
}, | ||
}, | ||
setupFiles: ["<rootDir>tests/config.ts"], | ||
}; |
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
33 changes: 33 additions & 0 deletions
33
webapp/src/components/atoms/ThemeRadio/ThemeRadio.component.tsx
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,33 @@ | ||
import { Theme } from "../../../domain"; | ||
import { useTheme } from "../../global"; | ||
|
||
export function ThemeRadio() { | ||
const {theme, changeTheme} = useTheme(); | ||
|
||
const isLight = theme === "light"; | ||
const isDark = theme === "dark"; | ||
|
||
return ( | ||
<div style={{ display: "flex" }}> | ||
<label htmlFor="theme-light-radio">☀️ Light Theme</label> | ||
<input | ||
id="theme-light-radio" | ||
type="radio" | ||
name="theme" | ||
value="light" | ||
checked={isLight} | ||
onClick={() => changeTheme("light")} | ||
/> | ||
|
||
<label htmlFor="theme-dark-radio">🌒 Dark Theme</label> | ||
<input | ||
id="theme-dark-radio" | ||
type="radio" | ||
name="theme" | ||
value="dark" | ||
checked={isDark} | ||
onClick={() => changeTheme("dark")} | ||
/> | ||
</div> | ||
); | ||
} |
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,30 @@ | ||
import React from "react"; | ||
import { Theme } from "../../domain"; | ||
import { Locale, LocaleContext } from "./locale"; | ||
import { ThemeDataContext } from "./theme"; | ||
|
||
export interface UIProviderProps { | ||
locale: Locale; | ||
children: React.ReactNode; | ||
themeData: { | ||
initialTheme: Theme; | ||
callback: (theme: Theme) => void; | ||
}; | ||
} | ||
|
||
export function UIProvider({ locale, children, themeData }: UIProviderProps) { | ||
const [theme, setTheme] = React.useState<Theme>(themeData.initialTheme); | ||
|
||
const changeTheme = (newTheme: Theme) => { | ||
themeData.callback(newTheme); | ||
setTheme(newTheme); | ||
} | ||
|
||
return ( | ||
<LocaleContext.Provider value={locale}> | ||
<ThemeDataContext.Provider value={{ theme, changeTheme }}> | ||
{children} | ||
</ThemeDataContext.Provider> | ||
</LocaleContext.Provider> | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
webapp/src/components/global/adapters/adaptThemeRepositoryToThemeData.ts
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,11 @@ | ||
import { UIProviderProps } from ".."; | ||
import { ThemeRepository } from "../../../data"; | ||
|
||
export const adaptThemeRepositoryToThemeData = ( | ||
themeRepository: ThemeRepository | ||
): UIProviderProps['themeData'] => { | ||
return { | ||
initialTheme: themeRepository.getTheme(), | ||
callback: (theme) => themeRepository.changeTheme(theme), | ||
}; | ||
}; |
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 @@ | ||
export * from './adaptThemeRepositoryToThemeData'; |
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,2 +1,4 @@ | ||
export * from './UIProvider'; | ||
export * from './formatDate'; | ||
export * from './locale'; | ||
export * from './theme'; |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from "react"; | ||
import { Theme } from "../../domain"; | ||
|
||
|
||
export interface ThemeData { | ||
theme: Theme; | ||
changeTheme: (theme: Theme) => void; | ||
} | ||
|
||
// @ts-ignore | ||
export const ThemeDataContext = React.createContext<ThemeData>(); | ||
export const useTheme = () => React.useContext(ThemeDataContext); |
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,13 +1,14 @@ | ||
export function Footer(){ | ||
import { LanguageSelect } from "../../atoms/LanguageSelect/LanguageSelect.component"; | ||
export function Footer() { | ||
return ( | ||
<footer> | ||
<LanguageSelect /> | ||
<small> | ||
<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/"> | ||
CC BY-NC-SA 4.0 2021 | ||
</a> | ||
{" "} | ||
</a>{" "} | ||
© Jonathan Celio | ||
</small> | ||
</footer> | ||
); | ||
} | ||
} |
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,11 +1,11 @@ | ||
import { ProfileCard } from '../ProfileCard' | ||
import { LanguageSelect } from '../../atoms/LanguageSelect/LanguageSelect.component'; | ||
import { ThemeRadio } from "../../atoms/ThemeRadio/ThemeRadio.component"; | ||
|
||
export function Header(){ | ||
return ( | ||
<header> | ||
<ProfileCard /> | ||
<LanguageSelect /> | ||
<ThemeRadio /> | ||
</header> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Theme } from "../domain/Theme"; | ||
|
||
export interface ThemeRepository { | ||
getTheme(): Theme; | ||
changeTheme(theme: Theme): void; | ||
} |
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 +1,2 @@ | ||
export * from './ArticleRepository'; | ||
export * from './ThemeRepository'; |
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 @@ | ||
export type Theme = 'dark' | 'light'; |
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,2 +1,3 @@ | ||
export * from './Article'; | ||
export * from './ArticleMetadata'; | ||
export * from './Theme'; |
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,24 @@ | ||
import { ThemeRepository } from "../data"; | ||
import { Theme } from "../domain"; | ||
|
||
const LOCAL_STORAGE_THEME_KEY = "theme"; | ||
const DEFAULT_THEME = "light"; | ||
|
||
export class ThemeLocalStorageRepository implements ThemeRepository { | ||
getTheme(): Theme { | ||
if(!global.localStorage){ | ||
return DEFAULT_THEME; | ||
} | ||
|
||
const theme = global.localStorage.getItem(LOCAL_STORAGE_THEME_KEY) as Theme; | ||
|
||
if(!theme){ | ||
return DEFAULT_THEME; | ||
} | ||
|
||
return theme; | ||
} | ||
changeTheme(theme: Theme): void { | ||
global.localStorage.setItem(LOCAL_STORAGE_THEME_KEY, theme); | ||
} | ||
} |
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 +1,2 @@ | ||
export * from './ArticleFileSystemRepository'; | ||
export * from './ThemeLocalStorageRepository'; |
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
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 @@ | ||
import "jest-ts-auto-mock"; |
Oops, something went wrong.
179725a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: