# Callbacks

## Structure

All callbacks (including [main callback](https://minwork.gitbook.io/long-press-hook/advanced/callback) function) has the same structure.

{% code title="Pseudocode" %}

```javascript
callbackFn(event, meta): void
```

{% endcode %}

{% code title="TypeScript" %}

```ts
type LongPressCallback<Target extends Element = Element, Context = unknown> = (
  event: LongPressEvent<Target>,
  meta: LongPressCallbackMeta<Context>
) => void
```

{% endcode %}

As a first argument callback receives [React event](https://minwork.gitbook.io/long-press-hook/advanced/options/react-events) from a proper [handler](https://minwork.gitbook.io/long-press-hook/advanced/hook-result-bind-function/handlers) (e.g. `onMouseDown`) and as second receives *meta* object with following structure:

*Pseudocode*

{% code title="Pseudocode" %}

```
{ [context: any], [reason: string] }
```

{% endcode %}

{% code title="TypeScript" %}

```ts
export type LongPressCallbackMeta<Context = unknown> = { 
    context?: Context; 
    reason?: LongPressCallbackReason 
};
```

{% endcode %}

Both object properties are optional.

### Context

`context` will be present if you pass it to [bind function](https://minwork.gitbook.io/long-press-hook/advanced/hook-result-bind-function). See [context](https://minwork.gitbook.io/long-press-hook/advanced/hook-result-bind-function/context) for more info.

### Reason

`reason` will be present in [onCancel](https://minwork.gitbook.io/long-press-hook/advanced/options/callbacks/long-press-cancelled) callback to indicate why long press was cancelled.&#x20;

Here is a list of all possible *reason* values

{% code title="Javascript" %}

```javascript
'cancelled-by-movement' | 'cancelled-by-release' | 'cancelled-outside-element'
```

{% endcode %}

{% code title="TypeScript" %}

```typescript
export enum LongPressCallbackReason {
  /**
   * Returned when mouse / touch / pointer was moved outside initial press area when _cancelOnMovement_ is active
   */
  CancelledByMovement = 'cancelled-by-movement',
  /**
   * Returned when click / tap / point was released before long press detection time threshold
   */
  CancelledByRelease = 'cancelled-by-release',
  /**
   * Returned when mouse / touch / pointer was moved outside element and _cancelOutsideElement_ option was set to `true`
   */
  CancelledOutsideElement = 'cancelled-outside-element',
}
```

{% endcode %}

{% content-ref url="callbacks/press-started" %}
[press-started](https://minwork.gitbook.io/long-press-hook/advanced/options/callbacks/press-started)
{% endcontent-ref %}

{% content-ref url="callbacks/on-move" %}
[on-move](https://minwork.gitbook.io/long-press-hook/advanced/options/callbacks/on-move)
{% endcontent-ref %}

{% content-ref url="callbacks/press-finished" %}
[press-finished](https://minwork.gitbook.io/long-press-hook/advanced/options/callbacks/press-finished)
{% endcontent-ref %}

{% content-ref url="callbacks/long-press-cancelled" %}
[long-press-cancelled](https://minwork.gitbook.io/long-press-hook/advanced/options/callbacks/long-press-cancelled)
{% endcontent-ref %}
