Hover Card
An hover card allows sighted users to preview content available behind a link.
Features
Install
Install the component from your command line.
Anatomy
Import all parts and piece them together.
<script setup lang="ts">import * as hoverCard from '@destyler/hover-card'import { normalizeProps, useMachine } from '@destyler/vue'import { computed, useId } from 'vue'
const [state, send] = useMachine(hoverCard.machine({ id: useId() }))
const api = computed(() => hoverCard.connect(state.value, send, normalizeProps))</script>
<template> <a v-bind="api.getTriggerProps()"></a> <div v-bind="api.getPositionerProps()"> <div v-bind="api.getContentProps()" > <div v-bind="api.getArrowProps()"> <div v-bind="api.getArrowTipProps()" /> </div> </div> </div></template>
import * as hoverCard from '@destyler/hover-card'import { normalizeProps, useMachine } from '@destyler/react'import { useId } from 'react'
export default function HoverCard() {const [state, send] = useMachine(hoverCard.machine({ id: useId(),}))
const api = hoverCard.connect(state, send, normalizeProps)
return ( <a {...api.getTriggerProps()}></a> <div {...api.getPositionerProps()}> <div {...api.getContentProps()}> <div {...api.getArrowProps()}> <div {...api.getArrowTipProps()} /> </div> </div> </div>)}
<script lang="ts"> import * as image from '@destyler/image' import { normalizeProps, useMachine } from '@destyler/svelte'
const id = $props.id()
const [state, send] = useMachine(image.machine({ id }))
const api = $derived(image.connect(state, send, normalizeProps))</script>
<div {...api.getRootProps()}> <img {...api.getImageProps()} /> <div {...api.getFallbackProps()} ></div></div>
import * as hoverCard from '@destyler/hover-card'import { normalizeProps, useMachine } from '@destyler/solid'import { createMemo, createUniqueId } from 'solid-js'import { Portal } from 'solid-js/web'
export default function HoverCard() { const [state, send] = useMachine(hoverCard.machine({ id: createUniqueId(), }))
const api = createMemo(() => hoverCard.connect(state, send, normalizeProps))
return ( <a {...api().getTriggerProps()}></a> <div {...api().getPositionerProps()}> <div {...api().getContentProps()}> <div {...api().getArrowProps()}> <div {...api().getArrowTipProps()} /> </div> </div> </div> )}
Making it opened initially
To make an hover card open by default, set the context’s open
property to true
const [state, send] = useMachine( hoverCard.machine({ open: true, }),)
Listening for open state changes
When the hover card is opened
or closed
, the onOpenChange
callback is invoked.
const [state, send] = useMachine( hoverCard.machine({ onOpenChange(details) { // details => { open: boolean } console.log("hovercard is:", details.open ? "opened" : "closed") }, }),)
Styling guide
Earlier, we mentioned that each hover card part has a data-part
attribute added to them to
select and style them in the DOM.
[data-part="trigger"] { /* styles for trigger */}
[data-part="content"] { /* styles for content */}
Open and closed state
The hover card exposes a data-state
attribute that can be used to style
the hover card based on its open-close state.
[data-part="trigger"][data-state="open"] { /* styles for open or closed state */}
[data-part="content"][data-state="open"] { /* styles for open or closed state */}
Arrow
exposes some variable that can be used to style the arrow.
[data-part="arrow"] { /* styles for arrow */ --arrow-background: white; --arrow-size: 8px;}
Methods and Properties
Machine Context
The hover card machine exposes the following context properties:
Partial<{ trigger: string; content: string; positioner: string; arrow: string; }>
(details: OpenChangeDetails) => void
number
number
boolean
boolean
PositioningOptions
"ltr" | "rtl"
string
() => ShadowRoot | Node | Document
Machine API
The hover card api
exposes the following methods:
boolean
(open: boolean) => void
(options?: Partial<PositioningOptions>) => void
Data Attributes
Trigger
data-scope
data-part
data-placement
data-state
Content
data-scope
data-part
data-state
data-placement
Arrow
data-scope
data-part