|
1 | 1 | <script lang="ts"> |
2 | | - import { mdiClose, mdiFilter, mdiSort } from '@mdi/js'; |
| 2 | + import type { SizeOptions } from '@floating-ui/dom'; |
| 3 | + import { mdiClose, mdiFilter, mdiSort, mdiTagMultiple } from '@mdi/js'; |
3 | 4 | import { getTranslate } from '@tolgee/svelte'; |
4 | 5 |
|
5 | 6 | import Marquee from '$lib/components/Marquee.svelte'; |
6 | 7 | import SvgIcon from '$lib/components/SVGIcon.svelte'; |
7 | 8 | import Select from '$lib/components/Select.svelte'; |
8 | | - import { type FilterField, type OrderByField, filter, filterOptions, order, orderByOptions, search } from '$lib/store/modFiltersStore'; |
| 9 | + import { type PopupSettings, popup } from '$lib/skeletonExtensions'; |
| 10 | + import { type FilterField, type OrderByField, filter, filterOptions, order, orderByOptions, search, selectedTags } from '$lib/store/modFiltersStore'; |
| 11 | + import { tagSearchMode } from '$lib/store/settingsStore'; |
| 12 | + import { settings } from '$wailsjs/go/models'; |
| 13 | +
|
| 14 | + export let availableTags: { id: string, name: string }[] = []; |
| 15 | +
|
| 16 | + $: if ($selectedTags.length > 0) { |
| 17 | + $selectedTags = $selectedTags.filter((t) => availableTags.some((a) => a.id === t)); |
| 18 | + } |
| 19 | +
|
| 20 | + const tagPopupName = 'modsTagFilter'; |
| 21 | + const tagPopup: PopupSettings = { |
| 22 | + event: 'click', |
| 23 | + target: tagPopupName, |
| 24 | + placement: 'bottom-start', |
| 25 | + closeQuery: '', // keep open when clicking tags so user can multi-select |
| 26 | + middleware: { |
| 27 | + offset: 6, |
| 28 | + size: { |
| 29 | + apply({ availableHeight, elements }: { availableHeight: number; elements: { floating: HTMLElement } }) { |
| 30 | + Object.assign(elements.floating.style, { |
| 31 | + maxHeight: `calc(${availableHeight}px - 1rem)`, |
| 32 | + }); |
| 33 | + }, |
| 34 | + } as SizeOptions, |
| 35 | + shift: { padding: 0 }, |
| 36 | + }, |
| 37 | + }; |
| 38 | +
|
| 39 | + function toggleTag(tag: string) { |
| 40 | + if ($selectedTags.includes(tag)) { |
| 41 | + $selectedTags = $selectedTags.filter((t) => t !== tag); |
| 42 | + } else { |
| 43 | + $selectedTags = [...$selectedTags, tag]; |
| 44 | + } |
| 45 | + } |
9 | 46 |
|
10 | 47 | const { t } = getTranslate(); |
11 | 48 |
|
|
45 | 82 | <SvgIcon class="h-5 w-5 text-error-500/80" icon={mdiClose} /> |
46 | 83 | </button> |
47 | 84 | </div> |
| 85 | + <div class="relative !h-full"> |
| 86 | + <div class="h-full w-full" use:popup={tagPopup}> |
| 87 | + <button |
| 88 | + class="btn px-2 text-sm space-x-1 !h-full" |
| 89 | + aria-label={$t('mods-list-filter.tag.button-label', 'Filter by tags')} |
| 90 | + type="button" |
| 91 | + on:contextmenu|preventDefault={() => ($selectedTags = [])} |
| 92 | + > |
| 93 | + <SvgIcon class="h-5 w-5 shrink-0" icon={mdiTagMultiple} /> |
| 94 | + {#if $selectedTags.length > 0} |
| 95 | + <span class="text-primary-600 font-medium tabular-nums">{$selectedTags.length}</span> |
| 96 | + {/if} |
| 97 | + </button> |
| 98 | + </div> |
| 99 | + <div |
| 100 | + class="card min-w-[24rem] max-h-96 shadow-xl z-10 duration-0 !mt-0 hidden opacity-0 pointer-events-none inert flex flex-col" |
| 101 | + aria-multiselectable="true" |
| 102 | + data-popup={tagPopupName} |
| 103 | + role="listbox" |
| 104 | + > |
| 105 | + <div |
| 106 | + class="flex items-center gap-2 px-3 py-2 border-b border-surface-400-600-token shrink-0" |
| 107 | + aria-label={$t('mods-list-filter.tag.match-mode', 'Match mode')} |
| 108 | + role="group" |
| 109 | + > |
| 110 | + <button |
| 111 | + class="flex-1 px-3 py-1.5 text-sm rounded transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 {$tagSearchMode === settings.TagSearchMode.ALL ? 'text-primary-600 font-medium bg-surface-300/20' : 'text-surface-400-700-token hover:bg-surface-300/20'}" |
| 112 | + aria-pressed={$tagSearchMode === settings.TagSearchMode.ALL} |
| 113 | + type="button" |
| 114 | + on:click|stopPropagation={() => tagSearchMode.set(settings.TagSearchMode.ALL)} |
| 115 | + > |
| 116 | + {$t('mods-list-filter.tag.match-all', 'Match all')} |
| 117 | + </button> |
| 118 | + <button |
| 119 | + class="flex-1 px-3 py-1.5 text-sm rounded transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 {$tagSearchMode === settings.TagSearchMode.ANY ? 'text-primary-600 font-medium bg-surface-300/20' : 'text-surface-400-700-token hover:bg-surface-300/20'}" |
| 120 | + aria-pressed={$tagSearchMode === settings.TagSearchMode.ANY} |
| 121 | + type="button" |
| 122 | + on:click|stopPropagation={() => tagSearchMode.set(settings.TagSearchMode.ANY)} |
| 123 | + > |
| 124 | + {$t('mods-list-filter.tag.match-any', 'Match any')} |
| 125 | + </button> |
| 126 | + </div> |
| 127 | + <div class="overflow-y-auto min-h-0 flex-1"> |
| 128 | + {#if availableTags.length > 0} |
| 129 | + <div class="columns-3 [column-gap:0.5rem] min-h-0 p-2"> |
| 130 | + {#each availableTags as tag} |
| 131 | + <button |
| 132 | + class="w-full text-left px-3 py-2 text-sm transition-colors rounded-none {$selectedTags.includes(tag.id) ? 'bg-surface-300/20' : 'bg-surface-50-900-token hover:!bg-surface-300/20'} flex items-center gap-2 break-inside-avoid" |
| 133 | + aria-selected={$selectedTags.includes(tag.id)} |
| 134 | + role="option" |
| 135 | + type="button" |
| 136 | + on:click={() => toggleTag(tag.id)} |
| 137 | + > |
| 138 | + {#if $selectedTags.includes(tag.id)} |
| 139 | + <span class="text-primary-600 font-medium" aria-hidden="true">✓</span> |
| 140 | + {/if} |
| 141 | + <span class="{$selectedTags.includes(tag.id) ? 'font-medium' : ''}">{tag.name}</span> |
| 142 | + </button> |
| 143 | + {/each} |
| 144 | + </div> |
| 145 | + {:else} |
| 146 | + <div class="px-3 py-2 text-sm text-surface-400-600-token"> |
| 147 | + {$t('mods-list-filter.tag.none-available', 'No tags available')} |
| 148 | + </div> |
| 149 | + {/if} |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + </div> |
48 | 153 | <Select |
49 | 154 | name="modsFilter" |
50 | 155 | class="!h-full" |
|
0 commit comments