Skip to content

Commit

Permalink
feat: update header with slot
Browse files Browse the repository at this point in the history
  • Loading branch information
mickasmt committed Sep 16, 2023
1 parent cbe6483 commit 006e591
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 47 deletions.
28 changes: 21 additions & 7 deletions src/components/layout/header.astro
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
import { siteConfig } from "@/config/site";
import { cn } from "@/lib/utils";
import MainNav from "./main-nav.astro";
import type { MainNavItem } from "@/types";
import Icon from "astro-icon";
import { SheetMobileNav } from "./sheet-mobile-nav";
type Props = {
items: MainNavItem[];
className?: string;
};
const { items, className } = Astro.props;
const { className } = Astro.props;
---

<header
Expand All @@ -19,9 +19,23 @@ const { items, className } = Astro.props;
className
)}
>
<nav class="relative container flex items-center justify-between w-full">
<MainNav items={items} />
<div class="relative container flex items-center justify-between w-full">
<div class="flex items-center gap-6 md:gap-8 lg:gap-10">
<a href="/" class="hidden items-center space-x-2 md:flex">
<Icon name="stars-outline" class="h-8 w-8" />
<span class="hidden font-bold sm:inline-block">
{siteConfig.name}
</span>
</a>

<SheetMobileNav client:load />

<div class="hidden md:flex">
<slot name="left-header" />
</div>
</div>

<!-- logo mobile center -->
<div
class="absolute top-1/2 left-1/2 -translate-x-4 -translate-y-4 md:hidden"
>
Expand All @@ -30,6 +44,6 @@ const { items, className } = Astro.props;
</a>
</div>

