Comma Agents
@comma-agents/tuiHooks

useMouse

Mouse interaction hooks — click, hover, and wheel scroll event handling via SGR mouse sequences.

The TUI supports mouse interaction in modern terminals via SGR (1006) mouse escape sequences. The MouseProvider (mounted by <Frame>) enables the necessary terminal modes and dispatches parsed mouse events to subscribers.

Three hooks provide click, hover, and scroll wheel capabilities. All require a <MouseProvider> ancestor.

MouseEvent

All mouse hooks receive MouseEvent objects:

FieldTypeDescription
kindMouseEventKind"press" | "release" | "move" | "drag" | "wheel-up" | "wheel-down"
button0 | 1 | 2 | nullMouse button (0=left, 1=middle, 2=right, null=no button)
columnnumber1-indexed terminal column
rownumber1-indexed terminal row
modifiersMouseModifiers{ shift, meta, ctrl }

useMouseClick

Fires onClick when a mouse press event with a matching button lands inside the referenced element:

import { useMouseClick } from "@comma-agents/tui";

const boxRef = useRef<DOMElement>(null);

useMouseClick({
  ref: boxRef,
  onClick: (event) => {
    // event.button is 0 (left click) by default
    console.log("Clicked!");
  },
});

<Box ref={boxRef}>Click me</Box>
OptionTypeDefaultDescription
refRefObject<DOMElement>--Element to hit-test against
onClick(event: MouseEvent) => void--Click handler
buttonsreadonly (0 | 1 | 2)[][0]Which buttons to respond to

useMouseHover

Tracks whether the mouse cursor is over a specific element:

import { useMouseHover } from "@comma-agents/tui";

const { isHovered } = useMouseHover({
  ref: elementRef,
  onEnter: () => console.log("mouse entered"),
  onLeave: () => console.log("mouse left"),
  onMove: (event) => { /* continuous tracking */ },
});
OptionTypeDescription
refRefObject<DOMElement>Element to track
onEnter(event: MouseEvent) => voidCalled when mouse enters the element
onLeave(event: MouseEvent) => voidCalled when mouse leaves the element
onMove(event: MouseEvent) => voidCalled on every move inside the element

Returns { isHovered: boolean }. Returns { isHovered: false } (does not throw) when no MouseProvider is mounted.

useMouseWheelScroll

Subscribes to wheel-up and wheel-down events, optionally scoped to an element:

import { useMouseWheelScroll } from "@comma-agents/tui";

useMouseWheelScroll({
  onScroll: ({ direction, column, row }) => {
    // direction is "up" or "down"
  },
  ref: viewportRef,  // optional: only events inside this element
});
OptionTypeDescription
onScroll(event: MouseScrollEvent) => voidWheel event handler
refRefObject<DOMElement> | undefinedOptional element scoping

When ref is omitted, all wheel ticks are forwarded regardless of position.

isInsideRef

AABB hit-test utility, also exported:

import { isInsideRef } from "@comma-agents/tui";

if (isInsideRef(myRef, column, row)) {
  // The mouse at (column, row) is inside myRef's bounding box
}

On this page