GitHub

@@ -141,7 +141,10 @@ export function useResizable(key: string, options: Ref<UseResizableProps> | UseR

141141142142

const isDragging = ref(false)

143143144-

const onMouseMove = (e: MouseEvent, initialPos: number, initialSize: number): void => {

144+

// Read once per drag: `getComputedStyle` on every pointer move forces a style recalc.

145+

const getRootFontSize = () => opts.value.unit === 'rem' ? Number.parseFloat(getComputedStyle(document.documentElement).fontSize) : 1

146+147+

const resize = (clientX: number, initialPos: number, initialSize: number, rootFontSize: number): void => {

145148

if (!el.value || !opts.value.resizable) {

146149

return

147150

}

@@ -152,11 +155,9 @@ export function useResizable(key: string, options: Ref<UseResizableProps> | UseR

152155

// In RTL mode, we need to invert the delta calculation

153156

let delta: number

154157

if (isRtl) {

155-

// In RTL mode, invert the logic

156-

delta = opts.value.side === 'left' ? initialPos - e.clientX : e.clientX - initialPos

158+

delta = opts.value.side === 'left' ? initialPos - clientX : clientX - initialPos

157159

} else {

158-

// Original LTR logic

159-

delta = opts.value.side === 'left' ? e.clientX - initialPos : initialPos - e.clientX

160+

delta = opts.value.side === 'left' ? clientX - initialPos : initialPos - clientX

160161

}

161162162163

const newSize = initialSize + delta

@@ -165,7 +166,6 @@ export function useResizable(key: string, options: Ref<UseResizableProps> | UseR

165166

let newValue: number

166167

if (opts.value.unit === 'rem') {

167168

// Convert pixels to rem

168-

const rootFontSize = Number.parseFloat(getComputedStyle(document.documentElement).fontSize)

169169

newValue = newSize / rootFontSize

170170

} else if (opts.value.unit === 'px') {

171171

// Use pixel value directly

@@ -201,11 +201,12 @@ export function useResizable(key: string, options: Ref<UseResizableProps> | UseR

201201202202

const initialX = e.clientX

203203

const initialWidth = elWidth

204+

const rootFontSize = getRootFontSize()

204205205206

isDragging.value = true

206207207208

const handleMouseMove = (e: MouseEvent) => {

208-

onMouseMove(e, initialX, initialWidth)

209+

resize(e.clientX, initialX, initialWidth, rootFontSize)

209210

}

210211211212

const handleMouseUp = () => {

@@ -218,51 +219,6 @@ export function useResizable(key: string, options: Ref<UseResizableProps> | UseR

218219

document.addEventListener('mouseup', handleMouseUp)

219220

}

220221221-

const onTouchMove = (e: TouchEvent, initialPos: number, initialSize: number): void => {

222-

if (!el.value || !opts.value.resizable || !e.touches[0]) {

223-

return

224-

}

225-226-

const parentSize = el.value.parentElement?.offsetWidth || 1

227-

const isRtl = dir.value === 'rtl'

228-229-

// In RTL mode, we need to invert the delta calculation

230-

let delta: number

231-

if (isRtl) {

232-

// In RTL mode, invert the logic

233-

delta = opts.value.side === 'left' ? initialPos - e.touches[0].clientX : e.touches[0].clientX - initialPos

234-

} else {

235-

// Original LTR logic

236-

delta = opts.value.side === 'left' ? e.touches[0].clientX - initialPos : initialPos - e.touches[0].clientX

237-

}

238-239-

const newSize = initialSize + delta

240-241-

// Calculate size based on the selected unit

242-

let newValue: number

243-

if (opts.value.unit === 'rem') {

244-

// Convert pixels to rem

245-

const rootFontSize = Number.parseFloat(getComputedStyle(document.documentElement).fontSize)

246-

newValue = newSize / rootFontSize

247-

} else if (opts.value.unit === 'px') {

248-

// Use pixel value directly

249-

newValue = newSize

250-

} else {

251-

// Default percentage calculation

252-

newValue = (newSize / parentSize) * 100

253-

}

254-255-

// Auto collapse when dragging near collapsedSize

256-

if (opts.value.collapsible && newValue < (opts.value.collapsedSize + 4)) {

257-

collapse(true)

258-

return

259-

} else if (isCollapsed.value) {

260-

collapse(false)

261-

}

262-263-

size.value = Math.min(opts.value.maxSize, Math.max(opts.value.minSize, newValue))

264-

}

265-266222

const onTouchStart = (e: TouchEvent) => {

267223

if (!el.value || !opts.value.resizable || !e.touches[0]) {

268224

return

@@ -278,11 +234,14 @@ export function useResizable(key: string, options: Ref<UseResizableProps> | UseR

278234279235

const initialX = e.touches[0].clientX

280236

const initialWidth = elWidth

237+

const rootFontSize = getRootFontSize()

281238282239

isDragging.value = true

283240284241

const handleTouchMove = (e: TouchEvent) => {

285-

onTouchMove(e, initialX, initialWidth)

242+

if (e.touches[0]) {

243+

resize(e.touches[0].clientX, initialX, initialWidth, rootFontSize)

244+

}

286245

}

287246288247

const handleTouchEnd = () => {

Read the original on github.com ↗