From fa42c6f8a00719f67db7d3bd9469496500059527 Mon Sep 17 00:00:00 2001 From: Ninosaurier Date: Wed, 18 Nov 2026 11:30:32 +0100 Subject: [PATCH] Add searchable sample picker --- src/components/InstrumentSelector.tsx | 169 +++++++++++++++++++------- src/styles.css | 131 +++++++++++++------- 2 files changed, 209 insertions(+), 91 deletions(-) diff --git a/src/components/InstrumentSelector.tsx b/src/components/InstrumentSelector.tsx index 6461ea6..ea0594f 100644 --- a/src/components/InstrumentSelector.tsx +++ b/src/components/InstrumentSelector.tsx @@ -1,5 +1,14 @@ -import { useRef, useState } from 'react'; -import { IonButton, IonIcon, IonPopover, IonSelect, IonSelectOption } from '@ionic/react'; +import { useMemo, useRef, useState } from 'react'; +import { + IonAccordion, + IonAccordionGroup, + IonButton, + IonIcon, + IonItem, + IonLabel, + IonPopover, + IonSearchbar, +} from '@ionic/react'; import { add } from 'ionicons/icons'; import { INSTRUMENT_BY_ID, @@ -14,17 +23,67 @@ interface InstrumentSelectorProps { } export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorProps) { - const [selectedCategoryId, setSelectedCategoryId] = useState('drums'); + const [searchQuery, setSearchQuery] = useState(''); + const [expandedCategoryIds, setExpandedCategoryIds] = useState([]); const popoverRef = useRef(null); const popover = usePopoverDisclosure(); - const selectedCategory = INSTRUMENT_CATEGORIES.find( - (category) => category.id === selectedCategoryId, - ) ?? INSTRUMENT_CATEGORIES[0]; - function handleCategoryChange(value: unknown): void { - if (typeof value !== 'string') return; - const category = INSTRUMENT_CATEGORIES.find((candidate) => candidate.id === value); - if (category) setSelectedCategoryId(category.id); + const filteredCategories = useMemo(() => { + const query = searchQuery.trim().toLowerCase(); + return INSTRUMENT_CATEGORIES.flatMap((category) => { + const categoryMatches = category.label.toLowerCase().includes(query); + const instruments = category.instrumentIds + .map((instrumentId) => INSTRUMENT_BY_ID[instrumentId]) + .filter((instrument) => ( + !query + || categoryMatches + || `${instrument.label} ${instrument.description}`.toLowerCase().includes(query) + )); + + return instruments.length > 0 ? [{ category, instruments }] : []; + }); + }, [searchQuery]); + + function handleSearchInput(value: string | null | undefined): void { + const nextQuery = value ?? ''; + const normalizedQuery = nextQuery.trim().toLowerCase(); + setSearchQuery(nextQuery); + + if (!normalizedQuery) { + setExpandedCategoryIds([]); + return; + } + + setExpandedCategoryIds( + INSTRUMENT_CATEGORIES + .filter((category) => ( + category.label.toLowerCase().includes(normalizedQuery) + || category.instrumentIds.some((instrumentId) => { + const instrument = INSTRUMENT_BY_ID[instrumentId]; + return `${instrument.label} ${instrument.description}` + .toLowerCase() + .includes(normalizedQuery); + }) + )) + .map((category) => category.id), + ); + } + + function handleAccordionChange(value: unknown): void { + if (Array.isArray(value)) { + setExpandedCategoryIds(value.filter((categoryId): categoryId is string => ( + typeof categoryId === 'string' + ))); + return; + } + + setExpandedCategoryIds(typeof value === 'string' ? [value] : []); + } + + function handlePopoverDidDismiss(): void { + popover.onDidDismiss(); + setSearchQuery(''); + setExpandedCategoryIds([]); } function addInstrument(value: unknown): void { @@ -36,30 +95,16 @@ export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorPro return (
- handleCategoryChange(event.detail.value)} - > - {INSTRUMENT_CATEGORIES.map((category) => ( - - {category.label} - - ))} - - - + + Samples -
- {selectedCategory.instrumentIds.map((instrumentId) => { - const instrument = INSTRUMENT_BY_ID[instrumentId]; - return ( - addInstrument(instrumentId)} - > - - {instrument.label} - {instrument.description} - - - ); - })} +
+ handleSearchInput(event.detail.value)} + onIonClear={() => handleSearchInput('')} + /> + + {filteredCategories.length > 0 ? ( + handleAccordionChange(event.detail.value)} + > + {filteredCategories.map(({ category, instruments }) => ( + + + {category.label} + +
+ {instruments.map((instrument) => ( + addInstrument(instrument.id)} + > + + {instrument.label} + {instrument.description} + + + ))} +
+
+ ))} +
+ ) : ( +

No matching samples.

+ )}
diff --git a/src/styles.css b/src/styles.css index 44eb3c5..517d5aa 100644 --- a/src/styles.css +++ b/src/styles.css @@ -832,54 +832,36 @@ ion-content.app-content { font-size: 17px; } -.instrument-selector { - width: 170px; - min-width: 140px; - height: 44px; - min-height: 44px; - flex: 1 1 150px; - margin: 0; - --background: var(--surface-raised); - --border-radius: 9px; - --color: var(--ink); - --padding-start: 11px; - --padding-end: 9px; - --placeholder-color: var(--ink); - --placeholder-opacity: 1; - font-size: 11px; - font-weight: 600; -} - -.instrument-selector::part(icon) { - color: var(--muted); - opacity: 1; -} - .instrument-selector-group { display: flex; min-width: 0; - flex: 0 1 auto; + flex: 0 0 auto; align-items: center; - gap: 6px; + margin-left: auto; } .instrument-add-button { - width: 44px; + min-width: 104px; height: 44px; - flex: 0 0 44px; + flex: 0 0 auto; margin: 0; --background: var(--surface-raised); --background-hover: #33323b; --background-activated: #3b3944; --border-radius: 9px; --color: var(--ink); - --padding-start: 0; - --padding-end: 0; + --padding-start: 12px; + --padding-end: 12px; + font-size: 11px; + font-weight: 600; + text-transform: none; } .instrument-add-button::part(native) { - width: 44px; + width: 100%; height: 44px; + gap: 6px; + padding-inline: 12px; } .instrument-add-button ion-icon { @@ -887,7 +869,7 @@ ion-content.app-content { } ion-popover.instrument-picker-popover { - --width: min(250px, calc(100vw - 24px)); + --width: min(420px, calc(100vw - 24px)); --background: var(--surface); --backdrop-opacity: 0; --box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45); @@ -895,23 +877,87 @@ ion-popover.instrument-picker-popover { .instrument-picker-menu { display: flex; + width: 100%; max-height: min(60vh, 420px); + min-height: 0; + box-sizing: border-box; flex-direction: column; - gap: 4px; - overflow-y: auto; + gap: 6px; + overflow: hidden; overscroll-behavior: contain; padding: 8px; } +.instrument-picker-search { + width: 100%; + flex: 0 0 auto; + min-height: 40px; + padding: 0; + --background: var(--surface-raised); + --border-radius: 8px; + --box-shadow: none; + --color: var(--ink); + --icon-color: var(--muted); + --placeholder-color: var(--soft); +} + +.instrument-picker-accordion-group { + width: 100%; + flex: 1 1 auto; + min-height: 0; + overflow-y: auto; + overscroll-behavior: contain; +} + +.instrument-picker-category { + display: block; + width: 100%; + margin-bottom: 4px; + overflow: hidden; + border-radius: 8px; + background: var(--surface-raised); +} + +.instrument-category-header { + --background: var(--surface-raised); + --color: var(--ink); + --min-height: 40px; + font-size: 11px; + font-weight: 600; +} + +.instrument-category-header::part(native) { + min-height: 40px; + padding-inline: 10px; +} + +.instrument-category-content { + display: flex; + flex-direction: column; + gap: 6px; + padding: 6px 8px 10px; +} + +.instrument-picker-empty { + margin: 0; + padding: 12px 6px; + color: var(--soft); + font-size: 11px; + text-align: center; +} + .instrument-picker-option { width: 100%; - height: 48px; - flex: 0 0 48px; + height: 52px; + flex: 0 0 52px; margin: 0; - --background: var(--surface-raised); - --background-hover: #33323b; + --background: var(--surface); + --background-hover: var(--surface-raised); --background-activated: #3b3944; - --border-radius: 8px; + --border-color: rgba(var(--primary-rgb), 0.12); + --border-style: solid; + --border-width: 1px; + --border-radius: 10px; --color: var(--ink); font-size: 11px; text-transform: none; @@ -1294,14 +1340,9 @@ ion-popover.instrument-picker-popover { min-width: 98px; } - .instrument-selector { - min-width: 130px; - font-size: 10px; - } - .instrument-selector-group { - min-width: 182px; - flex: 1 1 182px; + min-width: 0; + flex: 0 0 auto; } .pitch-control {