feat: nav-menu for mobile view

This commit is contained in:
saicaca 2023-11-06 21:55:52 +08:00
parent ab89683cd7
commit b64cf8eee3
3 changed files with 81 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import I18nKey from "../i18n/i18nKey";
import {i18n} from "../i18n/translation"; import {i18n} from "../i18n/translation";
import {LinkPreset, NavBarLink} from "../types/config"; import {LinkPreset, NavBarLink} from "../types/config";
import {navBarConfig, siteConfig} from "../config"; import {navBarConfig, siteConfig} from "../config";
import NavMenuPanel from "./widget/NavMenuPanel.astro";
const className = Astro.props.class; const className = Astro.props.class;
function isI18nKey(key: string): key is I18nKey { function isI18nKey(key: string): key is I18nKey {
@ -49,7 +50,7 @@ function getLinkPresetInfo(p: LinkPreset): NavBarLink {
{siteConfig.title} {siteConfig.title}
</div> </div>
</Button></a> </Button></a>
<div> <div class="hidden md:block">
{links.map((l) => { {links.map((l) => {
return <a aria-label={l.name} href={l.url} target={l.external ? "_blank" : null}> return <a aria-label={l.name} href={l.url} target={l.external ? "_blank" : null}>
<Button light class="font-bold px-5 rounded-lg active:scale-95"> <Button light class="font-bold px-5 rounded-lg active:scale-95">
@ -71,8 +72,13 @@ function getLinkPresetInfo(p: LinkPreset): NavBarLink {
<Icon name="material-symbols:dark-mode-outline-rounded" size={20} class="absolute opacity-[var(--display-dark-icon)]"></Icon> <Icon name="material-symbols:dark-mode-outline-rounded" size={20} class="absolute opacity-[var(--display-dark-icon)]"></Icon>
</Button> </Button>
</div> </div>
<div>
<Button name="Nav Menu" class="rounded-lg active:scale-90 block md:hidden" id="nav-menu-switch" iconName="material-symbols:menu-rounded" iconSize={20} isIcon light></Button>
</div>
</div> </div>
<DisplaySetting></DisplaySetting> <DisplaySetting></DisplaySetting>
<NavMenuPanel links={links}></NavMenuPanel>
</div> </div>
@ -91,7 +97,7 @@ function switchTheme() {
} }
} }
function loadThemeSwitchScript() { function loadButtonScript() {
let switchBtn = document.getElementById("scheme-switch"); let switchBtn = document.getElementById("scheme-switch");
switchBtn.addEventListener("click", function () { switchBtn.addEventListener("click", function () {
switchTheme() switchTheme()
@ -102,12 +108,18 @@ function loadThemeSwitchScript() {
let settingPanel = document.getElementById("display-setting"); let settingPanel = document.getElementById("display-setting");
settingPanel.classList.toggle("closed"); settingPanel.classList.toggle("closed");
}); });
let menuBtn = document.getElementById("nav-menu-switch");
menuBtn.addEventListener("click", function () {
let menuPanel = document.getElementById("nav-menu-panel");
menuPanel.classList.toggle("closed");
});
} }
loadThemeSwitchScript(); loadButtonScript();
document.addEventListener('astro:after-swap', () => { document.addEventListener('astro:after-swap', () => {
loadThemeSwitchScript(); loadButtonScript();
}, { once: false }); }, { once: false });

View File

@ -0,0 +1,52 @@
---
import {NavBarLink} from "../../types/config";
import {siteConfig} from "../../config";
import {Icon} from "astro-icon/components";
interface Props {
links: NavBarLink[],
}
const links = Astro.props.links;
const enableBanner = siteConfig.banner.enable;
---
<div id="nav-menu-panel" class:list={["card-base closed absolute transition-all fixed right-4 border-[var(--primary)] px-2 py-2",
{"border-[3px]": !enableBanner}
]}>
{links.map((link) => (
<a href={link.url} class="group flex justify-between items-center py-2 pl-3 pr-1 rounded-md gap-8
hover:bg-[var(--btn-plain-bg-hover)] active:bg-[var(--btn-plain-bg-active)] transition
"
target={link.external ? "_blank" : null}
>
<div class="transition text-black/75 dark:text-white/75 font-bold group-hover:text-[var(--primary)] group-active:text-[var(--primary)]">
{link.name}
</div>
{!link.external && <Icon name="material-symbols:chevron-right-rounded"
class="transition text-[var(--primary)]" size="20"
>
</Icon>}
{link.external && <Icon name="fa6-solid:arrow-up-right-from-square"
class="transition text-black/25 dark:text-white/25 -translate-x-1" size="12"
>
</Icon>}
</a>
))}
</div>
<style>
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
#nav-menu-panel {
@apply top-[84px]
}
#nav-menu-panel.closed {
@apply top-[76px] opacity-0 pointer-events-none
}
}
</style>

View File

@ -198,7 +198,6 @@ function disableAnimation() {
} }
} }
function activateDisplaySettings() { function activateDisplaySettings() {
let output = document.getElementById("hueValue"); let output = document.getElementById("hueValue");
let slider = document.getElementById("colorSlider"); let slider = document.getElementById("colorSlider");
@ -227,9 +226,10 @@ function activateDisplaySettings() {
setHue(hue); setHue(hue);
} }
function setClickOutsideToClose(panel, switchBtn) {
document.addEventListener("click", event => { document.addEventListener("click", event => {
var cDom = document.getElementById("display-setting"); var cDom = document.getElementById(panel);
let settingBtn = document.getElementById("display-settings-switch"); let settingBtn = document.getElementById(switchBtn);
var tDom = event.target; var tDom = event.target;
if (cDom == tDom || cDom.contains(tDom) || settingBtn == tDom || settingBtn.contains(tDom)) { if (cDom == tDom || cDom.contains(tDom) || settingBtn == tDom || settingBtn.contains(tDom)) {
return; return;
@ -237,6 +237,9 @@ function activateDisplaySettings() {
cDom.classList.add("closed"); cDom.classList.add("closed");
}); });
} }
setClickOutsideToClose("display-setting", "display-settings-switch")
setClickOutsideToClose("nav-menu-panel", "nav-menu-switch")
}
function loadTheme() { function loadTheme() {
if (localStorage.theme === 'dark' || (!('theme' in localStorage) && if (localStorage.theme === 'dark' || (!('theme' in localStorage) &&