forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

combobox.tsx132 lines, 5.6 KiBHistoryRawDownload
1import { Combobox as ComboboxPrimitive } from "@kobalte/core/combobox";
2import type { ComponentProps, ValidComponent } from "solid-js";
3import { splitProps } from "solid-js";
4import { IconCheck, IconChevronDown } from "@/components/ui/icons";
5import { cn } from "@/lib/cn";
6import { useT } from "@/stores/preferences-context";
7
8/**
9 * Kobalte's Combobox root with its screen-reader strings (listbox name,
10 * trigger name, focus/selection/count announcements) in the app language —
11 * Kobalte ships them in English only. A call site can still pass its own
12 * `translations`, which wins.
13 */
14function ComboboxRoot(props: ComponentProps<typeof ComboboxPrimitive>) {
15 const t = useT();
16 const translations = () => ({
17 focusAnnouncement: (optionText: string, isSelected: boolean) =>
18 isSelected ? t("combobox.optionSelected", { option: optionText }) : optionText,
19 // Kobalte's type narrows this to its own English literal; the runtime
20 // only needs a string (or undefined for "say nothing").
21 countAnnouncement: ((optionCount: number) => t("combobox.optionCount", { count: optionCount })) as unknown as (
22 optionCount: number,
23 ) => "one option available" | undefined,
24 selectedAnnouncement: (optionText: string) => t("combobox.optionSelected", { option: optionText }),
25 triggerLabel: t("combobox.showSuggestions"),
26 listboxLabel: t("combobox.suggestions"),
27 });
28 return <ComboboxPrimitive translations={translations()} {...props} />;
29}
30
31export const Combobox = Object.assign(ComboboxRoot, ComboboxPrimitive) as unknown as typeof ComboboxPrimitive;
32export const ComboboxItemLabel = ComboboxPrimitive.ItemLabel;
33export const ComboboxHiddenSelect = ComboboxPrimitive.HiddenSelect;
34
35export function ComboboxControl<T extends ValidComponent = "div">(
36 props: ComponentProps<typeof ComboboxPrimitive.Control<T>>,
37) {
38 const [local, rest] = splitProps(props as ComponentProps<typeof ComboboxPrimitive.Control>, ["class"]);
39 return (
40 <ComboboxPrimitive.Control
41 data-slot="combobox-control"
42 class={cn(
43 "relative flex h-9 w-full items-center rounded-md border border-input bg-background/90 shadow-sm transition-all",
44 "hover:border-ring/45 focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-1 data-disabled:cursor-not-allowed data-disabled:opacity-50",
45 local.class,
46 )}
47 {...rest}
48 />
49 );
50}
51
52export function ComboboxInput<T extends ValidComponent = "input">(
53 props: ComponentProps<typeof ComboboxPrimitive.Input<T>>,
54) {
55 const [local, rest] = splitProps(props as ComponentProps<typeof ComboboxPrimitive.Input>, ["class"]);
56 return (
57 <ComboboxPrimitive.Input
58 class={cn(
59 "flex h-full min-w-0 flex-1 bg-transparent pl-3.5 pr-1 text-xs font-medium text-foreground outline-hidden placeholder:text-muted-foreground disabled:cursor-not-allowed",
60 local.class,
61 )}
62 {...rest}
63 />
64 );
65}
66
67export function ComboboxTrigger<T extends ValidComponent = "button">(
68 props: ComponentProps<typeof ComboboxPrimitive.Trigger<T>>,
69) {
70 const t = useT();
71 const [local, rest] = splitProps(props as ComponentProps<typeof ComboboxPrimitive.Trigger>, ["class"]);
72 return (
73 <ComboboxPrimitive.Trigger
74 class={cn("flex h-full shrink-0 items-center pr-3 pl-1 text-muted-foreground/70 outline-hidden", local.class)}
75 // Kobalte's built-in name is an English "Show suggestions".
76 aria-label={t("combobox.showSuggestions")}
77 {...rest}
78 >
79 <ComboboxPrimitive.Icon>
80 <IconChevronDown class="h-4 w-4" />
81 </ComboboxPrimitive.Icon>
82 </ComboboxPrimitive.Trigger>
83 );
84}
85
86export function ComboboxContent<T extends ValidComponent = "div">(
87 props: ComponentProps<typeof ComboboxPrimitive.Content<T>>,
88) {
89 const [local, rest] = splitProps(props as ComponentProps<typeof ComboboxPrimitive.Content>, [
90 "class",
91 "children",
92 ]);
93 // Capped at the viewport: sized by its longest option, the list otherwise
94 // outgrows a phone screen and drags the whole page sideways. Long options
95 // wrap instead (ComboboxItem).
96 return (
97 <ComboboxPrimitive.Portal>
98 <ComboboxPrimitive.Content
99 class={cn(
100 "z-[70] mt-1.5 max-h-72 min-w-(--kb-popper-anchor-width) max-w-[calc(100vw-1rem)] overflow-hidden rounded-md border border-border/80 bg-popover p-1 text-popover-foreground shadow-xl shadow-black/10 outline-hidden",
101 "origin-(--kb-combobox-content-transform-origin) animate-in fade-in-0 zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
102 local.class,
103 )}
104 {...rest}
105 >
106 <ComboboxPrimitive.Listbox class="max-h-64 overflow-y-auto" />
107 {local.children}
108 </ComboboxPrimitive.Content>
109 </ComboboxPrimitive.Portal>
110 );
111}
112
113export function ComboboxItem<T extends ValidComponent = "li">(
114 props: ComponentProps<typeof ComboboxPrimitive.Item<T>>,
115) {
116 const [local, rest] = splitProps(props as ComponentProps<typeof ComboboxPrimitive.Item>, ["class", "children"]);
117 return (
118 <ComboboxPrimitive.Item
119 class={cn(
120 "relative flex cursor-pointer select-none items-center justify-between gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden transition-colors [&>:first-child]:min-w-0 [&>:first-child]:break-words",
121 "data-highlighted:bg-accent data-highlighted:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-40",
122 local.class,
123 )}
124 {...rest}
125 >
126 {local.children}
127 <ComboboxPrimitive.ItemIndicator>
128 <IconCheck class="h-3.5 w-3.5 shrink-0 text-primary-text" />
129 </ComboboxPrimitive.ItemIndicator>
130 </ComboboxPrimitive.Item>
131 );
132}