@molecule/app-pointer-events-dom
Utility · ui · App (browser) · v1.1.0 · Apache-2.0
DOM pointer event handling shared by Molecule web UI kits
npm install @molecule/app-pointer-events-domHow it works
@molecule/app-pointer-events-dom is a utility package for the app (browser) side (ui).
import { bindWebDrag } from '@molecule/app-pointer-events-dom'
const host = document.querySelector<HTMLElement>('[data-mol-id="drag-handle"]')
if (host) {
const binding = bindWebDrag(host, { onStart: (point) => console.log(point.x) })
// Call binding.update(handlers) on updates and binding.destroy() on unmount.
}Works with: @molecule/app-ui
Reference
Shared DOM drag, hover, and Ctrl-wheel zoom gestures for the Molecule web UI kits.
Use the renderer's drag binding with ElementDrag from @molecule/app-ui.
All web kits share press ownership, pointer capture, touch intent, and
cancellation on pointer cancel, lost capture, window blur, and host removal.
Use markPressTarget for custom controls that must keep their own presses.
Use bindWebHover for one mouse or pen transition per boundary and
bindWebWheelZoom to handle Ctrl-wheel steps on a surface.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { bindWebDrag } from '@molecule/app-pointer-events-dom'
const host = document.querySelector<HTMLElement>('[data-mol-id="drag-handle"]')
if (host) {
const binding = bindWebDrag(host, { onStart: (point) => console.log(point.x) })
// Call binding.update(handlers) on updates and binding.destroy() on unmount.
}
Type
utility
Installation
npm install @molecule/app-pointer-events-dom @molecule/app-ui
API
Interfaces
WebDrag
The renderer-facing controls for one drag host.
interface WebDrag {
/**
* Updates the callbacks used by the gesture. Removing the handlers cancels the contact.
*
* @param drag - The current handlers, or undefined to disable dragging.
*/
update(drag: ElementDrag | undefined): void
/**
* Offers a pointer press to this host.
*
* @param host - The element receiving the press.
* @param down - The native pointer event shared by all hosts.
* @param drag - The press settings, when supplied by a renderer's event binding.
* @param target - The renderer's normalized target, when it differs from the native target.
*/
start(
host: HTMLElement | SVGElement,
down: PointerEvent,
drag?: ElementDrag,
target?: EventTarget | null,
): void
/** Cancels the active contact, if any. */
cancel(): void
}
WebDragBinding
The lifecycle of a drag attached to a DOM element.
interface WebDragBinding {
/**
* Updates the handlers for this host.
*
* @param drag - The current handlers, or undefined to disable dragging.
*/
update(drag: ElementDrag | undefined): void
/** Removes the pointer listener and cancels any active contact. */
destroy(): void
}
WebHoverHandlers
Callbacks for the shared hover transition.
interface WebHoverHandlers {
/** Runs when a mouse or pen enters the host. */
onEnter?: (event: MouseEvent) => void
/** Runs when the hovering mouse or pen leaves the host. */
onLeave?: (event: MouseEvent) => void
}
WebHoverBinding
The lifecycle of a hover attached to a DOM element.
interface WebHoverBinding {
/**
* Updates the callbacks. Pass undefined to disable hover.
*
* @param handlers - The current hover callbacks.
*/
update(handlers: WebHoverHandlers | undefined): void
/** Removes the event listeners. */
destroy(): void
}
WebWheelZoomBinding
The lifecycle of the Ctrl-wheel shortcut on a surface.
interface WebWheelZoomBinding {
/**
* Updates the callback. Pass undefined to leave wheel input to the browser.
*
* @param onStep - The current zoom callback.
*/
update(onStep: WebWheelZoomHandler | undefined): void
/** Removes the wheel listener. */
destroy(): void
}
Types
WebWheelZoomHandler
Called with the direction of one Ctrl-wheel step.
type WebWheelZoomHandler = (direction: 'in' | 'out') => void
Functions
markPressTarget(host)
Marks a custom press target so an enclosing drag leaves its activation alone.
function markPressTarget(host: Element): void
host— The element that owns the activation.
createWebDrag(initialDrag)
Acquires a press on the deepest drag host and follows its pointer until release or interruption.
function createWebDrag(initialDrag?: ElementDrag): WebDrag
initialDrag— The initial handlers, if the host starts enabled.
Returns: The controls a renderer uses to handle pointerdown, updates and unmount.
bindWebDrag(host, drag)
Binds a drag to a native bubbling pointerdown listener.
function bindWebDrag(host: HTMLElement | SVGElement, drag?: ElementDrag): WebDragBinding
host— The element that can own the press.drag— The initial handlers, if the host starts enabled.
Returns: The binding to update or destroy with the host.
createWebHover()
Creates one enter/leave transition for a host. Touch and its compatibility mouse events never hover. A later mouse or pen pointer resumes hover.
function createWebHover(): (entered: boolean, event: HoverEvent) => boolean
Returns: A function that reports whether an event changes the host's hover state.
bindWebHover(host, initialHandlers)
Binds shared hover to a DOM element. The bubbling over/out events and the direct enter/leave events use the same transition. Moving between a host's descendants leaves its hover state alone.
function bindWebHover(
host: HTMLElement | SVGElement,
initialHandlers?: WebHoverHandlers,
): WebHoverBinding
host— The HTML or SVG element that receives hover.initialHandlers— The callbacks to use until the first update.
Returns: The binding to update or destroy with the host.
bindWebWheelZoom(host, initialStep)
Listens non-passively for Ctrl-wheel with a finite, nonzero vertical delta. The handled event is consumed so the browser leaves page zoom alone.
function bindWebWheelZoom(
host: Partial<EventTarget>,
initialStep?: WebWheelZoomHandler,
): WebWheelZoomBinding
host— The surface receiving wheel events. A handle without DOM listeners is inert.initialStep— The zoom callback to use until the first update.
Returns: The binding to update or destroy with the host.
Injection Notes
Requirements
Peer dependencies:
@molecule/app-ui^1.9.0
Runtime Dependencies
@molecule/app-ui