51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import type React from 'react';
|
|
import { useTriggerGestures } from '../../../os/hooks/useTriggerGestures';
|
|
import type { TransitionId } from '../navigation.declaration';
|
|
import { useAppNavigate } from '../navigation';
|
|
|
|
type SystemTriggerId = 'system.back';
|
|
type GestureId = TransitionId | SystemTriggerId;
|
|
|
|
export function useNotesGestures() {
|
|
const { go, back, navigateTo } = useAppNavigate();
|
|
const { bindTap, bindLongPress, bindDoubleTap } = useTriggerGestures<GestureId>({
|
|
execute: (id, params) => {
|
|
if (id === 'system.back') return;
|
|
go(id, params);
|
|
},
|
|
});
|
|
|
|
type TriggerTapOptions<T extends HTMLElement> = {
|
|
params?: Record<string, string | number>;
|
|
preventDefault?: boolean;
|
|
stopPropagation?: boolean;
|
|
beforeTrigger?: (event: React.SyntheticEvent<T>) => void;
|
|
onTrigger?: () => void;
|
|
};
|
|
|
|
const bindBack = <T extends HTMLElement>(
|
|
options?: (TriggerTapOptions<T> & { steps?: number }) | undefined,
|
|
) => {
|
|
if (!options) {
|
|
const binding = bindTap<T>('system.back', { params: { steps: 1 } });
|
|
return { ...binding, 'data-trigger-type': 'back' as const };
|
|
}
|
|
const { steps, params, ...rest } = options;
|
|
const binding = bindTap<T>('system.back', {
|
|
...rest,
|
|
params: { ...(params ?? {}), steps: steps ?? 1 },
|
|
});
|
|
return { ...binding, 'data-trigger-type': 'back' as const };
|
|
};
|
|
|
|
return {
|
|
bindTap,
|
|
bindLongPress,
|
|
bindDoubleTap,
|
|
bindBack,
|
|
go,
|
|
back,
|
|
navigateTo,
|
|
};
|
|
}
|