Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
feat: refactored Navbar component (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
hussedev authored Dec 10, 2024
1 parent 5fcb0a7 commit bd218d5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 53 deletions.
62 changes: 9 additions & 53 deletions src/primitives/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,23 @@ import * as React from "react";

import { tv } from "tailwind-variants";

import { GitcoinLogo } from "@/assets";

const defaultLogo = GitcoinLogo;
import { NavbarLogo, NavbarLogoProps, NavbarTitle, NavbarTitleProps } from "./components";

const navbarVariants = tv({
slots: {
base: "top-0 flex h-[64px] w-full shrink-0 flex-col items-center justify-center gap-2 p-[10px_80px]",
container: "flex w-full items-center justify-between",
leftSection: "flex items-center gap-4",
divider: "h-4 border-r-1.5 border-grey-700",
text: "font-ui-mono text-lg",
rightSection: "flex items-center",
},
});

interface LogoProps {
link?: string;
img?: string | React.FunctionComponent<React.SVGAttributes<SVGElement>>;
size?: string;
color?: string;
}

interface TextProps {
text: string;
link?: string;
className?: string;
}

export interface NavbarProps {
primaryLogo?: LogoProps;
secondaryLogo?: LogoProps;
primaryLogo?: NavbarLogoProps;
secondaryLogo?: NavbarLogoProps;
showDivider?: boolean;
text: TextProps;
text: NavbarTitleProps;
children?: React.ReactNode;
}

Expand All @@ -45,45 +29,17 @@ export const Navbar = ({
text,
children,
}: NavbarProps) => {
const { base, container, leftSection, divider, text: textStyle, rightSection } = navbarVariants();

const renderLogo = ({ link, img, size = "h-10 max-w-12", color = "black" }: LogoProps) => {
const logoClasses = `${size} text-${color}`;

if (!img) {
return <img src={defaultLogo} alt="Default Logo" className={logoClasses} />;
}

if (typeof img === "string") {
return <img src={img} alt="Logo" className={logoClasses} />;
}

const LogoComponent = img;
return (
<a href={link || "#"} style={{ color: color }}>
<LogoComponent className={logoClasses} />
</a>
);
};

const renderText = ({ text, link, className = "" }: TextProps) => {
const textClasses = `${textStyle()} ${className}`;
return (
<a href={link || "#"} className={textClasses}>
{text}
</a>
);
};
const { base, container, leftSection, divider, rightSection } = navbarVariants();

return (
<nav className={base()}>
<div className={container()}>
<div className={leftSection()}>
{renderLogo(primaryLogo || {})}
{showDivider && <div className={divider()}></div>}
<NavbarLogo {...primaryLogo} />
{showDivider && <div className={divider()} />}
<div className="flex items-center gap-2">
{secondaryLogo && renderLogo(secondaryLogo)}
{renderText(text)}
{secondaryLogo && <NavbarLogo {...secondaryLogo} />}
<NavbarTitle {...text} />
</div>
</div>
<div className={rightSection()}>{children}</div>
Expand Down
35 changes: 35 additions & 0 deletions src/primitives/Navbar/components/NavbarLogo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { GitcoinLogo } from "@/assets";

const defaultLogo = GitcoinLogo;

export interface NavbarLogoProps {
link?: string;
img?: string | React.FunctionComponent<React.SVGAttributes<SVGElement>>;
size?: string;
color?: string;
}

export const NavbarLogo = ({
link,
img,
size = "h-10 max-w-12",
color = "black",
}: NavbarLogoProps) => {
const logoClasses = `${size} text-${color}`;

if (!img) {
return <img src={defaultLogo} alt="Default Logo" className={logoClasses} />;
}

if (typeof img === "string") {
return <img src={img} alt="Logo" className={logoClasses} />;
}

const LogoComponent = img;

return (
<a href={link || "#"} style={{ color }}>
<LogoComponent className={logoClasses} />
</a>
);
};
15 changes: 15 additions & 0 deletions src/primitives/Navbar/components/NavbarTitle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { cn } from "@/lib";

export interface NavbarTitleProps {
text: string;
link?: string;
className?: string;
}

export const NavbarTitle = ({ text, link, className = "" }: NavbarTitleProps) => {
return (
<a href={link || "#"} className={cn("font-ui-mono text-lg", className)}>
{text}
</a>
);
};
2 changes: 2 additions & 0 deletions src/primitives/Navbar/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./NavbarLogo";
export * from "./NavbarTitle";

0 comments on commit bd218d5

Please sign in to comment.