diff --git a/src/lib/components/NavBar/NavBar.module.scss b/src/lib/components/NavBar/NavBar.module.scss index fa7ffce..ce444bd 100644 --- a/src/lib/components/NavBar/NavBar.module.scss +++ b/src/lib/components/NavBar/NavBar.module.scss @@ -25,7 +25,8 @@ $variants: ("neutral", "primary", "secondary", "info", "danger", "warning", "suc .logo { height: var(--base-sizing-40x); - img { + display: flex; + * { height: 100%; } } diff --git a/src/lib/components/NavBar/NavBar.stories.tsx b/src/lib/components/NavBar/NavBar.stories.tsx index 5f7f49b..71c48bf 100644 --- a/src/lib/components/NavBar/NavBar.stories.tsx +++ b/src/lib/components/NavBar/NavBar.stories.tsx @@ -9,6 +9,7 @@ const argValues: NavBarProps = { logo: { href: "https://motif-ui.com/", imgPath: LOGO_URL, + alt: "logo", }, button: { label: "Login", icon: "person" }, mainMenu: { diff --git a/src/lib/components/NavBar/NavBar.test.tsx b/src/lib/components/NavBar/NavBar.test.tsx index 42b9037..71b2eed 100644 --- a/src/lib/components/NavBar/NavBar.test.tsx +++ b/src/lib/components/NavBar/NavBar.test.tsx @@ -173,6 +173,7 @@ describe("NavBar", () => { , @@ -181,6 +182,37 @@ describe("NavBar", () => { expect(getByRole("img")).toHaveAttribute("src", "some_image"); }); + it("should render custom component using logoSlot prop", () => { + const { getByRole } = render( + + Logo + + } + />, + ); + + expect(getByRole("link")).toHaveAttribute("href", "https://example.com/"); + expect(getByRole("img")).toHaveAttribute("src", "logo.png"); + }); + + it("should prioritize logoSlot over logo prop when both are provided", () => { + const { getByRole, queryByAltText } = render( + + Logo slot + + } + />, + ); + + expect(getByRole("img")).toHaveAttribute("src", "logo-slot.png"); + expect(queryByAltText("Logo prop")).not.toBeInTheDocument(); + }); + it("should render either an action menu with the props in the actionMenu prop or a main menu with the props in the mainMenu prop", () => { const mockClick = jest.fn(); const { getByText, getByRole, rerender } = render( diff --git a/src/lib/components/NavBar/NavBar.tsx b/src/lib/components/NavBar/NavBar.tsx index c0b9937..27adcaf 100644 --- a/src/lib/components/NavBar/NavBar.tsx +++ b/src/lib/components/NavBar/NavBar.tsx @@ -21,6 +21,7 @@ const NavBar = (props: PropsWithRef) => { className, style, logo, + logoSlot, search, button, mainMenu, @@ -35,7 +36,7 @@ const NavBar = (props: PropsWithRef) => { {mainMenu && ( setMenuCollapsed(p => !p)} className={styles.hamburger} /> )} - {logo && } + {(logo || logoSlot) && {logoSlot}}
{mainMenu && }
{search && } {button && } diff --git a/src/lib/components/NavBar/components/NavBarLogo.tsx b/src/lib/components/NavBar/components/NavBarLogo.tsx index 703b23a..453b700 100644 --- a/src/lib/components/NavBar/components/NavBarLogo.tsx +++ b/src/lib/components/NavBar/components/NavBarLogo.tsx @@ -1,3 +1,4 @@ +import { PropsWithChildren } from "react"; import styles from "../NavBar.module.scss"; export type NavBarLogoProps = { @@ -6,14 +7,22 @@ export type NavBarLogoProps = { href?: string; }; -const NavBarLogo = (props: NavBarLogoProps) => { - const { href, imgPath, alt } = props; - return href ? ( - - {alt} - - ) : ( - {alt} +const NavBarLogo = (props: PropsWithChildren<{ logo?: NavBarLogoProps }>) => { + const { logo, children } = props; + const { alt, imgPath, href } = logo || {}; + return ( +
+ {children ?? + (logo && + (href ? ( + + {alt} + + ) : ( + {alt} + )))} +
); }; + export default NavBarLogo; diff --git a/src/lib/components/NavBar/types.ts b/src/lib/components/NavBar/types.ts index f47d99f..7675797 100644 --- a/src/lib/components/NavBar/types.ts +++ b/src/lib/components/NavBar/types.ts @@ -1,3 +1,4 @@ +import { ReactElement } from "react"; import { NavBarLogoProps } from "./components/NavBarLogo"; import { NavBarButtonProps } from "./components/NavBarButton"; import { MenuProps } from "./components/NavBarMenu/types"; @@ -79,6 +80,10 @@ export type NavBarDefaultableProps = { * ``` */ logo?: NavBarLogoProps; + /** + * Custom logo component. Takes precedence over logo prop. + */ + logoSlot?: ReactElement; }; export type NavBarContextProps = {