<slot />
</nav>
<slot name="right-header" />
</div>
</header>
54 changes: 20 additions & 34 deletions src/components/layout/main-nav.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
---
import { siteConfig } from "@/config/site";
import { cn, extractSegmentURL } from "@/lib/utils";
import type { MainNavItem } from "@/types";
import { Icon } from "astro-icon";
import { SheetMobileNav } from "@/components/layout/sheet-mobile-nav";
type Props = {
items: MainNavItem[];
Expand All @@ -13,34 +10,23 @@ const { items } = Astro.props;
const segment = extractSegmentURL(Astro.url.pathname);
---

<div class="flex items-center gap-6 md:gap-8 lg:gap-10">
<a href="/" class="hidden items-center space-x-2 md:flex">
<Icon name="stars-outline" class="h-8 w-8" />
<span class="hidden font-bold sm:inline-block">
{siteConfig.name}
</span>
</a>

{
items?.length ? (
<nav class="hidden gap-6 md:flex">
{items?.map((item) => (
<a
href={item.disabled ? "#" : item.href}
class={cn(
"flex items-center text-lg font-medium transition-colors hover:text-foreground/80 sm:text-sm",
item.href.startsWith(`/${segment}`)
? "text-foreground"
: "text-foreground/60",
item.disabled && "cursor-not-allowed opacity-80"
)}
>
{item.title}
</a>
))}
</nav>
) : null
}

<SheetMobileNav client:load />
</div>
{
items?.length ? (
<nav class="hidden gap-6 md:flex">
{items?.map((item) => (
<a
href={item.disabled ? "#" : item.href}
class={cn(
"flex items-center text-lg font-medium transition-colors hover:text-foreground/80 sm:text-sm",
item.href.startsWith(`/${segment}`)
? "text-foreground"
: "text-foreground/60",
item.disabled && "cursor-not-allowed opacity-80"
)}
>
{item.title}
</a>
))}
</nav>
) : null
}
155 changes: 155 additions & 0 deletions src/components/main-navigation-menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import * as React from "react";

import { cn } from "@/lib/utils";
import { Icons } from "@/icons";
import {
NavigationMenu,
NavigationMenuContent,
NavigationMenuItem,
NavigationMenuList,
NavigationMenuTrigger,
navigationMenuTriggerStyle,
} from "@/components/ui/navigation-menu";
import { Badge } from "@/components/ui/badge";

type MenuItem = {
title: string;
href: string;
description: string;
disabled?: boolean;
badge?: string;
};

const examples: MenuItem[] = [
{
title: "Blog",
href: "/blog",
description:
"A Markdown/MDX blog built using Content Collections in Astro.",
},
{
title: "Docs",
href: "/docs/getting-started",
description:
"A Markdown/MDX documentation site built using Contents Collections in Astro.",
},
{
title: "Ecommerce",
href: "/examples/ecommerce",
description:
"Different pages of an ecommerce app fetching data from an API.",
disabled: true,
badge: "SOON",
},
{
title: "Social Media",
href: "/examples/social-media",
description: "Different components & pages of an social media app.",
disabled: true,
badge: "SOON",
},
{
title: "Tabs",
href: "/",
description:
"A set of layered sections of content—known as tab panels—that are displayed one at a time.",
},
{
title: "Tooltip",
href: "/",
description:
"A popup that displays information related to an element when the element receives keyboard focus or the mouse hovers over it.",
},
];

export function MainNavigationMenu() {
return (
<NavigationMenu>
<NavigationMenuList>
<NavigationMenuItem>
<NavigationMenuTrigger>Getting started</NavigationMenuTrigger>
<NavigationMenuContent>
<ul className="grid gap-3 p-6 md:w-[400px] lg:w-[500px] lg:grid-cols-[.75fr_1fr]">
<li className="row-span-3">
<a
className="flex h-full w-full select-none flex-col justify-end rounded-md bg-gradient-to-b from-muted/50 to-muted p-6 no-underline outline-none focus:shadow-md"
href="/"
>
<Icons.logo className="h-8 w-8" />
<div className="mb-2 mt-3 text-lg font-medium">Astronomy</div>
<p className="text-sm leading-tight text-muted-foreground">
Modern application with differents sections examples built
using shadcn/ui.
</p>
</a>
</li>

<ListItem href="/#features" title="Features">
Re-usable components built using Radix UI and Tailwind CSS.
</ListItem>
<ListItem href="/" title="Installation">
How to install dependencies and structure your app.
</ListItem>
<ListItem href="/" title="Typography">
Styles for headings, paragraphs, lists...etc
</ListItem>
</ul>
</NavigationMenuContent>
</NavigationMenuItem>

<NavigationMenuItem>
<NavigationMenuTrigger>Examples</NavigationMenuTrigger>
<NavigationMenuContent>
<ul className="grid w-[400px] gap-3 p-4 md:w-[500px] md:grid-cols-2 lg:w-[600px] ">
{examples.map((example) => (
<ListItem
key={example.title}
title={example.title}
// disabled={example?.disabled}
// badge={example.badge}
href={example.href}
>
{example.description}
</ListItem>
))}
</ul>
</NavigationMenuContent>
</NavigationMenuItem>

<NavigationMenuItem>
<a href="/about" className={navigationMenuTriggerStyle()}>
About
</a>
</NavigationMenuItem>
</NavigationMenuList>
</NavigationMenu>
);
}

const ListItem = React.forwardRef<
React.ElementRef<"a">,
React.ComponentPropsWithoutRef<"a">
>(({ className, title, children, ...props }, ref) => {
return (
<li>
<a
ref={ref}
className={cn(
"block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground disabled:hover:bg-transparent disabled:text-muted-foreground",
className
)}
// disabled={disabled}
{...props}
>
<div className="text-sm font-medium leading-none">
{title}
{/* {badge ? <Badge variant="outline" className="ml-2 text-sm h-5" radius="sm">{badge}</Badge> : ""} */}
</div>
<p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
{children}
</p>
</a>
</li>
);
});
ListItem.displayName = "ListItem";
7 changes: 5 additions & 2 deletions src/layouts/docs-layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { docsConfig } from "@/config/docs";
import { siteConfig } from "@/config/site";
import Icon from "astro-icon";
import BaseLayout from "./base-layout.astro";
import MainNav from "@/components/layout/main-nav.astro";
type Props = {
title: string;
Expand All @@ -15,8 +16,10 @@ const { title, description } = Astro.props;
---

<BaseLayout title={title} description={description}>
<Header items={docsConfig.mainNav} className="border-b">
<nav>
<Header className="border-b">
<MainNav items={docsConfig.mainNav} slot="left-header" />

<nav slot="right-header">
<a href={siteConfig.links.github} target="_blank" rel="noreferrer">
<Icon name="mdi:github" className="h-8 w-8" />
<span class="sr-only">GitHub</span>
Expand Down
10 changes: 6 additions & 4 deletions src/layouts/main-layout.astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
import Footer from "@/components/layout/footer.astro";
import Header from "@/components/layout/header.astro";
import { MainNavigationMenu } from "@/components/main-navigation-menu";
import { buttonVariants } from "@/components/ui/button";
import { marketingConfig } from "@/config/marketing";
import { cn } from "@/lib/utils";
import BaseLayout from "./base-layout.astro";
import Header from "@/components/layout/header.astro";
type Props = {
title: string;
Expand All @@ -16,8 +16,10 @@ const { title, description, mainClass } = Astro.props;
---

<BaseLayout title={title} description={description}>
<Header items={marketingConfig.mainNav}>
<nav>
<Header>
<MainNavigationMenu slot="left-header" client:load />

<nav slot="right-header">
<a
href="/login"
class={cn(buttonVariants({ variant: "secondary", size: "sm" }), "px-4")}
Expand Down

0 comments on commit 006e591

Please sign in to comment.