32 lines
1.1 KiB
React
Raw Normal View History

2023-09-14 22:55:14 +01:00
import { useContext } from "react";
import classNames from "classnames";
import { TabContext } from "utils/contexts/tab";
2023-09-15 08:17:57 -07:00
export function slugify(tabName) {
2023-09-16 14:37:06 -07:00
return tabName ? encodeURIComponent(tabName.toString().replace(/\s+/g, '-').toLowerCase()) : ''
2023-09-15 08:17:57 -07:00
}
2023-09-14 22:55:14 +01:00
export default function Tab({ tab }) {
const { activeTab, setActiveTab } = useContext(TabContext);
return (
<li key={tab} role="presentation"
className={classNames(
"text-theme-700 dark:text-theme-200 relative h-8 w-full rounded-md flex m-1",
)}>
<button id={`${tab}-tab`} type="button" role="tab"
2023-09-15 08:17:57 -07:00
aria-controls={`#${tab}`} aria-selected={activeTab === slugify(tab) ? "true" : "false"}
2023-09-14 22:55:14 +01:00
className={classNames(
"h-full w-full rounded-md",
2023-09-15 08:17:57 -07:00
activeTab === slugify(tab) ? "bg-theme-300/20 dark:bg-white/10" : "hover:bg-theme-100/20 dark:hover:bg-white/5",
2023-09-14 22:55:14 +01:00
)}
2023-09-15 08:17:57 -07:00
onClick={() => {
setActiveTab(slugify(tab));
window.location.hash = `#${slugify(tab)}`;
}}
2023-09-14 22:55:14 +01:00
>{tab}</button>
</li>
);
}