|
| 1 | +<script lang="ts"> |
| 2 | + import { goto } from '$app/navigation'; |
| 3 | + import { page } from '$app/state'; |
| 4 | + import { cn } from '$lib/utils.js'; |
| 5 | + import { Badge } from '$ui/badge'; |
| 6 | + import { Button } from '$ui/button'; |
| 7 | + import * as Command from '$ui/command'; |
| 8 | + import * as Popover from '$ui/popover'; |
| 9 | + import { Separator } from '$ui/separator'; |
| 10 | + import Check from '@lucide/svelte/icons/check'; |
| 11 | + import Funnel from '@lucide/svelte/icons/funnel'; |
| 12 | + import { SvelteURLSearchParams } from 'svelte/reactivity'; |
| 13 | +
|
| 14 | + type Props = { |
| 15 | + query: string; |
| 16 | + title: string; |
| 17 | + options: { |
| 18 | + label: string; |
| 19 | + value: string; |
| 20 | + // This should be `Component` after @lucide/svelte updates types |
| 21 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 22 | + icon?: any; |
| 23 | + }[]; |
| 24 | + }; |
| 25 | +
|
| 26 | + let { query, title, options }: Props = $props(); |
| 27 | +
|
| 28 | + let selectedValue = $derived(page.url.searchParams.get(query) ?? ''); |
| 29 | +</script> |
| 30 | + |
| 31 | +<Popover.Root> |
| 32 | + <Popover.Trigger> |
| 33 | + {#snippet child({ props })} |
| 34 | + <Button {...props} variant="outline" size="sm" class="h-8 border-dashed"> |
| 35 | + <Funnel /> |
| 36 | + {title} |
| 37 | + {#if selectedValue} |
| 38 | + <Separator orientation="vertical" class="mx-2 h-4" /> |
| 39 | + <Badge variant="secondary" class="rounded-sm px-1 font-normal lg:hidden"> |
| 40 | + {selectedValue.length} |
| 41 | + </Badge> |
| 42 | + <div class="hidden space-x-1 lg:flex"> |
| 43 | + {#each options.filter((opt) => selectedValue === opt.value) as option (option)} |
| 44 | + <Badge variant="secondary" class="rounded-sm px-1 font-normal"> |
| 45 | + {option.label} |
| 46 | + </Badge> |
| 47 | + {/each} |
| 48 | + </div> |
| 49 | + {/if} |
| 50 | + </Button> |
| 51 | + {/snippet} |
| 52 | + </Popover.Trigger> |
| 53 | + <Popover.Content class="w-[200px] p-0" align="start"> |
| 54 | + <Command.Root> |
| 55 | + <!-- <Command.Input placeholder={title} /> --> |
| 56 | + <Command.List> |
| 57 | + <Command.Empty>No results found.</Command.Empty> |
| 58 | + <Command.Group> |
| 59 | + {#each options as option (option)} |
| 60 | + {@const isSelected = selectedValue.includes(option.value)} |
| 61 | + <Command.Item |
| 62 | + onSelect={() => { |
| 63 | + if (isSelected) { |
| 64 | + selectedValue = ''; |
| 65 | + const newQuery = new SvelteURLSearchParams(page.url.searchParams); |
| 66 | + newQuery.delete(query); |
| 67 | + goto(`${page.url.pathname}?${newQuery}`); |
| 68 | + } else { |
| 69 | + selectedValue = option.value; |
| 70 | + goto( |
| 71 | + `${page.url.pathname}?${new SvelteURLSearchParams({ |
| 72 | + ...Object.fromEntries(page.url.searchParams), |
| 73 | + [query]: option.value, |
| 74 | + })}` |
| 75 | + ); |
| 76 | + } |
| 77 | + }} |
| 78 | + > |
| 79 | + <div |
| 80 | + class={cn( |
| 81 | + 'border-primary mr-2 flex size-4 items-center justify-center rounded-full border', |
| 82 | + isSelected ? 'bg-primary text-primary-foreground' : 'opacity-50 [&_svg]:invisible' |
| 83 | + )} |
| 84 | + > |
| 85 | + <Check class="mt-0.5 size-3" /> |
| 86 | + </div> |
| 87 | + {#if option.icon} |
| 88 | + {@const Icon = option.icon} |
| 89 | + <Icon class="text-muted-foreground" /> |
| 90 | + {/if} |
| 91 | + |
| 92 | + <span>{option.label}</span> |
| 93 | + </Command.Item> |
| 94 | + {/each} |
| 95 | + </Command.Group> |
| 96 | + </Command.List> |
| 97 | + </Command.Root> |
| 98 | + </Popover.Content> |
| 99 | +</Popover.Root> |
0 commit comments