|
| 1 | +import { createEffect, createSignal, onCleanup, onMount } from 'solid-js'; |
| 2 | +import { clamp } from '../utils/clamp'; |
| 3 | + |
| 4 | +export interface UseMovePosition { |
| 5 | + x: number; |
| 6 | + y: number; |
| 7 | +} |
| 8 | + |
| 9 | +export function clampUseMovePosition(position: UseMovePosition) { |
| 10 | + return { |
| 11 | + x: clamp(position.x, 0, 1), |
| 12 | + y: clamp(position.y, 0, 1), |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +interface useMoveHandlers { |
| 17 | + onScrubStart?: () => void; |
| 18 | + onScrubEnd?: () => void; |
| 19 | +} |
| 20 | + |
| 21 | +export function useMove<T extends HTMLElement = HTMLDivElement>( |
| 22 | + onChange: (value: UseMovePosition) => void, |
| 23 | + handlers?: useMoveHandlers, |
| 24 | + dir: 'ltr' | 'rtl' = 'ltr', |
| 25 | +) { |
| 26 | + const [ref, setRef] = createSignal<T>(); |
| 27 | + let mounted = false; |
| 28 | + let isSliding = false; |
| 29 | + let frame = 0; |
| 30 | + const [active, setActive] = createSignal(false); |
| 31 | + |
| 32 | + onMount(() => { |
| 33 | + mounted = true; |
| 34 | + }); |
| 35 | + |
| 36 | + createEffect(() => { |
| 37 | + const onScrub = ({ x, y }: UseMovePosition) => { |
| 38 | + cancelAnimationFrame(frame); |
| 39 | + |
| 40 | + frame = requestAnimationFrame(() => { |
| 41 | + if (mounted && ref()) { |
| 42 | + ref()!.style.userSelect = 'none'; |
| 43 | + const rect = ref()!.getBoundingClientRect(); |
| 44 | + |
| 45 | + if (rect.width && rect.height) { |
| 46 | + const _x = clamp((x - rect.left) / rect.width, 0, 1); |
| 47 | + |
| 48 | + onChange({ |
| 49 | + x: dir === 'ltr' ? _x : 1 - _x, |
| 50 | + y: clamp((y - rect.top) / rect.height, 0, 1), |
| 51 | + }); |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | + }; |
| 56 | + |
| 57 | + const bindEvents = () => { |
| 58 | + document.addEventListener('mousemove', onMouseMove); |
| 59 | + document.addEventListener('mouseup', stopScrubbing); |
| 60 | + document.addEventListener('touchmove', onTouchMove); |
| 61 | + document.addEventListener('touchend', stopScrubbing); |
| 62 | + }; |
| 63 | + |
| 64 | + const unbindEvents = () => { |
| 65 | + document.removeEventListener('mousemove', onMouseMove); |
| 66 | + document.removeEventListener('mouseup', stopScrubbing); |
| 67 | + document.removeEventListener('touchmove', onTouchMove); |
| 68 | + document.removeEventListener('touchend', stopScrubbing); |
| 69 | + }; |
| 70 | + |
| 71 | + const startScrubbing = () => { |
| 72 | + if (!isSliding && mounted) { |
| 73 | + isSliding = true; |
| 74 | + typeof handlers?.onScrubStart === 'function' && handlers.onScrubStart(); |
| 75 | + setActive(true); |
| 76 | + bindEvents(); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + const stopScrubbing = () => { |
| 81 | + if (isSliding && mounted) { |
| 82 | + isSliding = false; |
| 83 | + setActive(false); |
| 84 | + unbindEvents(); |
| 85 | + setTimeout(() => { |
| 86 | + typeof handlers?.onScrubEnd === 'function' && handlers.onScrubEnd(); |
| 87 | + }, 0); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const onMouseDown = (event: MouseEvent) => { |
| 92 | + startScrubbing(); |
| 93 | + event.preventDefault(); |
| 94 | + onMouseMove(event); |
| 95 | + }; |
| 96 | + |
| 97 | + const onMouseMove = (event: MouseEvent) => onScrub({ x: event.clientX, y: event.clientY }); |
| 98 | + |
| 99 | + const onTouchStart = (event: TouchEvent) => { |
| 100 | + if (event.cancelable) { |
| 101 | + event.preventDefault(); |
| 102 | + } |
| 103 | + |
| 104 | + startScrubbing(); |
| 105 | + onTouchMove(event); |
| 106 | + }; |
| 107 | + |
| 108 | + const onTouchMove = (event: TouchEvent) => { |
| 109 | + if (event.cancelable) { |
| 110 | + event.preventDefault(); |
| 111 | + } |
| 112 | + |
| 113 | + onScrub({ |
| 114 | + x: event?.changedTouches?.[0]?.clientX ?? 0, |
| 115 | + y: event?.changedTouches?.[0]?.clientY ?? 0, |
| 116 | + }); |
| 117 | + }; |
| 118 | + |
| 119 | + ref()?.addEventListener('mousedown', onMouseDown); |
| 120 | + ref()?.addEventListener('touchstart', onTouchStart, { passive: false }); |
| 121 | + |
| 122 | + onCleanup(() => { |
| 123 | + if (ref()) { |
| 124 | + ref()!.removeEventListener('mousedown', onMouseDown); |
| 125 | + ref()!.removeEventListener('touchstart', onTouchStart); |
| 126 | + } |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + return { ref: setRef, active }; |
| 131 | +} |
0 commit comments