useIsMouseInactive (SolidJS)

Get a signal that lets you know when the mouse has stopped moving.

typescriptjavascript
import {
Accessor,
createEffect,
createMemo,
createSignal,
onCleanup,
onMount,
} from 'solid-js'
interface UseIsMouseInactiveOptions {
timeout?: number | Accessor<number>
root?: Element | Accessor<Element | null | undefined>
}
export const useIsMouseInactive = (props: UseIsMouseInactiveOptions = {}) => {
const timeout = createMemo(() => {
if (typeof props.timeout === 'function') return props.timeout()
return props.timeout ?? 5000
})
const root = createMemo(() => {
if (typeof props.root === 'function') return props.root()
return props.root ?? window
})
const [inactive, setInactive] = createSignal(false)
let timer: NodeJS.Timeout
const onMouseMove = () => {
setInactive(false)
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
setInactive(true)
}, timeout())
}
createEffect(() => {
const el = root()
if (!el) return
onMount(() => {
el.addEventListener('mousemove', onMouseMove)
onMouseMove()
})
onCleanup(() => {
el.removeEventListener('mousemove', onMouseMove)
})
})
return inactive
}

Usage

tsxjsx
import type { VoidComponent } from 'solid-js'
export const Example: VoidComponent = () => {
const [element, setElement] = createSignal<HTMLElement>()
const isInactive = useIsMouseInactive({
root: element,
})
return (
<div class="grid h-96 place-items-center">
<div
ref={setElement}
class="h-64 w-64 bg-red-500"
classList={{
// Hide cursor when mouse hasn't moved for a while
'cursor-none touch-none': isInactive(),
}}
/>
</div>
)
}
Created 08/27/22 • Updated 09/03/22
Found a mistake, or want to suggest an improvement? Edit on GitHub here
and see edit history here

License & Attribution

Attribution

You should place the following attribution (and probably the license below it) above your copy of the code, but 🤷‍♂️.

This snippet "useIsMouseInactive (SolidJS)" from "https://soorria.com/snippets/use-is-mouse-inactive-solid" 
was created by Soorria Saruva (https://soorria.com) and is covered under the 
MIT license found here: https://github.com/soorria/soorria.com/blob/master/LICENSE.

License

If you want, you can (and probably should) include the entire license below. I'm not a lawyer, though so idrk.

MIT License Copyright (c) 2022 Soorria Saruva

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.