Skip to content

Commit

Permalink
Prettier light mode button
Browse files Browse the repository at this point in the history
  • Loading branch information
githubsaturn committed Oct 12, 2024
1 parent 665347f commit ae940de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
19 changes: 17 additions & 2 deletions src/containers/PageRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FileTextOutlined,
GiftTwoTone,
GithubOutlined,
GlobalOutlined,
LaptopOutlined,
LogoutOutlined,
SettingOutlined,
Expand Down Expand Up @@ -49,6 +50,7 @@ class PageRoot extends ApiComponent<
{
versionInfo: IVersionInfo | undefined
collapsed: boolean
showLanguageSelector: boolean
}
> {
private mainContainer: RefObject<HTMLDivElement>
Expand All @@ -59,6 +61,7 @@ class PageRoot extends ApiComponent<
this.state = {
versionInfo: undefined,
collapsed: false,
showLanguageSelector: false,
}
}

Expand Down Expand Up @@ -248,7 +251,7 @@ class PageRoot extends ApiComponent<
</span>
<span
style={{
marginInlineEnd: 50,
marginInlineEnd: 20,
}}
>
<DarkModeSwitch />
Expand All @@ -258,7 +261,7 @@ class PageRoot extends ApiComponent<
marginInlineEnd: 50,
}}
>
<LanguageSelector />
{self.createLanguageSelector()}
</span>
<span>
<Button
Expand Down Expand Up @@ -444,6 +447,18 @@ class PageRoot extends ApiComponent<
</Layout>
)
}
createLanguageSelector(): React.ReactNode {
const self = this
return self.state.showLanguageSelector ? (
<LanguageSelector />
) : (
<Button
onClick={() => self.setState({ showLanguageSelector: true })}
shape="circle"
icon={<GlobalOutlined />}
/>
)
}
}

function mapStateToProps(state: any) {
Expand Down
15 changes: 7 additions & 8 deletions src/containers/global/DarkModeSwitch.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { BulbFilled, BulbOutlined } from '@ant-design/icons'
import { Switch } from 'antd'
import { SunFilled, SunOutlined } from '@ant-design/icons'
import { Button } from 'antd'
import { useContext } from 'react'
import DarkModeContext from '../../contexts/DarkModeContext'

const DarkModeSwitch = () => {
const { isDarkMode, setIsDarkMode } = useContext(DarkModeContext)

return (
<Switch
checkedChildren={<BulbOutlined />}
unCheckedChildren={<BulbFilled />}
checked={isDarkMode}
onChange={(checked) => {
setIsDarkMode(checked)
<Button
onClick={() => {
setIsDarkMode(!isDarkMode)
}}
shape="circle"
icon={isDarkMode ? <SunOutlined /> : <SunFilled />}
/>
)
}
Expand Down
7 changes: 5 additions & 2 deletions src/utils/StorageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ class StorageHelper {
// If not preference exists, return DarkMode based on users colorScheme
return isDarkMode
? JSON.parse(isDarkMode)
: window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
: // default to dark mode
!(
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: light)').matches
)
}

setLanguageInLocalStorage(language: string) {
Expand Down

0 comments on commit ae940de

Please sign in to comment.