Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fluid-tabs): added fluid tabs component #351

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions animata/card/fluid-tabs.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import FluidTabs from "@/animata/card/fluid-tabs";
import { Meta, StoryObj } from "@storybook/react";

const meta = {
title: "Tabs/Fluid Tabs",
component: FluidTabs,
parameters: {
layout: "centered",
},
tags: ["autodocs"],
argTypes: {},
} satisfies Meta<typeof FluidTabs>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Primary: Story = {
args: {},
};
77 changes: 77 additions & 0 deletions animata/card/fluid-tabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"use client";

import { useEffect, useRef, useState } from "react";
import { AnimatePresence, motion } from "framer-motion";
import { Inbox, Landmark, PieChart } from "lucide-react";

const tabs = [
{
id: "accounts",
label: "Accounts",
icon: <Landmark size={15} className="mt-1" />,
},
{ id: "deposits", label: "Deposits", icon: <Inbox size={15} className="mt-1" /> },
{ id: "funds", label: "Funds", icon: <PieChart className="mt-1" size={15} /> },
];

export default function FluidTabs() {
const [activeTab, setActiveTab] = useState("funds");
const [touchedTab, setTouchedTab] = useState<string | null>(null);
const [prevActiveTab, setPrevActiveTab] = useState("funds");
const timeoutRef = useRef<NodeJS.Timeout | null>(null);

useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, []);

const handleTabClick = (tabId: string) => {
setPrevActiveTab(activeTab);
setActiveTab(tabId);
setTouchedTab(tabId);
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(() => {
setTouchedTab(null);
}, 300); // Blur effect duration
};

const getTabIndex = (tabId: string) => tabs.findIndex((tab) => tab.id === tabId);

return (
<div className="flex h-screen items-center justify-center bg-gray-100">
Rudra-Sankha-Sinhamahapatra marked this conversation as resolved.
Show resolved Hide resolved
<div className="relative mx-4 flex space-x-1 rounded-full bg-[#f5f1eb] p-1 shadow-lg">
<AnimatePresence initial={false}>
<motion.div
key={activeTab}
className="absolute inset-y-1 rounded-full bg-white"
initial={{ x: `${getTabIndex(prevActiveTab) * 100}%` }}
animate={{ x: `${getTabIndex(activeTab) * 100}%` }}
transition={{ type: "spring", stiffness: 300, damping: 30 }}
style={{ width: `${100 / tabs.length}%` }}
/>
</AnimatePresence>
{tabs.map((tab) => (
<motion.button
key={tab.id}
className={`relative z-10 rounded-full px-4 py-2 text-sm font-medium transition-colors duration-300 ${
activeTab === tab.id ? "text-black" : "text-gray-500"
} ${touchedTab === tab.id ? "blur-sm" : ""}`}
onClick={() => handleTabClick(tab.id)}
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
Rudra-Sankha-Sinhamahapatra marked this conversation as resolved.
Show resolved Hide resolved
>
<span className="flex items-center space-x-2">
Rudra-Sankha-Sinhamahapatra marked this conversation as resolved.
Show resolved Hide resolved
<span>{tab.icon}</span>
<span className="font-bold">{tab.label}</span>
</span>
</motion.button>
))}
</div>
</div>
);
}
52 changes: 52 additions & 0 deletions content/docs/card/fluid-tabs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Fluid Tabs
description: The component is a sliding animation card
author: RudraSankha
---

<ComponentPreview name="tabs-fluid-tabs--primary" />

## Installation

<Steps>
<Step>Install dependencies</Step>

```bash
npm install framer-motion lucide-react
```

<Step>Update `tailwind.config.js`</Step>

Add the following to your tailwind.config.js file.

```json
module.exports = {
theme: {
extend: {
}
}
}
```

<Step>Run the following command</Step>

It will create a new file `fluid-tabs.tsx` inside the `components/animata/card` directory.

```bash
mkdir -p components/animata/card && touch components/animata/card/fluid-tabs.tsx
```

<Step>Paste the code</Step>{" "}

Open the newly created file and paste the following code:

```jsx file=<rootDir>/animata/card/fluid-tabs.tsx

```

</Steps>

## Credits

Built by [Rudra Sankha Sinhamahapatra](https://github.com/Rudra-Sankha-Sinhamahapatra)
Twitter Handle [Rudra Sankha](https://x.com/RudraSankha)
Loading