86 lines
2.8 KiB
TypeScript
86 lines
2.8 KiB
TypeScript
import { memo } from 'react';
|
|
import { IonButton, IonIcon, IonPopover } from '@ionic/react';
|
|
import { chevronDown } from 'ionicons/icons';
|
|
import { INSTRUMENT_BY_ID } from '../domain/instruments';
|
|
import type { InstrumentId } from '../domain/instruments';
|
|
import { usePopoverDisclosure } from '../hooks/usePopoverDisclosure';
|
|
|
|
interface SoundRingSelectorProps {
|
|
soundIds: readonly InstrumentId[];
|
|
focusedSoundId: InstrumentId | null;
|
|
colorBySoundId: Partial<Record<InstrumentId, string>>;
|
|
onSelectSound: (soundId: InstrumentId) => void;
|
|
}
|
|
|
|
export const SoundRingSelector = memo(function SoundRingSelector({
|
|
soundIds,
|
|
focusedSoundId,
|
|
colorBySoundId,
|
|
onSelectSound,
|
|
}: SoundRingSelectorProps) {
|
|
const popover = usePopoverDisclosure();
|
|
const focusedSound = focusedSoundId ? INSTRUMENT_BY_ID[focusedSoundId] : undefined;
|
|
|
|
return (
|
|
<>
|
|
<IonButton
|
|
className="sound-ring-selector-trigger"
|
|
fill="clear"
|
|
disabled={soundIds.length === 0}
|
|
onClick={(event) => popover.open(event.nativeEvent)}
|
|
aria-label={focusedSound ? `Selected ring: ${focusedSound.label}` : 'Select a sound ring'}
|
|
aria-haspopup="dialog"
|
|
aria-expanded={popover.isOpen}
|
|
>
|
|
<span className="sound-ring-trigger-content">
|
|
{focusedSoundId && (
|
|
<span
|
|
className="ring-selector-swatch"
|
|
style={{ backgroundColor: colorBySoundId[focusedSoundId] ?? '#b8b4e0' }}
|
|
/>
|
|
)}
|
|
<span>{focusedSound ? `${focusedSound.label} ring` : 'Sound rings'}</span>
|
|
<IonIcon icon={chevronDown} />
|
|
</span>
|
|
</IonButton>
|
|
|
|
<IonPopover
|
|
className="sound-ring-popover"
|
|
isOpen={popover.isOpen}
|
|
event={popover.event}
|
|
reference="event"
|
|
side="bottom"
|
|
alignment="end"
|
|
dismissOnSelect
|
|
onDidDismiss={popover.onDidDismiss}
|
|
>
|
|
<div className="sound-ring-options" role="group" aria-label="Sound rings">
|
|
{soundIds.map((soundId) => {
|
|
const selected = soundId === focusedSoundId;
|
|
return (
|
|
<IonButton
|
|
key={soundId}
|
|
className="sound-ring-option"
|
|
fill={selected ? 'solid' : 'clear'}
|
|
aria-pressed={selected}
|
|
onClick={() => {
|
|
popover.close();
|
|
onSelectSound(soundId);
|
|
}}
|
|
>
|
|
<span className="sound-ring-option-content">
|
|
<span
|
|
className="ring-selector-swatch"
|
|
style={{ backgroundColor: colorBySoundId[soundId] ?? '#b8b4e0' }}
|
|
/>
|
|
<span>{INSTRUMENT_BY_ID[soundId].label} ring</span>
|
|
</span>
|
|
</IonButton>
|
|
);
|
|
})}
|
|
</div>
|
|
</IonPopover>
|
|
</>
|
|
);
|
|
});
|