-
-
Notifications
You must be signed in to change notification settings - Fork 209
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 #6024 from zhyd1997/feat/ticket-2892-dark-mode-sup…
…port add dark mode support
- Loading branch information
Showing
25 changed files
with
304 additions
and
12 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 |
---|---|---|
|
@@ -4,4 +4,8 @@ | |
font-size: 0.9rem; | ||
font-style: italic; | ||
} | ||
|
||
&--label { | ||
color: var(--jhlite-global-color-text); | ||
} | ||
} |
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
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
2 changes: 2 additions & 0 deletions
2
src/main/webapp/app/common/primary/header/Header.component.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
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
42 changes: 42 additions & 0 deletions
42
src/main/webapp/app/common/primary/theme-button/ThemeButton.component.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,42 @@ | ||
import { ThemeRepository } from '@/module/domain/ThemeRepository'; | ||
import { ThemePreference as Theme } from '@/module/secondary/GetMediaPreference'; | ||
import { defineComponent, ref, onMounted, inject } from 'vue'; | ||
|
||
/** | ||
* ThemeSwitchButton | ||
* | ||
* 1. use localStorage saved theme first. | ||
* 2. if no value set, use `prefer-color-scheme` defined. | ||
* 3. can change theme by toggle button. | ||
* 4. the change must be saved in localStorage for later theme decision. | ||
*/ | ||
export default defineComponent({ | ||
name: 'ThemeButton', | ||
|
||
setup() { | ||
const isDarkTheme = ref<boolean>(true); | ||
|
||
const theme = ref<Theme>('dark-theme'); | ||
|
||
const themeRepository = inject('themeRepository') as ThemeRepository; | ||
|
||
onMounted(() => { | ||
const initUserTheme = themeRepository.get(); | ||
isDarkTheme.value = initUserTheme === 'dark-theme'; | ||
theme.value = initUserTheme; | ||
themeRepository.choose(initUserTheme); | ||
}); | ||
|
||
const toggleTheme = () => { | ||
isDarkTheme.value = !isDarkTheme.value; | ||
theme.value = isDarkTheme.value ? 'dark-theme' : 'light-theme'; | ||
themeRepository.choose(theme.value); | ||
}; | ||
|
||
return { | ||
isDarkTheme, | ||
toggleTheme, | ||
theme, | ||
}; | ||
}, | ||
}); |
65 changes: 65 additions & 0 deletions
65
src/main/webapp/app/common/primary/theme-button/ThemeButton.vue
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,65 @@ | ||
<template> | ||
<div class="jhlite-container"> | ||
<input | ||
id="switch" | ||
class="jhlite-container_toggle" | ||
type="checkbox" | ||
name="mode" | ||
:checked="!isDarkTheme" | ||
data-selector="theme-switch-button" | ||
@change="toggleTheme" | ||
/> | ||
<label for="switch" class="jhlite-theme-switch_label" :aria-label="theme"> | ||
<span>🌙</span> | ||
<span>☀️</span> | ||
<div class="jhlite-theme-switch_toggle" :class="{ 'jhlite-theme-switch_toggle-checked': theme === 'dark-theme' }"></div> | ||
</label> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" src="./ThemeButton.component.ts"></script> | ||
|
||
<style lang="scss"> | ||
.jhlite-container { | ||
display: flex; | ||
} | ||
.jhlite-container_toggle { | ||
height: 0; | ||
width: 0; | ||
visibility: hidden; | ||
} | ||
.jhlite-theme-switch_label { | ||
align-items: center; | ||
background: var(--jhlite-text-color-primary); | ||
border: calc(var(--jhlite-element-size) * 0.025) solid var(--jhlite-accent-color); | ||
border-radius: var(--jhlite-element-size); | ||
cursor: pointer; | ||
display: flex; | ||
font-size: calc(var(--jhlite-element-size) * 0.3); | ||
height: calc(var(--jhlite-element-size) * 0.35); | ||
position: relative; | ||
padding: calc(var(--jhlite-element-size) * 0.1); | ||
transition: background 0.5s ease; | ||
justify-content: space-between; | ||
width: var(--jhlite-element-size); | ||
z-index: 1; | ||
} | ||
.jhlite-theme-switch_toggle { | ||
position: absolute; | ||
background-color: var(--jhlite-bg-color-primary); | ||
border-radius: 50%; | ||
top: calc(var(--jhlite-element-size) * 0.07); | ||
left: calc(var(--jhlite-element-size) * 0.07); | ||
height: calc(var(--jhlite-element-size) * 0.4); | ||
width: calc(var(--jhlite-element-size) * 0.4); | ||
transform: translateX(0); | ||
transition: transform 0.3s ease, background-color 0.5s ease; | ||
} | ||
.jhlite-theme-switch_toggle-checked { | ||
transform: translateX(calc(var(--jhlite-element-size) * 0.6)) !important; | ||
} | ||
</style> |
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,4 @@ | ||
import ThemeButtonComponent from './ThemeButton.component'; | ||
import ThemeButtonVue from './ThemeButton.vue'; | ||
|
||
export { ThemeButtonComponent, ThemeButtonVue }; |
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 { ThemePreference as Theme } from '@/module/secondary/GetMediaPreference'; | ||
|
||
export interface ThemeRepository { | ||
get(): Theme; | ||
choose(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
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
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,8 @@ | ||
export type ThemePreference = 'light-theme' | 'dark-theme'; | ||
|
||
export const getMediaPreference = (win: Window): ThemePreference => { | ||
if (typeof win !== 'undefined' && win.matchMedia) { | ||
return win.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark-theme' : 'light-theme'; | ||
} | ||
return 'light-theme'; | ||
}; |
17 changes: 17 additions & 0 deletions
17
src/main/webapp/app/module/secondary/LocalWindowThemeRepository.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,17 @@ | ||
import { ThemeRepository } from '../domain/ThemeRepository'; | ||
import { ThemePreference as Theme, getMediaPreference } from './GetMediaPreference'; | ||
|
||
const THEME_STORAGE_KEY = 'theme'; | ||
|
||
export class LocalWindowThemeRepository implements ThemeRepository { | ||
constructor(private readonly win: Window, private readonly storage: Storage) {} | ||
|
||
get(): Theme { | ||
return (this.storage.getItem(THEME_STORAGE_KEY) as Theme) || getMediaPreference(this.win); | ||
} | ||
|
||
choose(theme: Theme): void { | ||
this.storage.setItem(THEME_STORAGE_KEY, theme); | ||
this.win.document.documentElement.className = theme; | ||
} | ||
} |
Oops, something went wrong.