mirror of
https://github.com/khairul169/code-share.git
synced 2025-04-29 00:59:37 +07:00
24 lines
602 B
TypeScript
24 lines
602 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 = ({ asChild, href, ...props }: LinkProps) => {
|
|
const pageContext = usePageContext();
|
|
const { urlPathname } = pageContext;
|
|
const Comp = asChild ? Slot : "a";
|
|
|
|
const isActive =
|
|
href === "/" ? urlPathname === href : urlPathname.startsWith(href!);
|
|
|
|
return <Comp data-active={isActive || undefined} href={href} {...props} />;
|
|
};
|
|
|
|
export default Link;
|