Skip to content

Commit

Permalink
refactor: header components
Browse files Browse the repository at this point in the history
  • Loading branch information
rxyhn committed Sep 16, 2024
1 parent 61bd031 commit 1a85452
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 152 deletions.
48 changes: 0 additions & 48 deletions public/js/main.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/BaseHead.astro
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,5 @@ const { title, description } = Astro.props;
<meta name="title" content={title} />
<meta name="description" content={description} />

<!-- Global Scripts -->
<script is:inline src="/js/main.js"></script>

<!-- View Transitions -->
<ViewTransitions />
55 changes: 55 additions & 0 deletions src/components/DrawerToggle.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
import { Icon } from "astro-icon/components";
import { cn } from "@/lib/utils";
---

<button
id="drawer_toggle"
aria-label={`Toggle drawer open and closed`}
class={cn(
"flex md:hidden",
"size-9 rounded-full p-2 items-center justify-center",
"bg-transparent hover:bg-black/20 dark:hover:bg-white/20",
"stroke-current hover:stroke-black hover:dark:stroke-white",
"border border-black/25 dark:border-white/25",
"transition-colors duration-300 ease-in-out"
)}
>
<Icon id="drawer-open" name="menu" class="size-full" />
<Icon id="drawer-close" name="close" class="size-full" />
</button>

<style>
#drawer_toggle > #drawer-open {
@apply block;
}

#drawer_toggle > #drawer-close {
@apply hidden;
}

#drawer_toggle.open > #drawer-open {
@apply hidden;
}

#drawer_toggle.open > #drawer-close {
@apply block;
}
</style>

<script is:inline>
function toggleDrawer() {
const drawer = document.getElementById("drawer");
const drawerButton = document.getElementById("drawer_toggle");
drawer?.classList.toggle("open");
drawerButton?.classList.toggle("open");
}

function initializeDrawerButton() {
const drawerButton = document.getElementById("drawer_toggle");
drawerButton?.addEventListener("click", toggleDrawer);
}

document.addEventListener("astro:after-swap", initializeDrawerButton);
initializeDrawerButton();
</script>
100 changes: 6 additions & 94 deletions src/components/Header.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
import Container from "@/components/Container.astro";
import ThemeToggle from "./ThemeToggle.astro";
import DrawerToggle from "./DrawerToggle.astro";
import { Icon } from "astro-icon/components";
import { SITE, LINKS } from "@/consts";
import { cn } from "@/lib/utils";
Expand Down Expand Up @@ -33,7 +35,7 @@ const subpath = pathname.match(/[^/]+/g);
"transition-colors duration-300 ease-in-out",
pathname === LINK.HREF || "/" + subpath?.[0] === LINK.HREF
? "bg-black text-white dark:bg-white dark:text-black"
: "hover:bg-black/20 hover:text-black dark:hover:bg-white/20 dark:hover:text-white",
: "hover:bg-black/20 hover:text-black dark:hover:bg-white/20 dark:hover:text-white"
)}
>
{LINK.TEXT}
Expand All @@ -52,7 +54,7 @@ const subpath = pathname.match(/[^/]+/g);
"bg-transparent hover:bg-black/20 dark:hover:bg-white/20",
"stroke-current hover:stroke-black hover:dark:stroke-white",
"border border-black/25 dark:border-white/25",
"transition-colors duration-300 ease-in-out",
"transition-colors duration-300 ease-in-out"
)}
>
<a
Expand All @@ -64,103 +66,13 @@ const subpath = pathname.match(/[^/]+/g);
<Icon name="github" class="block size-full" />
</a>
</button>

<button
id="header-theme-button"
aria-label={`Toggle light and dark theme`}
class={cn(
"flex",
"size-9 rounded-full p-2 items-center justify-center",
"bg-transparent hover:bg-black/20 dark:hover:bg-white/20",
"stroke-current hover:stroke-black hover:dark:stroke-white",
"border border-black/25 dark:border-white/25",
"transition-colors duration-300 ease-in-out",
)}
>
<Icon name="sun" class="block size-full dark:hidden" />
<Icon name="moon" class="hidden size-full dark:block" />
</button>

