Skip to main content

Overview

The Tabs component provides a simple tab interface for navigating between different content sections.

Basic Usage

import { NSTabs } from '@newtonschool/grauity';
import { useState } from 'react';

function MyComponent() {
  const [activeTab, setActiveTab] = useState(0);

  return (
    <>
      <NSTabs
        tabItems={['Weekly', 'Monthly', 'Yearly']}
        initialActiveTab={activeTab}
        onTabFocusChange={(index) => setActiveTab(index)}
      />
      
      {activeTab === 0 && <div>Weekly content</div>}
      {activeTab === 1 && <div>Monthly content</div>}
      {activeTab === 2 && <div>Yearly content</div>}
    </>
  );
}

Props

tabItems
React.ReactNode[]
required
Array of tab items to be rendered. Each item can be any React node (string, element, etc.).
onTabFocusChange
function
Callback function called when the focused tab changes.Signature: (tabIndex: number) => void
initialActiveTab
number
default:0
Index of the tab that should be initially focused.
backgroundColor
string
Background color for the tab container.
focusBackgroundColor
string
Background color for the active/focused tab.
focusColor
string
Text color for the active/focused tab.
color
string
Default text color for inactive tabs.
className
string
default:""
Additional CSS class name for styling.

String Tabs

<Tabs
  tabItems={['Tab 1', 'Tab 2', 'Tab 3']}
  onTabFocusChange={(index) => console.log('Tab changed:', index)}
/>

With Custom Components

<Tabs
  tabItems={[
    <span><NSIcon name="calendar" /> Weekly</span>,
    <span><NSIcon name="chart" /> Monthly</span>,
  ]}
  onTabFocusChange={(index) => handleTabChange(index)}
/>

Custom Colors

<Tabs
  tabItems={['Design', 'Development', 'Marketing']}
  backgroundColor="#f5f5f5"
  focusBackgroundColor="#1976d2"
  focusColor="#ffffff"
  color="#666666"
/>

Controlled Tabs

function ControlledTabs() {
  const [currentTab, setCurrentTab] = useState(0);

  return (
    <div>
      <Tabs
        tabItems={['Profile', 'Settings', 'Notifications']}
        initialActiveTab={currentTab}
        onTabFocusChange={setCurrentTab}
      />
      
      <Button onClick={() => setCurrentTab(1)}>
        Go to Settings
      </Button>
      
      <div>
        {currentTab === 0 && <ProfileContent />}
        {currentTab === 1 && <SettingsContent />}
        {currentTab === 2 && <NotificationsContent />}
      </div>
    </div>
  );
}

Styled Container

<div style={{ width: '400px' }}>
  <Tabs
    tabItems={['Option A', 'Option B', 'Option C']}
    className="custom-tabs"
  />
</div>

Build docs developers (and LLMs) love