跳至正文
获取 Pro
基础原语U.09

选择器

FreeRegistry
components/linyi/select.tsx
@base-ui/reactclass-variance-authoritylucide-react
"use client";
 
import * as React from "react";
import { Select as BaseSelect } from "@base-ui/react/select";
import { cva, type VariantProps } from "class-variance-authority";
import { Check, ChevronDown } from "lucide-react";
import { cn } from "@/lib/cn";
 
export const Select = BaseSelect.Root;
export const SelectGroup = BaseSelect.Group;
export const SelectValue = BaseSelect.Value;
 
export const selectTriggerVariants = cva(
"inline-flex w-full items-center justify-between gap-2 rounded-control border border-line-strong bg-surface text-fg transition-colors cursor-pointer outline-none hover:border-fg/30 focus-visible:border-accent focus-visible:ring-2 focus-visible:ring-accent/20 data-[disabled]:opacity-45 data-[disabled]:pointer-events-none",
{
variants: {
size: {
sm: "h-control-sm px-3 text-xs",
md: "h-control-md px-3.5 text-xs",
lg: "h-control-lg px-4 text-sm",
},
},
defaultVariants: {
size: "md",
},
}
);
 
export interface SelectTriggerProps
extends React.ComponentPropsWithRef<typeof BaseSelect.Trigger>,
VariantProps<typeof selectTriggerVariants> {
placeholder?: string;
}
 
export function SelectTrigger({
className,
size = "md",
children,
...props
}: SelectTriggerProps) {
return (
<BaseSelect.Trigger
className={cn(selectTriggerVariants({ size }), className)}
{...props}
>
<span className="truncate text-left">{children}</span>
<BaseSelect.Icon className="flex-shrink-0 text-fg-muted transition-transform duration-150 data-[popup-open]:rotate-180">
<ChevronDown className="h-3.5 w-3.5" aria-hidden="true" />
</BaseSelect.Icon>
</BaseSelect.Trigger>
);
}
 
export interface SelectContentProps
extends React.ComponentPropsWithRef<typeof BaseSelect.Popup> {
sideOffset?: number;
align?: React.ComponentProps<typeof BaseSelect.Positioner>["align"];
}
 
export function SelectContent({
className,
sideOffset = 6,
align = "start",
children,
...props
}: SelectContentProps) {
return (
<BaseSelect.Portal>
<BaseSelect.Positioner
sideOffset={sideOffset}
align={align}
alignItemWithTrigger={false}
className="z-[var(--z-popover)] outline-none"
>
<BaseSelect.Popup
className={cn(
"min-w-[var(--anchor-width)] max-h-[260px] overflow-y-auto rounded-[14px] border border-line-strong bg-surface/95 p-1.5 text-fg shadow-popover backdrop-blur-md outline-none",
"origin-[var(--transform-origin)] transition-[opacity,transform] duration-150 ease-[cubic-bezier(0.22,1,0.36,1)]",
"data-[starting-style]:opacity-0 data-[starting-style]:scale-95 data-[ending-style]:opacity-0 data-[ending-style]:scale-95",
className
)}
{...props}
>
<BaseSelect.List className="space-y-0.5 outline-none">{children}</BaseSelect.List>
</BaseSelect.Popup>
</BaseSelect.Positioner>
</BaseSelect.Portal>
);
}
 
export function SelectLabel({
className,
...props
}: React.ComponentPropsWithRef<typeof BaseSelect.GroupLabel>) {
return (
<BaseSelect.GroupLabel
className={cn("px-2.5 py-1.5 font-mono-num text-[10px] text-fg-muted select-none", className)}
{...props}
/>
);
}
 
export function SelectItem({
className,
children,
...props
}: React.ComponentPropsWithRef<typeof BaseSelect.Item>) {
return (
<BaseSelect.Item
className={cn(
"relative flex min-h-[32px] w-full cursor-pointer select-none items-center justify-between gap-2 rounded-[8px] px-2.5 py-1.5 text-xs text-fg outline-none transition-colors",
"data-[highlighted]:bg-well data-[selected]:font-medium data-[disabled]:pointer-events-none data-[disabled]:opacity-40",
className
)}
{...props}
>
<BaseSelect.ItemText className="truncate">{children}</BaseSelect.ItemText>
<BaseSelect.ItemIndicator className="flex-shrink-0 text-accent">
<Check className="h-3.5 w-3.5" aria-hidden="true" />
</BaseSelect.ItemIndicator>
</BaseSelect.Item>
);
}
 
export function SelectSeparator({
className,
...props
}: React.ComponentPropsWithRef<typeof BaseSelect.Separator>) {
return <BaseSelect.Separator className={cn("my-1 h-px bg-line", className)} {...props} />;
}