Tabs
A set of layered sections of content that display one panel at a time.
Installation
bash npx @moe/cli add tabs Install the following dependency:
npx expo install @rn-primitives/tabsCopy/paste the following code to @/components/ui/tabs.tsx
import * as TabsPrimitive from "@rn-primitives/tabs";
import * as React from "react";
import { cn } from "@/lib/utils";
import { TextClassContext } from "@/components/ui/text";
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(
"web:inline-flex h-10 native:h-12 items-center justify-center rounded-md bg-muted p-1 native:px-1.5",
className,
)}
{...props}
/>
));
TabsList.displayName = TabsPrimitive.List.displayName;
const TabsTrigger = React.forwardRef<
React.ElementRef<typeof TabsPrimitive.Trigger>,
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger>
>(({ className, ...props }, ref) => {
const { value } = TabsPrimitive.useRootContext();
return (
<TextClassContext.Provider
value={cn(
"text-sm native:text-base font-medium text-muted-foreground web:transition-all",
value === props.value && "text-foreground",
)}
>
<TabsPrimitive.Trigger
ref={ref}
className={cn(
"inline-flex items-center justify-center shadow-none web:whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium web:ring-offset-background web:transition-all web:focus-visible:outline-none web:focus-visible:ring-2 web:focus-visible:ring-ring web:focus-visible:ring-offset-2",
props.disabled && "web:pointer-events-none opacity-50",
props.value === value &&
"bg-background shadow-lg shadow-foreground/5",
className,
)}
{...props}
/>
</TextClassContext.Provider>
);
});
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(
"web:ring-offset-background web:focus-visible:outline-none web:focus-visible:ring-2 web:focus-visible:ring-ring web:focus-visible:ring-offset-2",
className,
)}
{...props}
/>
));
TabsContent.displayName = TabsPrimitive.Content.displayName;
export { Tabs, TabsContent, TabsList, TabsTrigger };Usage
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | - | The controlled value |
onValueChange | (value: string) => void | - | Event handler called when value changes |
defaultValue | string | - | The default value |