@gravatar-com/hovercards
Version:
Add profile hovercards to Gravatar images.
152 lines • 6.14 kB
TypeScript
/// <reference types="node" />
import type { Placement } from './compute-position';
export type Account = Record<'url' | 'shortname' | 'iconUrl' | 'name', string>;
export interface ProfileData {
hash: string;
preferredUsername: string;
thumbnailUrl: string;
displayName: string;
currentLocation?: string;
aboutMe?: string;
accounts?: Account[];
}
export type CreateHovercard = (profileData: ProfileData, options?: {
additionalClass?: string;
myHash?: string;
i18n?: Record<string, string>;
}) => HTMLDivElement;
export type Attach = (target: HTMLElement, options?: {
dataAttributeName?: string;
ignoreSelector?: string;
}) => void;
export type Detach = () => void;
export type OnQueryHovercardRef = (ref: HTMLElement) => HTMLElement;
export type OnFetchProfileStart = (hash: string) => void;
export type OnFetchProfileSuccess = (hash: string, profileData: ProfileData) => void;
export type OnFetchProfileFailure = (hash: string, error: Error) => void;
export type OnHovercardShown = (hash: string, hovercard: HTMLDivElement) => void;
export type OnHovercardHidden = (hash: string, hovercard: HTMLDivElement) => void;
export type Options = Partial<{
placement: Placement;
offset: number;
autoFlip: boolean;
delayToShow: number;
delayToHide: number;
additionalClass: string;
myHash: string;
i18n: Record<string, string>;
onQueryHovercardRef: OnQueryHovercardRef;
onFetchProfileStart: OnFetchProfileStart;
onFetchProfileSuccess: OnFetchProfileSuccess;
onFetchProfileFailure: OnFetchProfileFailure;
onHovercardShown: OnHovercardShown;
onHovercardHidden: OnHovercardHidden;
}>;
interface HovercardRef {
id: string;
hash: string;
params: string;
ref: HTMLElement;
}
export default class Hovercards {
_placement: Placement;
_offset: number;
_autoFlip: boolean;
_delayToShow: number;
_delayToHide: number;
_additionalClass: string;
_myHash: string;
_onQueryHovercardRef: OnQueryHovercardRef;
_onFetchProfileStart: OnFetchProfileStart;
_onFetchProfileSuccess: OnFetchProfileSuccess;
_onFetchProfileFailure: OnFetchProfileFailure;
_onHovercardShown: OnHovercardShown;
_onHovercardHidden: OnHovercardHidden;
_i18n: Record<string, string>;
_hovercardRefs: HovercardRef[];
_showHovercardTimeoutIds: Map<string, NodeJS.Timeout>;
_hideHovercardTimeoutIds: Map<string, NodeJS.Timeout>;
_cachedProfiles: Map<string, ProfileData>;
constructor({ placement, autoFlip, offset, delayToShow, delayToHide, additionalClass, myHash, onQueryHovercardRef, onFetchProfileStart, onFetchProfileSuccess, onFetchProfileFailure, onHovercardShown, onHovercardHidden, i18n, }?: Options);
/**
* Queries hovercard refs on or within the target element
*
* @param {HTMLElement} target - The element to query.
* @param {string} dataAttributeName - Data attribute name associated with Gravatar hashes.
* @param {string} [ignoreSelector] - The selector to ignore certain elements.
* @return {HTMLElement[]} - The queried hovercard refs.
* @private
*/
_queryHovercardRefs(target: HTMLElement, dataAttributeName: string, ignoreSelector?: string): HovercardRef[];
/**
* Creates a skeleton hovercard element.
*
* @return {HTMLDivElement} The created skeleton hovercard element.
*/
_createHovercardSkeleton(): HTMLDivElement;
/**
* Creates a hovercard element with the provided profile data.
*
* @param {ProfileData} profileData - The profile data to populate the hovercard.
* @param {Object} [options] - Optional parameters for the hovercard.
* @param {string} [options.additionalClass] - Additional CSS class for the hovercard.
* @param {string} [options.myHash] - The hash of the current user.
* @param {Object} [options.i18n] - The i18n object.
* @return {HTMLDivElement} - The created hovercard element.
*/
static createHovercard: CreateHovercard;
/**
* Waits for a specified delay and fetches the user's profile data,
* then shows the hovercard relative to the ref element.
*
* @param {HovercardRef} hovercardRef - The hovercard ref object.
* @return {void}
* @private
*/
_showHovercard({ id, hash, params, ref }: HovercardRef): void;
/**
* Waits for a specified delay and hides the hovercard.
*
* @param {string} id - The ID associated with the hovercard.
* @return {void}
* @private
*/
_hideHovercard(id: string): void;
/**
* Handles the mouseenter event for hovercard refs.
*
* @param {MouseEvent} e - The mouseenter event object.
* @param hovercardRef - The hovercard ref object.
* @return {void}
* @private
*/
_handleMouseEnter(e: MouseEvent, hovercardRef: HovercardRef): void;
/**
* Handles the mouseleave event for hovercard refs.
*
* @param {MouseEvent} e - The mouseleave event object.
* @param hovercardRef - The hovercard ref object.
* @param hovercardRef.id - The ID associated with the hovercard.
* @return {void}
* @private
*/
_handleMouseLeave(e: MouseEvent, { id }: HovercardRef): void;
/**
* Attaches event listeners on or within the target element.
*
* @param {HTMLElement} target - The target element to set.
* @param {Object} [options={}] - The optional parameters.
* @param options.dataAttributeName - Data attribute name associated with Gravatar hashes.
* @param options.ignoreSelector - The selector to ignore certain elements.
* @return {void}
*/
attach: Attach;
/**
* Removes event listeners from hovercard refs and resets the stored list of these refs.
*
* @return {void}
*/
detach: Detach;
}
export {};
//# sourceMappingURL=core.d.ts.map