97 lines
3.5 KiB
TypeScript
97 lines
3.5 KiB
TypeScript
import { memo } from 'react';
|
|
import { IonButton, IonIcon, IonPopover } from '@ionic/react';
|
|
import { chevronDown } from 'ionicons/icons';
|
|
import { INSTRUMENT_CATEGORY_BY_ID } from '../domain/instruments';
|
|
import type { InstrumentCategoryId } from '../domain/instruments';
|
|
import type { CategoryLabelOverrides } from '../hooks/useCategoryLabels';
|
|
import { usePopoverDisclosure } from '../hooks/usePopoverDisclosure';
|
|
|
|
interface CategoryRingSelectorProps {
|
|
categoryIds: readonly InstrumentCategoryId[];
|
|
focusedCategoryId: InstrumentCategoryId | null;
|
|
colorByCategoryId: Partial<Record<InstrumentCategoryId, string>>;
|
|
labelByCategoryId: CategoryLabelOverrides;
|
|
onSelectCategory: (categoryId: InstrumentCategoryId) => void;
|
|
}
|
|
|
|
export const CategoryRingSelector = memo(function CategoryRingSelector({
|
|
categoryIds,
|
|
focusedCategoryId,
|
|
colorByCategoryId,
|
|
labelByCategoryId,
|
|
onSelectCategory,
|
|
}: CategoryRingSelectorProps) {
|
|
const popover = usePopoverDisclosure();
|
|
const focusedCategory = focusedCategoryId
|
|
? INSTRUMENT_CATEGORY_BY_ID[focusedCategoryId]
|
|
: undefined;
|
|
const focusedLabel = focusedCategory
|
|
? labelByCategoryId[focusedCategory.id] ?? focusedCategory.label
|
|
: undefined;
|
|
|
|
return (
|
|
<>
|
|
<IonButton
|
|
id="category-ring-selector-trigger"
|
|
className="sound-ring-selector-trigger"
|
|
fill="clear"
|
|
disabled={categoryIds.length === 0}
|
|
aria-label={focusedCategory ? `Selected ring: ${focusedLabel}` : 'Select a category ring'}
|
|
aria-haspopup="dialog"
|
|
aria-expanded={popover.isOpen}
|
|
>
|
|
<span className="sound-ring-trigger-content">
|
|
{focusedCategory && (
|
|
<>
|
|
<span
|
|
className="ring-selector-swatch"
|
|
style={{ backgroundColor: colorByCategoryId[focusedCategory.id] ?? '#b8b4e0' }}
|
|
/>
|
|
<span className="category-letter-badge">{focusedCategory.letter}</span>
|
|
</>
|
|
)}
|
|
<span>{focusedLabel ? `${focusedLabel} ring` : 'Category rings'}</span>
|
|
<IonIcon icon={chevronDown} />
|
|
</span>
|
|
</IonButton>
|
|
|
|
<IonPopover
|
|
className="sound-ring-popover"
|
|
trigger="category-ring-selector-trigger"
|
|
triggerAction="click"
|
|
side="bottom"
|
|
alignment="end"
|
|
dismissOnSelect
|
|
onDidPresent={popover.onDidPresent}
|
|
onDidDismiss={popover.onDidDismiss}
|
|
>
|
|
<div className="sound-ring-options" role="group" aria-label="Category rings">
|
|
{categoryIds.map((categoryId) => {
|
|
const category = INSTRUMENT_CATEGORY_BY_ID[categoryId];
|
|
const label = labelByCategoryId[categoryId] ?? category.label;
|
|
const selected = categoryId === focusedCategoryId;
|
|
return (
|
|
<IonButton
|
|
key={categoryId}
|
|
className="sound-ring-option"
|
|
fill={selected ? 'solid' : 'clear'}
|
|
aria-pressed={selected}
|
|
onClick={() => onSelectCategory(categoryId)}
|
|
>
|
|
<span className="sound-ring-option-content">
|
|
<span
|
|
className="ring-selector-swatch"
|
|
style={{ backgroundColor: colorByCategoryId[categoryId] ?? '#b8b4e0' }}
|
|
/>
|
|
<span className="category-letter-badge">{category.letter}</span>
|
|
<span>{label} ring</span>
|
|
</span>
|
|
</IonButton>
|
|
);
|
|
})}
|
|
</div>
|
|
</IonPopover>
|
|
</>
|
|
);
|
|
});
|