Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 5x 7x 7x 7x 7x 7x 7x 7x 7x 20x 7x 7x 7x 6x 6x 6x 7x 13x 13x 7x 10x 8x 8x 7x 7x 7x 8x 10x 3x 7x 8x 5x 5x 5x 8x 3x 7x 10x 5x 2x 2x 6x 5x 5x 5x 5x 5x 3x 5x 1x 5x 5x 2x 5x 8x 7x 7x 7x 7x 7x 7x 7x | import { createRuntimeId } from '../accessibility/ids';
import type {
ChangeDetails,
ControllableValueOptions,
RuntimeController,
RuntimeEventSource,
} from '../core/types';
import { createDisposableScope } from '../core/disposables';
import { createTimeoutManager } from '../core/timers';
import { listen } from '../dom/dom';
import type { FloatingPositionOptions } from '../positioning/positioning';
import {
createOpenController,
type OpenChangeReason,
type OpenLifecycleEvents,
type OpenSnapshot,
} from './openable';
/** Immutable Tooltip state and relationship metadata. @public */
export interface TooltipSnapshot extends OpenSnapshot {
/** Stable trigger ID. */
readonly triggerId: string;
/** Value for trigger `aria-describedby` while open. */
readonly ariaDescribedby: string | null;
/** Tooltip role for non-interactive consumer content. */
readonly role: 'tooltip';
}
/** Tooltip delay, scope, state, and positioning options. @public */
export interface TooltipOptions extends Partial<
ControllableValueOptions<boolean, OpenChangeReason>
> {
/** Deterministic relationship ID base. */
readonly id?: string;
/** Delay before pointer-triggered opening in milliseconds. @defaultValue `700` */
readonly openDelay?: number;
/** Delay before pointer-triggered closing in milliseconds. @defaultValue `100` */
readonly closeDelay?: number;
/** Scope in which only one tooltip may be active. @defaultValue `default` */
readonly scope?: string;
/** Shared anchored positioning options. */
readonly positioning?: FloatingPositionOptions;
}
/** Headless non-interactive Tooltip controller. @public */
export interface TooltipController
extends RuntimeController<TooltipSnapshot>, RuntimeEventSource<OpenLifecycleEvents> {
/** Binds trigger/content listeners and positioning; touch pointer entry is intentionally ignored. */
bind(trigger: HTMLElement, content: HTMLElement): () => void;
/** Opens after the configured pointer delay, or immediately for keyboard focus. */
scheduleOpen(reason?: 'hover' | 'focus', event?: Event): void;
/** Closes after the configured pointer delay, or immediately for focus exit. */
scheduleClose(reason?: 'hover' | 'focus-out', event?: Event): void;
/** Immediately closes and cancels pending timers. */
close(details?: ChangeDetails<OpenChangeReason>): void;
}
const activeScopes = new Map<string, () => void>();
/** Creates a Tooltip with cleanup-safe delays and one-active-tooltip-per-scope coordination. @public */
export function createTooltip(options: TooltipOptions = {}): TooltipController {
const id = options.id ?? createRuntimeId('tooltip');
const scope = options.scope ?? 'default';
const overlay = createOpenController({
id: `${id}-content`,
role: 'tooltip',
closeOnFocusOutside: false,
closeOnOutsidePointer: false,
restoreFocus: false,
...(options.defaultValue !== undefined ? { defaultValue: options.defaultValue } : {}),
...(options.getValue ? { getValue: options.getValue } : {}),
...(options.onValueChange ? { onValueChange: options.onValueChange } : {}),
...(options.subscribeValue ? { subscribeValue: options.subscribeValue } : {}),
...(options.positioning ? { positioning: options.positioning } : {}),
});
const timer = createTimeoutManager();
let binding = createDisposableScope();
let destroyed = false;
const listeners = new Set<(snapshot: Readonly<TooltipSnapshot>) => void>();
const map = (snapshot: OpenSnapshot): TooltipSnapshot =>
Object.freeze({
...snapshot,
triggerId: `${id}-trigger`,
ariaDescribedby: snapshot.open ? `${id}-content` : null,
role: 'tooltip',
});
let current = map(overlay.getSnapshot());
const clearTimer = timer.clear;
const close = (
changeDetails: ChangeDetails<OpenChangeReason> = { reason: 'programmatic' },
): void => {
clearTimer();
overlay.close(changeDetails);
if (activeScopes.get(scope) === close) activeScopes.delete(scope);
};
const unsubscribe = overlay.subscribe((snapshot) => {
current = map(snapshot);
for (const listener of [...listeners]) listener(current);
});
const scheduleOpen = (reason: 'hover' | 'focus' = 'hover', event?: Event): void => {
if (destroyed) return;
clearTimer();
const run = () => {
activeScopes.get(scope)?.();
activeScopes.set(scope, close);
overlay.open(event ? { reason, event } : { reason });
};
const delay = reason === 'focus' ? 0 : (options.openDelay ?? 700);
if (delay === 0) run();
else timer.schedule(run, delay);
};
const scheduleClose = (reason: 'hover' | 'focus-out' = 'hover', event?: Event): void => {
if (destroyed) return;
clearTimer();
const run = () => close(event ? { reason, event } : { reason });
const delay = reason === 'focus-out' ? 0 : (options.closeDelay ?? 100);
if (delay === 0) run();
else timer.schedule(run, delay);
};
return {
getSnapshot: () => current,
subscribe(listener) {
if (destroyed) return () => undefined;
listeners.add(listener);
return () => listeners.delete(listener);
},
on: overlay.on,
off: overlay.off,
once: overlay.once,
bind(trigger, content) {
if (destroyed) return () => undefined;
binding.dispose();
const scope = createDisposableScope();
binding = scope;
scope.add(overlay.bind({ trigger, anchor: trigger, content }));
scope.add(
listen<PointerEvent>(trigger, 'pointerenter', (event) => {
if (event.pointerType !== 'touch') scheduleOpen('hover', event);
}),
);
scope.add(
listen<PointerEvent>(trigger, 'pointerleave', (event) => scheduleClose('hover', event)),
);
scope.add(listen<FocusEvent>(trigger, 'focusin', (event) => scheduleOpen('focus', event)));
scope.add(
listen<FocusEvent>(trigger, 'focusout', (event) => scheduleClose('focus-out', event)),
);
return () => scope.dispose();
},
scheduleOpen,
scheduleClose,
close,
destroy() {
if (destroyed) return;
destroyed = true;
clearTimer();
binding.dispose();
unsubscribe();
listeners.clear();
if (activeScopes.get(scope) === close) activeScopes.delete(scope);
overlay.destroy();
},
};
}
|