Event Plugins
Event plugins are plugins that can’t manipulate views but can read them and emit events about them.
Why Event Plugins?
The main rule when using plugins in Veloxi is that nesting plugins is not allowed. In other words, each view can only be assigned to a single plugin.
However, there are instances where we need to encapsulate certain logic within a plugin and reuse it across multiple plugins. One such scenario is the detection of a drag event on a specific view.
While it’s possible to implement the drag event within our plugin using various global events that Veloxi provides (such as PointerDownEvent
and PointerMoveEvent
), this approach isn’t practical, as these events may need to be reused in multiple plugins.
Since event plugins can’t manipulate view, it’s safe to use them inside our plugins. For instance, Veloxi has a built-in event plugin named DragEventPlugin
, which detects drag events occurring on specific views.
Let’s see how to use it.
Using Event Plugins
To use an event plugin, you need first to import it and then use useEventPlugin(EventPlugin)
function on it.
Built-in Event Plugins
Currently, Veloxi provides two built-in event plugin that you can use them in your projects: DragEventPlugin and SwipeEventPlugin. Let’s learn about them.
DragEventPlugin
The DragEventPlugin is used to listen for drag events on a view. The event object in the listener includes the following:
- view: The view instance that was dragged.
- previousX: The previous x position of the drag event (from the last frame).
- previousY: The previous y position of the drag event (from the last frame).
- x: The current x position of the drag event.
- y: The current y position of the drag event.
- isDragging: A boolean that indicates whether the view is currently being dragged.
- target: The EventTarget of the dragged element.
- directions: An array of drag directions, including
up
,down
,left
, andright
. - width: The horizontal distance moved by the user during dragging, measured in pixels.
- height: The vertical distance moved by the user during dragging, measured in pixels.
SwipeEventPlugin
The SwipeEventPlugin is used to listen for swipe events on a view. The event object in the listener includes the following:
- view: The view instance that was swiped.
- direction: A string indicating the swipe direction, which can be one of the following:
up
,down
,left
, orright
.
Create Your Own Event Plugin
In the current version of Veloxi, you can create Event Plugins only using classes.
Creating Event Plugins is very similar to regular plugins, with a few distinctions:
- Event Plugins do not include update or render hooks.
- Event Plugins should not preform view updates.
- Event Plugins should not be scoped.
To create an Event Plugin, use the EventPlugin
as the base class.