import { ReactiveController, ReactiveControllerHost } from "lit"; /** * Represents an item that can be tickled (highlighted via hover or keyboard navigation) */ type TickledItem = { /** * Unique identifier for the item (e.g., index, id, path) */ readonly id: T; /** * Whether the item is currently enabled/interactive */ readonly enabled?: boolean; /** * `false`: hover may still tickle the item, but keyboard cursor movement * skips it. Independent of `enabled` (which blocks both). */ readonly focusable?: boolean; }; /** * Input mode determines which mechanism controls the tickled state */ type InputMode = `keyboard` | `mouse`; /** * Configuration for TickledController */ type TickledControllerConfig = { /** * Function to retrieve all items that can be tickled */ getItems: () => ReadonlyArray>; /** * Optional O(1) shortcut for hover: whether the item with this id may be * tickled. When provided, hover does not call `getItems()` at all — which * matters for large virtualised lists where building the item array is the * dominant cost. Return `true` for unknown ids (hover on an item that isn't * listed is allowed, as without this hook). */ isEnabled?: (id: T) => boolean; /** * Optional callback when an item becomes tickled * Use this for side effects like opening submenus * @param id - The id of the newly tickled item, or undefined if nothing is tickled */ onTickle?: (id: T | undefined) => void; /** * Optional callback fired after any state change (tickled id or input mode) * Use this to sync DOM attributes for slotted content that can't use reactive rendering * * For components that render items directly, this is usually not needed as * Lit's reactive rendering will handle updates automatically via requestUpdate() * * @example * ```ts * onStateChange: () => { * // Sync data-tickled attributes for slotted items * const items = this.getItems(); * items.forEach((item, i) => { * if (this.tickled.isTickled(i)) { * item.setAttribute('data-tickled', ''); * } else { * item.removeAttribute('data-tickled'); * } * }); * } * ``` */ onStateChange?: () => void; /** * Optional initial input mode (defaults to 'mouse') */ initialInputMode?: InputMode; /** * When this returns `true`, pointer hover no longer drives the cursor: * `handlePointerEnter` / `handlePointerLeave` become no-ops and hover never * switches the input mode to `mouse`. Keyboard navigation, programmatic * `focus()` / `setTickled()`, and click handling are unaffected. Read on every * hover event, so a host can flip it via a reactive property. Default: `false`. */ hoverDisabled?: () => boolean; /** * Whether keyboard navigation wraps from last to first and vice versa. * - 'stop' (default): navigation stops at the boundary * - 'wrap': navigation wraps around */ itemWrap?: `stop` | `wrap`; }; /** * ReactiveController that manages "tickled" state for interactive lists. * * A "tickled" item is one that is under active attention via hover (mouse mode) * or keyboard focus (keyboard mode). Only one item can be tickled at a time. * * The input mode determines which mechanism controls tickled state: * - Mouse mode: hover controls tickled, keyboard navigation ignored * - Keyboard mode: keyboard navigation controls tickled, hover ignored * * Last interaction wins - any mouse hover switches to mouse mode, any keyboard * navigation switches to keyboard mode. * * ## Global Mouse Tracking * * This controller uses a single, ref-counted global `pointermove` listener (shared * across all instances) to track actual mouse movement. This prevents `pointerenter` * events caused by content scrolling from incorrectly switching to mouse mode when * the user is actually using keyboard navigation. The listener is attached when the * first host connects and removed once the last host disconnects. * * @example * ```ts * class MyList extends LitElement { * private tickled = new TickledController(this, { * getItems: () => this.items.map((item, i) => ({ id: i, enabled: !item.disabled })), * onTickle: (id) => { * // Optional: perform side effect when item is tickled * console.log('Tickled item:', id); * } * }); * * render() { * return html` *
    this.tickled.handlePointerLeave(e)}> * ${this.items.map((item, i) => html` *
  • this.tickled.handlePointerEnter(i)} * >${item.label}
  • * `)} *
* `; * } * * private handleKeyDown(e: KeyboardEvent) { * if (e.key === 'ArrowDown') { * this.tickled.handleKeyboardNavigation('next'); * } else if (e.key === 'ArrowUp') { * this.tickled.handleKeyboardNavigation('previous'); * } * } * } * ``` */ declare class TickledController implements ReactiveController { private host; private config; private _tickledId; private _inputMode; private hoverDisabled; private static _lastMouseClientX; private static _lastMouseClientY; private static _mouseMovedSinceKeyboard; private static _listenerRefCount; private static readonly _onGlobalPointerMove; constructor(host: ReactiveControllerHost, config: TickledControllerConfig); hostConnected(): void; hostDisconnected(): void; /** * Get the current input mode */ get inputMode(): InputMode; /** * Get the currently tickled item id */ get tickledId(): T | undefined; /** * The item currently under the cursor, or `undefined`. Alias of `tickledId` * for parity with other controllers. */ get current(): T | undefined; /** * Move the cursor programmatically (e.g. to reveal a match or re-seat it * after the item set changed). Does not change the input mode. Alias of * `setTickled`. */ focus(id: T | undefined): void; /** * Check if a specific item is tickled */ isTickled(id: T): boolean; /** * Handle global pointer move event. * Called automatically by the global listener. * Tracks mouse movement to distinguish real hover from content scrolling. */ private static handleGlobalPointerMove; /** * Ignore hover (pointerenter / pointerleave) until the pointer genuinely * moves. Call this when something other than the pointer changes what is * under it — a keypress, or the item set being swapped. */ static ignoreHoverUntilPointerMoves(): void; /** * Check if actual mouse movement has occurred since last keyboard interaction. * Used to distinguish real hover/leave from content-scrolling-induced events. */ static get hasMouseMovedSinceKeyboard(): boolean; /** * Handle pointer enter event on an item (mouse hover). * Only processes if actual mouse movement is detected (not content scrolling). * Switches to mouse mode and tickles the item. */ handlePointerEnter(id: T): void; /** * Handle pointer leave event on an item. * Only clears tickled state if actual mouse movement is detected. * @param _e The pointer event (not used, but kept for API compatibility) */ handlePointerLeave(_e: PointerEvent): void; /** * Handle keyboard navigation. * Switches to keyboard mode and moves tickled state. * * @param direction - 'next', 'previous', 'first', 'last', or specific id */ handleKeyboardNavigation(direction: `next` | `previous` | `first` | `last` | T): void; /** * Manually set the tickled item (useful for programmatic control). * Does NOT change input mode. */ setTickled(id: T | undefined): void; /** * Clear the tickled state (e.g., on pointer leave). * Note: For pointer leave events, prefer using handlePointerLeave() which * checks for actual mouse movement. */ clearTickled(): void; /** * Manually set the input mode. * When switching to keyboard mode, resets the mouse movement tracking. */ setInputMode(mode: InputMode): void; } export { TickledItem as i, TickledController as n, TickledControllerConfig as r, InputMode as t };