useHover

Handles pointer hover interactions for an element. Normalizes behavior across browsers and platforms, and ignores emulated mouse events on touch devices.

Hover me!
    import React from 'react';
    import {useHover} from 'react-aria';
    
    function Example() {
      let [events, setEvents] = React.useState<string[]>([]);
      let {hoverProps, isHovered} = useHover({
        onHoverStart: e => setEvents(
          events => [...events, `hover start with ${e.pointerType}`]
        ),
        onHoverEnd: e => setEvents(
          events => [...events, `hover end with ${e.pointerType}`]
        )
      });
    
      return (
        <>
          <div
            {...hoverProps}
            style={{
              background: isHovered ? 'darkgreen' : 'green',
              color: 'white',
              display: 'inline-block',
              padding: '8px 12px',
              borderRadius: 8,
              cursor: 'pointer'
            }}
            role="button"
            tabIndex={0}>
            Hover me!
          </div>
          <ul
            style={{
              maxHeight: '200px',
              overflow: 'auto'
            }}>
            {events.map((e, i) => <li key={i}>{e}</li>)}
          </ul>
        </>
      );
    }
    

    Features

    useHover is similar to the :hover CSS pseudo class, but only applies on mouse interactions. :hover is sticky on touch devices, applying continuously until the user interacts with another element, and on devices with both mouse and touch support there is no CSS-only way to apply hover states only when interacting with a pointer. Read our blog post to learn more.

    API

    useHover(props: ):

    HoverProps

    NameType
    onHoverChange(isHovering: boolean) => void
    Handler that is called when the hover state changes.
    onHoverEnd(e: ) => void
    Handler that is called when a hover interaction ends.
    onHoverStart(e: ) => void
    Handler that is called when a hover interaction starts.
    isDisabledboolean
    Whether the hover events should be disabled.

    HoverResult

    NameType
    isHoveredboolean
    hoverPropsDOMAttributes
    Props to spread on the target element.

    HoverEvent

    NameType
    targetHTMLElement
    The target element of the hover event.
    pointerType'mouse''pen'
    The pointer type that triggered the hover event.
    type'hoverstart''hoverend'
    The type of hover event being fired.