Skip to content

Commit

Permalink
add dark mode toggle to account dropdown menu
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielburnworth committed Dec 11, 2024
1 parent ff2be12 commit 66c9a45
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions frontend/css/app/navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ nav {
font-size: 1.2rem;
}
}
.dark-mode-toggle {
padding: 0.5rem 0 0 1rem;
}
}
.bp5-popover-arrow {
visibility: hidden;
Expand Down
17 changes: 17 additions & 0 deletions frontend/nav/__tests__/additional_menu_test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
jest.mock("../../config_storage/actions", () => ({
setWebAppConfigValue: jest.fn()
}));

import React from "react";
import { mount, shallow } from "enzyme";
import { AdditionalMenu } from "../additional_menu";
import { AccountMenuProps } from "../interfaces";
import { fireEvent, render, screen } from "@testing-library/react";
import { setWebAppConfigValue } from "../../config_storage/actions";
import { BooleanSetting } from "../../session_keys";

describe("AdditionalMenu", () => {
const fakeProps = (): AccountMenuProps => ({
isStaff: false,
close: jest.fn(),
dispatch: jest.fn(),
darkMode: false,
});

it("renders the account menu", () => {
Expand Down Expand Up @@ -40,4 +49,12 @@ describe("AdditionalMenu", () => {
const wrapper = shallow(<AdditionalMenu {...fakeProps()} />);
wrapper.find("Link").at(2).simulate("click");
});

it("toggles dark mode", () => {
render(<AdditionalMenu {...fakeProps()} />);
const toggle = screen.getByText("🌣");
fireEvent.click(toggle);
expect(setWebAppConfigValue).toHaveBeenCalledWith(
BooleanSetting.dark_mode, true);
});
});
14 changes: 14 additions & 0 deletions frontend/nav/additional_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { t } from "../i18next_wrapper";
import { ExternalUrl } from "../external_urls";
import { FilePath, Icon, Path } from "../internal_urls";
import { logout } from "../logout";
import { setWebAppConfigValue } from "../config_storage/actions";
import { BooleanSetting } from "../session_keys";
import { ToggleButton } from "../ui";

export const AdditionalMenu = (props: AccountMenuProps) => {
return <div className="nav-additional-menu">
Expand Down Expand Up @@ -40,6 +43,17 @@ export const AdditionalMenu = (props: AccountMenuProps) => {
{t("Logout and destroy token")}
</a>
</div>}
<div className={"dark-mode-toggle"}>
<label>{t("Dark Mode")}</label>
<ToggleButton
className={"no-float"}
toggleValue={props.darkMode}
toggleAction={() => {
props.dispatch(setWebAppConfigValue(
BooleanSetting.dark_mode, !props.darkMode));
}}
customText={{ textFalse: "🌣", textTrue: "☽" }} />
</div>
<div className="app-version">
<label>{t("APP VERSION")}</label>:&nbsp;
<a href={ExternalUrl.webAppRepo} target="_blank" rel={"noreferrer"}>
Expand Down
6 changes: 5 additions & 1 deletion frontend/nav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ export class NavBar extends React.Component<NavBarProps, Partial<NavBarState>> {
onClick={this.toggle("accountMenuOpen")}>
{firstName}
</div>}
content={<AdditionalMenu close={this.close} isStaff={this.isStaff} />} />
content={<AdditionalMenu
close={this.close}
dispatch={this.props.dispatch}
darkMode={!!this.props.getConfigValue(BooleanSetting.dark_mode)}
isStaff={this.isStaff} />} />
</div>;
};

Expand Down
2 changes: 2 additions & 0 deletions frontend/nav/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export interface NavLinksProps {
export interface AccountMenuProps {
isStaff: boolean;
close: (property: keyof NavBarState) => ToggleEventHandler;
dispatch: Function;
darkMode: boolean;
}

export interface EStopButtonProps {
Expand Down

0 comments on commit 66c9a45

Please sign in to comment.