Add searchable sample picker
This commit is contained in:
1 parent
5425e1e3ff
commit
fa42c6f8a0
2 files changed
+209
-91
No files matched your search
@@ -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<string[]>([]);
|
||||
const popoverRef = useRef<HTMLIonPopoverElement>(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 (
|
||||
<div className="instrument-selector-group">
|
||||
<IonSelect
|
||||
className="instrument-selector"
|
||||
fill="solid"
|
||||
interface="popover"
|
||||
aria-label="Choose an instrument category"
|
||||
value={selectedCategoryId}
|
||||
onIonChange={(event) => handleCategoryChange(event.detail.value)}
|
||||
>
|
||||
{INSTRUMENT_CATEGORIES.map((category) => (
|
||||
<IonSelectOption key={category.id} value={category.id}>
|
||||
{category.label}
|
||||
</IonSelectOption>
|
||||
))}
|
||||
</IonSelect>
|
||||
|
||||
<IonButton
|
||||
id="instrument-add-trigger"
|
||||
className="instrument-add-button"
|
||||
fill="solid"
|
||||
aria-label={`Show ${selectedCategory.label} instruments`}
|
||||
aria-label="Open samples"
|
||||
aria-haspopup="dialog"
|
||||
aria-expanded={popover.isOpen}
|
||||
>
|
||||
<IonIcon icon={add} />
|
||||
<IonIcon icon={add} slot="start" />
|
||||
Samples
|
||||
</IonButton>
|
||||
|
||||
<IonPopover
|
||||
@@ -70,26 +115,58 @@ export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorPro
|
||||
side="top"
|
||||
alignment="start"
|
||||
onDidPresent={popover.onDidPresent}
|
||||
onDidDismiss={popover.onDidDismiss}
|
||||
onDidDismiss={handlePopoverDidDismiss}
|
||||
>
|
||||
<div className="instrument-picker-menu" role="group" aria-label={`${selectedCategory.label} instruments`}>
|
||||
{selectedCategory.instrumentIds.map((instrumentId) => {
|
||||
const instrument = INSTRUMENT_BY_ID[instrumentId];
|
||||
return (
|
||||
<IonButton
|
||||
className="instrument-picker-option"
|
||||
fill="clear"
|
||||
key={instrumentId}
|
||||
aria-label={`Add ${instrument.label}`}
|
||||
onClick={() => addInstrument(instrumentId)}
|
||||
>
|
||||
<span className="instrument-picker-option-copy">
|
||||
<span>{instrument.label}</span>
|
||||
<span>{instrument.description}</span>
|
||||
</span>
|
||||
</IonButton>
|
||||
);
|
||||
})}
|
||||
<div className="instrument-picker-menu">
|
||||
<IonSearchbar
|
||||
className="instrument-picker-search"
|
||||
aria-label="Search samples"
|
||||
placeholder="Search samples"
|
||||
value={searchQuery}
|
||||
debounce={0}
|
||||
showClearButton="always"
|
||||
onIonInput={(event) => handleSearchInput(event.detail.value)}
|
||||
onIonClear={() => handleSearchInput('')}
|
||||
/>
|
||||
|
||||
{filteredCategories.length > 0 ? (
|
||||
<IonAccordionGroup
|
||||
className="instrument-picker-accordion-group"
|
||||
multiple
|
||||
value={expandedCategoryIds}
|
||||
onIonChange={(event) => handleAccordionChange(event.detail.value)}
|
||||
>
|
||||
{filteredCategories.map(({ category, instruments }) => (
|
||||
<IonAccordion
|
||||
className="instrument-picker-category"
|
||||
key={category.id}
|
||||
value={category.id}
|
||||
>
|
||||
<IonItem className="instrument-category-header" slot="header" lines="none">
|
||||
<IonLabel>{category.label}</IonLabel>
|
||||
</IonItem>
|
||||
<div className="instrument-category-content" slot="content">
|
||||
{instruments.map((instrument) => (
|
||||
<IonButton
|
||||
className="instrument-picker-option"
|
||||
fill="solid"
|
||||
key={instrument.id}
|
||||
aria-label={`Add ${instrument.label}`}
|
||||
onClick={() => addInstrument(instrument.id)}
|
||||
>
|
||||
<span className="instrument-picker-option-copy">
|
||||
<span>{instrument.label}</span>
|
||||
<span>{instrument.description}</span>
|
||||
</span>
|
||||
</IonButton>
|
||||
))}
|
||||
</div>
|
||||
</IonAccordion>
|
||||
))}
|
||||
</IonAccordionGroup>
|
||||
) : (
|
||||
<p className="instrument-picker-empty" role="status">No matching samples.</p>
|
||||
)}
|
||||
</div>
|
||||
</IonPopover>
|
||||
</div>
|
||||
|
||||
+86
-45
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user