mirror of
https://github.com/khairul169/code-share.git
synced 2025-04-28 16:49:36 +07:00
33 lines
724 B
TypeScript
33 lines
724 B
TypeScript
import React from "react";
|
|
import { usePageContext } from "./context";
|
|
import { Slot } from "@radix-ui/react-slot";
|
|
|
|
type LinkProps = {
|
|
href?: string;
|
|
className?: string;
|
|
asChild?: boolean;
|
|
children?: React.ReactNode;
|
|
};
|
|
|
|
const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
|
|
({ asChild, href, ...props }, ref) => {
|
|
const pageContext = usePageContext();
|
|
const { urlPathname } = pageContext;
|
|
const Comp = asChild ? Slot : "a";
|
|
|
|
const isActive =
|
|
href === "/" ? urlPathname === href : urlPathname.startsWith(href!);
|
|
|
|
return (
|
|
<Comp
|
|
ref={ref}
|
|
data-active={isActive || undefined}
|
|
href={href}
|
|
{...props}
|
|
/>
|
|
);
|
|
}
|
|
);
|
|
|
|
export default Link;
|