(options: UseResizableOptions)
| 54 | } |
| 55 | |
| 56 | export function useResizable(options: UseResizableOptions): UseResizable { |
| 57 | const { storageKey, defaultWidth, min, max, reverse = false } = options; |
| 58 | |
| 59 | function clamp(value: number): number { |
| 60 | if (!Number.isFinite(value)) return defaultWidth; |
| 61 | return Math.min(toValue(max), Math.max(min, Math.round(value))); |
| 62 | } |
| 63 | |
| 64 | const width = ref<number>(clamp(readStored(storageKey) ?? defaultWidth)); |
| 65 | const dragging = ref(false); |
| 66 | |
| 67 | function setWidth(value: number): void { |
| 68 | const next = clamp(value); |
| 69 | width.value = next; |
| 70 | writeStored(storageKey, next); |
| 71 | } |
| 72 | |
| 73 | // Drag bookkeeping — captured at pointerdown so we resize relative to the |
| 74 | // start point rather than absolute cursor coordinates. |
| 75 | let startX = 0; |
| 76 | let startWidth = 0; |
| 77 | let activeEl: HTMLElement | null = null; |
| 78 | let activePointerId = -1; |
| 79 | |
| 80 | function onPointerMove(event: PointerEvent): void { |
| 81 | if (!dragging.value) return; |
| 82 | const delta = event.clientX - startX; |
| 83 | setWidth(startWidth + (reverse ? -delta : delta)); |
| 84 | } |
| 85 | |
| 86 | function endDrag(): void { |
| 87 | if (!dragging.value) return; |
| 88 | dragging.value = false; |
| 89 | if (typeof document !== 'undefined') { |
| 90 | document.body.style.userSelect = ''; |
| 91 | document.body.style.cursor = ''; |
| 92 | } |
| 93 | if (activeEl) { |
| 94 | try { |
| 95 | activeEl.releasePointerCapture(activePointerId); |
| 96 | } catch { |
| 97 | // pointer capture may already be released |
| 98 | } |
| 99 | activeEl.removeEventListener('pointermove', onPointerMove); |
| 100 | activeEl.removeEventListener('pointerup', endDrag); |
| 101 | activeEl.removeEventListener('pointercancel', endDrag); |
| 102 | } |
| 103 | activeEl = null; |
| 104 | activePointerId = -1; |
| 105 | } |
| 106 | |
| 107 | function onPointerDown(event: PointerEvent): void { |
| 108 | event.preventDefault(); |
| 109 | dragging.value = true; |
| 110 | startX = event.clientX; |
| 111 | // The stored width can exceed the current cap (e.g. after the window narrows |
| 112 | // or a side panel opens). Clamp the drag start so the handle responds |
| 113 | // immediately instead of first covering an invisible delta. |
nothing calls this directly
no test coverage detected