Next.js Discord

Discord Forum

Extending the Tab component from shadcn to work with different pages?

Unanswered
Skipjack tuna posted this in #help-forum
Open in Discord
Skipjack tunaOP
So the original Tabs component in shadcn allows for different content sections. But what I want it to do is to display each tab as a page, and have the root Tabs component as a layout. So for example (route/layout.tsx):
<Tabs defaultValue="overview" className="w-full flex flex-col gap-4">
          <TabsList className="grid grid-cols-4 w-fit">
            <TabsTrigger value="overview">Overview</TabsTrigger>
            <TabsTrigger value="services">Services</TabsTrigger>
            <TabsTrigger value="bookings">Bookings</TabsTrigger>
            <TabsTrigger value="profile">Profile</TabsTrigger>
          </TabsList>
          {children}
        </Tabs>

and then an inidividual tab (route/overview):
  return (
    <TabsContent value="overview">
      <Overview
        businessUser={businessUser as BusinessUser}
        user={userData as User}
      />
    </TabsContent>
  );


How would I extend Tabs to allow this? I want each tabstrigger to push a route so it can change the page under the layout

5 Replies

Skipjack tunaOP
Here's how Im trying to structure it
well... you could try duplicating the code but keeping the styling and using next/link for the values..? (idk how well it would work tho)
Skipjack tunaOP
Yeah so
"use client"

import * as React from "react"
import * as TabsPrimitive from "@radix-ui/react-tabs"

import { cn } from "@/lib/utils"

const Tabs = TabsPrimitive.Root

const TabsList = React.forwardRef<
  React.ElementRef<typeof TabsPrimitive.List>,
  React.ComponentPropsWithoutRef<typeof TabsPrimitive.List>
>(({ className, ...props }, ref) => (
  <TabsPrimitive.List
    ref={ref}
    className={cn(
      "inline-flex h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground",
      className
    )}
    {...props}
  />
))
TabsList.displayName = TabsPrimitive.List.displayName

const TabsTrigger = React.forwardRef<
  React.ElementRef<typeof TabsPrimitive.Trigger>,
  React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger>
>(({ className, ...props }, ref) => (
  <TabsPrimitive.Trigger
    ref={ref}
    className={cn(
      "inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-background data-[state=active]:text-foreground data-[state=active]:shadow-sm",
      className
    )}
    {...props}
  />
))
TabsTrigger.displayName = TabsPrimitive.Trigger.displayName

const TabsContent = React.forwardRef<
  React.ElementRef<typeof TabsPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof TabsPrimitive.Content>
>(({ className, ...props }, ref) => (
  <TabsPrimitive.Content
    ref={ref}
    className={cn(
      "mt-2 ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
      className
    )}
    {...props}
  />
))
TabsContent.displayName = TabsPrimitive.Content.displayName

export { Tabs, TabsList, TabsTrigger, TabsContent }

This is the tabs component from shadcn, im just wondering how I would extend it
well, you wouldn't need much of the same code, so id assume you can just use the className and a div with next/link (and use usePathname to know which is selected)
but i am not sure