import * as stylex from "@stylexjs/stylex";
import {
Select as UiSdkSelect,
SelectContent,
SelectGroup,
SelectGroupLabel,
SelectItem,
SelectItemIndicator,
SelectItemText,
SelectLabel,
SelectTrigger,
SelectValue,
type SelectProps,
} from "@ui-sdk/react/select";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import {
defineSlotRecipe,
resolveSlotStyles,
type SlotRecipeVariants,
type StyleOverrideProps,
} from "#lib/recipe-props.ts";
import { semanticShadows } from "@stylexblocks/ui/semantic-tokens/shadows.stylex.ts";
import { selectIndicatorIconVars, selectItemIconVars } from '@stylexblocks/ui/slot-recipes/select.stylex.ts'
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { cursor } from '@stylexblocks/ui/tokens/cursor.stylex.ts'
import { durations } from '@stylexblocks/ui/tokens/durations.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 { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
import { zIndices } from '@stylexblocks/ui/tokens/z-indices.stylex.ts'
const selectRoot = stylex.create({
base: {
display: 'flex',
flexDirection: 'column',
gap: spacing._1_5,
width: sizes.full,
},
})
const selectRootSizes = stylex.create({
xs: {
'--select-trigger-height': sizes.controlXs,
'--select-trigger-padding-x': spacing._2,
},
sm: {
'--select-trigger-height': sizes.controlSm,
'--select-trigger-padding-x': spacing._2_5,
},
md: {
'--select-trigger-height': sizes.controlMd,
'--select-trigger-padding-x': spacing._3,
},
lg: {
'--select-trigger-height': sizes.controlLg,
'--select-trigger-padding-x': spacing._4,
},
})
const selectTrigger = stylex.create({
base: {
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
width: sizes.full,
minHeight: 'var(--select-trigger-height)',
paddingInline: 'var(--select-trigger-padding-x)',
userSelect: 'none',
textAlign: 'start',
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,
},
focusRingWidth: 0,
opacity: {
default: null,
':is(:disabled, [disabled], [data-disabled])': 0.5,
},
cursor: {
default: null,
':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
},
borderColor: {
default: null,
':is([data-invalid], [aria-invalid="true"])': semanticColors.borderError,
':hover': {
default: semanticColors.borderEmphasized,
':focus-visible': 'var(--focus-ring-color)',
},
},
},
})
const selectTriggerSizes = stylex.create({
xs: {
borderRadius: semanticRadii.control,
fontSize: fontSizes.xs,
lineHeight: lineHeights.xs,
gap: spacing._1,
},
sm: {
borderRadius: semanticRadii.control,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
gap: spacing._1,
},
md: {
borderRadius: semanticRadii.control,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
gap: spacing._2,
},
lg: {
borderRadius: semanticRadii.control,
fontSize: fontSizes.md,
lineHeight: lineHeights.md,
paddingBlock: spacing._3,
gap: spacing._2,
},
})
const selectTriggerVariants = stylex.create({
outline: {
backgroundColor: semanticColors.bg,
borderWidth: '1px',
borderStyle: 'solid',
borderColor: {
default: semanticColors.border,
':is([data-invalid], [aria-invalid="true"])': semanticColors.borderError,
':hover': {
default: semanticColors.borderEmphasized,
':focus-visible': 'var(--focus-ring-color)',
},
},
},
subtle: {
borderWidth: '1px',
borderStyle: 'solid',
borderColor: {
default: 'transparent',
':is([data-invalid], [aria-invalid="true"])': semanticColors.borderError,
':hover': {
default: semanticColors.borderEmphasized,
':focus-visible': 'var(--focus-ring-color)',
},
},
backgroundColor: semanticColors.bgMuted,
},
})
const selectIndicatorGroup = stylex.create({
base: {
display: 'flex',
alignItems: 'center',
gap: spacing._1,
position: 'absolute',
right: 0,
top: 0,
bottom: 0,
paddingInline: 'var(--select-trigger-padding-x)',
pointerEvents: 'none',
},
})
const selectIndicator = stylex.create({
base: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
[selectIndicatorIconVars.width]: sizes._3_5,
[selectIndicatorIconVars.height]: sizes._3_5,
},
})
const selectIndicatorSizes = stylex.create({
xs: {
[selectIndicatorIconVars.width]: sizes._3_5,
[selectIndicatorIconVars.height]: sizes._3_5,
},
sm: {
[selectIndicatorIconVars.width]: sizes._4,
[selectIndicatorIconVars.height]: sizes._4,
},
md: {
[selectIndicatorIconVars.width]: sizes._4,
[selectIndicatorIconVars.height]: sizes._4,
},
lg: {
[selectIndicatorIconVars.width]: sizes._5,
[selectIndicatorIconVars.height]: sizes._5,
},
})
const selectIndicatorIcon = stylex.create({
base: {
width: selectIndicatorIconVars.width,
height: selectIndicatorIconVars.height,
},
})
const selectContent = stylex.create({
base: {
outline: 'none',
color: semanticColors.fg,
maxHeight: 'var(--available-height)',
'--menu-z-index': zIndices.layer3,
zIndex: 'calc(var(--menu-z-index) + var(--layer-index, 0))',
overflowY: 'auto',
scrollbarWidth: 'thin',
transformOrigin: 'var(--transform-origin)',
animationName: {
default: null,
':is([data-open], [data-state="open"])': 'fade-in, slide-from-top',
':is([data-closed], [data-state="closed"])': 'fade-out',
},
animationDuration: {
default: null,
':is([data-open], [data-state="open"])': durations.fast,
':is([data-closed], [data-state="closed"])': durations.faster,
},
},
})
const selectContentSizes = stylex.create({
xs: {
padding: spacing._1,
gap: spacing._1,
fontSize: fontSizes.xs,
lineHeight: lineHeights.xs,
},
sm: {
padding: spacing._1,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
},
md: {
padding: spacing._1,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
},
lg: {
padding: spacing._1_5,
fontSize: fontSizes.md,
lineHeight: lineHeights.md,
},
})
const selectItem = stylex.create({
base: {
position: 'relative',
userSelect: 'none',
display: 'flex',
alignItems: 'center',
gap: spacing._2,
cursor: cursor.option,
justifyContent: 'space-between',
flex: 1,
textAlign: 'start',
borderRadius: semanticRadii.control,
pointerEvents: {
default: null,
':is(:disabled, [disabled], [data-disabled])': 'none',
},
opacity: {
default: null,
':is(:disabled, [disabled], [data-disabled])': 0.5,
},
[selectItemIconVars.width]: sizes._4,
[selectItemIconVars.height]: sizes._4,
},
})
const selectItemSizes = stylex.create({
xs: {
paddingBlock: spacing._1,
paddingInline: spacing._2,
},
sm: {
paddingBlock: spacing._1,
paddingInline: spacing._1_5,
},
md: {
paddingBlock: spacing._1_5,
paddingInline: spacing._2,
},
lg: {
paddingBlock: spacing._2,
paddingInline: spacing._3,
},
})
const selectItemIcon = stylex.create({
base: {
width: selectItemIconVars.width,
height: selectItemIconVars.height,
},
})
const selectControl = stylex.create({
base: {
position: 'relative',
},
})
const selectItemText = stylex.create({
base: {
flex: 1,
},
})
const selectItemGroup = stylex.create({
base: {
marginTop: {
default: null,
':first-child': 0,
},
},
})
const selectItemGroupSizes = stylex.create({
sm: {
marginTop: {
default: spacing._1,
':first-child': 0,
},
},
md: {
marginTop: {
default: spacing._1_5,
':first-child': 0,
},
},
lg: {
marginTop: {
default: spacing._2,
':first-child': 0,
},
},
})
const selectItemGroupLabel = stylex.create({
base: {
paddingBlock: spacing._1,
fontWeight: fontWeights.medium,
},
})
const selectItemGroupLabelSizes = stylex.create({
xs: {
paddingBlock: spacing._1,
paddingInline: spacing._2,
},
sm: {
paddingBlock: spacing._1,
paddingInline: spacing._1_5,
},
md: {
paddingBlock: spacing._1_5,
paddingInline: spacing._2,
},
lg: {
paddingBlock: spacing._2,
paddingInline: spacing._3,
},
})
const selectLabel = stylex.create({
base: {
fontWeight: fontWeights.medium,
userSelect: 'none',
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
opacity: {
default: null,
':is(:disabled, [disabled], [data-disabled])': 0.5,
},
cursor: {
default: null,
':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
},
},
})
const selectValueText = stylex.create({
base: {
lineClamp: 1,
maxWidth: '80%',
},
})
const selectItemIndicator = stylex.create({
base: {},
})
const selectItemIndicatorSizes = stylex.create({
md: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
})
export type SelectVariant = keyof typeof selectTriggerVariants
export type SelectSize = keyof typeof selectRootSizes
const selectRecipe = defineSlotRecipe({
slots: {
root: { styles: selectRoot, sizes: selectRootSizes },
trigger: {
styles: selectTrigger,
sizes: selectTriggerSizes,
variants: selectTriggerVariants,
},
indicatorGroup: { styles: selectIndicatorGroup },
indicator: { styles: selectIndicator, sizes: selectIndicatorSizes },
content: { styles: selectContent, sizes: selectContentSizes },
item: { styles: selectItem, sizes: selectItemSizes },
control: { styles: selectControl },
itemText: { styles: selectItemText },
itemGroup: { styles: selectItemGroup, sizes: selectItemGroupSizes },
itemGroupLabel: { styles: selectItemGroupLabel, sizes: selectItemGroupLabelSizes },
label: { styles: selectLabel },
valueText: { styles: selectValueText },
itemIndicator: { styles: selectItemIndicator, sizes: selectItemIndicatorSizes },
},
defaultVariants: { size: "md", variant: "outline" },
});
// chrome the generated recipe leaves to the composition layer: unstyled
// trigger button, popup panel surface, and item hover feedback. UI SDK's
// Select content is a top-layer popover that keeps the browser's own
// popover border, so the panel clears it (padding and background come from
// the recipe and the panel below); `--anchor-width` matches the trigger.
const chrome = stylex.create({
btnReset: {
appearance: "none",
backgroundColor: "transparent",
borderWidth: 0,
borderStyle: "none",
padding: 0,
margin: 0,
fontFamily: "inherit",
fontSize: "inherit",
color: "inherit",
cursor: "pointer",
textAlign: "inherit",
},
anchor: { position: "relative" },
indicatorGroup: {
position: "absolute",
insetInlineEnd: 0,
top: 0,
bottom: 0,
pointerEvents: "none",
},
panel: {
display: {
default: null,
":popover-open": "flex",
},
flexDirection: "column",
borderWidth: 0,
borderStyle: "none",
backgroundColor: semanticColors.bgPanel,
borderRadius: semanticRadii.panel,
boxShadow: semanticShadows.overlay,
minWidth: "var(--anchor-width)",
},
// UI SDK sets `data-highlighted` for pointer hover and keyboard focus alike
itemHover: {
backgroundColor: {
default: null,
":hover": semanticColors.bgMuted,
"[data-highlighted]": semanticColors.bgMuted,
},
outline: "none",
},
});
function ChevronDownIcon(props: React.ComponentProps<"svg">) {
return (
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" {...props}>
<path d="m4 6 4 4 4-4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
</svg>
);
}
function CheckIcon(props: React.ComponentProps<"svg">) {
return (
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" {...props}>
<path d="m3.5 8.5 3 3 6-7" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
</svg>
);
}
const { withProvider, withContext } = createSlotRecipeContext(selectRecipe);
type SelectVariants = SlotRecipeVariants<typeof selectRecipe>;
export type SelectVariantProps = Partial<SelectVariants>;
function SelectRootNode(
props: SelectProps<unknown, boolean | undefined> & {
className?: string;
style?: React.CSSProperties;
},
) {
const { className, style, children, ...rest } = props;
return (
<div className={className} style={style}>
<UiSdkSelect {...(rest as SelectProps<unknown>)}>{children}</UiSdkSelect>
</div>
);
}
const StyledRoot = withProvider(SelectRootNode, "root", "SelectRoot");
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
type ReadonlyListData<I> = I extends Array<infer U> ? ReadonlyArray<U> : I;
/**
* Select — UI SDK's select parts styled with the StyleX slot recipe.
* `<Select.Root variant size>` renders the recipe's root layout element
* around UI SDK's context-only Select; every part inherits the variants
* through context. Content opens in the browser's top layer, positioned
* against the trigger, so there is no Portal or Positioner part. Icon /
* ItemIndicator ship default chevron / check icons. Composition via `render`.
*/
function Root<Value = string, Multiple extends boolean | undefined = false>(
props: DistributiveOmit<SelectProps<Value, Multiple>, "items" | "emptyItems"> & {
items?: ReadonlyListData<SelectProps<Value, Multiple>["items"]>;
emptyItems?: ReadonlyListData<SelectProps<Value, Multiple>["emptyItems"]>;
} & Partial<SelectVariants> &
StyleOverrideProps & { className?: string },
) {
const { items, emptyItems, ...rest } = props;
return (
<StyledRoot
{...(rest as React.ComponentProps<typeof StyledRoot>)}
items={
(Array.isArray(items) ? [...items] : items) as React.ComponentProps<
typeof StyledRoot
>["items"]
}
emptyItems={
(Array.isArray(emptyItems) ? [...emptyItems] : emptyItems) as React.ComponentProps<
typeof StyledRoot
>["emptyItems"]
}
/>
);
}
Root.displayName = "SelectRoot";
const StyledIcon = withContext("span", "indicator", "SelectIcon");
function Icon(props: React.ComponentProps<typeof StyledIcon>) {
const { children, ...rest } = props;
return (
<StyledIcon aria-hidden="true" {...rest}>
{children ?? <ChevronDownIcon {...stylex.props(selectIndicatorIcon.base)} />}
</StyledIcon>
);
}
Icon.displayName = "SelectIcon";
const StyledItemIndicator = withContext(
SelectItemIndicator,
(v) => [
...resolveSlotStyles(selectRecipe.slots.itemIndicator, v),
selectItemIndicatorSizes.md,
],
"SelectItemIndicator",
);
function ItemIndicator(props: React.ComponentProps<typeof StyledItemIndicator>) {
const { children, ...rest } = props;
return (
<StyledItemIndicator {...rest}>
{children ?? <CheckIcon {...stylex.props(selectItemIcon.base)} />}
</StyledItemIndicator>
);
}
ItemIndicator.displayName = "SelectItemIndicator";
const StyledContent = withContext(
SelectContent,
(v) => [...resolveSlotStyles(selectRecipe.slots.content, v), chrome.panel],
"SelectContent",
);
function Content(props: React.ComponentProps<typeof StyledContent>) {
return <StyledContent sideOffset={4} {...props} />;
}
Content.displayName = "SelectContent";
const StyledItem = withContext(
SelectItem,
(v) => [...resolveSlotStyles(selectRecipe.slots.item, v), chrome.itemHover],
"SelectItem",
);
function Item<Value = string>(
props: Omit<React.ComponentProps<typeof SelectItem<Value>>, "style"> &
StyleOverrideProps,
) {
return <StyledItem {...(props as React.ComponentProps<typeof StyledItem>)} />;
}
Item.displayName = "SelectItem";
export const Select = {
Root,
Label: withContext(SelectLabel, "label", "SelectLabel"),
Control: withContext(
"div",
(v) => [...resolveSlotStyles(selectRecipe.slots.control, v), chrome.anchor],
"SelectControl",
),
Trigger: withContext(
SelectTrigger,
(v) => [chrome.btnReset, ...resolveSlotStyles(selectRecipe.slots.trigger, v)],
"SelectTrigger",
),
Value: withContext(SelectValue, "valueText", "SelectValue"),
IndicatorGroup: withContext(
"div",
(v) => [...resolveSlotStyles(selectRecipe.slots.indicatorGroup, v), chrome.indicatorGroup],
"SelectIndicatorGroup",
),
Icon,
Content,
Item,
ItemText: withContext(SelectItemText, "itemText", "SelectItemText"),
ItemIndicator,
Group: withContext(SelectGroup, "itemGroup", "SelectGroup"),
GroupLabel: withContext(SelectGroupLabel, "itemGroupLabel", "SelectGroupLabel"),
};