Skip to content

Commit

Permalink
feat: Dark mode improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-meier committed Jul 5, 2024
1 parent 0ffd704 commit afd82a8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const MainMenu = () => {
const isActiveItem = (item: string) => {
return item.toLowerCase() === activeItem.toLowerCase();
};

const renderLinks = () => {
const isLoggedIn = userInfo?.isAuthenticated ?? false;
return MenuItems.filter(
Expand All @@ -58,13 +58,13 @@ const MainMenu = () => {
(x.displayState === "loggedIn" && isLoggedIn) ||
(x.displayState === "loggedOut" && !isLoggedIn)
).map((menuItem, index) => {
const {name, disableNavigation, position, element} = menuItem;
const {name, disableClickHandler, position, element, clickHandler} = menuItem;
return (
<Menu.Item
key={index}
name={name}
active={isActiveItem(name)}
onClick={!disableNavigation ? handleItemClick : undefined}
onClick={!disableClickHandler ? clickHandler || handleItemClick : undefined}
position={position}
>
{element}
Expand Down
37 changes: 24 additions & 13 deletions src/Chrono/ClientApp/src/Shared/Components/MainMenu/MenuItems.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Image} from "semantic-ui-react";
import {Icon, Image} from "semantic-ui-react";
import {ThemeService} from "../../ThemeService.ts";

type MenuItemDefinition = {
name: string;
element?: JSX.Element;
disableNavigation?: boolean;
clickHandler?: () => void;
disableClickHandler?: boolean;
position?: "left" | "right";
displayState: "always" | "loggedIn" | "loggedOut";
};
Expand All @@ -13,16 +14,14 @@ const MenuItems: MenuItemDefinition[] = [
{
name: "logo",
element: (
<a onClick={() => ThemeService.toggle()}>
<Image
size="tiny"
src="/logo.png"
style={{margin: "0 auto"}}
alt=""
/>
</a>
<Image
size="tiny"
src="/logo.png"
style={{margin: "0 auto"}}
alt=""
/>
),
disableNavigation: true,
disableClickHandler: true,
displayState: "always",
},
{
Expand All @@ -43,14 +42,26 @@ const MenuItems: MenuItemDefinition[] = [
},
{
name: "logout",
position: "right",
displayState: "loggedIn",
},
{
name: "login",
position: "right",
displayState: "loggedOut",
},
{
name: "modeToggle",
displayState: "always",
position: "right",
element: (
<Icon id="modeIcon" name={ThemeService.isDarkModeEnabled() ? 'sun' : 'moon'} />
),
clickHandler: () => {
const modeIcon = document.getElementById('modeIcon')!;
modeIcon.classList.toggle("sun");
modeIcon.classList.toggle("moon");
ThemeService.toggle()
}
},
];

export default MenuItems;
11 changes: 9 additions & 2 deletions src/Chrono/ClientApp/src/Shared/ThemeService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
enable,
disable,
setFetchMethod,
} from 'darkreader';

const theme = {
Expand All @@ -11,7 +12,9 @@ const theme = {

export class ThemeService {
static init() {
if (localStorage.getItem('isDarkMode') === 'true')
setFetchMethod(window.fetch);

if (this.isDarkModeEnabled())
enable(theme);
else
disable()
Expand All @@ -28,9 +31,13 @@ export class ThemeService {
}

static toggle() {
if (localStorage.getItem('isDarkMode') === 'true')
if (this.isDarkModeEnabled())
this.disableDarkMode();
else
this.enableDarkMode();
}

static isDarkModeEnabled() {
return localStorage.getItem('isDarkMode') === 'true';
}
}
4 changes: 4 additions & 0 deletions src/Chrono/ClientApp/src/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ code {
div.masterData.tabs > div.attached.tab {
border-bottom: none;
}

#modeIcon {
font-size: 1em !important;
}

0 comments on commit afd82a8

Please sign in to comment.