underline-tabs.tsx
Overview
The underline-tabs.tsx file defines a set of React components that implement a stylized tab interface with an underline indicator. It leverages the Radix UI Tabs Primitive for accessible tab behavior and composes it with custom styles and UI components from a local UI library (../ui/tabs). The main purpose of this file is to provide reusable, styled tab components that visually emphasize the active tab by underlining it.
These components are designed for integration into larger React applications where tabbed navigation or content switching is needed, and they focus on a minimalist underline design consistent with modern UI patterns.
Components
1. UnderlineTabsList
Description
A React forwardRef component that renders the container for the tab triggers (the clickable tab headers). It uses the TabsList component from the local UI library and applies custom styles to create a horizontal tab list with an underline style for the active tab.
Declaration
export const UnderlineTabsList: React.ForwardRefExoticComponent<
React.ComponentPropsWithoutRef<typeof TabsPrimitive.List> & React.RefAttributes<React.ElementRef<typeof TabsPrimitive.List>>
>
Parameters
className(optional): Additional CSS class names to customize styling....props: Other props passed down to the underlyingTabsListcomponent.ref: React ref forwarded to the underlyingTabsListelement.
Styling Highlights
text-foreground: Applies foreground text color.h-auto gap-2 rounded-none border-b bg-transparent px-0 py-1: Styles the tabs list with no rounding, a bottom border (underline effect), and padding.Merges any additional
classNamepassed via props.
Usage Example
<UnderlineTabsList>
{/* Tab triggers go here */}
</UnderlineTabsList>
2. UnderlineTabsTrigger
Description
A React forwardRef component rendering an individual tab trigger (clickable tab title). It wraps the TabsTrigger component from the local UI library and applies underline styling to indicate the active tab.
Declaration
export const UnderlineTabsTrigger: React.ForwardRefExoticComponent<
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger> & React.RefAttributes<React.ElementRef<typeof TabsPrimitive.Trigger>>
>
Parameters
className(optional): Additional CSS class names....props: Other props passed toTabsTrigger.ref: React ref forwarded to the underlyingTabsTriggerelement.
Styling Highlights
Hover states change background and text color (
hover:bg-accent,hover:text-foreground).Active state is visually indicated by an underline using CSS pseudo-elements and data attributes:
data-[state=active]:after:bg-primaryapplies a primary color underline.The underline is positioned absolutely at the bottom of the tab.
Transitions and shadows are disabled when active for a clean underline effect.
Usage Example
<UnderlineTabsTrigger value="tab1">Tab 1</UnderlineTabsTrigger>
3. Re-exports from ../ui/tabs
The file also exports the following components under new names to maintain a consistent naming scheme:
TabsasUnderlineTabsTabsContentasUnderlineTabsContent
These are imported from the local UI library and represent the main Tabs container and the content area for each tab, respectively. They are not redefined here but are essential for composing the full tab interface.
Usage Example
<UnderlineTabs defaultValue="tab1">
<UnderlineTabsList>
<UnderlineTabsTrigger value="tab1">Tab 1</UnderlineTabsTrigger>
<UnderlineTabsTrigger value="tab2">Tab 2</UnderlineTabsTrigger>
</UnderlineTabsList>
<UnderlineTabsContent value="tab1">
Content for Tab 1
</UnderlineTabsContent>
<UnderlineTabsContent value="tab2">
Content for Tab 2
</UnderlineTabsContent>
</UnderlineTabs>
Implementation Details
React ForwardRef: Both
UnderlineTabsListandUnderlineTabsTriggeruseReact.forwardRefto forward refs to the underlying Radix UI components. This is important for accessibility and integration with other libraries or testing tools.Styling with
cnutility: Thecnfunction (likely a shorthand for "classNames") is used to concatenate and conditionally apply CSS classes. This promotes modular and reusable styling.Use of Radix UI Tabs Primitives: The Radix UI primitives ensure that tabs are accessible out-of-the-box, handling keyboard navigation, focus management, and ARIA attributes automatically.
CSS Data Attributes: Styling relies on Radix's
data-stateattributes (data-[state=active]) to style active tabs dynamically without additional JavaScript.Positioning of Underline: The underline effect on the active tab is implemented using the
::afterpseudo-element, absolutely positioned at the bottom of the tab trigger with a small height (h-0.5).
Interaction with Other Parts of the System
Local UI Library (
../ui/tabs): This file depends on a UI library providing base tab components (Tabs,TabsContent,TabsList,TabsTrigger). These components likely wrap Radix primitives with base styles and behavior.Utility
cnFunction: The file imports a utility functioncnfrom@/lib/utilswhich is used for CSS class concatenation. This utility is likely shared across the project for consistent class name handling.Radix UI Tabs Primitives: The file imports types and base components from
@radix-ui/react-tabs, using this library for accessible tab functionality.Usage Context: These components are intended to be used wherever tabbed navigation or content is required, enabling consistent styling and behavior across the application.
Visual Diagram
componentDiagram
component UnderlineTabs {
<<re-exported>>
+Tabs
}
component UnderlineTabsContent {
<<re-exported>>
+TabsContent
}
component UnderlineTabsList {
+forwardRef
+className: string
+props
}
component UnderlineTabsTrigger {
+forwardRef
+className: string
+props
}
UnderlineTabsList --> TabsList
UnderlineTabsTrigger --> TabsTrigger
TabsList --> TabsPrimitive.List
TabsTrigger --> TabsPrimitive.Trigger
Summary
The underline-tabs.tsx file is a focused React module that composes accessible tab components with a custom underline style for active tabs. It extends Radix UI primitives and a local UI library to provide reusable, forwardRef-enabled tab components suitable for modern, accessible React applications. The styling approach leverages CSS pseudo-elements and data attributes to elegantly indicate active tabs without extra JavaScript logic.
By exporting both the stylized tab list and triggers along with the tabs container and content, this file encapsulates the underline tab pattern, promoting consistency and reusability throughout the project.