Skip to content

Commit

Permalink
Central category link style
Browse files Browse the repository at this point in the history
  • Loading branch information
dgopsq committed Aug 3, 2023
1 parent 5ade3ba commit 7f68bdb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
29 changes: 13 additions & 16 deletions src/components/navigation/Categories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";

import { Link } from "@/components/core/Link";
import { generateCategoryRoute } from "@/misc/navigation";
import { CategoryLink } from "@/components/navigation/CategoryLink";
import { classNames } from "@/misc/styles";
import { Category } from "@/queries/categories/data";
import { Popover, Transition } from "@headlessui/react";
Expand All @@ -20,7 +19,7 @@ export const Categories: React.FC<Props> = ({ categories }) => {
<div className="flex h-full justify-center space-x-8">
{categories.map((category) => (
<Popover key={category.name} className="flex">
{({ open }) => (
{({ open, close }) => (
<>
<div className="relative flex">
<Popover.Button
Expand Down Expand Up @@ -53,12 +52,11 @@ export const Categories: React.FC<Props> = ({ categories }) => {
<div className="relative bg-white">
<div className="mx-auto max-w-7xl px-8 my-10">
<div>
<Link
href={generateCategoryRoute(category.slug)}
className="text-lg font-medium text-gray-900"
>
{category.name}
</Link>
<CategoryLink
variant="main"
slug={category.slug}
name={category.name}
/>
</div>

<ul
Expand All @@ -68,13 +66,12 @@ export const Categories: React.FC<Props> = ({ categories }) => {
>
{category.children.map((subCategory) => (
<li key={subCategory.name}>
<Popover.Button
as={Link}
href={generateCategoryRoute(subCategory.slug)}
className="text-gray-500 hover:text-gray-800 text-sm font-medium"
>
{subCategory.name}
</Popover.Button>
<CategoryLink
variant="sub"
slug={subCategory.slug}
name={subCategory.name}
onClick={close}
/>
</li>
))}
</ul>
Expand Down
38 changes: 38 additions & 0 deletions src/components/navigation/CategoryLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Link } from "@/components/core/Link";
import { generateCategoryRoute } from "@/misc/navigation";
import { match } from "ts-pattern";

type Props = {
variant: "main" | "sub";
slug: string;
name: string;
onClick?: () => void;
};

/**
*
*/
export const CategoryLink: React.FC<Props> = ({
variant,
slug,
name,
onClick,
}) => {
const route = generateCategoryRoute(slug);

return (
<Link
href={route}
className={match(variant)
.with("main", () => "text-lg font-medium text-gray-900")
.with(
"sub",
() => "text-gray-500 hover:text-gray-800 text-sm font-medium"
)
.exhaustive()}
onClick={onClick}
>
{name}
</Link>
);
};
25 changes: 11 additions & 14 deletions src/components/navigation/MobileNavbar.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"use client";

import { CloseIcon } from "@/components/core/Icon";
import { Link } from "@/components/core/Link";
import { generateCategoryRoute } from "@/misc/navigation";
import { CategoryLink } from "@/components/navigation/CategoryLink";
import { Category } from "@/queries/categories/data";
import { Dialog, Transition } from "@headlessui/react";
import { Fragment } from "react";
Expand Down Expand Up @@ -62,13 +61,12 @@ export const MobileNavbar: React.FC<Props> = ({
<ul className="space-y-8">
{categories.map((category) => (
<li key={category.slug}>
<Link
href={generateCategoryRoute(category.slug)}
className="text-lg font-medium text-gray-900"
<CategoryLink
variant="main"
slug={category.slug}
name={category.name}
onClick={() => setOpen(false)}
>
{category.name}
</Link>
/>

<div>
<ul
Expand All @@ -78,13 +76,12 @@ export const MobileNavbar: React.FC<Props> = ({
>
{category.children.map((subCategory) => (
<li key={subCategory.name}>
<Link
href={generateCategoryRoute(subCategory.slug)}
className="text-gray-500 hover:text-gray-800 text-sm font-medium"
<CategoryLink
variant="sub"
slug={subCategory.slug}
name={subCategory.name}
onClick={() => setOpen(false)}
>
{subCategory.name}
</Link>
/>
</li>
))}
</ul>
Expand Down

0 comments on commit 7f68bdb

Please sign in to comment.