{
+ 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) => {
diff --git a/src/hooks/useIsTouch.ts b/src/hooks/useIsTouch.ts
new file mode 100644
index 0000000..d09c596
--- /dev/null
+++ b/src/hooks/useIsTouch.ts
@@ -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;
+}
\ No newline at end of file
diff --git a/src/hooks/useVolumeKeys.ts b/src/hooks/useVolumeKeys.ts
new file mode 100644
index 0000000..a99a441
--- /dev/null
+++ b/src/hooks/useVolumeKeys.ts
@@ -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;
+ /** +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 };
+}
\ No newline at end of file