Skip to content

Commit

Permalink
Add timeout option.
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrinicolas committed Mar 10, 2019
1 parent 81cad13 commit e9dc96f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.0 - 2019-03-10

- Add `timeout` option.

## 1.0.1 - 2019-03-09

- Move react to peer-dependency.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ The `useMouseDown` and `useMouseUp` are both a shortcut to respectively set the
- `up` (default: `false`): If the element should listen to mouseup event.
- `touch` (default: `true`): If the element should listen to touch equivalent
events.
- `timeout` (default: `10`): Short timeout in milliseconds to prevents multiple
events.

You can provide functions that should listen to each event with theses options:

Expand Down
4 changes: 2 additions & 2 deletions example/dist/index.js

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions example/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ const DropDown = () => {
const props = useMouseDown(() => setOpen(state => !state));

return (
<div className="dropdown">
<button type="button" {...props}>
Open drop-down
</button>
<ul style={{ display: open ? 'block' : 'none' }}>
<DropDownItem handleClick={() => setOpen(false)}>Action #1</DropDownItem>
<DropDownItem handleClick={() => setOpen(false)}>Action #2</DropDownItem>
<DropDownItem handleClick={() => setOpen(false)}>Action #3</DropDownItem>
</ul>
</div>
<>
<div className="dropdown">
<button type="button" {...props}>
Open drop-down
</button>
<ul style={{ display: open ? 'block' : 'none' }}>
<DropDownItem handleClick={() => setOpen(false)}>Action #1</DropDownItem>
<DropDownItem handleClick={() => setOpen(false)}>Action #2</DropDownItem>
<DropDownItem handleClick={() => setOpen(false)}>Action #3</DropDownItem>
</ul>
</div>
</>
);
};

Expand Down
1 change: 1 addition & 0 deletions example/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pre {
text-align: left;
border: none;
cursor: pointer;
background-color: white;
transition: 0.12s;
}
.dropdown ul li button:hover {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-mouse-action",
"version": "1.0.1",
"version": "1.1.0",
"description": "React Hooks to listen to both mouse down or up and click events with a once called function.",
"author": "Dimitri NICOLAS <dimitri@ooeo.fr>",
"license": "MIT",
Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useCallback, useEffect, useState } from 'react';
import { HooksInput } from './types/options';
import { IProps } from './types/props';

const DEFAULT_OPTIONS = { down: false, up: false, touch: true };
const DEFAULT_OPTIONS = { down: false, up: false, touch: true, timeout: 10 };

/**
* Test if a mouse event can trigger a click.
Expand All @@ -26,6 +26,7 @@ export const useMouseAction = (options: HooksInput): IProps => {
down,
up,
touch,
timeout,
onClick,
onMouseDown,
onMouseUp,
Expand All @@ -52,7 +53,7 @@ export const useMouseAction = (options: HooksInput): IProps => {
*
*/
const onMouseDownEnd = () => {
setTimeout(() => setMouseDown(false), 10);
setTimeout(() => setMouseDown(false), timeout);
};

/**
Expand All @@ -71,7 +72,7 @@ export const useMouseAction = (options: HooksInput): IProps => {
*/
const onMouseUpTriggered = () => {
setMouseUpTriggered(true);
setTimeout(() => setMouseUpTriggered(false), 10);
setTimeout(() => setMouseUpTriggered(false), timeout);
};

/**
Expand Down Expand Up @@ -199,7 +200,7 @@ export const useMouseAction = (options: HooksInput): IProps => {
* prevent next coming click event to be catched
* by a newly rendered element above the button.
*/
setTimeout(() => onAction(event), 10);
setTimeout(() => onAction(event), timeout);
}
};

Expand All @@ -220,7 +221,7 @@ export const useMouseAction = (options: HooksInput): IProps => {
* to be catched by a newly rendered element
* above the button.
*/
setTimeout(() => onAction(event), 10);
setTimeout(() => onAction(event), timeout);
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ interface IOptions {
*/
touch?: boolean;

/**
* Short time to prevent multiple events.
* @default 10
*/
timeout?: number;

/**
* Native click event handler passed into returned props.
*/
Expand Down

0 comments on commit e9dc96f

Please sign in to comment.