|
1 | 1 | <script lang="ts"> |
2 | | - import { Tooltip, Dialog } from "bits-ui"; |
| 2 | + import { Combobox } from "bits-ui"; |
| 3 | + import CaretUpDown from "phosphor-svelte/lib/CaretUpDown"; |
| 4 | + import Check from "phosphor-svelte/lib/Check"; |
| 5 | + import CaretDoubleUp from "phosphor-svelte/lib/CaretDoubleUp"; |
| 6 | + import CaretDoubleDown from "phosphor-svelte/lib/CaretDoubleDown"; |
| 7 | + import DemoContainer from "$lib/components/demo-container.svelte"; |
3 | 8 |
|
4 | | - let open = $state(false); |
| 9 | + const fruits = [ |
| 10 | + { value: "mango", label: "Mango" }, |
| 11 | + { value: "watermelon", label: "Watermelon" }, |
| 12 | + { value: "apple", label: "Apple" }, |
| 13 | + { value: "pineapple", label: "Pineapple" }, |
| 14 | + { value: "orange", label: "Orange" }, |
| 15 | + { value: "grape", label: "Grape" }, |
| 16 | + { value: "strawberry", label: "Strawberry" }, |
| 17 | + { value: "banana", label: "Banana" }, |
| 18 | + { value: "kiwi", label: "Kiwi" }, |
| 19 | + { value: "peach", label: "Peach" }, |
| 20 | + { value: "cherry", label: "Cherry" }, |
| 21 | + { value: "blueberry", label: "Blueberry" }, |
| 22 | + { value: "raspberry", label: "Raspberry" }, |
| 23 | + { value: "blackberry", label: "Blackberry" }, |
| 24 | + { value: "plum", label: "Plum" }, |
| 25 | + { value: "apricot", label: "Apricot" }, |
| 26 | + { value: "pear", label: "Pear" }, |
| 27 | + { value: "grapefruit", label: "Grapefruit" }, |
| 28 | + ]; |
| 29 | +
|
| 30 | + let searchValue = $state(""); |
| 31 | +
|
| 32 | + const filteredFruits = $derived( |
| 33 | + searchValue === "" |
| 34 | + ? fruits |
| 35 | + : fruits.filter((fruit) => |
| 36 | + fruit.label.toLowerCase().includes(searchValue.toLowerCase()) |
| 37 | + ) |
| 38 | + ); |
5 | 39 | </script> |
6 | 40 |
|
7 | | -<Tooltip.Provider> |
8 | | - <Tooltip.Root delayDuration={200} disableCloseOnTriggerClick={true}> |
9 | | - <Tooltip.Trigger |
10 | | - class="inline-flex size-fit items-center justify-center" |
11 | | - onclick={() => (open = true)} |
| 41 | +<DemoContainer> |
| 42 | + <Combobox.Root |
| 43 | + type="multiple" |
| 44 | + name="favoriteFruit" |
| 45 | + onOpenChangeComplete={(o) => { |
| 46 | + if (!o) searchValue = ""; |
| 47 | + }} |
| 48 | + > |
| 49 | + <Combobox.Trigger |
| 50 | + class="bg-background h-input pointer-events-auto flex w-[200px] items-center justify-between rounded-[9px] border px-3 py-2" |
12 | 51 | > |
13 | | - Hover Me & Then Click |
14 | | - </Tooltip.Trigger> |
15 | | - <Tooltip.Content sideOffset={8} side="bottom"> |
16 | | - <div |
17 | | - class="rounded-input border-dark-10 bg-background shadow-popover outline-hidden z-0 flex items-center justify-center border p-3 text-sm font-medium" |
18 | | - > |
19 | | - Tooltip Content |
20 | | - </div> |
21 | | - </Tooltip.Content> |
22 | | - </Tooltip.Root> |
23 | | - |
24 | | - <Dialog.Root bind:open> |
25 | | - <Dialog.Portal> |
26 | | - <Dialog.Content |
27 | | - class="rounded-input border-dark-10 bg-background shadow-popover outline-hidden z-0 border p-3 text-sm font-medium" |
| 52 | + Select a fruit |
| 53 | + <CaretUpDown class="text-muted-foreground size-6" /> |
| 54 | + </Combobox.Trigger> |
| 55 | + <Combobox.Portal> |
| 56 | + <Combobox.Content |
| 57 | + class="focus-override border-muted bg-background shadow-popover 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 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 outline-hidden z-50 h-96 max-h-[var(--bits-combobox-content-available-height)] w-[var(--bits-combobox-anchor-width)] min-w-[var(--bits-combobox-anchor-width)] select-none rounded-xl border px-1 py-3 data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1" |
| 58 | + sideOffset={10} |
28 | 59 | > |
29 | | - <p>Dialog Content</p> |
30 | | - <p> |
31 | | - Click "Close" to close dialog and hover tooltip again. The tooltip will not |
32 | | - appear. |
33 | | - </p> |
34 | | - <Dialog.Close class="block">Close</Dialog.Close> |
35 | | - </Dialog.Content> |
36 | | - </Dialog.Portal> |
37 | | - </Dialog.Root> |
38 | | -</Tooltip.Provider> |
| 60 | + <!-- <Combobox.Input |
| 61 | + oninput={(e) => (searchValue = e.currentTarget.value)} |
| 62 | + class="h-input rounded-9px border-border-input bg-background placeholder:text-foreground-alt/50 focus:ring-foreground focus:ring-offset-background focus:outline-hidden inline-flex w-[296px] touch-none truncate border px-11 text-base transition-colors focus:ring-2 focus:ring-offset-2 sm:text-sm" |
| 63 | + placeholder="Search a fruit" |
| 64 | + aria-label="Search a fruit" |
| 65 | + /> --> |
| 66 | + <Combobox.ScrollUpButton class="flex w-full items-center justify-center py-1"> |
| 67 | + <CaretDoubleUp class="size-3" /> |
| 68 | + </Combobox.ScrollUpButton> |
| 69 | + <Combobox.Viewport class="p-1"> |
| 70 | + {#each filteredFruits as fruit, i (i + fruit.value)} |
| 71 | + <Combobox.Item |
| 72 | + class="rounded-button data-highlighted:bg-muted outline-hidden flex h-10 w-full select-none items-center py-3 pl-5 pr-1.5 text-sm capitalize" |
| 73 | + value={fruit.value} |
| 74 | + label={fruit.label} |
| 75 | + > |
| 76 | + {#snippet children({ selected })} |
| 77 | + {fruit.label} |
| 78 | + {#if selected} |
| 79 | + <div class="ml-auto"> |
| 80 | + <Check /> |
| 81 | + </div> |
| 82 | + {/if} |
| 83 | + {/snippet} |
| 84 | + </Combobox.Item> |
| 85 | + {:else} |
| 86 | + <span class="block px-5 py-2 text-sm text-muted-foreground"> |
| 87 | + No results found, try again. |
| 88 | + </span> |
| 89 | + {/each} |
| 90 | + </Combobox.Viewport> |
| 91 | + <Combobox.ScrollDownButton class="flex w-full items-center justify-center py-1"> |
| 92 | + <CaretDoubleDown class="size-3" /> |
| 93 | + </Combobox.ScrollDownButton> |
| 94 | + </Combobox.Content> |
| 95 | + </Combobox.Portal> |
| 96 | + </Combobox.Root> |
| 97 | +</DemoContainer> |
0 commit comments