Skip to content

Commit

Permalink
Use hashrouter for navigation.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTV12345 committed Sep 20, 2023
1 parent 7c46ea9 commit 3706c87
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 48 deletions.
5 changes: 0 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ export const RootElement = ()=>{
</div>
}






export const App = ()=> {

return (
Expand Down
6 changes: 4 additions & 2 deletions src/components/MobileDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {faInfoCircle, faDownload, faWrench, faExternalLink, faContactCard} from
import {FC} from "react";
import {ThemeToggler} from "./ThemeToggler.tsx";
import {useUIStore} from "../store/store.ts";
import {useNavigate} from "react-router-dom";

type MobileDrawerProps = {
isOpen: boolean,
Expand All @@ -11,9 +12,10 @@ type MobileDrawerProps = {

export const MobileDrawer:FC<MobileDrawerProps> = ({isOpen, setOpen}) => {
const toggleTheme = useUIStore(state=>state.toggleDarkMode)
const navigate = useNavigate()
const navigateToElement = (elementId: string)=>{
location.hash = elementId
setOpen(false)
document.getElementById(elementId)?.scrollIntoView({block: "start", inline: "nearest"})
navigate('#'+elementId)
}

return <div className={` ${!isOpen?'h-0 overflow-hidden':' h-[100vh]'} w-full bg-transparent md:hidden z-10 absolute pointer-events-none`}
Expand Down
6 changes: 1 addition & 5 deletions src/components/ThemeToggler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import {faMoon, faSun} from "@fortawesome/free-solid-svg-icons";
import {useUIStore} from "../store/store.ts";
import {useEffect} from "react";
import {handleManualThemeChange} from "../utils/ThemeChanger.ts";

export const handleManualThemeChange = ()=>{
const newState = !useUIStore.getState().isDarkMode
useUIStore.getState().toggleDarkMode()
localStorage.setItem('isDarkMode', newState.toString())
}

export const ThemeToggler = ()=>{
const darkMode = useUIStore(state=>state.isDarkMode)
Expand Down
4 changes: 2 additions & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from 'react'
import ReactDOM from 'react-dom/client'
import {App, RootElement} from './App.tsx'
import './index.css'
import {createBrowserRouter, RouterProvider} from "react-router-dom";
import {createHashRouter, RouterProvider} from "react-router-dom";
import {createRoutesFromElements, Route} from "react-router";
import {PluginViewer} from "./pages/PluginViewer.tsx";


const router = createBrowserRouter(createRoutesFromElements(
const router = createHashRouter(createRoutesFromElements(
<Route element={<RootElement/>}>
<Route index element={<App/>}/>
<Route path="/plugins" element={<PluginViewer/>}/>
Expand Down
44 changes: 10 additions & 34 deletions src/pages/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,19 @@
import brandSvg from '../assets/img/brand.svg';
import {Suspense, useCallback, useEffect, useState} from "react";
import {Suspense, useState} from "react";
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import {faBars} from "@fortawesome/free-solid-svg-icons";
import {MobileDrawer} from "../components/MobileDrawer.tsx";
import {ThemeToggler} from "../components/ThemeToggler.tsx";
import {useNavigate} from "react-router-dom";
export const Header = () => {
const [hash, setHash] = useState<string>(window.location.hash)
const [mobileDrawerOpen, setMobileDrawerOpen] = useState<boolean>(false)

const hashChangeHandler = useCallback(() => {
setHash(window.location.hash);
}, []);
const navigate = useNavigate()

const navigateToElement = (elementId: string)=>{
location.hash = elementId
document.getElementById(elementId)?.scrollIntoView({block: "start", inline: "nearest"})
navigate('#'+elementId)
}

useEffect(() => {
window.addEventListener('hashchange', hashChangeHandler);
return () => {
window.removeEventListener('hashchange', hashChangeHandler);
};
}, []);

useEffect(()=> {
if(location.hash && location.hash.length>0){
const elements = document.getElementsByClassName("active")
for (let i = 0; i < elements.length; i++) {
elements[i].classList.remove("active")
}
document.querySelector("[title='"+hash.slice(1)+"']")?.classList.add("active")
const element = document.getElementById(hash.slice(1))
if(element){
element.scrollIntoView({block: "start", inline: "nearest"})
}
}
}
,[hash])




Expand All @@ -51,11 +27,11 @@ export const Header = () => {

<div id="nav">
<ul>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement('#about')} title="about">About</a></li>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement('#download')} title="download">Download</a></li>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement('#contribute')} title="contribute">Contribute</a></li>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement("#links")} title="links">Links</a></li>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement('#contact')} title="contact">Contact</a></li>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement('about')} title="about">About</a></li>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement('download')} title="download">Download</a></li>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement('contribute')} title="contribute">Contribute</a></li>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement("links")} title="links">Links</a></li>
<li><a className="text-[#555] dark:text-white" onClick={()=>navigateToElement('contact')} title="contact">Contact</a></li>
<li className="dark:bg-secondary-dark"><ThemeToggler/></li>
</ul>
</div>
Expand Down
7 changes: 7 additions & 0 deletions src/utils/ThemeChanger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {useUIStore} from "../store/store.ts";

export const handleManualThemeChange = ()=>{
const newState = !useUIStore.getState().isDarkMode
useUIStore.getState().toggleDarkMode()
localStorage.setItem('isDarkMode', newState.toString())
}

0 comments on commit 3706c87

Please sign in to comment.