4.0.0 • Published 4 years ago

@react-hook/hover v4.0.0

Weekly downloads
2,400
License
MIT
Repository
github
Last release
4 years ago

A React hook for tracking the hover state of DOM elements in browsers where hovering is possible. If the browser does not support hover states (e.g. a phone) the isHovering value will always be false.

Quick Start

Check out the example on CodeSandbox

import * as React from 'react'
import useHover from '@react-hook/hover'

const Component = (props) => {
  const target = React.useRef(null)
  const isHovering = useHover(target, {enterDelay: 200, leaveDelay: 200})
  return <div ref={target}>{isHovering ? 'Hovering' : 'Not hovering'}</div>
}

API

useHover(target, options?)

function useHover<T extends HTMLElement>(
  target: React.RefObject<T> | T | null,
  options: UseHoverOptions = {}
): boolean

Arguments

ArgumentTypeRequired?Description
targetReact.RefObject | T | nullYesA React ref created by useRef() or an HTML element
optionsUseHoverOptionsYesConfiguration options for the hook. See UseHoverOptions below.

Returns boolean

This hook returns true if the element in ref is in a hover state, otherwise false. This value is always false on devices that don't have hover states, i.e. phones.

UseHoverOptions

PropertyTypeDescription
enterDelaynumberDelays setting isHovering to true for this amount in ms
leaveDelaynumberDelays setting isHovering to false for htis amount in ms

LICENSE

MIT

4.0.0

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago