import React, {
type AnchorHTMLAttributes,
type ComponentProps,
type DetailedHTMLProps,
} from "react";
import type { DocsLinkType } from ".";
import Stack from "../../layout/Stack";
import { twMerge } from "tailwind-merge";
import Row from "../../layout/Row";
import Divider from "../../layout/Divider";
import { ChevronDown, Circle } from "lucide-react";
import Button from "../../layout/Button";
import twuiLinkMatchesPath from "../../utils/link-matches-path";
type Props = DetailedHTMLProps<
AnchorHTMLAttributes,
HTMLAnchorElement
> & {
docLink: DocsLinkType;
wrapperProps?: ComponentProps;
strict?: boolean;
childWrapperProps?: ComponentProps;
autoExpandAll?: boolean;
child?: boolean;
};
/**
* # TWUI Docs Left Aside Link
* @note use dataset attribute `data-strict` for strict matching
*
* @className `twui-docs-left-aside-link`
*/
export default function TWUIDocsLink({
docLink,
wrapperProps,
childWrapperProps,
strict,
autoExpandAll,
child,
...props
}: Props) {
const [isActive, setIsActive] = React.useState(false);
const [expand, setExpand] = React.useState(autoExpandAll || false);
const linkRef = React.useRef(null);
React.useEffect(() => {
if (typeof window !== "undefined") {
const basePathMatch = twuiLinkMatchesPath({
href: docLink.href,
pathname: window.location.pathname,
});
const isStrictMatch = Boolean(
linkRef.current?.getAttribute("data-strict"),
);
if (strict || isStrictMatch) {
setIsActive(
twuiLinkMatchesPath({
href: docLink.href,
pathname: window.location.pathname,
strict: true,
}),
);
} else {
setIsActive(basePathMatch);
}
if (basePathMatch) {
setExpand(true);
}
}
}, []);
return (
{child && }
{docLink.title}
{docLink.children?.[0] && (
)}
{docLink.children && expand && (
{docLink.children.map((link, index) => (
))}
)}
);
}