import { Combobox as ComboboxPrimitive } from "@kobalte/core/combobox"; import type { ComponentProps, ValidComponent } from "solid-js"; import { splitProps } from "solid-js"; import { IconCheck, IconChevronDown } from "@/components/ui/icons"; import { cn } from "@/lib/cn"; import { useT } from "@/stores/preferences-context"; /** * Kobalte's Combobox root with its screen-reader strings (listbox name, * trigger name, focus/selection/count announcements) in the app language — * Kobalte ships them in English only. A call site can still pass its own * `translations`, which wins. */ function ComboboxRoot(props: ComponentProps) { const t = useT(); const translations = () => ({ focusAnnouncement: (optionText: string, isSelected: boolean) => isSelected ? t("combobox.optionSelected", { option: optionText }) : optionText, // Kobalte's type narrows this to its own English literal; the runtime // only needs a string (or undefined for "say nothing"). countAnnouncement: ((optionCount: number) => t("combobox.optionCount", { count: optionCount })) as unknown as ( optionCount: number, ) => "one option available" | undefined, selectedAnnouncement: (optionText: string) => t("combobox.optionSelected", { option: optionText }), triggerLabel: t("combobox.showSuggestions"), listboxLabel: t("combobox.suggestions"), }); return ; } export const Combobox = Object.assign(ComboboxRoot, ComboboxPrimitive) as unknown as typeof ComboboxPrimitive; export const ComboboxItemLabel = ComboboxPrimitive.ItemLabel; export const ComboboxHiddenSelect = ComboboxPrimitive.HiddenSelect; export function ComboboxControl( props: ComponentProps>, ) { const [local, rest] = splitProps(props as ComponentProps, ["class"]); return ( ); } export function ComboboxInput( props: ComponentProps>, ) { const [local, rest] = splitProps(props as ComponentProps, ["class"]); return ( ); } export function ComboboxTrigger( props: ComponentProps>, ) { const t = useT(); const [local, rest] = splitProps(props as ComponentProps, ["class"]); return ( ); } export function ComboboxContent( props: ComponentProps>, ) { const [local, rest] = splitProps(props as ComponentProps, [ "class", "children", ]); // Capped at the viewport: sized by its longest option, the list otherwise // outgrows a phone screen and drags the whole page sideways. Long options // wrap instead (ComboboxItem). return ( {local.children} ); } export function ComboboxItem( props: ComponentProps>, ) { const [local, rest] = splitProps(props as ComponentProps, ["class", "children"]); return ( :first-child]:min-w-0 [&>:first-child]:break-words", "data-highlighted:bg-accent data-highlighted:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-40", local.class, )} {...rest} > {local.children} ); }