172 lines
6.8 KiB
TypeScript
172 lines
6.8 KiB
TypeScript
import * as React from 'react';
|
|
import * as SelectPrimitive from '@radix-ui/react-select';
|
|
import { Check, ChevronDown, ChevronUp } from 'lucide-react';
|
|
import { cn } from '../lib/cn';
|
|
|
|
const Select = SelectPrimitive.Root;
|
|
const SelectGroup = SelectPrimitive.Group;
|
|
const SelectValue = SelectPrimitive.Value;
|
|
|
|
const SelectTrigger = React.forwardRef<
|
|
React.ElementRef<typeof SelectPrimitive.Trigger>,
|
|
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
|
|
>(({ className, children, ...props }, ref) => (
|
|
<SelectPrimitive.Trigger
|
|
ref={ref}
|
|
className={cn(
|
|
'group flex h-9 w-full min-w-0 items-center justify-between gap-2 rounded-lg border border-border bg-card px-3 py-1 text-sm shadow-xs outline-none transition-[color,box-shadow,background-color,border-color]',
|
|
'hover:border-border/80 hover:bg-card/70',
|
|
'placeholder:text-muted-foreground',
|
|
'focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/40',
|
|
'data-[state=open]:border-ring data-[state=open]:ring-[3px] data-[state=open]:ring-ring/40',
|
|
'disabled:cursor-not-allowed disabled:opacity-50',
|
|
'data-[placeholder]:text-muted-foreground',
|
|
// Selected text mustn't push the chevron out of the trigger.
|
|
'[&>span:not([data-radix-select-icon])]:flex-1 [&>span:not([data-radix-select-icon])]:min-w-0 [&>span:not([data-radix-select-icon])]:truncate [&>span:not([data-radix-select-icon])]:text-left',
|
|
className,
|
|
)}
|
|
{...props}
|
|
>
|
|
{children}
|
|
<SelectPrimitive.Icon asChild data-radix-select-icon>
|
|
<ChevronDown className="size-4 shrink-0 opacity-60 transition-transform group-data-[state=open]:rotate-180" />
|
|
</SelectPrimitive.Icon>
|
|
</SelectPrimitive.Trigger>
|
|
));
|
|
SelectTrigger.displayName = 'SelectTrigger';
|
|
|
|
const SelectScrollUpButton = React.forwardRef<
|
|
React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
|
|
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
|
|
>(({ className, ...props }, ref) => (
|
|
<SelectPrimitive.ScrollUpButton
|
|
ref={ref}
|
|
className={cn(
|
|
'flex cursor-default items-center justify-center py-1 text-muted-foreground',
|
|
className,
|
|
)}
|
|
{...props}
|
|
>
|
|
<ChevronUp className="size-4" />
|
|
</SelectPrimitive.ScrollUpButton>
|
|
));
|
|
SelectScrollUpButton.displayName = 'SelectScrollUpButton';
|
|
|
|
const SelectScrollDownButton = React.forwardRef<
|
|
React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
|
|
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
|
|
>(({ className, ...props }, ref) => (
|
|
<SelectPrimitive.ScrollDownButton
|
|
ref={ref}
|
|
className={cn(
|
|
'flex cursor-default items-center justify-center py-1 text-muted-foreground',
|
|
className,
|
|
)}
|
|
{...props}
|
|
>
|
|
<ChevronDown className="size-4" />
|
|
</SelectPrimitive.ScrollDownButton>
|
|
));
|
|
SelectScrollDownButton.displayName = 'SelectScrollDownButton';
|
|
|
|
const SelectContent = React.forwardRef<
|
|
React.ElementRef<typeof SelectPrimitive.Content>,
|
|
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
|
|
>(({ className, children, position = 'popper', ...props }, ref) => (
|
|
<SelectPrimitive.Portal>
|
|
<SelectPrimitive.Content
|
|
ref={ref}
|
|
position={position}
|
|
sideOffset={4}
|
|
className={cn(
|
|
// Solid popover surface — explicit bg + ring for visibility against any background.
|
|
'relative z-50 max-h-(--radix-select-content-available-height) min-w-(--radix-select-trigger-width) overflow-hidden rounded-xl border border-border bg-popover text-popover-foreground shadow-xl ring-1 ring-foreground/5',
|
|
'data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
|
|
'data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95',
|
|
position === 'popper' &&
|
|
'data-[side=bottom]:translate-y-1 data-[side=top]:-translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1',
|
|
className,
|
|
)}
|
|
{...props}
|
|
>
|
|
<SelectScrollUpButton />
|
|
<SelectPrimitive.Viewport
|
|
className={cn(
|
|
'p-1',
|
|
position === 'popper' &&
|
|
'h-(--radix-select-trigger-height) w-full min-w-(--radix-select-trigger-width)',
|
|
)}
|
|
>
|
|
{children}
|
|
</SelectPrimitive.Viewport>
|
|
<SelectScrollDownButton />
|
|
</SelectPrimitive.Content>
|
|
</SelectPrimitive.Portal>
|
|
));
|
|
SelectContent.displayName = 'SelectContent';
|
|
|
|
const SelectLabel = React.forwardRef<
|
|
React.ElementRef<typeof SelectPrimitive.Label>,
|
|
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
|
|
>(({ className, ...props }, ref) => (
|
|
<SelectPrimitive.Label
|
|
ref={ref}
|
|
className={cn(
|
|
'px-2 py-1.5 text-[10px] font-semibold uppercase tracking-wider text-muted-foreground',
|
|
className,
|
|
)}
|
|
{...props}
|
|
/>
|
|
));
|
|
SelectLabel.displayName = 'SelectLabel';
|
|
|
|
const SelectItem = React.forwardRef<
|
|
React.ElementRef<typeof SelectPrimitive.Item>,
|
|
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
|
|
>(({ className, children, ...props }, ref) => (
|
|
<SelectPrimitive.Item
|
|
ref={ref}
|
|
className={cn(
|
|
'relative flex w-full cursor-default select-none items-center gap-2 rounded-md py-1.5 pl-2 pr-8 text-sm outline-none transition-colors',
|
|
'focus:bg-accent focus:text-accent-foreground',
|
|
'data-[state=checked]:font-medium',
|
|
'data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
|
|
className,
|
|
)}
|
|
{...props}
|
|
>
|
|
<span className="absolute right-2 flex size-3.5 items-center justify-center text-primary">
|
|
<SelectPrimitive.ItemIndicator>
|
|
<Check className="size-4" />
|
|
</SelectPrimitive.ItemIndicator>
|
|
</span>
|
|
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
|
|
</SelectPrimitive.Item>
|
|
));
|
|
SelectItem.displayName = 'SelectItem';
|
|
|
|
const SelectSeparator = React.forwardRef<
|
|
React.ElementRef<typeof SelectPrimitive.Separator>,
|
|
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
|
|
>(({ className, ...props }, ref) => (
|
|
<SelectPrimitive.Separator
|
|
ref={ref}
|
|
className={cn('-mx-1 my-1 h-px bg-border', className)}
|
|
{...props}
|
|
/>
|
|
));
|
|
SelectSeparator.displayName = 'SelectSeparator';
|
|
|
|
export {
|
|
Select,
|
|
SelectGroup,
|
|
SelectValue,
|
|
SelectTrigger,
|
|
SelectContent,
|
|
SelectLabel,
|
|
SelectItem,
|
|
SelectSeparator,
|
|
SelectScrollUpButton,
|
|
SelectScrollDownButton,
|
|
};
|