73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
import { memo } from 'react';
|
|
import type { CSSProperties } from 'react';
|
|
import { IonButton, IonIcon, IonPopover } from '@ionic/react';
|
|
import { chevronDown } from 'ionicons/icons';
|
|
import { MAX_TEMPO_BPM, MIN_TEMPO_BPM } from '../domain/sequencer';
|
|
import { usePopoverDisclosure } from '../hooks/usePopoverDisclosure';
|
|
|
|
interface TempoControlProps {
|
|
tempoBpm: number;
|
|
onTempoChange: (tempoBpm: number) => void;
|
|
}
|
|
|
|
const TEMPO_TRACK_HEIGHT = 164;
|
|
const TEMPO_THUMB_SIZE = 18;
|
|
|
|
export const TempoControl = memo(function TempoControl({ tempoBpm, onTempoChange }: TempoControlProps) {
|
|
const popover = usePopoverDisclosure();
|
|
const tempoProgress = ((tempoBpm - MIN_TEMPO_BPM) / (MAX_TEMPO_BPM - MIN_TEMPO_BPM)) * 100;
|
|
const tempoThumbCenter = TEMPO_THUMB_SIZE / 2
|
|
+ ((100 - tempoProgress) / 100) * (TEMPO_TRACK_HEIGHT - TEMPO_THUMB_SIZE);
|
|
|
|
return (
|
|
<>
|
|
<IonButton
|
|
className="tempo-trigger"
|
|
fill="solid"
|
|
onClick={(event) => popover.open(event.nativeEvent)}
|
|
aria-label={`Change tempo, currently ${tempoBpm} BPM`}
|
|
aria-haspopup="dialog"
|
|
aria-expanded={popover.isOpen}
|
|
>
|
|
<span className="tempo-trigger-content">
|
|
<span>{tempoBpm} BPM</span>
|
|
<IonIcon icon={chevronDown} />
|
|
</span>
|
|
</IonButton>
|
|
|
|
<IonPopover
|
|
className="tempo-popover"
|
|
isOpen={popover.isOpen}
|
|
event={popover.event}
|
|
reference="event"
|
|
side="top"
|
|
alignment="start"
|
|
onDidDismiss={popover.onDidDismiss}
|
|
>
|
|
<div className="tempo-popover-content">
|
|
<output className="tempo-value" aria-live="polite">{tempoBpm} BPM</output>
|
|
<div
|
|
className="tempo-range-track"
|
|
style={{
|
|
'--tempo-progress': `${tempoProgress}%`,
|
|
'--tempo-thumb-center': `${tempoThumbCenter}px`,
|
|
} as CSSProperties}
|
|
>
|
|
<input
|
|
className="tempo-range"
|
|
type="range"
|
|
aria-label="Tempo in beats per minute"
|
|
min={MIN_TEMPO_BPM}
|
|
max={MAX_TEMPO_BPM}
|
|
step={1}
|
|
value={tempoBpm}
|
|
onChange={(event) => onTempoChange(event.currentTarget.valueAsNumber)}
|
|
/>
|
|
<span className="tempo-range-thumb" aria-hidden="true" />
|
|
</div>
|
|
</div>
|
|
</IonPopover>
|
|
</>
|
|
);
|
|
});
|