Docs
Combobox
Combobox
Autocomplete input and command palette with a list of suggestions.
<script lang="ts">
import Check from "lucide-svelte/icons/check";
import ChevronsUpDown from "lucide-svelte/icons/chevrons-up-down";
import { tick } from "svelte";
import * as Command from "$lib/components/ui/command/index.js";
import * as Popover from "$lib/components/ui/popover/index.js";
import { Button } from "$lib/components/ui/button/index.js";
import { cn } from "$lib/utils.js";
const frameworks = [
{
value: "sveltekit",
label: "SvelteKit"
},
{
value: "next.js",
label: "Next.js"
},
{
value: "nuxt.js",
label: "Nuxt.js"
},
{
value: "remix",
label: "Remix"
},
{
value: "astro",
label: "Astro"
}
];
let open = $state(false);
let value = $state("");
let triggerRef = $state<HTMLButtonElement>(null!);
const selectedValue = $derived(
frameworks.find((f) => f.value === value)?.label ?? "Select a framework..."
);
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger() {
open = false;
tick().then(() => {
triggerRef.focus();
});
}
</script>
<Popover.Root bind:open>
<Popover.Trigger bind:ref={triggerRef}>
{#snippet child({ props })}
<Button
variant="outline"
class="w-[200px] justify-between"
{...props}
role="combobox"
aria-expanded={open}
>
{selectedValue || "Select a framework..."}
<ChevronsUpDown class="opacity-50" />
</Button>
{/snippet}
</Popover.Trigger>
<Popover.Content class="w-[200px] p-0">
<Command.Root>
<Command.Input placeholder="Search framework..." class="h-9" />
<Command.List>
<Command.Empty>No framework found.</Command.Empty>
<Command.Group>
{#each frameworks as framework}
<Command.Item
value={framework.value}
onSelect={() => {
value = framework.value;
closeAndFocusTrigger();
}}
>
<Check
class={cn(value !== framework.value && "text-transparent")}
/>
{framework.label}
</Command.Item>
{/each}
</Command.Group>
</Command.List>
</Command.Root>
</Popover.Content>
</Popover.Root>
<script lang="ts">
import Check from "lucide-svelte/icons/check";
import ChevronsUpDown from "lucide-svelte/icons/chevrons-up-down";
import { tick } from "svelte";
import * as Command from "$lib/components/ui/command/index.js";
import * as Popover from "$lib/components/ui/popover/index.js";
import { Button } from "$lib/components/ui/button/index.js";
import { cn } from "$lib/utils.js";
const frameworks = [
{
value: "sveltekit",
label: "SvelteKit"
},
{
value: "next.js",
label: "Next.js"
},
{
value: "nuxt.js",
label: "Nuxt.js"
},
{
value: "remix",
label: "Remix"
},
{
value: "astro",
label: "Astro"
}
];
let open = $state(false);
let value = $state("");
let triggerRef = $state<HTMLButtonElement>(null!);
const selectedValue = $derived(
frameworks.find((f) => f.value === value)?.label
);
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger() {
open = false;
tick().then(() => {
triggerRef.focus();
});
}
</script>
<Popover.Root bind:open>
<Popover.Trigger bind:ref={triggerRef}>
{#snippet child({ props })}
<Button
variant="outline"
class="w-[200px] justify-between"
{...props}
role="combobox"
aria-expanded={open}
>
{selectedValue || "Select a framework..."}
<ChevronsUpDown class="opacity-50" />
</Button>
{/snippet}
</Popover.Trigger>
<Popover.Content class="w-[200px] p-0">
<Command.Root>
<Command.Input placeholder="Search framework..." />
<Command.List>
<Command.Empty>No framework found.</Command.Empty>
<Command.Group>
{#each frameworks as framework}
<Command.Item
value={framework.value}
onSelect={() => {
value = framework.value;
closeAndFocusTrigger();
}}
>
<Check
class={cn(value !== framework.value && "text-transparent")}
/>
{framework.label}
</Command.Item>
{/each}
</Command.Group>
</Command.List>
</Command.Root>
</Popover.Content>
</Popover.Root>
Installation
The Combobox is built using a composition of the <Popover />
and the <Command />
components.
See installation instructions for the Popover and the Command components.
Usage
<script lang="ts">
import Check from "lucide-svelte/icons/check";
import ChevronsUpDown from "lucide-svelte/icons/chevrons-up-down";
import { tick } from "svelte";
import * as Command from "$lib/components/ui/command/index.js";
import * as Popover from "$lib/components/ui/popover/index.js";
import { Button } from "$lib/components/ui/button/index.js";
import { cn } from "$lib/utils.js";
const frameworks = [
{
value: "sveltekit",
label: "SvelteKit",
},
{
value: "next.js",
label: "Next.js",
},
{
value: "nuxt.js",
label: "Nuxt.js",
},
{
value: "remix",
label: "Remix",
},
{
value: "astro",
label: "Astro",
},
];
let open = $state(false);
let value = $state("");
let triggerRef = $state<HTMLButtonElement>(null!);
const selectedValue = $derived(
frameworks.find((f) => f.value === value)?.label
);
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger() {
open = false;
tick().then(() => {
triggerRef.focus();
});
}
</script>
<Popover.Root bind:open>
<Popover.Trigger bind:ref={triggerRef}>
{#snippet child({ props })}
<Button
variant="outline"
class="w-[200px] justify-between"
{...props}
role="combobox"
aria-expanded={open}
>
{selectedValue || "Select a framework..."}
<ChevronsUpDown class="ml-2 size-4 shrink-0 opacity-50" />
</Button>
{/snippet}
</Popover.Trigger>
<Popover.Content class="w-[200px] p-0">
<Command.Root>
<Command.Input placeholder="Search framework..." />
<Command.List>
<Command.Empty>No framework found.</Command.Empty>
<Command.Group>
{#each frameworks as framework}
<Command.Item
value={framework.value}
onSelect={() => {
value = framework.value;
closeAndFocusTrigger();
}}
>
<Check
class={cn(
"mr-2 size-4",
value !== framework.value && "text-transparent"
)}
/>
{framework.label}
</Command.Item>
{/each}
</Command.Group>
</Command.List>
</Command.Root>
</Popover.Content>
</Popover.Root>
Examples
Combobox
<script lang="ts">
import Check from "lucide-svelte/icons/check";
import ChevronsUpDown from "lucide-svelte/icons/chevrons-up-down";
import { tick } from "svelte";
import * as Command from "$lib/components/ui/command/index.js";
import * as Popover from "$lib/components/ui/popover/index.js";
import { Button } from "$lib/components/ui/button/index.js";
import { cn } from "$lib/utils.js";
const frameworks = [
{
value: "sveltekit",
label: "SvelteKit"
},
{
value: "next.js",
label: "Next.js"
},
{
value: "nuxt.js",
label: "Nuxt.js"
},
{
value: "remix",
label: "Remix"
},
{
value: "astro",
label: "Astro"
}
];
let open = $state(false);
let value = $state("");
let triggerRef = $state<HTMLButtonElement>(null!);
const selectedValue = $derived(
frameworks.find((f) => f.value === value)?.label ?? "Select a framework..."
);
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger() {
open = false;
tick().then(() => {
triggerRef.focus();
});
}
</script>
<Popover.Root bind:open>
<Popover.Trigger bind:ref={triggerRef}>
{#snippet child({ props })}
<Button
variant="outline"
class="w-[200px] justify-between"
{...props}
role="combobox"
aria-expanded={open}
>
{selectedValue || "Select a framework..."}
<ChevronsUpDown class="opacity-50" />
</Button>
{/snippet}
</Popover.Trigger>
<Popover.Content class="w-[200px] p-0">
<Command.Root>
<Command.Input placeholder="Search framework..." class="h-9" />
<Command.List>
<Command.Empty>No framework found.</Command.Empty>
<Command.Group>
{#each frameworks as framework}
<Command.Item
value={framework.value}
onSelect={() => {
value = framework.value;
closeAndFocusTrigger();
}}
>
<Check
class={cn(value !== framework.value && "text-transparent")}
/>
{framework.label}
</Command.Item>
{/each}
</Command.Group>
</Command.List>
</Command.Root>
</Popover.Content>
</Popover.Root>
<script lang="ts">
import Check from "lucide-svelte/icons/check";
import ChevronsUpDown from "lucide-svelte/icons/chevrons-up-down";
import { tick } from "svelte";
import * as Command from "$lib/components/ui/command/index.js";
import * as Popover from "$lib/components/ui/popover/index.js";
import { Button } from "$lib/components/ui/button/index.js";
import { cn } from "$lib/utils.js";
const frameworks = [
{
value: "sveltekit",
label: "SvelteKit"
},
{
value: "next.js",
label: "Next.js"
},
{
value: "nuxt.js",
label: "Nuxt.js"
},
{
value: "remix",
label: "Remix"
},
{
value: "astro",
label: "Astro"
}
];
let open = $state(false);
let value = $state("");
let triggerRef = $state<HTMLButtonElement>(null!);
const selectedValue = $derived(
frameworks.find((f) => f.value === value)?.label
);
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger() {
open = false;
tick().then(() => {
triggerRef.focus();
});
}
</script>
<Popover.Root bind:open>
<Popover.Trigger bind:ref={triggerRef}>
{#snippet child({ props })}
<Button
variant="outline"
class="w-[200px] justify-between"
{...props}
role="combobox"
aria-expanded={open}
>
{selectedValue || "Select a framework..."}
<ChevronsUpDown class="opacity-50" />
</Button>
{/snippet}
</Popover.Trigger>
<Popover.Content class="w-[200px] p-0">
<Command.Root>
<Command.Input placeholder="Search framework..." />
<Command.List>
<Command.Empty>No framework found.</Command.Empty>
<Command.Group>
{#each frameworks as framework}
<Command.Item
value={framework.value}
onSelect={() => {
value = framework.value;
closeAndFocusTrigger();
}}
>
<Check
class={cn(value !== framework.value && "text-transparent")}
/>
{framework.label}
</Command.Item>
{/each}
</Command.Group>
</Command.List>
</Command.Root>
</Popover.Content>
</Popover.Root>
Popover
<script lang="ts">
import { tick } from "svelte";
import { useId } from "bits-ui";
import * as Command from "$lib/components/ui/command/index.js";
import * as Popover from "$lib/components/ui/popover/index.js";
import { buttonVariants } from "$lib/components/ui/button/index.js";
type Status = {
value: string;
label: string;
};
const statuses: Status[] = [
{
value: "backlog",
label: "Backlog"
},
{
value: "todo",
label: "Todo"
},
{
value: "in progress",
label: "In Progress"
},
{
value: "done",
label: "Done"
},
{
value: "canceled",
label: "Canceled"
}
];
let open = $state(false);
let value = $state("");
const selectedStatus = $derived(
statuses.find((s) => s.value === value) ?? null
);
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger(triggerId: string) {
open = false;
tick().then(() => {
document.getElementById(triggerId)?.focus();
});
}
const triggerId = useId();
</script>
<div class="flex items-center space-x-4">
<p class="text-muted-foreground text-sm">Status</p>
<Popover.Root bind:open>
<Popover.Trigger
class={buttonVariants({
variant: "outline",
class: "w-[150px] justify-start"
})}
id={triggerId}
>
{selectedStatus ? selectedStatus.label : "+ Set status"}
</Popover.Trigger>
<Popover.Content class="p-0" align="start" side="right">
<Command.Root>
<Command.Input placeholder="Change status..." />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
<Command.Group>
{#each statuses as status}
<Command.Item
value={status.value}
onSelect={() => {
value = status.value;
closeAndFocusTrigger(triggerId);
}}
>
{status.label}
</Command.Item>
{/each}
</Command.Group>
</Command.List>
</Command.Root>
</Popover.Content>
</Popover.Root>
</div>
<script lang="ts">
import Circle from "lucide-svelte/icons/circle";
import CircleArrowUp from "lucide-svelte/icons/circle-arrow-up";
import CircleCheck from "lucide-svelte/icons/circle-check";
import CircleHelp from "lucide-svelte/icons/circle-help";
import CircleX from "lucide-svelte/icons/circle-x";
import { type ComponentType, tick } from "svelte";
import { useId } from "bits-ui";
import { cn } from "$lib/utils.js";
import * as Popover from "$lib/components/ui/popover/index.js";
import * as Command from "$lib/components/ui/command/index.js";
import { buttonVariants } from "$lib/components/ui/button/index.js";
type Status = {
value: string;
label: string;
icon: ComponentType;
};
const statuses: Status[] = [
{
value: "backlog",
label: "Backlog",
icon: CircleHelp
},
{
value: "todo",
label: "Todo",
icon: Circle
},
{
value: "in progress",
label: "In Progress",
icon: CircleArrowUp
},
{
value: "done",
label: "Done",
icon: CircleCheck
},
{
value: "canceled",
label: "Canceled",
icon: CircleX
}
];
let open = $state(false);
let value = $state("");
const selectedStatus = $derived(statuses.find((s) => s.value === value));
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger(triggerId: string) {
open = false;
tick().then(() => {
document.getElementById(triggerId)?.focus();
});
}
const triggerId = useId();
</script>
<div class="flex items-center space-x-4">
<p class="text-muted-foreground text-sm">Status</p>
<Popover.Root bind:open>
<Popover.Trigger
id={triggerId}
class={buttonVariants({
variant: "outline",
size: "sm",
class: "w-[150px] justify-start"
})}
>
{#if selectedStatus}
{@const Icon = selectedStatus.icon}
<Icon class="mr-2 size-4 shrink-0" />
{selectedStatus.label}
{:else}
+ Set status
{/if}
</Popover.Trigger>
<Popover.Content class="w-[200px] p-0" side="right" align="start">
<Command.Root>
<Command.Input placeholder="Change status..." />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
<Command.Group>
{#each statuses as status}
<Command.Item
value={status.value}
onSelect={() => {
value = status.value;
closeAndFocusTrigger(triggerId);
}}
>
{@const Icon = status.icon}
<Icon
class={cn(
"mr-2 size-4",
status.value !== selectedStatus?.value &&
"text-foreground/40"
)}
/>
<span>
{status.label}
</span>
</Command.Item>
{/each}
</Command.Group>
</Command.List>
</Command.Root>
</Popover.Content>
</Popover.Root>
</div>
Dropdown menu
<script lang="ts">
import Ellipsis from "lucide-svelte/icons/ellipsis";
import { tick } from "svelte";
import * as Command from "$lib/components/ui/command/index.js";
import * as DropdownMenu from "$lib/components/ui/dropdown-menu/index.js";
import { Button } from "$lib/components/ui/button/index.js";
const labels = [
"feature",
"bug",
"enhancement",
"documentation",
"design",
"question",
"maintenance"
];
let open = $state(false);
let selectedLabel = $state("feature");
let triggerRef = $state<HTMLButtonElement>(null!);
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger() {
open = false;
tick().then(() => {
triggerRef.focus();
});
}
</script>
<div
class="flex w-full flex-col items-start justify-between rounded-md border px-4 py-3 sm:flex-row sm:items-center"
>
<p class="text-sm font-medium leading-none">
<span
class="bg-primary text-primary-foreground mr-2 rounded-lg px-2 py-1 text-xs"
>
{selectedLabel}
</span>
<span class="text-muted-foreground">Create a new project</span>
</p>
<DropdownMenu.Root bind:open>
<DropdownMenu.Trigger bind:ref={triggerRef}>
{#snippet child({ props })}
<Button variant="ghost" size="sm" {...props} aria-label="Open menu">
<Ellipsis />
</Button>
{/snippet}
</DropdownMenu.Trigger>
<DropdownMenu.Content class="w-[200px]" align="end">
<DropdownMenu.Group>
<DropdownMenu.GroupHeading>Actions</DropdownMenu.GroupHeading>
<DropdownMenu.Item>Assign to...</DropdownMenu.Item>
<DropdownMenu.Item>Set due date...</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Sub>
<DropdownMenu.SubTrigger>Apply label</DropdownMenu.SubTrigger>
<DropdownMenu.SubContent class="p-0">
<Command.Root value={selectedLabel}>
<Command.Input
autofocus
placeholder="Filter label..."
class="h-9"
/>
<Command.List>
<Command.Empty>No label found.</Command.Empty>
<Command.Group>
{#each labels as label}
<Command.Item
value={label}
onSelect={() => {
selectedLabel = label;
closeAndFocusTrigger();
}}
>
{label}
</Command.Item>
{/each}
</Command.Group>
</Command.List>
</Command.Root>
</DropdownMenu.SubContent>
</DropdownMenu.Sub>
<DropdownMenu.Separator />
<DropdownMenu.Item class="text-red-600">
Delete
<DropdownMenu.Shortcut>⌘⌫</DropdownMenu.Shortcut>
</DropdownMenu.Item>
</DropdownMenu.Group>
</DropdownMenu.Content>
</DropdownMenu.Root>
</div>
<script lang="ts">
import Calendar from "lucide-svelte/icons/calendar";
import Ellipsis from "lucide-svelte/icons/ellipsis";
import Tags from "lucide-svelte/icons/tags";
import Trash from "lucide-svelte/icons/trash";
import User from "lucide-svelte/icons/user";
import { tick } from "svelte";
import * as DropdownMenu from "$lib/components/ui/dropdown-menu/index.js";
import * as Command from "$lib/components/ui/command/index.js";
import { Button } from "$lib/components/ui/button/index.js";
const labels = [
"feature",
"bug",
"enhancement",
"documentation",
"design",
"question",
"maintenance"
];
let open = $state(false);
let selectedLabel = $state("feature");
let triggerRef = $state<HTMLButtonElement>(null!);
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger() {
open = false;
tick().then(() => {
triggerRef.focus();
});
}
</script>
<div
class="flex w-full flex-col items-start justify-between rounded-md border px-4 py-3 sm:flex-row sm:items-center"
>
<p class="text-sm font-medium leading-none">
<span
class="bg-primary text-primary-foreground mr-2 rounded-lg px-2 py-1 text-xs"
>
{selectedLabel}
</span>
<span class="text-muted-foreground">Create a new project</span>
</p>
<DropdownMenu.Root bind:open>
<DropdownMenu.Trigger bind:ref={triggerRef}>
{#snippet child({ props })}
<Button variant="ghost" size="sm" {...props} aria-label="Open menu">
<Ellipsis />
</Button>
{/snippet}
</DropdownMenu.Trigger>
<DropdownMenu.Content class="w-[200px]" align="end">
<DropdownMenu.Group>
<DropdownMenu.GroupHeading>Actions</DropdownMenu.GroupHeading>
<DropdownMenu.Item>
<User class="mr-2 size-4" />
Assign to...
</DropdownMenu.Item>
<DropdownMenu.Item>
<Calendar class="mr-2 size-4" />
Set due date...
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Sub>
<DropdownMenu.SubTrigger>
<Tags class="mr-2 size-4" />
Apply label
</DropdownMenu.SubTrigger>
<DropdownMenu.SubContent class="p-0">
<Command.Root value={selectedLabel}>
<Command.Input autofocus placeholder="Filter label..." />
<Command.List>
<Command.Empty>No label found.</Command.Empty>
<Command.Group>
{#each labels as label}
<Command.Item
value={label}
onSelect={() => {
selectedLabel = label;
closeAndFocusTrigger();
}}
>
{label}
</Command.Item>
{/each}
</Command.Group>
</Command.List>
</Command.Root>
</DropdownMenu.SubContent>
</DropdownMenu.Sub>
<DropdownMenu.Separator />
<DropdownMenu.Item class="text-red-600">
<Trash class="mr-2 size-4" />
Delete
<DropdownMenu.Shortcut>⌘⌫</DropdownMenu.Shortcut>
</DropdownMenu.Item>
</DropdownMenu.Group>
</DropdownMenu.Content>
</DropdownMenu.Root>
</div>
Form
Since the Combobox is built using the <Popover />
and the <Command />
components, we need to use the <Form.Control />
component. <Form.Control />
enables us to apply the right aria-*
attributes to non-standard form elements, and adds a hidden input to ensure the form is submitted with the correct value.
Note: You must be on version 0.5.0
or higher of formsnap
for this to work correctly.
<script lang="ts" module>
import { z } from "zod";
const languages = [
{ label: "English", value: "en" },
{ label: "French", value: "fr" },
{ label: "German", value: "de" },
{ label: "Spanish", value: "es" },
{ label: "Portuguese", value: "pt" },
{ label: "Russian", value: "ru" },
{ label: "Japanese", value: "ja" },
{ label: "Korean", value: "ko" },
{ label: "Chinese", value: "zh" }
] as const;
type Language = (typeof languages)[number]["value"];
export const formSchema = z.object({
language: z.enum(
languages.map((f) => f.value) as [Language, ...Language[]],
{
errorMap: () => ({ message: "Please select a valid language." })
}
)
});
export type FormSchema = typeof formSchema;
</script>
<script lang="ts">
import Check from "lucide-svelte/icons/check";
import ChevronsUpDown from "lucide-svelte/icons/chevrons-up-down";
import SuperDebug, {
type Infer,
type SuperValidated,
superForm
} from "sveltekit-superforms";
import { tick } from "svelte";
import { zodClient } from "sveltekit-superforms/adapters";
import { toast } from "svelte-sonner";
import { useId } from "bits-ui";
import { browser } from "$app/environment";
import { page } from "$app/state";
import * as Form from "$lib/components/ui/form/index.js";
import * as Popover from "$lib/components/ui/popover/index.js";
import * as Command from "$lib/components/ui/command/index.js";
import { cn } from "$lib/utils.js";
import { buttonVariants } from "$lib/components/ui/button/index.js";
let {
form: data = page.data.combobox
}: { form: SuperValidated<Infer<FormSchema>> } = $props();
const form = superForm(data, {
validators: zodClient(formSchema),
onUpdated: ({ form: f }) => {
if (f.valid) {
toast.success(`You submitted ${JSON.stringify(f.data, null, 2)}`);
} else {
toast.error("Please fix the errors in the form.");
}
}
});
const { form: formData, enhance } = form;
let open = $state(false);
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger(triggerId: string) {
open = false;
tick().then(() => {
document.getElementById(triggerId)?.focus();
});
}
const triggerId = useId();
</script>
<form method="POST" action="/?/combobox" class="space-y-6" use:enhance>
<Form.Field {form} name="language" class="flex flex-col">
<Popover.Root bind:open>
<Form.Control id={triggerId}>
{#snippet children({ props })}
<Form.Label>Language</Form.Label>
<Popover.Trigger
class={cn(
buttonVariants({ variant: "outline" }),
"w-[200px] justify-between",
!$formData.language && "text-muted-foreground"
)}
role="combobox"
{...props}
>
{languages.find((f) => f.value === $formData.language)?.label ??
"Select language"}
<ChevronsUpDown class="opacity-50" />
</Popover.Trigger>
<input hidden value={$formData.language} name={props.name} />
{/snippet}
</Form.Control>
<Popover.Content class="w-[200px] p-0">
<Command.Root>
<Command.Input
autofocus
placeholder="Search language..."
class="h-9"
/>
<Command.Empty>No language found.</Command.Empty>
<Command.Group>
{#each languages as language}
<Command.Item
value={language.value}
onSelect={() => {
$formData.language = language.value;
closeAndFocusTrigger(triggerId);
}}
>
{language.label}
<Check
class={cn(
language.value !== $formData.language && "text-transparent"
)}
/>
</Command.Item>
{/each}
</Command.Group>
</Command.Root>
</Popover.Content>
</Popover.Root>
<Form.Description>
This is the language that will be used in the dashboard.
</Form.Description>
<Form.FieldErrors />
</Form.Field>
<Form.Button>Submit</Form.Button>
{#if browser}
<SuperDebug data={$formData} />
{/if}
</form>
<script lang="ts" module>
import { z } from "zod";
const languages = [
{ label: "English", value: "en" },
{ label: "French", value: "fr" },
{ label: "German", value: "de" },
{ label: "Spanish", value: "es" },
{ label: "Portuguese", value: "pt" },
{ label: "Russian", value: "ru" },
{ label: "Japanese", value: "ja" },
{ label: "Korean", value: "ko" },
{ label: "Chinese", value: "zh" }
] as const;
type Language = (typeof languages)[number]["value"];
export const formSchema = z.object({
language: z.enum(
languages.map((f) => f.value) as [Language, ...Language[]],
{
errorMap: () => ({ message: "Please select a valid language." })
}
)
});
export type FormSchema = typeof formSchema;
</script>
<script lang="ts">
import SuperDebug, {
type Infer,
type SuperValidated,
superForm
} from "sveltekit-superforms";
import { tick } from "svelte";
import Check from "lucide-svelte/icons/check";
import ChevronsUpDown from "lucide-svelte/icons/chevrons-up-down";
import { zodClient } from "sveltekit-superforms/adapters";
import { toast } from "svelte-sonner";
import { useId } from "bits-ui";
import { browser } from "$app/environment";
import { page } from "$app/state";
import * as Form from "$lib/components/ui/form/index.js";
import * as Popover from "$lib/components/ui/popover/index.js";
import * as Command from "$lib/components/ui/command/index.js";
import { cn } from "$lib/utils.js";
import { buttonVariants } from "$lib/components/ui/button/index.js";
let data: SuperValidated<Infer<FormSchema>> = page.data.combobox;
export { data as form };
const form = superForm(data, {
validators: zodClient(formSchema),
onUpdated: ({ form: f }) => {
if (f.valid) {
toast.success(`You submitted ${JSON.stringify(f.data, null, 2)}`);
} else {
toast.error("Please fix the errors in the form.");
}
}
});
const { form: formData, enhance } = form;
let open = false;
// We want to refocus the trigger button when the user selects
// an item from the list so users can continue navigating the
// rest of the form with the keyboard.
function closeAndFocusTrigger(triggerId: string) {
open = false;
tick().then(() => {
document.getElementById(triggerId)?.focus();
});
}
const triggerId = useId();
</script>
<form method="POST" action="/?/combobox" class="space-y-6" use:enhance>
<Form.Field {form} name="language" class="flex flex-col">
<Popover.Root bind:open>
<Form.Control id={triggerId}>
{#snippet children({ props })}
<Form.Label>Language</Form.Label>
<Popover.Trigger
class={cn(
buttonVariants({ variant: "outline" }),
"w-[200px] justify-between",
!$formData.language && "text-muted-foreground"
)}
role="combobox"
{...props}
>
{languages.find((f) => f.value === $formData.language)?.label ??
"Select language"}
<ChevronsUpDown class="opacity-50" />
</Popover.Trigger>
<input hidden value={$formData.language} name={props.name} />
{/snippet}
</Form.Control>
<Popover.Content class="w-[200px] p-0">
<Command.Root>
<Command.Input
autofocus
placeholder="Search language..."
class="h-9"
/>
<Command.Empty>No language found.</Command.Empty>
<Command.Group>
{#each languages as language}
<Command.Item
value={language.label}
onSelect={() => {
$formData.language = language.value;
closeAndFocusTrigger(triggerId);
}}
>
{language.label}
<Check
class={cn(
"ml-auto",
language.value !== $formData.language && "text-transparent"
)}
/>
</Command.Item>
{/each}
</Command.Group>
</Command.Root>
</Popover.Content>
</Popover.Root>
<Form.Description>
This is the language that will be used in the dashboard.
</Form.Description>
<Form.FieldErrors />
</Form.Field>
<Form.Button>Submit</Form.Button>
{#if browser}
<SuperDebug data={$formData} />
{/if}
</form>
On This Page