feat: allow volume button t control sliders on mobile
This commit is contained in:
@@ -17,6 +17,12 @@
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.choice-select.is-selected {
|
||||
outline: 2px solid var(--accent-brand);
|
||||
outline-offset: -2px;
|
||||
border-radius: 1.6cqh;
|
||||
}
|
||||
|
||||
.choice-select[data-disabled] {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { TriangleDownIcon } from '../../assets/icons/Triangle';
|
||||
import { useVolumeKeys } from '../../hooks/useVolumeKeys';
|
||||
import './ChoiceSelect.css';
|
||||
|
||||
type CommitResult = Promise<boolean | void> | boolean | void;
|
||||
@@ -299,6 +300,20 @@ function ChoiceSelect<T extends string>({
|
||||
return true;
|
||||
};
|
||||
|
||||
const { isSelected, select } = useVolumeKeys({
|
||||
rootRef,
|
||||
disabled,
|
||||
onStep: (volumeDirection) => {
|
||||
// Same mapping as the wheel: up = previous item, down = next item.
|
||||
const direction = volumeDirection === 1 ? -1 : 1;
|
||||
if (open) {
|
||||
setHighlight((h) => Math.min(options.length - 1, Math.max(0, h + direction)));
|
||||
} else if (step(direction)) {
|
||||
scheduleWheelCommit();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
useNonPassiveWheel(triggerRef, (e) => {
|
||||
e.preventDefault();
|
||||
if (disabled) return;
|
||||
@@ -347,10 +362,10 @@ function ChoiceSelect<T extends string>({
|
||||
preview(opt);
|
||||
commitLatest();
|
||||
} else {
|
||||
// Picking the already-shown value still flushes any wheel change that is waiting on its debounce.
|
||||
commitLatest();
|
||||
}
|
||||
setOpen(false);
|
||||
select();
|
||||
triggerRef.current?.focus();
|
||||
}
|
||||
|
||||
@@ -383,10 +398,11 @@ function ChoiceSelect<T extends string>({
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`choice-select ${className}`}
|
||||
className={`choice-select ${isSelected ? 'is-selected' : ''} ${className}`}
|
||||
data-disabled={disabled || undefined}
|
||||
ref={rootRef}
|
||||
style={style}
|
||||
onPointerDown={select}
|
||||
>
|
||||
<button
|
||||
id={id}
|
||||
|
||||
@@ -11,6 +11,20 @@
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.choice-stepper.is-selected {
|
||||
outline: 2px solid var(--accent-brand);
|
||||
outline-offset: -2px;
|
||||
border-radius: 1.6cqh;
|
||||
}
|
||||
|
||||
.choice-stepper.is-touch .choice-stepper__input {
|
||||
pointer-events: none; /* taps go to the root, so the whole stepper gets selected */
|
||||
caret-color: transparent;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.choice-stepper__input {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { JSX, useEffect, useRef, useState } from 'react';
|
||||
import Button from '../Button/Button';
|
||||
import { TriangleUpIcon, TriangleDownIcon } from '../../assets/icons/Triangle';
|
||||
import { useVolumeKeys } from '../../hooks/useVolumeKeys';
|
||||
import { useIsTouch } from '../../hooks/useIsTouch';
|
||||
import './ChoiceStepper.css';
|
||||
|
||||
const WHEEL_COMMIT_DELAY = 250;
|
||||
@@ -114,6 +116,8 @@ function StepperCore<T>({
|
||||
const [text, setText] = useState(formatLabel(value));
|
||||
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const isMobile = useIsTouch();
|
||||
|
||||
// Latest-value refs, safe to read from async code and timers.
|
||||
const valueRef = useRef(value);
|
||||
@@ -245,6 +249,15 @@ function StepperCore<T>({
|
||||
}, WHEEL_COMMIT_DELAY);
|
||||
}
|
||||
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
const { isSelected, select, deselect } = useVolumeKeys({
|
||||
rootRef,
|
||||
disabled,
|
||||
onStep: (direction) => {
|
||||
if (step(direction) !== null) scheduleWheelCommit();
|
||||
},
|
||||
});
|
||||
|
||||
useNonPassiveWheel(inputRef, (e) => {
|
||||
if (disabled) return;
|
||||
e.preventDefault();
|
||||
@@ -330,13 +343,20 @@ function StepperCore<T>({
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={`choice-stepper ${className ?? ''}`} data-disabled={disabled || undefined}>
|
||||
<div
|
||||
ref={rootRef}
|
||||
className={`choice-stepper ${isMobile ? 'is-touch' : ''} ${isSelected ? 'is-selected' : ''} ${className ?? ''}`}
|
||||
data-disabled={disabled || undefined}
|
||||
onPointerDown={select}
|
||||
>
|
||||
<input
|
||||
ref={inputRef}
|
||||
id={id}
|
||||
className="choice-stepper__input"
|
||||
value={text}
|
||||
disabled={disabled}
|
||||
readOnly={isMobile}
|
||||
onFocus={deselect}
|
||||
onChange={(e) => {
|
||||
typing.current = true;
|
||||
setText(e.target.value);
|
||||
|
||||
@@ -14,6 +14,12 @@
|
||||
-webkit-user-select: none;
|
||||
}
|
||||
|
||||
.gauge-viewport.is-selected {
|
||||
outline: 0.4cqh solid var(--accent-brand);
|
||||
outline-offset: -0.4cqh;
|
||||
border-radius: 1.6cqh;
|
||||
}
|
||||
|
||||
.gauge-track {
|
||||
position: relative;
|
||||
flex: 1 1 auto;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useVolumeKeys } from '../../hooks/useVolumeKeys';
|
||||
import './VerticalSlider.css';
|
||||
|
||||
type StepFn = (value: number) => number;
|
||||
@@ -192,8 +193,9 @@ export default function VerticalSlider({
|
||||
const sliderRef = useRef<HTMLDivElement>(null);
|
||||
const trackRef = useRef<HTMLDivElement>(null);
|
||||
const thumbRef = useRef<HTMLDivElement>(null);
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// --- shared interaction state (all three input methods use these) ------
|
||||
// --- shared interaction state (all input methods use these) ------------
|
||||
|
||||
/** Latest value shown locally. Source of truth for "where is the thumb right now". */
|
||||
const latestValue = useRef(value);
|
||||
@@ -259,7 +261,8 @@ export default function VerticalSlider({
|
||||
}
|
||||
|
||||
/**
|
||||
* Single serialized commit pipeline shared by drag, keyboard, wheel and text entry.
|
||||
* Single serialized commit pipeline shared by drag, keyboard, wheel,
|
||||
* volume buttons and text entry.
|
||||
*
|
||||
* - Only one onChange is ever in flight.
|
||||
* - While one is in flight, newer values just overwrite `pendingCommit`
|
||||
@@ -409,6 +412,7 @@ export default function VerticalSlider({
|
||||
onInputRef.current?.(next);
|
||||
}
|
||||
|
||||
/** Returns the value one step up (+1) or down (-1) from `base`. */
|
||||
/** Returns the value one step up (+1) or down (-1) from `base`. */
|
||||
function nudge(base: number, direction: 1 | -1) {
|
||||
if (listValues) {
|
||||
@@ -416,9 +420,35 @@ export default function VerticalSlider({
|
||||
const nextIdx = Math.min(listValues.length - 1, Math.max(0, idx + direction));
|
||||
return listValues[nextIdx];
|
||||
}
|
||||
return normalizeValue(base + direction * step(base));
|
||||
|
||||
// Use the step size of the region we're moving INTO. Without this, moving
|
||||
// down from a threshold (e.g. -20) uses the fine step, then gets snapped
|
||||
// back by the coarse grid of the lower region and never leaves.
|
||||
const probe = direction === 1 ? base : base - 1e-9;
|
||||
const s = step(Math.min(max, Math.max(min, probe)));
|
||||
|
||||
return normalizeValue(base + direction * s);
|
||||
}
|
||||
|
||||
/** Moves one step and commits after a short idle delay (shared by wheel and volume buttons). */
|
||||
function nudgeAndCommitDebounced(direction: 1 | -1) {
|
||||
updateValue(nudge(latestValue.current, direction));
|
||||
|
||||
if (wheelTimeout.current) clearTimeout(wheelTimeout.current);
|
||||
wheelTimeout.current = setTimeout(() => {
|
||||
wheelTimeout.current = null;
|
||||
commitLatestIfChanged();
|
||||
}, WHEEL_COMMIT_DELAY_MS);
|
||||
}
|
||||
|
||||
// --- hardware volume buttons (mobile only, via the shared hook) --------
|
||||
|
||||
const { isSelected, select, deselect } = useVolumeKeys({
|
||||
rootRef,
|
||||
disabled,
|
||||
onStep: nudgeAndCommitDebounced, // volume up = increase, down = decrease
|
||||
});
|
||||
|
||||
// --- keyboard ----------------------------------------------------------
|
||||
|
||||
function handleKeyDown(e: React.KeyboardEvent<HTMLDivElement>) {
|
||||
@@ -462,14 +492,7 @@ export default function VerticalSlider({
|
||||
|
||||
// deltaY < 0 means scrolling up/away from the user — treat that as
|
||||
// increasing the value, matching the vertical slider's "up = more".
|
||||
const direction: 1 | -1 = e.deltaY < 0 ? 1 : -1;
|
||||
updateValue(nudge(latestValue.current, direction));
|
||||
|
||||
if (wheelTimeout.current) clearTimeout(wheelTimeout.current);
|
||||
wheelTimeout.current = setTimeout(() => {
|
||||
wheelTimeout.current = null;
|
||||
commitLatestIfChanged();
|
||||
}, WHEEL_COMMIT_DELAY_MS);
|
||||
nudgeAndCommitDebounced(e.deltaY < 0 ? 1 : -1);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
@@ -579,7 +602,10 @@ export default function VerticalSlider({
|
||||
|
||||
return (
|
||||
<div
|
||||
className={['gauge-viewport', disabled && 'is-disabled', className].filter(Boolean).join(' ')}
|
||||
ref={rootRef}
|
||||
className={['gauge-viewport', disabled && 'is-disabled', isSelected && 'is-selected', className]
|
||||
.filter(Boolean)
|
||||
.join(' ')}
|
||||
style={style}
|
||||
>
|
||||
<div
|
||||
@@ -603,10 +629,14 @@ export default function VerticalSlider({
|
||||
aria-valuemax={max}
|
||||
aria-valuenow={displayValue}
|
||||
tabIndex={disabled ? -1 : 0}
|
||||
onFocus={select}
|
||||
onKeyDown={handleKeyDown}
|
||||
onKeyUp={handleKeyUp}
|
||||
onBlur={handleSliderBlur}
|
||||
onPointerDown={startDrag}
|
||||
onPointerDown={(e) => {
|
||||
select();
|
||||
startDrag(e);
|
||||
}}
|
||||
onPointerMove={handlePointerMove}
|
||||
onPointerUp={endDrag}
|
||||
onPointerCancel={endDrag}
|
||||
@@ -632,7 +662,10 @@ export default function VerticalSlider({
|
||||
disabled={disabled}
|
||||
value={editingText ?? formattedValue}
|
||||
style={{ width: '100%' }}
|
||||
onFocus={() => setEditingText(displayValue.toFixed(decimals))} // plain number while editing
|
||||
onFocus={() => {
|
||||
deselect(); // volume keys shouldn't move the slider while typing
|
||||
setEditingText(displayValue.toFixed(decimals)); // plain number while editing
|
||||
}}
|
||||
onChange={(e) => setEditingText(e.target.value)}
|
||||
onBlur={commitEditing}
|
||||
onKeyDown={(e) => {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
const QUERY = '(pointer: coarse)';
|
||||
|
||||
/** True when the primary input is a finger, regardless of screen width or orientation. */
|
||||
export function useIsTouch() {
|
||||
const [isTouch, setIsTouch] = useState(() => window.matchMedia(QUERY).matches);
|
||||
|
||||
useEffect(() => {
|
||||
const mq = window.matchMedia(QUERY);
|
||||
const handler = (e: MediaQueryListEvent) => setIsTouch(e.matches);
|
||||
mq.addEventListener('change', handler);
|
||||
return () => mq.removeEventListener('change', handler);
|
||||
}, []);
|
||||
|
||||
return isTouch;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { useEffect, useId, useRef, useState } from 'react';
|
||||
import { useIsTouch } from './useIsTouch';
|
||||
|
||||
/** The control that currently owns the hardware volume buttons. */
|
||||
let activeId: string | null = null;
|
||||
|
||||
function setNativeIntercept(on: boolean) {
|
||||
(window as any).VolumeBridge?.setIntercept(on);
|
||||
}
|
||||
|
||||
type Options = {
|
||||
rootRef: React.RefObject<HTMLElement | null>;
|
||||
/** +1 = volume up, -1 = volume down. Always sees fresh state. */
|
||||
onStep: (direction: 1 | -1) => void;
|
||||
disabled?: boolean;
|
||||
};
|
||||
|
||||
export function useVolumeKeys({ rootRef, onStep, disabled }: Options) {
|
||||
const isMobile = useIsTouch();
|
||||
const id = useId();
|
||||
const [selected, setSelected] = useState(false);
|
||||
|
||||
const onStepRef = useRef(onStep);
|
||||
onStepRef.current = onStep;
|
||||
|
||||
function select() {
|
||||
if (!isMobile || disabled) return;
|
||||
activeId = id;
|
||||
setNativeIntercept(true);
|
||||
setSelected(true);
|
||||
}
|
||||
|
||||
function deselect() {
|
||||
setSelected(false);
|
||||
if (activeId === id) {
|
||||
activeId = null;
|
||||
setNativeIntercept(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Release on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (activeId === id) {
|
||||
activeId = null;
|
||||
setNativeIntercept(false);
|
||||
}
|
||||
};
|
||||
}, [id]);
|
||||
|
||||
// Release when disabled
|
||||
useEffect(() => {
|
||||
if (disabled) deselect();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [disabled]);
|
||||
|
||||
// Release when tapping outside
|
||||
useEffect(() => {
|
||||
if (!isMobile) return;
|
||||
const onDown = (e: PointerEvent) => {
|
||||
if (rootRef.current?.contains(e.target as Node)) return;
|
||||
setSelected(false);
|
||||
if (activeId === id) {
|
||||
activeId = null;
|
||||
setNativeIntercept(false);
|
||||
}
|
||||
};
|
||||
document.addEventListener('pointerdown', onDown, true);
|
||||
return () => document.removeEventListener('pointerdown', onDown, true);
|
||||
}, [isMobile, id, rootRef]);
|
||||
|
||||
// Volume events from the native bridge
|
||||
useEffect(() => {
|
||||
if (!isMobile || disabled) return;
|
||||
|
||||
const onUp = () => {
|
||||
if (activeId === id) onStepRef.current(1);
|
||||
};
|
||||
const onDown = () => {
|
||||
if (activeId === id) onStepRef.current(-1);
|
||||
};
|
||||
|
||||
document.addEventListener('volumeupbutton', onUp);
|
||||
document.addEventListener('volumedownbutton', onDown);
|
||||
return () => {
|
||||
document.removeEventListener('volumeupbutton', onUp);
|
||||
document.removeEventListener('volumedownbutton', onDown);
|
||||
};
|
||||
}, [isMobile, disabled, id]);
|
||||
|
||||
return { isSelected: isMobile && selected, select, deselect };
|
||||
}
|
||||
Reference in New Issue
Block a user