Magnet Tabs
MagnetTabs is a stylish tab navigation component for switching between sections in a user interface. It’s ideal for dashboards, settings pages, admin panels, or product views—anywhere content needs to be organized into tabs for better usability.
Preview
Tab 1
Tab 2
Tab 3
Tab 4
Tab 5
Tab 6
Tab 7
Tab 8
Tab 9
Tab 10
Follow below steps 👇🏻
Install dependencies
1npm i framer-motion
Component
Create a file magnet-tabs.tsx in your components folder and paste this code
1'use client';23import React from 'react';4import { motion } from 'framer-motion';56interface MagnetTabsProps {7slug: string;8options: string[];9onSelect: (option: string) => void;10activeTab: string;11}1213const MagnetTabs = ({ slug, options, onSelect, activeTab }: MagnetTabsProps) => {14const [hovered, setHovered] = React.useState<string | undefined>(undefined);1516return (17<div className="flex items-start justify-start">18<ul className="flex border-[1px] border-black/10 border-b-0">19{options.map((option) => {20const isActive = activeTab === option;21return (22<li23onMouseEnter={() => setHovered(option)}24onMouseLeave={() => setHovered(undefined)}25key={slug + option}26onClick={() => onSelect(option)}27className="relative cursor-pointer shrink-0"28>29<p30className={`z-10 relative px-2 py-1 transition-all ${31isActive ? 'opacity-100' : 'opacity-50 hover:opacity-100'32}`}33>34{option}35</p>3637{isActive && (38<motion.div39layout40layoutId={slug + 'magnet'}41transition={{ duration: 0.3, ease: 'backOut' }}42className="w-full h-1 absolute bottom-full left-0 bg-red-500 rounded-sm"43/>44)}4546{(hovered === option || (hovered === undefined && isActive)) && (47<motion.div48layout49layoutId={slug + 'tab-bar-highlight'}50transition={{ duration: 0.3, ease: 'backOut' }}51className="w-full h-full absolute bottom-0 left-0 bg-white/10 rounded-sm"52/>53)}54</li>55);56})}57</ul>58</div>59);60};6162export default MagnetTabs;
Usage
1import React, { useState } from 'react';2import MagnetTabs from './ui/magnet-tabs';34const MagnetTabsDemo = () => {5const [activeTab, setActiveTab] = useState('Tab 1');67return (8<MagnetTabs9slug="magnet-tabs"10options={[11'Tab 1',12'Tab 2',13'Tab 3',14'Tab 4',15'Tab 5',16'Tab 6',17'Tab 7',18'Tab 8',19'Tab 9',20'Tab 10'21]}22onSelect={(option) => setActiveTab(option)}23activeTab={activeTab}24/>25);26};2728export default MagnetTabsDemo;
⭐️ Got a question or feedback?
Feel free to reach out!