hover card

For sighted users to preview content available behind the link.

PreviousNext
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardButtonComponent } from '../../button/button.component';
import { ZardHoverCardComponent, ZardHoverCardDirective } from '../hover-card.component';

/** Demonstrates the default hover card behavior and content composition. */
@Component({
  selector: 'z-demo-hover-card-preview',
  imports: [ZardButtonComponent, ZardHoverCardComponent, ZardHoverCardDirective],
  template: `
    <button type="button" z-button zType="link" [zHoverCard]="content">Hover Here</button>

    <ng-template #content>
      <z-hover-card>
        <div class="space-y-1">
          <h4 class="text-sm font-semibold">Angular</h4>
          <p class="text-sm">The web framework that empowers developers - created and maintained by @angular.</p>
          <div class="text-muted-foreground text-xs">Released September 2016</div>
        </div>
      </z-hover-card>
    </ng-template>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoHoverCardPreviewComponent {}

Installation

Copy
npx zard-cli@latest add hover-card

Usage

import { ZardHoverCardComponent, ZardHoverCardDirective } from '@/shared/components/hover-card/hover-card.component';
Copy
<button [zHoverCard]="content">
  Hover Here
</button>

<ng-template #content>
  <z-hover-card>
    The web framework that empowers developers - created and maintained by @angular.
  </z-hover-card>
</ng-template>
Copy

Composition

Use the following composition to build a hover-card:

trigger[zHoverCard]
└── ng-template
    └── z-hover-card
Copy

Examples

Sides

Set zPlacement to top, bottom, left or right to choose which side of the trigger the hover card opens on. The overlay flips to another side automatically when the preferred placement does not fit in the viewport.
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardButtonComponent } from '../../button/button.component';
import { ZardHoverCardComponent, ZardHoverCardDirective } from '../hover-card.component';

/** Demonstrates every supported hover card placement. */
@Component({
  selector: 'z-demo-hover-card-sides',
  imports: [ZardButtonComponent, ZardHoverCardComponent, ZardHoverCardDirective],
  template: `
    <div class="flex flex-wrap gap-2">
      <button
        type="button"
        z-button
        zType="outline"
        [zHoverCard]="leftContent"
        zPlacement="left"
        [zOpenDelay]="100"
        [zCloseDelay]="100"
      >
        Left
      </button>
      <button
        type="button"
        z-button
        zType="outline"
        [zHoverCard]="topContent"
        zPlacement="top"
        [zOpenDelay]="100"
        [zCloseDelay]="100"
      >
        Top
      </button>

      <button
        type="button"
        z-button
        zType="outline"
        [zHoverCard]="bottomContent"
        zPlacement="bottom"
        [zOpenDelay]="100"
        [zCloseDelay]="100"
      >
        Bottom
      </button>
      <button
        type="button"
        z-button
        zType="outline"
        [zHoverCard]="rightContent"
        zPlacement="right"
        [zOpenDelay]="100"
        [zCloseDelay]="100"
      >
        Right
      </button>
    </div>

    <ng-template #leftContent>
      <z-hover-card>
        <div class="flex flex-col gap-1">
          <h4 class="font-medium">Hover Card</h4>
          <p>This hover card appears on the left side of the trigger.</p>
        </div>
      </z-hover-card>
    </ng-template>

    <ng-template #topContent>
      <z-hover-card>
        <div class="flex flex-col gap-1">
          <h4 class="font-medium">Hover Card</h4>
          <p>This hover card appears on the top side of the trigger.</p>
        </div>
      </z-hover-card>
    </ng-template>

    <ng-template #bottomContent>
      <z-hover-card>
        <div class="flex flex-col gap-1">
          <h4 class="font-medium">Hover Card</h4>
          <p>This hover card appears on the bottom side of the trigger.</p>
        </div>
      </z-hover-card>
    </ng-template>

    <ng-template #rightContent>
      <z-hover-card>
        <div class="flex flex-col gap-1">
          <h4 class="font-medium">Hover Card</h4>
          <p>This hover card appears on the right side of the trigger.</p>
        </div>
      </z-hover-card>
    </ng-template>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoHoverCardSidesComponent {}

Delay

Hover intent defaults to a 700ms open delay and a 300ms close delay — long enough to ignore a passing cursor, unlike a tooltip. Override either with the zOpenDelay and zCloseDelay inputs, in milliseconds.
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardButtonComponent } from '../../button/button.component';
import { ZardHoverCardComponent, ZardHoverCardDirective } from '../hover-card.component';

/** Contrasts the default hover intent delay with an instant, tooltip-like open. */
@Component({
  selector: 'z-demo-hover-card-delay',
  imports: [ZardButtonComponent, ZardHoverCardComponent, ZardHoverCardDirective],
  template: `
    <div class="flex flex-wrap gap-2">
      <button type="button" z-button zType="outline" [zHoverCard]="instantContent" [zOpenDelay]="0" [zCloseDelay]="0">
        No delay
      </button>
      <button type="button" z-button zType="outline" [zHoverCard]="slowContent" [zOpenDelay]="1000" [zCloseDelay]="500">
        1s delay
      </button>
    </div>

    <ng-template #instantContent>
      <z-hover-card>
        <div class="flex flex-col gap-1">
          <h4 class="font-medium">No delay</h4>
          <p>Opens immediately, like a tooltip — a quick pass of the cursor is enough to trigger it.</p>
        </div>
      </z-hover-card>
    </ng-template>

    <ng-template #slowContent>
      <z-hover-card>
        <div class="flex flex-col gap-1">
          <h4 class="font-medium">1s delay</h4>
          <p>Opens after a full second and closes after 500ms, set with [zOpenDelay] and [zCloseDelay].</p>
        </div>
      </z-hover-card>
    </ng-template>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoHoverCardDelayComponent {}

RTL

To enable RTL support in zard/ui, see the RTL configuration guide.
// You will notice this example is rendered inside a wrapper that sets the dir and data-lang attributes, and
// reads its text from a translations helper. This is because this site is not RTL by default.
// In your application you won't need either: set dir="rtl" on <html> (or use the CDK Dir directive on a subtree)
// and write the text of your own language. See the RTL guide: https://zardui.com/docs/rtl

import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardButtonComponent } from '@/shared/components/button/button.component';
import {
  ZardHoverCardComponent,
  ZardHoverCardDirective,
  type ZardHoverCardSide,
} from '@/shared/components/hover-card/hover-card.component';
import { injectRtlTranslation, type ZardRtlTranslations } from '@/shared/demo-utils/rtl-translation';

interface HoverCardRtlValues {
  name: string;
  price: string;
  'inline-start': string;
  left: string;
  top: string;
  bottom: string;
  right: string;
  'inline-end': string;
}

const translations: ZardRtlTranslations<HoverCardRtlValues> = {
  en: {
    dir: 'ltr',
    values: {
      name: 'Wireless Headphones',
      price: '$99.99',
      'inline-start': 'Inline Start',
      left: 'Left',
      top: 'Top',
      bottom: 'Bottom',
      right: 'Right',
      'inline-end': 'Inline End',
    },
  },
  ar: {
    dir: 'rtl',
    values: {
      name: 'سماعات لاسلكية',
      price: '٩٩.٩٩ $',
      'inline-start': 'بداية السطر',
      left: 'يسار',
      top: 'أعلى',
      bottom: 'أسفل',
      right: 'يمين',
      'inline-end': 'نهاية السطر',
    },
  },
  he: {
    dir: 'rtl',
    values: {
      name: 'אוזניות אלחוטיות',
      price: '99.99 $',
      'inline-start': 'תחילת השורה',
      left: 'שמאל',
      top: 'למעלה',
      bottom: 'למטה',
      right: 'ימין',
      'inline-end': 'סוף השורה',
    },
  },
};

@Component({
  selector: 'z-demo-hover-card-rtl',
  imports: [ZardButtonComponent, ZardHoverCardComponent, ZardHoverCardDirective],
  template: `
    <div class="grid gap-4">
      <div class="flex flex-wrap justify-center gap-2">
        @for (side of physicalSides; track side) {
          <button
            type="button"
            z-button
            zType="outline"
            [zHoverCard]="content"
            [zPlacement]="side"
            [zOpenDelay]="100"
            [zCloseDelay]="100"
          >
            {{ i18n.t()[side] }}
          </button>
        }
      </div>
      <div class="flex flex-wrap justify-center gap-2">
        @for (side of logicalSides; track side) {
          <button
            type="button"
            z-button
            zType="outline"
            [zHoverCard]="content"
            [zPlacement]="side"
            [zOpenDelay]="100"
            [zCloseDelay]="100"
          >
            {{ i18n.t()[side] }}
          </button>
        }
      </div>
    </div>

    <ng-template #content>
      <z-hover-card class="flex w-64 flex-col gap-1">
        <div class="font-semibold">{{ i18n.t().name }}</div>
        <div class="text-muted-foreground text-sm">{{ i18n.t().price }}</div>
      </z-hover-card>
    </ng-template>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoHoverCardRtlComponent {
  protected readonly i18n = injectRtlTranslation(translations);

  protected readonly physicalSides: ZardHoverCardSide[] = ['left', 'top', 'bottom', 'right'];
  protected readonly logicalSides: ZardHoverCardSide[] = ['inline-start', 'inline-end'];
}

API Reference

[zHoverCard]

Directive that opens rich content on hover or focus.

Prop
[zHoverCard]

Hover card template (required), bound on the trigger: [zHoverCard]="content"

Type
TemplateRef<void>
Default
—
[zPlacement]

Side of the trigger; inline-* flips in RTL

Type
'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end'
Default
'bottom'
[zOpenDelay]

Open delay in ms

Type
number
Default
700
[zCloseDelay]

Close delay in ms

Type
number
Default
300
[zVisible]

Controls visibility programmatically

Type
boolean
Default
false
(zVisibleChange)

Emitted when visibility changes

Type
EventEmitter<boolean>
Default
—

z-hover-card

The hover card content.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
''
github iconwhatsapp icondiscord iconX icon

Made with ❤ in Brazil. Open source and available on GitHub .