Components
Hover card

Hover Card

The Hover Card component is a user-friendly, interactive tool designed to display information when a user hovers over a specific trigger element. It offers a variety of features, such as controlled state, customizable positioning, and the use of render props for a more dynamic and flexible experience.

Usage

To get started, import the necessary components from the package:

import {
  HoverCard,
  HoverCardTrigger,
  HoverCardContent,
  HoverCardArrow,
  HoverCardArrowTip,
  HoverCardPositioner,
  Portal,
} from '@ark-ui/react'

Here is an example of how to use the HoverCard component:

import { Portal } from '@zag-js/react'
import { useState } from 'react'
import {
  HoverCard,
  HoverCardArrow,
  HoverCardArrowTip,
  HoverCardContent,
  HoverCardPositioner,
  HoverCardTrigger,
} from '@ark-ui/react'

const Basic = () => (
  <HoverCard>
    <HoverCardTrigger>Hover me</HoverCardTrigger>
    <Portal>
      <HoverCardPositioner>
        <HoverCardContent>
          <HoverCardArrow>
            <HoverCardArrowTip />
          </HoverCardArrow>
          Content
        </HoverCardContent>
      </HoverCardPositioner>
    </Portal>
  </HoverCard>
)

Controlled HoverCard

The controlled HoverCard component provides an interface for managing the state of the hover card using the open prop. The onClose prop is called when the hover card needs to be closed:

import { Portal } from '@zag-js/react'
import { useState } from 'react'
import {
  HoverCard,
  HoverCardArrow,
  HoverCardArrowTip,
  HoverCardContent,
  HoverCardPositioner,
  HoverCardTrigger,
} from '@ark-ui/react'

const Controlled = () => {
  const [isOpen, setOpen] = useState(false)
  return (
    <>
      <button onClick={() => setOpen(!isOpen)}>click me</button>
      <HoverCard open={isOpen} onOpenChange={() => setOpen(false)}>
        <HoverCardTrigger>Hover me</HoverCardTrigger>
        <Portal>
          <HoverCardPositioner>
            <HoverCardContent>
              <HoverCardArrow>
                <HoverCardArrowTip />
              </HoverCardArrow>
              Content
            </HoverCardContent>
          </HoverCardPositioner>
        </Portal>
      </HoverCard>
    </>
  )
}

Custom Positioning

The HoverCard component can be customized in its placement and distance from the trigger element through the positioning prop:

import { Portal } from '@zag-js/react'
import { useState } from 'react'
import {
  HoverCard,
  HoverCardArrow,
  HoverCardArrowTip,
  HoverCardContent,
  HoverCardPositioner,
  HoverCardTrigger,
} from '@ark-ui/react'

const Positioning = () => (
  <HoverCard positioning={{ placement: 'right', gutter: 12 }}>
    <HoverCardTrigger>Hover me</HoverCardTrigger>
    <Portal>
      <HoverCardPositioner>
        <HoverCardContent>
          <HoverCardArrow>
            <HoverCardArrowTip />
          </HoverCardArrow>
          Content
        </HoverCardContent>
      </HoverCardPositioner>
    </Portal>
  </HoverCard>
)

Render Prop Usage

The HoverCard component can also accept a render prop, giving the user more control over rendering behavior. This is useful for dynamically updating the trigger based on the state of the HoverCard:

import { Portal } from '@zag-js/react'
import { useState } from 'react'
import {
  HoverCard,
  HoverCardArrow,
  HoverCardArrowTip,
  HoverCardContent,
  HoverCardPositioner,
  HoverCardTrigger,
} from '@ark-ui/react'

const RenderProp = () => (
  <HoverCard>
    {(api) => (
      <>
        <HoverCardTrigger>Hover me {api.isOpen ? '▲' : '▼'} </HoverCardTrigger>
        <Portal>
          <HoverCardPositioner>
            <HoverCardContent>
              <HoverCardArrow>
                <HoverCardArrowTip />
              </HoverCardArrow>
              Content
            </HoverCardContent>
          </HoverCardPositioner>
        </Portal>
      </>
    )}
  </HoverCard>
)

Conclusion

The HoverCard component is an intuitive and powerful tool for displaying contextual information in your applications. With its range of features and high degree of customizability, it can meet a wide variety of design and functionality requirements.

Previous

Environment

Next

Menu