Skip to content

Commit e9dc96f

Browse files
Add timeout option.
1 parent 81cad13 commit e9dc96f

File tree

8 files changed

+34
-18
lines changed

8 files changed

+34
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
This project adheres to
44
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

6+
## 1.1.0 - 2019-03-10
7+
8+
- Add `timeout` option.
9+
610
## 1.0.1 - 2019-03-09
711

812
- Move react to peer-dependency.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ The `useMouseDown` and `useMouseUp` are both a shortcut to respectively set the
6060
- `up` (default: `false`): If the element should listen to mouseup event.
6161
- `touch` (default: `true`): If the element should listen to touch equivalent
6262
events.
63+
- `timeout` (default: `10`): Short timeout in milliseconds to prevents multiple
64+
events.
6365

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

example/dist/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/index.jsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ const DropDown = () => {
2929
const props = useMouseDown(() => setOpen(state => !state));
3030

3131
return (
32-
<div className="dropdown">
33-
<button type="button" {...props}>
34-
Open drop-down
35-
</button>
36-
<ul style={{ display: open ? 'block' : 'none' }}>
37-
<DropDownItem handleClick={() => setOpen(false)}>Action #1</DropDownItem>
38-
<DropDownItem handleClick={() => setOpen(false)}>Action #2</DropDownItem>
39-
<DropDownItem handleClick={() => setOpen(false)}>Action #3</DropDownItem>
40-
</ul>
41-
</div>
32+
<>
33+
<div className="dropdown">
34+
<button type="button" {...props}>
35+
Open drop-down
36+
</button>
37+
<ul style={{ display: open ? 'block' : 'none' }}>
38+
<DropDownItem handleClick={() => setOpen(false)}>Action #1</DropDownItem>
39+
<DropDownItem handleClick={() => setOpen(false)}>Action #2</DropDownItem>
40+
<DropDownItem handleClick={() => setOpen(false)}>Action #3</DropDownItem>
41+
</ul>
42+
</div>
43+
</>
4244
);
4345
};
4446

example/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pre {
9090
text-align: left;
9191
border: none;
9292
cursor: pointer;
93+
background-color: white;
9394
transition: 0.12s;
9495
}
9596
.dropdown ul li button:hover {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-mouse-action",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "React Hooks to listen to both mouse down or up and click events with a once called function.",
55
"author": "Dimitri NICOLAS <dimitri@ooeo.fr>",
66
"license": "MIT",

src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useCallback, useEffect, useState } from 'react';
33
import { HooksInput } from './types/options';
44
import { IProps } from './types/props';
55

6-
const DEFAULT_OPTIONS = { down: false, up: false, touch: true };
6+
const DEFAULT_OPTIONS = { down: false, up: false, touch: true, timeout: 10 };
77

88
/**
99
* Test if a mouse event can trigger a click.
@@ -26,6 +26,7 @@ export const useMouseAction = (options: HooksInput): IProps => {
2626
down,
2727
up,
2828
touch,
29+
timeout,
2930
onClick,
3031
onMouseDown,
3132
onMouseUp,
@@ -52,7 +53,7 @@ export const useMouseAction = (options: HooksInput): IProps => {
5253
*
5354
*/
5455
const onMouseDownEnd = () => {
55-
setTimeout(() => setMouseDown(false), 10);
56+
setTimeout(() => setMouseDown(false), timeout);
5657
};
5758

5859
/**
@@ -71,7 +72,7 @@ export const useMouseAction = (options: HooksInput): IProps => {
7172
*/
7273
const onMouseUpTriggered = () => {
7374
setMouseUpTriggered(true);
74-
setTimeout(() => setMouseUpTriggered(false), 10);
75+
setTimeout(() => setMouseUpTriggered(false), timeout);
7576
};
7677

7778
/**
@@ -199,7 +200,7 @@ export const useMouseAction = (options: HooksInput): IProps => {
199200
* prevent next coming click event to be catched
200201
* by a newly rendered element above the button.
201202
*/
202-
setTimeout(() => onAction(event), 10);
203+
setTimeout(() => onAction(event), timeout);
203204
}
204205
};
205206

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

src/types/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ interface IOptions {
3232
*/
3333
touch?: boolean;
3434

35+
/**
36+
* Short time to prevent multiple events.
37+
* @default 10
38+
*/
39+
timeout?: number;
40+
3541
/**
3642
* Native click event handler passed into returned props.
3743
*/

0 commit comments

Comments
 (0)