Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: close nav menu when theme is toggled #745

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/components/ToggleSwitch/ToggleSwitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ import emoji from "react-easy-emoji";
import StyleContext from "../../contexts/StyleContext";
import "./ToggleSwitch.scss";

const ToggleSwitch = () => {
const ToggleSwitch = ({closeMenu}) => {
const {isDark} = useContext(StyleContext);
const [isChecked, setChecked] = useState(isDark);
const styleContext = useContext(StyleContext);

const handleToggle = () => {
styleContext.changeTheme();
setChecked(!isChecked);
closeMenu(); // Close the menu when the theme is toggled
};

return (
<label className="switch">
<input
type="checkbox"
checked={isDark}
onChange={() => {
styleContext.changeTheme();
setChecked(!isChecked);
}}
/>
<input type="checkbox" checked={isDark} onChange={handleToggle} />
<span className="slider round">
<span className="emoji">{isChecked ? emoji("🌜") : emoji("☀️")}</span>
</span>
Expand Down
20 changes: 17 additions & 3 deletions src/components/header/Header.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useContext} from "react";
import React, {useContext, useRef} from "react";
import Headroom from "react-headroom";
import "./Header.scss";
import ToggleSwitch from "../ToggleSwitch/ToggleSwitch";
Expand All @@ -24,6 +24,15 @@ function Header() {
const viewTalks = talkSection.display;
const viewResume = resumeSection.display;

const menuCheckboxRef = useRef(null);

//close the menu when theme is changes from mobile nav
const closeMenu = () => {
if (menuCheckboxRef.current) {
menuCheckboxRef.current.checked = false;
}
};

return (
<Headroom>
<header className={isDark ? "dark-menu header" : "header"}>
Expand All @@ -32,7 +41,12 @@ function Header() {
<span className="logo-name">{greeting.username}</span>
<span className="grey-color">/&gt;</span>
</a>
<input className="menu-btn" type="checkbox" id="menu-btn" />
<input
className="menu-btn"
type="checkbox"
id="menu-btn"
ref={menuCheckboxRef}
/>
<label
className="menu-icon"
htmlFor="menu-btn"
Expand Down Expand Up @@ -82,7 +96,7 @@ function Header() {
<li>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<a>
<ToggleSwitch />
<ToggleSwitch closeMenu={closeMenu} />
</a>
</li>
</ul>
Expand Down