Fix sample picker and tempo slider alignment
This commit is contained in:
1 parent
302128fdbc
commit
3caf21794d
5 files changed
+134
-143
No files matched your search
@@ -1,13 +1,13 @@
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import type { CSSProperties } from 'react';
|
||||
import { useMemo, useRef, useState } from 'react';
|
||||
import {
|
||||
IonAccordion,
|
||||
IonAccordionGroup,
|
||||
IonButton,
|
||||
IonContent,
|
||||
IonIcon,
|
||||
IonItem,
|
||||
IonLabel,
|
||||
IonPopover,
|
||||
IonModal,
|
||||
IonSearchbar,
|
||||
} from '@ionic/react';
|
||||
import { add } from 'ionicons/icons';
|
||||
@@ -17,7 +17,8 @@ import {
|
||||
isInstrumentId,
|
||||
} from '../domain/instruments';
|
||||
import type { InstrumentId } from '../domain/instruments';
|
||||
import { usePopoverDisclosure } from '../hooks/usePopoverDisclosure';
|
||||
|
||||
const PICKER_SHEET_BREAKPOINTS = [0, 0.82, 1];
|
||||
|
||||
interface InstrumentSelectorProps {
|
||||
onSelectInstrument: (instrumentId: InstrumentId) => void;
|
||||
@@ -26,27 +27,8 @@ interface InstrumentSelectorProps {
|
||||
export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorProps) {
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const [expandedCategoryIds, setExpandedCategoryIds] = useState<string[]>([]);
|
||||
const [viewportHeight, setViewportHeight] = useState(
|
||||
() => window.visualViewport?.height ?? window.innerHeight,
|
||||
);
|
||||
const popoverRef = useRef<HTMLIonPopoverElement>(null);
|
||||
const popover = usePopoverDisclosure();
|
||||
|
||||
useEffect(() => {
|
||||
const visualViewport = window.visualViewport;
|
||||
|
||||
function updateViewportHeight(): void {
|
||||
setViewportHeight(visualViewport?.height ?? window.innerHeight);
|
||||
}
|
||||
|
||||
window.addEventListener('resize', updateViewportHeight);
|
||||
visualViewport?.addEventListener('resize', updateViewportHeight);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('resize', updateViewportHeight);
|
||||
visualViewport?.removeEventListener('resize', updateViewportHeight);
|
||||
};
|
||||
}, []);
|
||||
const [isPickerOpen, setIsPickerOpen] = useState(false);
|
||||
const modalRef = useRef<HTMLIonModalElement>(null);
|
||||
|
||||
const filteredCategories = useMemo(() => {
|
||||
const query = searchQuery.trim().toLowerCase();
|
||||
@@ -100,8 +82,8 @@ export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorPro
|
||||
setExpandedCategoryIds(typeof value === 'string' ? [value] : []);
|
||||
}
|
||||
|
||||
function handlePopoverDidDismiss(): void {
|
||||
popover.onDidDismiss();
|
||||
function handlePickerDidDismiss(): void {
|
||||
setIsPickerOpen(false);
|
||||
setSearchQuery('');
|
||||
setExpandedCategoryIds([]);
|
||||
}
|
||||
@@ -109,7 +91,7 @@ export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorPro
|
||||
function addInstrument(value: unknown): void {
|
||||
if (!isInstrumentId(value)) return;
|
||||
|
||||
void popoverRef.current?.dismiss();
|
||||
void modalRef.current?.dismiss();
|
||||
onSelectInstrument(value);
|
||||
}
|
||||
|
||||
@@ -119,77 +101,80 @@ export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorPro
|
||||
id="instrument-add-trigger"
|
||||
className="instrument-add-button"
|
||||
fill="solid"
|
||||
onClick={() => setIsPickerOpen(true)}
|
||||
aria-label="Open samples"
|
||||
aria-haspopup="dialog"
|
||||
aria-expanded={popover.isOpen}
|
||||
aria-expanded={isPickerOpen}
|
||||
>
|
||||
<IonIcon icon={add} slot="start" />
|
||||
Samples
|
||||
</IonButton>
|
||||
|
||||
<IonPopover
|
||||
ref={popoverRef}
|
||||
className="instrument-picker-popover"
|
||||
style={{ '--picker-viewport-height': `${viewportHeight}px` } as CSSProperties}
|
||||
trigger="instrument-add-trigger"
|
||||
triggerAction="click"
|
||||
side="top"
|
||||
alignment="start"
|
||||
onDidPresent={popover.onDidPresent}
|
||||
onDidDismiss={handlePopoverDidDismiss}
|
||||
<IonModal
|
||||
ref={modalRef}
|
||||
className="instrument-picker-modal"
|
||||
isOpen={isPickerOpen}
|
||||
breakpoints={PICKER_SHEET_BREAKPOINTS}
|
||||
initialBreakpoint={0.82}
|
||||
expandToScroll={false}
|
||||
handle
|
||||
onDidDismiss={handlePickerDidDismiss}
|
||||
>
|
||||
<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('')}
|
||||
/>
|
||||
<IonContent className="instrument-picker-content" scrollY={false}>
|
||||
<div className="instrument-picker-menu">
|
||||
<IonSearchbar
|
||||
className="instrument-picker-search"
|
||||
aria-label="Search samples"
|
||||
placeholder="Search samples"
|
||||
value={searchQuery}
|
||||
debounce={0}
|
||||
showClearButton="always"
|
||||
onIonFocus={() => void modalRef.current?.setCurrentBreakpoint(1)}
|
||||
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>
|
||||
{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>
|
||||
</IonContent>
|
||||
</IonModal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user