Documentation
Search
K

Custom event listeners

Attach any custom event listeners to specific nodes in your component, or to the entire component itself.

Custom event handlers

Use the eventHandlers prop to register any JavaScript functions that should fire based on any event you care about. Mason offers two methods - you can target the entire component or you can target specific elements within the component.

Targeting the entire component:

Provide an eventHandlers prop to your component as an object that contains all of the custom event handler functions you want to register on your component.
Register functions on the events you care about, with the syntax:
onEventName: (e) => // Any JavaScript function here
For example:
import React from 'react';
import { Canvas } from '@mason-api/react-sdk';
class MyFeed extends React.Component {
render() {
return <Canvas
id="YOUR_COMPONENT_ID"
eventHandlers={
{
onClick: (e) => alert("You clicked something in your component"),
onMouseOver: (e) => alert("You moved your mouse over something in your component")
}
/>;
}
}

Targeting specific elements within your component:

Provide an eventHandlers prop to your component as an object that contains all of the custom event handler functions you want to register on your component.
Register functions on the events and nodes you care about, with the syntax:
onLabelNameEventName: (e) => // Any JavaScript function here
You define the LabelName in the builder. For example, if you had a button named "login button", the onClick event listener syntax would be onLoginButtonClick.
For example:
import React from 'react';
import { Canvas } from '@mason-api/react-sdk';
class MyFeed extends React.Component {
render() {
return <Canvas
id="YOUR_COMPONENT_ID"
eventHandlers={
{
onButtonOneClick: (e) => alert('Click triggered on Button One'),
onButtonThreeDoubleClick: (e) => alert('Double-clicked triggered on Button Three'),
onButtonFiveMouseDown: (e) => alert('Mousedown triggered on Button Five'),
onButtonSevenMouseUp: (e) => alert('Mouseup triggered on Button Seven'),
onButtonNineMouseOut: (e) => alert('Mouseout triggered on Button Nine'),
}
}
/>
}
}

Supported event listeners

JavaScript event
Mason Syntax
click
onClick / onLabelNameClick
dblclick
onDoubleClick / onLabelNameDoubleClick
mousedown
onMouseDown / onLabelNameMouseDown
mouseup
onMouseUp / onLabelNameMouseUp
mouseout
onMouseOut / onLabelNameMouseOut
onMouseLeave / onLabelNameMouseLeave
onMouseEnter / onLabelNameMouseEnter
mouseover
onMouseOver / onLabelNameMouseOver
keydown
onKeyDown / onLabelNameKeyDown
keyup
onKeyUp / onLabelNameKeyUp
focus
onFocus / onLabelNameFocus
blur
onBlur / onLabelNameBlur

Live demo

See a working demo of all supported event listeners at https://showcase.trymason.com/events