119 lines
4.3 KiB
TypeScript
119 lines
4.3 KiB
TypeScript
import { Link, useNavigate } from "react-router";
|
|
import clsx from "clsx";
|
|
import { Fragment, ReactNode, useId } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
export type TabWithIcon = {
|
|
name?: string;
|
|
href?: string;
|
|
current: boolean;
|
|
icon?: ReactNode;
|
|
onClick?: () => void;
|
|
className?: string;
|
|
};
|
|
interface Props {
|
|
tabs: TabWithIcon[];
|
|
className?: string;
|
|
justify?: "start" | "center" | "end" | "between";
|
|
breakpoint?: "sm" | "md" | "lg" | "xl" | "2xl";
|
|
}
|
|
export default function TabsWithIcons({ tabs, className, justify, breakpoint = "md" }: Props) {
|
|
const { t } = useTranslation();
|
|
const navigate = useNavigate();
|
|
const tabsSelectId = useId();
|
|
return (
|
|
<div className={className}>
|
|
<div
|
|
className={clsx(
|
|
breakpoint === "sm" && "sm:hidden",
|
|
breakpoint === "md" && "md:hidden",
|
|
breakpoint === "lg" && "lg:hidden",
|
|
breakpoint === "xl" && "xl:hidden",
|
|
breakpoint === "2xl" && "2xl:hidden"
|
|
)}
|
|
>
|
|
<label htmlFor={tabsSelectId} className="sr-only">
|
|
{t("shared.select")}
|
|
</label>
|
|
<select
|
|
id={tabsSelectId}
|
|
name="tabs"
|
|
className="bg-surface-neutral-primary text-text-neutral-primary border-border-default focus-visible:border-border-default focus-visible:shadow-focus block w-full rounded-md focus-visible:outline-hidden"
|
|
value={tabs.find((tab) => tab.current)?.name}
|
|
onChange={(e) => {
|
|
const tab = tabs.find((tab) => tab.name === e.target.value);
|
|
if (tab?.href) {
|
|
navigate(tab.href);
|
|
} else if (tab?.onClick) {
|
|
tab.onClick();
|
|
}
|
|
}}
|
|
>
|
|
{tabs.map((tab) => (
|
|
<option key={tab.name} value={tab.name}>
|
|
{tab.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
<div
|
|
className={clsx(
|
|
breakpoint === "sm" && "hidden sm:block",
|
|
breakpoint === "md" && "hidden md:block",
|
|
breakpoint === "lg" && "hidden lg:block",
|
|
breakpoint === "xl" && "hidden xl:block",
|
|
breakpoint === "2xl" && "hidden 2xl:block"
|
|
)}
|
|
>
|
|
<div className="border-border-default border-b">
|
|
<nav
|
|
className={clsx(
|
|
"-mb-px flex space-x-4",
|
|
justify === "start" && "justify-start",
|
|
justify === "center" && "justify-center",
|
|
justify === "end" && "justify-end",
|
|
justify === "between" && "justify-between"
|
|
)}
|
|
aria-label={t("components.tabs.ariaLabel")}
|
|
>
|
|
{tabs.map((tab, idx) => (
|
|
<Fragment key={idx}>
|
|
{tab.href && (
|
|
<Link
|
|
key={tab.name}
|
|
to={tab.href}
|
|
className={clsx(
|
|
tab.current ? "border-border-default" : "hover:border-border-default text-text-neutral-tertiary hover:text-text-neutral-primary/80 border-transparent",
|
|
"focus-visible:shadow-focus group inline-flex items-center space-x-2 border-b-2 px-1 py-2 text-sm font-medium focus-visible:outline-hidden",
|
|
tab.className
|
|
)}
|
|
aria-current={tab.current ? "page" : undefined}
|
|
>
|
|
{tab.icon}
|
|
<div className="truncate">{tab.name}</div>
|
|
</Link>
|
|
)}
|
|
{tab.onClick && (
|
|
<button
|
|
type="button"
|
|
onClick={tab.onClick}
|
|
className={clsx(
|
|
tab.current ? "border-border-default" : "hover:border-border-default text-text-neutral-tertiary hover:text-text-neutral-primary/80 border-transparent",
|
|
"focus-visible:shadow-focus group inline-flex w-full items-center space-x-2 border-b-2 px-1 py-2 text-sm font-medium focus-visible:outline-hidden",
|
|
tab.className
|
|
)}
|
|
aria-pressed={tab.current}
|
|
>
|
|
{tab.icon}
|
|
<div className="truncate">{tab.name}</div>
|
|
</button>
|
|
)}
|
|
</Fragment>
|
|
))}
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|