import * as stylex from "@stylexjs/stylex";
import {
Tabs as UiSdkTabs,
TabsIndicator as UiSdkTabsIndicator,
TabsList as UiSdkTabsList,
TabsPanel as UiSdkTabsPanel,
TabsTab as UiSdkTabsTab,
} from "@ui-sdk/react/tabs";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import {
defineSlotRecipe,
fromMap,
resolveSlotStyles,
} from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { cursor } from '@stylexblocks/ui/tokens/cursor.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { semanticShadows } from '@stylexblocks/ui/semantic-tokens/shadows.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
const tabsRoot = stylex.create({
base: {
'--tabs-trigger-radius': semanticRadii.control,
position: 'relative',
},
})
const tabsRootSizes = stylex.create({
xs: {
'--tabs-height': sizes.controlXs,
'--tabs-content-padding': spacing._2,
},
sm: {
'--tabs-height': sizes.controlSm,
'--tabs-content-padding': spacing._3,
},
md: {
'--tabs-height': sizes.controlMd,
'--tabs-content-padding': spacing._4,
},
lg: {
'--tabs-height': sizes.controlLg,
'--tabs-content-padding': spacing._4_5,
},
})
const tabsList = stylex.create({
base: {
display: 'inline-flex',
position: 'relative',
isolation: 'isolate',
'--tabs-indicator-shadow': semanticShadows.xs,
'--tabs-indicator-bg': semanticColors.bg,
minHeight: 'var(--tabs-height)',
},
})
const tabsListVariants = stylex.create({
line: {
display: 'flex',
borderColor: semanticColors.border,
},
pills: {
gap: spacing._2,
},
ghost: {
gap: spacing._2,
},
enclosed: {
backgroundColor: semanticColors.bgMuted,
boxShadow: semanticShadows.inset,
borderRadius: radii.md,
borderWidth: '1px',
padding: spacing._1,
minHeight: 'calc(var(--tabs-height) - 4px)',
},
outline: {
'--line-thickness': '1px',
'--line-offset': 'calc(var(--line-thickness) * -1)',
borderColor: semanticColors.border,
display: 'flex',
},
})
const tabsListFitteds = stylex.create({
true: {
display: 'flex',
},
})
const tabsListJustifys = stylex.create({
start: {
justifyContent: 'flex-start',
},
center: {
justifyContent: 'center',
},
end: {
justifyContent: 'flex-end',
},
})
const tabsTrigger = stylex.create({
base: {
outline: 'none',
minWidth: 'var(--tabs-height)',
height: 'var(--tabs-height)',
display: 'flex',
alignItems: 'center',
fontWeight: fontWeights.medium,
position: 'relative',
cursor: {
default: cursor.button,
':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
},
gap: spacing._2,
zIndex: {
default: null,
':focus-visible': 1,
},
outlineWidth: {
default: null,
':focus-visible': '1px',
},
outlineOffset: {
default: null,
':focus-visible': '2px',
},
outlineStyle: {
default: null,
':focus-visible': 'solid',
},
outlineColor: {
default: null,
':focus-visible': colorPalette.focusRing,
},
opacity: {
default: null,
':is(:disabled, [disabled], [data-disabled])': 0.5,
},
},
})
const tabsTriggerSizes = stylex.create({
xs: {
paddingBlock: spacing._1,
paddingInline: spacing._2,
fontSize: fontSizes.xs,
lineHeight: lineHeights.xs,
},
sm: {
paddingBlock: spacing._1,
paddingInline: spacing._3,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
},
md: {
paddingBlock: spacing._2,
paddingInline: spacing._4,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
},
lg: {
paddingBlock: spacing._2,
paddingInline: spacing._4_5,
fontSize: fontSizes.md,
lineHeight: lineHeights.md,
},
})
const tabsTriggerVariants = stylex.create({
line: {
color: {
default: semanticColors.fgMuted,
':hover': semanticColors.fgSubtle,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
semanticColors.fg,
},
boxShadow: {
default: null,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])': `inset 0 -2px 0 0 ${colorPalette.solid}`,
},
backgroundColor: {
default: null,
':is(:disabled, [disabled], [data-disabled])': {
default: null,
':active': 'initial',
},
},
},
subtle: {
borderRadius: 'var(--tabs-trigger-radius)',
color: {
default: semanticColors.fgMuted,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
colorPalette.fg,
},
backgroundColor: {
default: null,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
colorPalette.muted,
},
},
pills: {
borderRadius: 'var(--tabs-trigger-radius)',
color: {
default: semanticColors.fgMuted,
':hover': semanticColors.fgSubtle,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
semanticColors.fg,
},
borderWidth: '1px',
borderColor: semanticColors.borderEmphasized,
backgroundColor: {
default: null,
':hover': semanticColors.bgMuted,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
'var(--tabs-indicator-bg)',
},
boxShadow: {
default: null,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
'var(--tabs-indicator-shadow)',
},
},
ghost: {
borderRadius: 'var(--tabs-trigger-radius)',
color: {
default: semanticColors.fgSubtle,
':hover': colorPalette.fg,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
colorPalette.fg,
},
backgroundColor: {
default: null,
':hover': colorPalette.muted,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
colorPalette.muted,
},
},
enclosed: {
justifyContent: 'center',
color: {
default: semanticColors.fgMuted,
':hover': semanticColors.fgSubtle,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
semanticColors.fg,
},
borderRadius: `calc(${radii.md} - 1px)`,
backgroundColor: {
default: null,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
'var(--tabs-indicator-bg)',
},
boxShadow: {
default: null,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
'var(--tabs-indicator-shadow)',
},
},
outline: {
color: {
default: semanticColors.fgMuted,
':hover': semanticColors.fgSubtle,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
semanticColors.fg,
},
borderWidth: '1px',
borderColor: 'transparent',
backgroundColor: {
default: null,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
'var(--tabs-indicator-bg)',
},
boxShadow: {
default: null,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
'var(--tabs-indicator-shadow)',
},
},
plain: {
color: {
default: semanticColors.fgMuted,
':is([data-selected], [aria-selected="true"], [data-state="active"], [data-active])':
semanticColors.fg,
},
borderRadius: 'var(--tabs-trigger-radius)',
},
})
const tabsTriggerFitteds = stylex.create({
true: {
flex: 1,
textAlign: 'center',
justifyContent: 'center',
},
})
const tabsContent = stylex.create({
base: {
outlineWidth: {
default: null,
':focus-visible': 'var(--focus-ring-width, 0)',
},
outlineOffset: {
default: null,
':focus-visible': '0px',
},
outlineStyle: {
default: null,
':focus-visible': 'solid',
},
outlineColor: {
default: null,
':focus-visible': colorPalette.focusRing,
},
},
})
const tabsIndicator = stylex.create({
base: {
width: 'var(--width)',
height: 'var(--height)',
borderRadius: 'var(--tabs-indicator-radius)',
backgroundColor: 'var(--tabs-indicator-bg)',
boxShadow: 'var(--tabs-indicator-shadow)',
zIndex: -1,
},
})
export type TabsFitted = keyof typeof tabsListFitteds
export type TabsJustify = keyof typeof tabsListJustifys
export type TabsSize = keyof typeof tabsRootSizes
export type TabsVariant = keyof typeof tabsListVariants
// What the generated recipe doesn't carry: the tab's browser-button reset,
// the panel padding fed by the root's `--tabs-content-padding`, and the
// selected-tab treatment per variant (palette-aware). StyleX keys those off
// `data-selected` / `data-active` — UI SDK stamps `data-selected` on the
// selected tab; `data-active` is kept so the three engines share this block.
const chrome = stylex.create({
triggerReset: {
appearance: "none",
borderWidth: 0,
backgroundColor: "transparent",
},
content: { padding: "var(--tabs-content-padding)" },
});
const tabsTriggerSelected = stylex.create({
line: {
color: {
default: null,
":is([data-selected], [data-active])": semanticColors.fg,
},
boxShadow: {
default: null,
":is([data-selected], [data-active])": `inset 0 -2px 0 0 ${colorPalette.solid}`,
},
},
enclosed: {
backgroundColor: {
default: null,
":is([data-selected], [data-active])": "var(--tabs-indicator-bg)",
},
color: {
default: null,
":is([data-selected], [data-active])": semanticColors.fg,
},
boxShadow: {
default: null,
":is([data-selected], [data-active])": "var(--tabs-indicator-shadow)",
},
},
outline: {
backgroundColor: {
default: null,
":is([data-selected], [data-active])": "var(--tabs-indicator-bg)",
},
color: {
default: null,
":is([data-selected], [data-active])": semanticColors.fg,
},
boxShadow: {
default: null,
":is([data-selected], [data-active])": "var(--tabs-indicator-shadow)",
},
},
pills: {
backgroundColor: {
default: null,
":is([data-selected], [data-active])": "var(--tabs-indicator-bg)",
},
color: {
default: null,
":is([data-selected], [data-active])": semanticColors.fg,
},
boxShadow: {
default: null,
":is([data-selected], [data-active])": "var(--tabs-indicator-shadow)",
},
},
ghost: {
backgroundColor: {
default: null,
":is([data-selected], [data-active])": colorPalette.muted,
},
color: {
default: null,
":is([data-selected], [data-active])": colorPalette.fg,
},
},
subtle: {
backgroundColor: {
default: null,
":is([data-selected], [data-active])": colorPalette.muted,
},
color: {
default: null,
":is([data-selected], [data-active])": colorPalette.fg,
},
},
plain: {
color: {
default: null,
":is([data-selected], [data-active])": semanticColors.fg,
},
},
});
// defineSlotRecipe validates the shape at compile time: default values must
// exist in the axis maps, and slot keys must be `styles` or a declared axis.
// The list's variant map is sparse (no `subtle`/`plain`) — the mechanical
// resolver skips axis values a slot doesn't style.
const tabsSlotRecipe = defineSlotRecipe({
slots: {
root: { styles: tabsRoot, sizes: tabsRootSizes },
list: { styles: tabsList, variants: tabsListVariants },
trigger: {
styles: tabsTrigger,
sizes: tabsTriggerSizes,
variants: tabsTriggerVariants,
},
content: { styles: tabsContent },
indicator: { styles: tabsIndicator },
},
defaultVariants: { size: "md", variant: "line" },
});
// UI SDK's indicator only measures: it publishes the selected tab's box as
// `--active-tab-*` and leaves placement to CSS. The recipe sizes from Ark's
// `--width`/`--height`, so feed those and position it inside the list. Its
// variables are unset until `data-visible`, so hide it before then, and skip
// the transition on the first placement.
const indicatorPlacement = stylex.create({
base: {
"--width": "var(--active-tab-width)",
"--height": "var(--active-tab-height)",
position: "absolute",
top: "var(--active-tab-top)",
left: "var(--active-tab-left)",
visibility: {
default: "hidden",
":is([data-visible])": "visible",
},
transitionProperty: {
default: "top, left, width, height",
':is([data-activation-direction="none"])': "none",
},
transitionDuration: "200ms",
transitionTimingFunction: "ease",
},
});
const { withProvider, withContext } = createSlotRecipeContext(tabsSlotRecipe);
const StyledRoot = withProvider(UiSdkTabs, "root", "TabsRoot");
type RawTabsRootProps = React.ComponentProps<typeof StyledRoot>;
type TabsOnValueChange = NonNullable<RawTabsRootProps["onValueChange"]>;
type TabsChangeDetails = Parameters<TabsOnValueChange>[1];
function Root<T extends string = string>(
props: Omit<RawTabsRootProps, "value" | "defaultValue" | "onValueChange"> & {
value?: T | null;
defaultValue?: T | null;
onValueChange?: (value: T, details: TabsChangeDetails) => void;
},
) {
const { onValueChange, ...rest } = props;
return (
<StyledRoot
{...(rest as RawTabsRootProps)}
onValueChange={
onValueChange
? (next, details) => {
if (next !== null) onValueChange(next as T, details);
}
: undefined
}
/>
);
}
Root.displayName = "TabsRoot";
/**
* Tabs — UI SDK's tabs wearing the generated recipe. `<Tabs.Root variant
* size>` resolves the recipe once and every part inherits it through
* context; behavior (roving focus, Home/End, automatic or manual
* activation, controlled value) is UI SDK's. Tabs and panels carry
* `data-selected` / `data-disabled`, which the recipe already matches.
* Composition per part via `render`.
*/
export const Tabs = {
Root,
List: withContext(UiSdkTabsList, "list", "TabsList"),
Tab: withContext(
UiSdkTabsTab,
(v) => [
chrome.triggerReset,
...resolveSlotStyles(tabsSlotRecipe.slots.trigger, v),
fromMap(tabsTriggerSelected, v.variant),
],
"TabsTab",
),
Panel: withContext(
UiSdkTabsPanel,
() => [tabsContent.base, chrome.content],
"TabsPanel",
),
Indicator: withContext(
UiSdkTabsIndicator,
() => [tabsIndicator.base, indicatorPlacement.base],
"TabsIndicator",
),
};