import React, {
AnchorHTMLAttributes,
ComponentProps,
DetailedHTMLProps,
} from "react";
import { DocsLinkType } from ".";
import Stack from "../../layout/Stack";
import { twMerge } from "tailwind-merge";
import Row from "../../layout/Row";
import Divider from "../../layout/Divider";
import { ChevronDown } from "lucide-react";
import Button from "../../layout/Button";
type Props = DetailedHTMLProps<
AnchorHTMLAttributes,
HTMLAnchorElement
> & {
docLink: DocsLinkType;
wrapperProps?: ComponentProps;
strict?: boolean;
childWrapperProps?: ComponentProps;
autoExpandAll?: 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,
...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 = window.location.pathname.includes(
docLink.href
);
const isStrictMatch = Boolean(
linkRef.current?.getAttribute("data-strict")
);
if (strict || isStrictMatch) {
setIsActive(window.location.pathname === docLink.href);
} else {
setIsActive(basePathMatch);
}
if (basePathMatch) {
setExpand(true);
}
}
}, []);
return (
{docLink.title}
{docLink.children?.[0] && (
)}
{docLink.children && expand && (
{docLink.children.map((link, index) => (
))}
)}
);
}