React Context-powered tabs with Tailwind styling.
Don't clone the repo, don't npm install
anything, just copy the TailwindTabs.jsx
file into your own codebase. People install too much these days.
This project assumes you have React (16+) and Tailwind already installed. It's also compatible with Preact (just change the import at the top), but not as tested so YMMV. You can use this with any bundler!
I didn't want to mess with Tailwind JIT stuff for custom color classes, so just edit the classes at the top of the file for the colors that you want!
There's two ways to use these tabs, as a compound component, or as a single component.
Using this in the compound component style is very similar to the HTML <table>
setup (with <th>
s and <tr>
s and <td>
s). You can rearrange components however you'd like (for example putting the panels above the tab list). You can put components in the tab panels, not just strings!
import {
TailwindTabs,
TailwindTabList,
TailwindTab,
TailwindTabPanels,
TailwindTabPanel,
} from "./TailwindTabs.jsx";
function SomeComponent() {
return (
<TailwindTabs>
<TailwindTabList>
<TailwindTab>Tab #1</TailwindTab>
<TailwindTab>Tab #2</TailwindTab>
<TailwindTab isDisabled>Tab #3</TailwindTab>
</TailwindTabList>
<TailwindTabPanels>
<TailwindTabPanel>Content of Tab #1</TailwindTabPanel>
<TailwindTabPanel>
<SomeExampleComponent />
</TailwindTabPanel>
<TailwindTabPanel>Content of Tab #3</TailwindTabPanel>
</TailwindTabPanels>
</TailwindTabs>
);
}
Using this style is a bit more restricted, in that you can only pass in your data as an array with a label
and content
, and it generates the tabs for you.
import { TailwindComposedTabs } from "./TailwindTabs.jsx";
function SomeComponent() {
const tabData = [
{
label: "Tab #1",
content: "Content of Tab #1",
},
{
label: "Tab #2",
content: <SomeExampleComponent />,
},
{
label: "Tab #3",
content: <SomeExampleComponent />,
disabled: true
},
];
return <TailwindComposedTabs data={tabData} />;
}
These are the variables you'll edit to customize the colors you want for your tabs:
let tabBackgroundColorClass = "bg-white";
let borderColorClass = "border-pink-300";
let hoverBorderColorClass = "hover:border-accent-900";
let textColorClass = "text-black";
let hoverTextColorClass = "hover:text-pink-600";
Just take out the CSS classes and add your own! Tailwind isn't necessary for the logic to work. You can add whatever classes you'd like.
You can also rename the components if you want. I called them this so that they wouldn't collide with any UI libraries that might already exist that have their own Tab components.
It'd be cool if you tell me if you use this, but follow your dreams and don't let me stop you. Check out the license if your company needs to know the deets.
I use/like this the way it is in my various projects (if I'm being honest, I open sourced it because I was lazy about sharing it to myself on different machines), so please don't add new features. If you want new features, fork it! If you wanna fix a bug or something, that's when a PR or issue would be better.