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

[Tabs]: how to get active state of trigger? #3255

Open
da1z opened this issue Nov 26, 2024 · 2 comments
Open

[Tabs]: how to get active state of trigger? #3255

da1z opened this issue Nov 26, 2024 · 2 comments

Comments

@da1z
Copy link

da1z commented Nov 26, 2024

is it possible to get active state of trigger somehow other then data attribute? trying to add active state with frame motion and need conditional render of active indicator

@fadlytanjung
Copy link

Hi, have you tried using props onChangeValue and value? Here example

import { Tabs, TabsList, TabsTrigger, TabsContent } from '@radix-ui/react-tabs';
import { motion } from 'framer-motion';
import { useState } from 'react';

export default function AnimatedTabs() {
  const [activeTab, setActiveTab] = useState('tab1'); // Initial active tab

  return (
    <Tabs value={activeTab} onValueChange={setActiveTab}>
      <TabsList className="tabs-list">
        <div className="tabs-container">
          <TabsTrigger className="tab-trigger" value="tab1">
            Tab 1
          </TabsTrigger>
          <TabsTrigger className="tab-trigger" value="tab2">
            Tab 2
          </TabsTrigger>
          <TabsTrigger className="tab-trigger" value="tab3">
            Tab 3
          </TabsTrigger>
          {/* Active indicator */}
          <motion.div
            className="active-indicator"
            layout
            transition={{ type: 'spring', stiffness: 300, damping: 30 }}
            style={{
              left: `${100 * (['tab1', 'tab2', 'tab3'].indexOf(activeTab))}%`,
            }}
          />
        </div>
      </TabsList>
      <TabsContent value="tab1">Content for Tab 1</TabsContent>
      <TabsContent value="tab2">Content for Tab 2</TabsContent>
      <TabsContent value="tab3">Content for Tab 3</TabsContent>

      <style jsx>{`
        .tabs-container {
          position: relative;
          display: flex;
        }
        .tab-trigger {
          flex: 1;
          text-align: center;
          padding: 8px 16px;
          cursor: pointer;
        }
        .active-indicator {
          position: absolute;
          bottom: 0;
          height: 4px;
          width: 100%;
          background-color: blue;
        }
      `}</style>
    </Tabs>
  );
}

Or do you mean something else by your question?

@da1z
Copy link
Author

da1z commented Dec 12, 2024

I want to keep my tabs in uncontrolled state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants