{"slug":"hover-card","title":"Hover Card","description":"Using the hover-card machine in your project.","contentType":"component","framework":"react","content":"An hover card allows sighted users to preview content available behind a link\n\n## Resources\n\n\n[Latest version: v1.31.0](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\n## Installation\n\nTo use the hover card machine in your project, run the following command in your\ncommand line:\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\nFirst, import the hover card package into your project\n\n```jsx\nimport * as hoverCard from \"@zag-js/hover-card\"\n```\n\nThe hover card package exports two key functions:\n\n- `machine` — The state machine logic for the hover card widget.\n- `connect` — The function that translates the machine's state to JSX attributes\n  and event handlers.\n\nNext, import the required hooks and functions for your framework and use the\nhover-card machine in your project 🔥\n\n```jsx\nimport * as hoverCard from \"@zag-js/hover-card\"\nimport { useMachine, normalizeProps, Portal } from \"@zag-js/react\"\n\nfunction HoverCard() {\n  const service = useMachine(hoverCard.machine, { id: \"1\" })\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\nTo make an hover card open by default, set the `defaultOpen` property to `true`\n\n```jsx {2}\nconst service = useMachine(hoverCard.machine, {\n  defaultOpen: 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```jsx {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\nEarlier, we mentioned that each hover card part has a `data-part` attribute\nadded to them to select and style them in the DOM.\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\nZag exposes some variable that can be used 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```css\n[data-part=\"content\"] {\n  /* styles for content */\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; 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**`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**`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-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"}