Open History to see the last commit
1import { cva } from 'class-variance-authority'
2import { Tabs as TabsPrimitive } from '@ark-ui/solid'
3import { splitProps } from 'solid-js'
4import type { VariantProps } from 'class-variance-authority'
5import { cn } from '@/utils'
6β
7/**
8 * The root container that coordinates tab selection and orientation.
9 *
10 * @example
11 * ```tsx
12 * <Tabs defaultValue="overview">
13 * <TabsList>
14 * <TabsTrigger value="overview">Overview</TabsTrigger>
15 * <TabsTrigger value="settings">Settings</TabsTrigger>
16 * </TabsList>
17 * <TabsContent value="overview">Overview content</TabsContent>
18 * <TabsContent value="settings">Settings content</TabsContent>
19 * </Tabs>
20 * ```
21 */
22function Tabs(props: TabsPrimitive.RootProps) {
23 const [local, others] = splitProps(props, ['class', 'orientation'])
24β
25 const orientation = () => local.orientation ?? 'horizontal'
26 return (
27 <TabsPrimitive.Root
28 data-slot="tabs"
29 orientation={orientation()}
30 class={cn('group/tabs flex gap-2 data-horizontal:flex-col', local.class)}
31 {...others}
32 />
33 )
34}
35β
36const tabsListVariants = cva(
37 'group/tabs-list inline-flex w-fit items-center justify-center rounded-lg p-[3px] text-muted-foreground group-data-horizontal/tabs:h-8 group-data-vertical/tabs:h-fit group-data-vertical/tabs:flex-col data-[variant=line]:rounded-none',
38 {
39 variants: {
40 variant: {
41 default: 'bg-muted',
42 line: 'gap-1 bg-transparent',
43 },
44 },
45 defaultVariants: {
46 variant: 'default',
47 },
48 },
49)
50β
51type TabsListProps = TabsPrimitive.ListProps &
52 VariantProps<typeof tabsListVariants>
53β
54/**
55 * A styled list that contains the selectable TabsTrigger controls.
56 *
57 * @example
58 * ```tsx
59 * <Tabs defaultValue="overview">
60 * <TabsList>
61 * <TabsTrigger value="overview">Overview</TabsTrigger>
62 * <TabsTrigger value="settings">Settings</TabsTrigger>
63 * </TabsList>
64 * <TabsContent value="overview">Overview content</TabsContent>
65 * <TabsContent value="settings">Settings content</TabsContent>
66 * </Tabs>
67 * ```
68 */
69function TabsList(props: TabsListProps) {
70 const [local, others] = splitProps(props, ['class', 'variant'])
71β
72 const variant = () => local.variant ?? 'default'
73β
74 return (
75 <TabsPrimitive.List
76 data-slot="tabs-list"
77 data-variant={variant()}
78 class={cn(tabsListVariants({ variant: variant() }), local.class)}
79 {...others}
80 />
81 )
82}
83β
84/**
85 * A selectable tab control associated with a matching TabsContent value.
86 *
87 * @example
88 * ```tsx
89 * <Tabs defaultValue="overview">
90 * <TabsList>
91 * <TabsTrigger value="overview">Overview</TabsTrigger>
92 * <TabsTrigger value="settings">Settings</TabsTrigger>
93 * </TabsList>
94 * <TabsContent value="overview">Overview content</TabsContent>
95 * <TabsContent value="settings">Settings content</TabsContent>
96 * </Tabs>
97 * ```
98 */
99function TabsTrigger(props: TabsPrimitive.TriggerProps) {
100 const [local, others] = splitProps(props, ['class'])
101 return (
102 <TabsPrimitive.Trigger
103 data-slot="tabs-trigger"
104 class={cn(
105 "relative inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-md border border-transparent px-1.5 py-0.5 text-sm font-medium whitespace-nowrap text-foreground/60 transition-all group-data-vertical/tabs:w-full group-data-vertical/tabs:justify-start hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-1 focus-visible:outline-ring disabled:pointer-events-none disabled:opacity-50 has-data-[icon=inline-end]:pr-1 has-data-[icon=inline-start]:pl-1 dark:text-muted-foreground dark:hover:text-foreground group-data-[variant=default]/tabs-list:data-selected:shadow-sm group-data-[variant=line]/tabs-list:data-selected:shadow-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
106 'group-data-[variant=line]/tabs-list:bg-transparent group-data-[variant=line]/tabs-list:data-selected:bg-transparent dark:group-data-[variant=line]/tabs-list:data-selected:border-transparent dark:group-data-[variant=line]/tabs-list:data-selected:bg-transparent',
107 'data-selected:bg-background data-selected:text-foreground dark:data-selected:border-input dark:data-selected:bg-input/30 dark:data-selected:text-foreground',
108 'after:absolute after:bg-foreground after:opacity-0 after:transition-opacity group-data-horizontal/tabs:after:inset-x-0 group-data-horizontal/tabs:after:bottom-[-5px] group-data-horizontal/tabs:after:h-0.5 group-data-vertical/tabs:after:inset-y-0 group-data-vertical/tabs:after:-right-1 group-data-vertical/tabs:after:w-0.5 group-data-[variant=line]/tabs-list:data-selected:after:opacity-100',
109 local.class,
110 )}
111 {...others}
112 />
113 )
114}
115β
116/**
117 * The content panel shown when its value matches the selected tab.
118 *
119 * @example
120 * ```tsx
121 * <Tabs defaultValue="overview">
122 * <TabsList>
123 * <TabsTrigger value="overview">Overview</TabsTrigger>
124 * <TabsTrigger value="settings">Settings</TabsTrigger>
125 * </TabsList>
126 * <TabsContent value="overview">Overview content</TabsContent>
127 * <TabsContent value="settings">Settings content</TabsContent>
128 * </Tabs>
129 * ```
130 */
131function TabsContent(props: TabsPrimitive.ContentProps) {
132 const [local, others] = splitProps(props, ['class'])
133β
134 return (
135 <TabsPrimitive.Content
136 data-slot="tabs-content"
137 class={cn('flex-1 text-sm outline-none', local.class)}
138 {...others}
139 />
140 )
141}
142β
143export { Tabs, TabsList, TabsTrigger, TabsContent }