Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(VNumberInput): support control holding down #20987

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 55 additions & 13 deletions packages/vuetify/src/labs/VNumberInput/VNumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ export const VNumberInput = genericComponent<VNumberInputSlots>()({
const incrementSlotProps = computed(() => ({ click: onClickUp }))
const decrementSlotProps = computed(() => ({ click: onClickDown }))

// Control holding down states
let hldDwnReqNextTime = 0
let hldDwnReq = -1
const hldDwnReqDelay = 100
const hldDwnDelay = 500
let hldDwn: null | 'up' | 'down' = null
let hldDwnTimeout = -1

function controlHoldingDownWatcher (time: number) {
hldDwnReq = requestAnimationFrame(controlHoldingDownWatcher)
Copy link
Contributor

@J-Sek J-Sek Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • hldDwnReq » holdDownRaf or holdActionRaf

I actually don't care much about the suffix. It can be *Raf, *Frame, *Req. My issue is with saving 2 vowels in hldDwn for no obvious gain. IMO it drags attention contributing to cognitive load.

if (time < hldDwnReqNextTime) return
hldDwnReqNextTime = time + hldDwnReqDelay

if (hldDwn) {
toggleUpDown(hldDwn === 'up')
}
}
watch(() => props.precision, () => formatInputValue())

onMounted(() => {
Expand Down Expand Up @@ -172,16 +189,6 @@ export const VNumberInput = genericComponent<VNumberInputSlots>()({
}
}

function onClickUp (e: MouseEvent) {
e.stopPropagation()
toggleUpDown()
}

function onClickDown (e: MouseEvent) {
e.stopPropagation()
toggleUpDown(false)
}

function onBeforeinput (e: InputEvent) {
if (!e.data) return
const existingTxt = (e.target as HTMLInputElement)?.value
Expand Down Expand Up @@ -229,8 +236,41 @@ export const VNumberInput = genericComponent<VNumberInputSlots>()({
}
}

function onControlMousedown (e: MouseEvent) {
function onClickUp (e: MouseEvent) {
e.stopPropagation()
toggleUpDown()
}

function onClickDown (e: MouseEvent) {
e.stopPropagation()
toggleUpDown(false)
}

function onControlMouseup (e: MouseEvent) {
e.preventDefault()
e.stopPropagation()

cancelAnimationFrame(hldDwnReq)
clearTimeout(hldDwnTimeout)
hldDwn = null
}

function onUpControlMousedown (e: MouseEvent) {
e.stopPropagation()

hldDwnReq = requestAnimationFrame(controlHoldingDownWatcher)
hldDwnTimeout = window.setTimeout(() => {
hldDwn = 'up'
}, hldDwnDelay)
}

function onDownControlMousedown (e: MouseEvent) {
e.stopPropagation()

hldDwnReq = requestAnimationFrame(controlHoldingDownWatcher)
hldDwnTimeout = window.setTimeout(() => {
hldDwn = 'down'
}, hldDwnDelay)
}

function clampModel () {
Expand Down Expand Up @@ -288,7 +328,8 @@ export const VNumberInput = genericComponent<VNumberInputSlots>()({
aria-hidden="true"
icon={ incrementIcon.value }
onClick={ onClickUp }
onMousedown={ onControlMousedown }
onMouseup={ onControlMouseup }
onMousedown={ onUpControlMousedown }
size={ controlNodeSize.value }
tabindex="-1"
/>
Expand Down Expand Up @@ -323,7 +364,8 @@ export const VNumberInput = genericComponent<VNumberInputSlots>()({
size={ controlNodeSize.value }
tabindex="-1"
onClick={ onClickDown }
onMousedown={ onControlMousedown }
onMouseup={ onControlMouseup }
onMousedown={ onDownControlMousedown }
/>
) : (
<VDefaultsProvider
Expand Down