-
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.
Merge pull request #319 from vordgi/rd-318
rd-318 improve header links style
- Loading branch information
Showing
5 changed files
with
49 additions
and
14 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
41 changes: 31 additions & 10 deletions
41
packages/robindoc/src/components/blocks/nav-link/index.tsx
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,19 +1,40 @@ | ||
"use client"; | ||
|
||
import React, { forwardRef } from "react"; | ||
import Link, { LinkProps } from "next/link"; | ||
import Link, { type LinkProps } from "next/link"; | ||
import { usePathname } from "next/navigation"; | ||
import clsx from "clsx"; | ||
|
||
import { useNavigate } from "@src/components/contexts/navigate/use-navigate"; | ||
|
||
type NavLinkProps = React.AnchorHTMLAttributes<HTMLAnchorElement> & React.PropsWithChildren<LinkProps>; | ||
type NavLinkProps = React.AnchorHTMLAttributes<HTMLAnchorElement> & | ||
React.PropsWithChildren<LinkProps> & { | ||
activeClassName?: string; | ||
targetClassName?: string; | ||
}; | ||
|
||
export const NavLink = forwardRef<HTMLAnchorElement, NavLinkProps>(({ onClick, ...props }, ref) => { | ||
const { listeners } = useNavigate(); | ||
export const NavLink = forwardRef<HTMLAnchorElement, NavLinkProps>( | ||
({ onClick, className, href, targetClassName, activeClassName, ...props }, ref) => { | ||
const { listeners } = useNavigate(); | ||
const pathname = usePathname(); | ||
|
||
const clickHandler = (e: React.MouseEvent<HTMLAnchorElement>) => { | ||
[...listeners].forEach((el) => el.listener()); | ||
if (onClick) onClick(e); | ||
}; | ||
const clickHandler = (e: React.MouseEvent<HTMLAnchorElement>) => { | ||
[...listeners].forEach((el) => el.listener()); | ||
if (onClick) onClick(e); | ||
}; | ||
|
||
return <Link ref={ref} onClick={clickHandler} {...props} />; | ||
}); | ||
return ( | ||
<Link | ||
href={href} | ||
className={clsx( | ||
pathname.startsWith(href) && targetClassName, | ||
pathname === href && activeClassName, | ||
className, | ||
)} | ||
ref={ref} | ||
onClick={clickHandler} | ||
{...props} | ||
/> | ||
); | ||
}, | ||
); |
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