@rtorcato/js-common / events
events
Functions
on()
on<
K>(target,type,handler,options?): () =>void
Defined in: events/index.ts:16
Adds an event listener and returns a function to remove it.
Example
const off = on(button, 'click', (event) => console.log(event.clientX))
off() // listener removed
Type Parameters
K
K extends keyof HTMLElementEventMap
Parameters
target
EventTarget
The event target.
type
K
The event type.
handler
(event) => void
The event handler.
options?
boolean | AddEventListenerOptions
Event listener options.
Returns
Unsubscribe function.
() => void
emit()
emit(
target,type,detail?):boolean
Defined in: events/index.ts:41
Dispatches a custom event on the target.
Example
on(window, 'click', () => {})
emit(window, 'cart:add', { sku: 'A1' }) // true (nothing called preventDefault)
Parameters
target
EventTarget
The event target.
type
string
The event type.
detail?
unknown
Optional detail data.
Returns
boolean
True if not canceled.
onceEvent()
onceEvent<
K>(target,type):Promise<HTMLElementEventMap[K]>
Defined in: events/index.ts:61
Waits for a single event to occur and resolves a promise.
Named onceEvent rather than once so it cannot be confused with
functions.once, which memoises a call rather than awaiting an event.
Example
const event = await onceEvent(button, 'click')
event.type // 'click'
Type Parameters
K
K extends keyof HTMLElementEventMap
Parameters
target
EventTarget
The event target.
type
K
The event type.
Returns
Promise<HTMLElementEventMap[K]>
Resolves with the event object.
preventDefault()
preventDefault(
event):void
Defined in: events/index.ts:78
Prevents the default action for an event.
Parameters
event
Event
The event object.
Returns
void
stopPropagation()
stopPropagation(
event):void
Defined in: events/index.ts:86
Stops propagation for an event.
Parameters
event
Event
The event object.
Returns
void