{"slug":"hover-card","title":"Hover Card","description":"Using the hover-card machine in your project.","contentType":"component","framework":"react","content":"A hover card lets sighted users preview content behind a link.\n\n## Resources\n\n\n[Latest version: v1.39.1](https://www.npmjs.com/package/@zag-js/hover-card)\n[Logic Visualizer](https://zag-visualizer.vercel.app/hover-card)\n[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/hover-card)\n\n\n\n**Features**\n\n- Customize side, alignment, offsets\n- Optionally render a pointing arrow\n- Supports custom open and close delays\n- Opens on hover only\n- Ignored by screen readers\n- Supports multiple triggers sharing a single hover card instance\n\n## Installation\n\nInstall the hover card package:\n\n```bash\nnpm install @zag-js/hover-card @zag-js/react\n# or\nyarn add @zag-js/hover-card @zag-js/react\n```\n\n## Anatomy\n\nTo set up the hover card correctly, you'll need to understand its anatomy and\nhow we name its parts.\n\n> Each part includes a `data-part` attribute to help identify them in the DOM.\n\n\n\n## Usage\n\nImport the hover card package:\n\n```tsx\nimport * as hoverCard from \"@zag-js/hover-card\"\n```\n\nThe hover card package exports two key functions:\n\n- `machine` - State machine logic.\n- `connect` - Maps machine state to JSX props and event handlers.\n\nThen use the framework integration helpers:\n\n```tsx\nimport * as hoverCard from \"@zag-js/hover-card\"\nimport { useMachine, normalizeProps, Portal } from \"@zag-js/react\"\nimport { useId } from \"react\"\n\nfunction HoverCard() {\n  const service = useMachine(hoverCard.machine, { id: useId() })\n\n  const api = hoverCard.connect(service, normalizeProps)\n\n  return (\n    <>\n      <a\n        href=\"https://twitter.com/zag_js\"\n        target=\"_blank\"\n        {...api.getTriggerProps()}\n      >\n        Twitter\n      </a>\n\n      {api.open && (\n        <Portal>\n          <div {...api.getPositionerProps()}>\n            <div {...api.getContentProps()}>\n              <div {...api.getArrowProps()}>\n                <div {...api.getArrowTipProps()} />\n              </div>\n              Twitter Preview\n            </div>\n          </div>\n        </Portal>\n      )}\n    </>\n  )\n}\n```\n\n### Setting the initial state\n\nSet `defaultOpen` to `true` to start with the hover card open.\n\n```tsx {2}\nconst service = useMachine(hoverCard.machine, {\n  defaultOpen: true,\n})\n```\n\n### Controlled open state\n\nUse `open` and `onOpenChange` to control visibility externally.\n\n```tsx\nconst service = useMachine(hoverCard.machine, {\n  open,\n  onOpenChange(details) {\n    setOpen(details.open)\n  },\n})\n```\n\n### Customizing open and close delays\n\nUse `openDelay` and `closeDelay` to control hover timing.\n\n```tsx\nconst service = useMachine(hoverCard.machine, {\n  openDelay: 300,\n  closeDelay: 150,\n})\n```\n\n### Positioning the hover card\n\nUse `positioning` to control placement and offsets.\n\n```tsx\nconst service = useMachine(hoverCard.machine, {\n  positioning: {\n    placement: \"bottom-start\",\n    offset: { mainAxis: 8, crossAxis: 4 },\n  },\n})\n```\n\n### Multiple triggers\n\nA single hover card instance can be shared across multiple trigger elements.\nPass a `value` to `getTriggerProps` to identify each trigger.\n\n```tsx\nconst users = [\n  { id: \"1\", name: \"Alice\", avatar: \"/alice.png\" },\n  { id: \"2\", name: \"Bob\", avatar: \"/bob.png\" },\n]\n\nconst service = useMachine(hoverCard.machine, {\n  onTriggerValueChange({ value }) {\n    const user = users.find((u) => u.id === value) ?? null\n    setActiveUser(user)\n  },\n})\n\nconst api = hoverCard.connect(service, normalizeProps)\n\nreturn (\n  <>\n    {users.map((user) => (\n      <a {...api.getTriggerProps({ value: user.id })}>{user.name}</a>\n    ))}\n    <div {...api.getPositionerProps()}>\n      <div {...api.getContentProps()}>\n        {/* Content updates based on activeUser */}\n      </div>\n    </div>\n  </>\n)\n```\n\nWhen hovering a different trigger while the card is open, it repositions without\nclosing.\n\n### Disabling the hover card\n\nSet `disabled` to `true` to prevent it from opening.\n\n```tsx\nconst service = useMachine(hoverCard.machine, {\n  disabled: true,\n})\n```\n\n### Listening for open state changes\n\nWhen the hover card is `opened` or `closed`, the `onOpenChange` callback is\ninvoked.\n\n```tsx {2-5}\nconst service = useMachine(hoverCard.machine, {\n  onOpenChange(details) {\n    // details => { open: boolean }\n    console.log(\"hovercard is:\", details.open ? \"opened\" : \"closed\")\n  },\n})\n```\n\n## Styling guide\n\nEach part includes a `data-part` attribute you can target in CSS.\n\n```css\n[data-part=\"trigger\"] {\n  /* styles for trigger */\n}\n\n[data-part=\"content\"] {\n  /* styles for content */\n}\n```\n\n### Open and closed state\n\nThe hover card exposes a `data-state` attribute that can be used to style the\nhover card based on its open-close state.\n\n```css\n[data-part=\"trigger\"][data-state=\"open|closed\"] {\n  /* styles for open or closed state */\n}\n\n[data-part=\"content\"][data-state=\"open|closed\"] {\n  /* styles for open or closed state */\n}\n```\n\n### Arrow\n\nYou can use CSS variables to style the arrow.\n\n```css\n[data-part=\"arrow\"] {\n  /* styles for arrow */\n  --arrow-background: white;\n  --arrow-size: 8px;\n}\n```\n\n## Methods and Properties\n\n### Machine Context\n\nThe hover card machine exposes the following context properties:\n\n**`ids`**\nType: `Partial<{ trigger: string | ((value?: string) => string); content: string; positioner: string; arrow: string; }>`\nDescription: The ids of the elements in the popover. Useful for composition.\n\n**`onOpenChange`**\nType: `(details: OpenChangeDetails) => void`\nDescription: Function called when the hover card opens or closes.\n\n**`openDelay`**\nType: `number`\nDescription: The duration from when the mouse enters the trigger until the hover card opens.\n\n**`closeDelay`**\nType: `number`\nDescription: The duration from when the mouse leaves the trigger or content until the hover card closes.\n\n**`disabled`**\nType: `boolean`\nDescription: Whether the hover card is disabled\n\n**`open`**\nType: `boolean`\nDescription: The controlled open state of the hover card\n\n**`defaultOpen`**\nType: `boolean`\nDescription: The initial open state of the hover card when rendered.\nUse when you don't need to control the open state of the hover card.\n\n**`positioning`**\nType: `PositioningOptions`\nDescription: The user provided options used to position the popover content\n\n**`triggerValue`**\nType: `string`\nDescription: The controlled trigger value\n\n**`defaultTriggerValue`**\nType: `string`\nDescription: The initial trigger value when rendered.\nUse when you don't need to control the trigger value.\n\n**`onTriggerValueChange`**\nType: `(details: TriggerValueChangeDetails) => void`\nDescription: Function called when the trigger value changes.\n\n**`dir`**\nType: `\"ltr\" | \"rtl\"`\nDescription: The document's text/writing direction.\n\n**`id`**\nType: `string`\nDescription: The unique identifier of the machine.\n\n**`getRootNode`**\nType: `() => ShadowRoot | Node | Document`\nDescription: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.\n\n**`onPointerDownOutside`**\nType: `(event: PointerDownOutsideEvent) => void`\nDescription: Function called when the pointer is pressed down outside the component\n\n**`onFocusOutside`**\nType: `(event: FocusOutsideEvent) => void`\nDescription: Function called when the focus is moved outside the component\n\n**`onInteractOutside`**\nType: `(event: InteractOutsideEvent) => void`\nDescription: Function called when an interaction happens outside the component\n\n### Machine API\n\nThe hover card `api` exposes the following methods:\n\n**`open`**\nType: `boolean`\nDescription: Whether the hover card is open\n\n**`setOpen`**\nType: `(open: boolean) => void`\nDescription: Function to open the hover card\n\n**`triggerValue`**\nType: `string`\nDescription: The trigger value\n\n**`setTriggerValue`**\nType: `(value: string) => void`\nDescription: Function to set the trigger value\n\n**`reposition`**\nType: `(options?: Partial<PositioningOptions>) => void`\nDescription: Function to reposition the popover\n\n### Data Attributes\n\n**`Trigger`**\n\n**`data-scope`**: hover-card\n**`data-part`**: trigger\n**`data-placement`**: The placement of the trigger\n**`data-value`**: The value of the item\n**`data-current`**: Present when current\n**`data-state`**: \"open\" | \"closed\"\n\n**`Content`**\n\n**`data-scope`**: hover-card\n**`data-part`**: content\n**`data-state`**: \"open\" | \"closed\"\n**`data-nested`**: popover\n**`data-has-nested`**: popover\n**`data-placement`**: The placement of the content\n\n### CSS Variables\n\n<CssVarTable name=\"hover-card\" />\n\n## Accessibility\n\n### Keyboard Interactions\n\nThe hover card is intended for mouse users only so will not respond to keyboard\nnavigation.","package":"@zag-js/hover-card","editUrl":"https://github.com/chakra-ui/zag/edit/main/website/data/components/hover-card.mdx"}