# use-interaction

> React hook for getting and following user interaction type

Latest version **2.0.0-beta.3** (published 2020-03-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-interaction
pnpm add use-interaction
yarn add use-interaction
bun add use-interaction
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0-beta.3 |
| Published | 2020-03-03 |
| First published | 2020-02-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 9.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | idomusha |
| Maintainers | idomusha |
| Keywords | react, hook, user interaction, touch, mouse, keyboard, navigation, accessibility |

## Links

- npm: https://www.npmjs.com/package/use-interaction
- Repository: https://github.com/idomusha/use-interaction
- Homepage: https://idomusha.github.io/use-interaction/
- Issues: https://github.com/idomusha/use-interaction/issues
- npm.io page: https://npm.io/package/use-interaction

## Dependencies (3)

- [ulog](https://npm.io/package/ulog.md) ^1.1.0
- [react](https://npm.io/package/react.md) ^16.13.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.15

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.0-beta.3 (latest) — 2020-03-03
- 2.0.0-beta.2 — 2020-02-27
- 2.0.0-beta.1 — 2020-02-27
- 1.2.2 — 2020-02-16
- 1.2.1 — 2020-02-16
- 1.2.0 — 2020-02-16
- 1.1.0 — 2020-02-16
- 1.0.1 — 2020-02-14

## README

# React hook for following user interaction type

[![Build Status](https://travis-ci.org/idomusha/use-interaction.svg?branch=master&service=github)](https://travis-ci.org/idomusha/use-interaction)
[![npm version](https://badge.fury.io/js/use-interaction.svg?service=github)](https://badge.fury.io/js/use-interaction)
[![Coverage Status](https://coveralls.io/repos/github/idomusha/use-interaction/badge.svg?branch=master&service=github)](https://coveralls.io/github/idomusha/use-interaction?branch=master)

React hook `useInteraction()` allows to get the user interaction type: `touch`, `mouse` or `keyboard`.

### ▶︎ [Demo](https://idomusha.github.io/use-interaction/?log=debug)

## Installation

Using `yarn`:

```bash
yarn add use-interaction
```

Using `npm`:

```bash
npm i use-interaction --save
```

## Usage

Import the hook:

```javascript
import useInteraction from 'use-interaction'
```

### Following user interaction type

If the user interaction changes, the `pointerType`, `pointerHistory`, and `pointerAccuracy` values will be updated.
Keyboard strokes has no effect on the interaction type when the user is typing in a form element (input, select, and textarea). Only keyboard navigation is monitored.

```javascript
const [pointerType, pointerHistory, pointerAccuracy] = useInteraction()
```

### Full example

```javascript
import React from 'react'
import useInteraction from 'use-interaction'

export const Demo = () => {
  const [pointerType, pointerHistory, pointerAccuracy] = useInteraction()

  return (
    <code>
      pointer: {pointerType || `none`} (can
      {pointerType !== 'mouse' && 'not'} hover)
      <br />
      history: {`[${pointerHistory.join(', ')}]`}
      <br />
      accuracy: {pointerAccuracy || `none`}
      <br />
    </code>
  )
}
```

## Specification

### `useInteraction()` input

`object`

| Property Name |   Type    | Description                                                                                                                                             | Default Value |
| :------------ | :-------: | :------------------------------------------------------------------------------------------------------------------------------------------------------ | :-----------: |
| **initial**   | `boolean` | to not wait an action on the part of the user, the initial interaction type can be defined to be effective as soon as the page is loaded (i.e. `touch`) |    `null`     |

```javascript
import { isMobile } from 'react-device-detect'

const [pointerType, pointerHistory, pointerAccuracy] = useInteraction({
  initial: isMobile ? 'touch' : 'mouse',
})
```

### `useInteraction()` output

`array`

| Returned Array                |       Type       | Description                                                                                                  | Default Value |
| :---------------------------- | :--------------: | :----------------------------------------------------------------------------------------------------------- | :-----------: |
| 1st element (pointerType)     |     `string`     | current input of the user interaction: `touch`, `mouse` or `keyboard` (i.e. `touch`)                         |    `null`     |
| 2nd element (pointerHistory)  | `Array.<string>` | previous inputs of the user interaction listed in reverse chronological order (i.e. `['mouse', 'keyboard']`} |     `[]`      |
| 3rd element (pointerAccuracy) |     `number`     | [Experimental] max size of contact geometry collected from the current pointer (i.e. `23`)                   |    `null`     |

---
_Source: https://npm.io/package/use-interaction · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
