forked from theelims/ESP32-sveltekit
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Searchable drop-downs #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
88a500e
Physical and Virtual layer reorg, add layer tests
ewowi 8bed3a7
Fix doctest test_layers
ewowi 32a6c1b
Add lps_all, drivers and effects
ewowi 57cfccd
forEachLightIndex without check
ewowi 7ce8d5d
Palette gradients in dropdown
ewowi 85c68f3
FastLED driver non blocking, add frontend Module and monitor tests
ewowi 2e8ff85
Code style issues fixed
ewowi 10c4331
Implement searchable dropdowns for palette and node name, latest FastLED
ewowi 1065ecd
Searchable dropdown, key handles + small improvements
ewowi fbf20fa
🐰 advices
ewowi 002f973
More 🐰 stuff
ewowi ec450e6
final commit
ewowi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
242 changes: 242 additions & 0 deletions
242
interface/src/lib/components/moonbase/SearchableDropdown.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| <!-- | ||
| @title MoonBase | ||
| @file SearchableDropdown.svelte | ||
| @repo https://github.com/MoonModules/MoonLight, submit changes to this file as PRs | ||
| @Authors https://github.com/MoonModules/MoonLight/commits/main | ||
| @Copyright © 2026 GitHub MoonLight Commit Authors | ||
| @license GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 | ||
| @license For non GPL-v3 usage, commercial licenses must be purchased. Contact us for more information. | ||
|
|
||
| Reusable searchable dropdown with category tabs and tag cloud filtering. | ||
| Used by FieldRenderer for both node selector (selectFile) and palette selector. | ||
| --> | ||
|
|
||
| <script lang="ts"> | ||
| import { onMount, onDestroy, tick } from 'svelte'; | ||
| import { positionDropdown, extractEmojis } from '$lib/stores/moonbase_utilities'; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| export let values: any[] = []; | ||
| export let isSelected: (val: any, index: number) => boolean; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
| export let onSelect: (val: any, index: number, event: Event) => void; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
| export let disabled = false; | ||
| export let showTags = false; | ||
| export let minWidth = 'min-w-72'; | ||
|
|
||
| let open = false; | ||
| let dropdownEl: HTMLElement | undefined; | ||
| let listEl: HTMLElement | undefined; | ||
| let search = ''; | ||
| let categoryFilter = ''; | ||
| let tagFilter = ''; | ||
| let activeIndex = -1; | ||
|
|
||
| $: categories = [ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| ...new Set(values.map((v: any) => v.category).filter((c: string) => c)) | ||
| ] as string[]; | ||
|
|
||
| $: tags = showTags | ||
| ? (() => { | ||
| const t = new Set<string>(); | ||
| for (const v of values) { | ||
| for (const e of extractEmojis(v.name || '')) t.add(e); | ||
| } | ||
| return [...t]; | ||
| })() | ||
| : []; | ||
|
|
||
| $: filtered = values | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| .map((v: any, i: number) => ({ ...v, _sd_idx: i })) | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| .filter((v: any) => { | ||
| const name: string = v.name || ''; | ||
| if (categoryFilter && v.category !== categoryFilter) return false; | ||
| if (tagFilter && !extractEmojis(name).includes(tagFilter)) return false; | ||
| if (search && !name.toLowerCase().includes(search.toLowerCase())) return false; | ||
| return true; | ||
| }); | ||
|
|
||
| async function openDropdown() { | ||
| open = true; | ||
| search = ''; | ||
| activeIndex = -1; | ||
| await tick(); | ||
| if (!listEl || !dropdownEl) return; | ||
| const triggerEl = dropdownEl.querySelector('button') as HTMLElement; | ||
| positionDropdown(triggerEl, listEl); | ||
| // Initialise activeIndex to the currently selected item | ||
| activeIndex = filtered.findIndex((v) => isSelected(v, v._sd_idx)); | ||
| // Scroll selected item to center | ||
| const selectedEl = listEl.querySelector('[aria-selected="true"]') as HTMLElement | null; | ||
| if (selectedEl) { | ||
| const listHeight = listEl.clientHeight; | ||
| listEl.scrollTop = selectedEl.offsetTop - listHeight / 2 + selectedEl.offsetHeight / 2; | ||
| } | ||
| // Focus search input | ||
| const searchInput = listEl.querySelector('input[type="text"]') as HTMLInputElement; | ||
| if (searchInput) searchInput.focus(); | ||
| } | ||
|
|
||
| export function toggle() { | ||
| if (!disabled) { | ||
| if (!open) openDropdown(); | ||
| else open = false; | ||
| } | ||
| } | ||
|
|
||
| function closeOnOutsideClick(e: MouseEvent) { | ||
| if (open && dropdownEl && !dropdownEl.contains(e.target as Node)) { | ||
| open = false; | ||
| } | ||
| } | ||
|
|
||
| function handleKeydown(e: KeyboardEvent) { | ||
| if (!open) return; | ||
| if (e.key === 'Escape') { | ||
| e.preventDefault(); | ||
| open = false; | ||
| const triggerEl = dropdownEl?.querySelector('button') as HTMLElement | null; | ||
| triggerEl?.focus(); | ||
| } else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { | ||
| e.preventDefault(); | ||
| if (!listEl) return; | ||
| const options = Array.from(listEl.querySelectorAll('[role="option"]')) as HTMLElement[]; | ||
| const count = options.length; | ||
| if (count === 0) return; | ||
| activeIndex = | ||
| e.key === 'ArrowDown' ? Math.min(activeIndex + 1, count - 1) : Math.max(activeIndex - 1, 0); | ||
| options[activeIndex]?.focus(); | ||
| } else if (e.key === 'Enter' && activeIndex >= 0) { | ||
| e.preventDefault(); | ||
| const val = filtered[activeIndex]; | ||
| if (val) { | ||
| open = false; | ||
| onSelect(val, val._sd_idx, e as unknown as Event); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function handleTriggerKeydown(e: KeyboardEvent) { | ||
| if (disabled) return; | ||
| if (e.key === 'ArrowDown' || e.key === 'Enter' || e.key === ' ') { | ||
| e.preventDefault(); | ||
| if (!open) openDropdown(); | ||
| } | ||
| } | ||
|
|
||
| onMount(() => { | ||
| window.addEventListener('mousedown', closeOnOutsideClick); | ||
| window.addEventListener('keydown', handleKeydown); | ||
| }); | ||
|
|
||
| onDestroy(() => { | ||
| window.removeEventListener('mousedown', closeOnOutsideClick); | ||
| window.removeEventListener('keydown', handleKeydown); | ||
| }); | ||
|
|
||
| // Sticky top offset depends on how many header rows are visible | ||
| $: hasCategoryRow = categories.length > 1; | ||
| $: hasTagRow = tags.length > 0; | ||
| // search bar is ~2.5rem, category row is ~2.25rem | ||
| $: tagTopPx = hasCategoryRow ? '4.75rem' : '2.5rem'; | ||
| </script> | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| <div class="relative" bind:this={dropdownEl}> | ||
| <button | ||
| type="button" | ||
| class="select flex {minWidth} cursor-pointer items-center gap-2" | ||
| {disabled} | ||
| aria-haspopup="listbox" | ||
| aria-expanded={open} | ||
| onclick={() => toggle()} | ||
| onkeydown={handleTriggerKeydown} | ||
| > | ||
| <slot name="trigger"> | ||
| <span class="flex-1 truncate text-left text-sm">Select...</span> | ||
| </slot> | ||
| <span class="ml-1 text-xs opacity-60">▾</span> | ||
| </button> | ||
| {#if open} | ||
| <div | ||
| bind:this={listEl} | ||
| role="listbox" | ||
| class="border-base-300 bg-base-100 z-50 {minWidth} max-h-96 overflow-y-auto rounded border shadow-xl" | ||
| > | ||
| <!-- Search input --> | ||
| <div class="border-base-300 sticky top-0 z-10 border-b p-2"> | ||
| <input | ||
| type="text" | ||
| class="input input-sm w-full" | ||
| placeholder="Search..." | ||
| bind:value={search} | ||
| /> | ||
| </div> | ||
| <!-- Category tabs --> | ||
| {#if hasCategoryRow} | ||
| <div class="border-base-300 sticky top-10 z-10 flex flex-wrap gap-1 border-b p-1"> | ||
| <button | ||
| type="button" | ||
| class="btn btn-xs {categoryFilter === '' ? 'btn-primary' : 'btn-ghost'}" | ||
| onclick={() => { | ||
| categoryFilter = ''; | ||
| }}>All</button | ||
| > | ||
| {#each categories as cat (cat)} | ||
| <button | ||
| type="button" | ||
| class="btn btn-xs {categoryFilter === cat ? 'btn-primary' : 'btn-ghost'}" | ||
| onclick={() => { | ||
| categoryFilter = categoryFilter === cat ? '' : cat; | ||
| }}>{cat}</button | ||
| > | ||
| {/each} | ||
| </div> | ||
| {/if} | ||
| <!-- Tag cloud --> | ||
| {#if hasTagRow} | ||
| <div | ||
| class="border-base-300 sticky z-10 flex flex-wrap gap-1 border-b p-1" | ||
| style="top: {tagTopPx}" | ||
| > | ||
| {#each tags as tag (tag)} | ||
| <button | ||
| type="button" | ||
| class="btn btn-xs btn-circle {tagFilter === tag ? 'btn-accent' : 'btn-ghost'}" | ||
| onclick={() => { | ||
| tagFilter = tagFilter === tag ? '' : tag; | ||
| }} | ||
| title={tag}>{tag}</button | ||
| > | ||
| {/each} | ||
| </div> | ||
| {/if} | ||
| <!-- Items --> | ||
| {#each filtered as val (val._sd_idx)} | ||
| <button | ||
| type="button" | ||
| role="option" | ||
| aria-selected={isSelected(val, val._sd_idx)} | ||
| class="hover:bg-base-200 flex w-full cursor-pointer items-center gap-2 px-2 py-1.5 {isSelected( | ||
| val, | ||
| val._sd_idx | ||
| ) | ||
| ? 'bg-base-300' | ||
| : ''}" | ||
| onclick={(event) => { | ||
| open = false; | ||
| onSelect(val, val._sd_idx, event); | ||
| }} | ||
| > | ||
| <slot name="item" {val} index={val._sd_idx}> | ||
| <span class="truncate text-sm">{val.name}</span> | ||
| </slot> | ||
| </button> | ||
ewowi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {/each} | ||
| {#if filtered.length === 0} | ||
| <div class="p-2 text-sm opacity-50">No matches</div> | ||
| {/if} | ||
| </div> | ||
| {/if} | ||
| </div> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.