-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: combined hamburger and navbar navigation (#120)
- Loading branch information
1 parent
77469e7
commit a5e5ce2
Showing
5 changed files
with
180 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
import type { NavItem } from "../types"; | ||
import NavItemComponent from "./navigation/NavItem.astro"; | ||
interface Props { | ||
navigationItems: NavItem[]; | ||
hamburger?: boolean; | ||
prioritized?: boolean; | ||
class?: string; | ||
} | ||
const { | ||
navigationItems, | ||
class: classNames = "", | ||
prioritized = false, | ||
hamburger = false, | ||
} = Astro.props; | ||
const lastItemIndex = navigationItems.length - 1; | ||
--- | ||
|
||
<script> | ||
const prioritizedMenus = Array.from( | ||
document.querySelectorAll('ul[data-prioritized="true"]'), | ||
); | ||
|
||
function updateVisibilityFactory(menu: Element | null) { | ||
const items = Array.from( | ||
menu?.querySelectorAll('[data-component="nav-item"]') || [], | ||
); | ||
|
||
if (!menu || !items) { | ||
return; | ||
} | ||
|
||
return () => { | ||
const menuWidth = menu.clientWidth; | ||
// Adjust values to add preferred extra margins | ||
const margin = 24; | ||
let usedWidth = 50; | ||
items.forEach((item) => { | ||
item.classList.remove("hidden"); | ||
usedWidth += margin + (item?.scrollWidth || 0); | ||
|
||
if (usedWidth >= menuWidth) { | ||
item.classList.add("hidden"); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
for (const menu of prioritizedMenus) { | ||
const updateVisibility = updateVisibilityFactory(menu); | ||
if (!updateVisibility) { | ||
continue; | ||
} | ||
window.addEventListener("resize", updateVisibility); | ||
window.addEventListener("load", updateVisibility); | ||
} | ||
</script> | ||
|
||
<ul | ||
class={classNames} | ||
data-component="header-navigation" | ||
data-prioritized={prioritized ? "true" : "false"} | ||
> | ||
{ | ||
navigationItems.map(({ title, path, items }, index) => ( | ||
<NavItemComponent | ||
title={title} | ||
path={path} | ||
items={items} | ||
hamburger={hamburger} | ||
interactionMode={hamburger ? "click" : "hover"} | ||
subAlignment={index === lastItemIndex ? "right" : "left"} | ||
/> | ||
)) | ||
} | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./articles"; | ||
export * from "./images"; | ||
export * from "./navigation"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface MinimalNavItem { | ||
title: string; | ||
path?: string; | ||
} | ||
export interface NavItem extends MinimalNavItem { | ||
items?: MinimalNavItem[]; | ||
} |