<button
id="header-drawer-button"
aria-label={`Toggle drawer open and closed`}
class={cn(
"flex md:hidden",
"size-9 rounded-full p-2 items-center justify-center",
"bg-transparent hover:bg-black/20 dark:hover:bg-white/20",
"stroke-current hover:stroke-black hover:dark:stroke-white",
"border border-black/25 dark:border-white/25",
"transition-colors duration-300 ease-in-out",
)}
>
<svg
id="drawer-open"
class="size-full"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="1.5"
>
<path d="M3 12h18"></path>
<path d="M3 6h18"></path>
<path d="M3 18h18"></path>
</svg>
<svg
id="drawer-close"
class="size-full"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="1.5"
>
<path d="M18 6L6 18"></path>
<path d="M6 6l12 12"></path>
</svg>
</button>
<ThemeToggle />
<DrawerToggle />
</div>
</div>
</Container>
</header>

<style>
#header-drawer-button > #drawer-open {
@apply block;
}

#header-drawer-button > #drawer-close {
@apply hidden;
}

#header-drawer-button.open > #drawer-open {
@apply hidden;
}

#header-drawer-button.open > #drawer-close {
@apply block;
}
</style>

<script is:inline>
function toggleDrawer() {
const drawer = document.getElementById("drawer");
const drawerButton = document.getElementById("header-drawer-button");
drawer?.classList.toggle("open");
drawerButton?.classList.toggle("open");
}

function initializeDrawerButton() {
const drawerButton = document.getElementById("header-drawer-button");
drawerButton?.addEventListener("click", toggleDrawer);
}

document.addEventListener("astro:after-swap", initializeDrawerButton);
initializeDrawerButton();
</script>

<script is:inline>
function onScroll() {
const header = document.getElementById("header");
Expand Down
64 changes: 64 additions & 0 deletions src/components/ThemeToggle.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
import { Icon } from "astro-icon/components";
import { cn } from "@/lib/utils";
---

<button
id="theme_toggle"
aria-label={`Toggle light and dark theme`}
class={cn(
"flex",
"size-9 rounded-full p-2 items-center justify-center",
"bg-transparent hover:bg-black/20 dark:hover:bg-white/20",
"stroke-current hover:stroke-black hover:dark:stroke-white",
"border border-black/25 dark:border-white/25",
"transition-colors duration-300 ease-in-out"
)}
>
<Icon name="sun" class="block size-full dark:hidden" />
<Icon name="moon" class="hidden size-full dark:block" />
</button>

<style>
html {
color-scheme: light;
background-color: white;
}

html.dark {
color-scheme: dark;
background-color: black;
}
</style>

<script is:inline>
const theme = (() => {
if (typeof localStorage !== "undefined" && localStorage.getItem("theme")) {
return localStorage.getItem("theme");
}
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
return "dark";
}
return "light";
})();

if (theme === "light") {
document.documentElement.classList.remove("dark");
} else {
document.documentElement.classList.add("dark");
}

window.localStorage.setItem("theme", theme);

const handleToggleClick = () => {
const element = document.documentElement;
element.classList.toggle("dark");

const isDark = element.classList.contains("dark");
localStorage.setItem("theme", isDark ? "dark" : "light");
};

document
.getElementById("theme_toggle")
.addEventListener("click", handleToggleClick);
</script>
1 change: 1 addition & 0 deletions src/icons/close.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/menu.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 0 additions & 7 deletions src/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,9 @@

html {
overflow-y: scroll;
color-scheme: light;
background-color: white;
font-family: "Inter", sans-serif;
}

html.dark {
color-scheme: dark;
background-color: black;
}

html,
body {
@apply h-full w-full bg-white text-black/75 antialiased dark:bg-black dark:text-white/75;
Expand Down

0 comments on commit 1a85452

Please sign in to comment.