@shelf/react-outside-click 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/_/husky.sh +31 -0
- package/README.md +134 -0
- package/lib/components/ClickOutsideProvider.d.ts +2 -0
- package/lib/components/ClickOutsideProvider.js +8 -0
- package/lib/components/useClickOutside.d.ts +2 -0
- package/lib/components/useClickOutside.js +24 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/types.d.ts +12 -0
- package/lib/types.js +1 -0
- package/license +9 -0
- package/package.json +75 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
if [ -z "$husky_skip_init" ]; then
|
|
3
|
+
debug () {
|
|
4
|
+
if [ "$HUSKY_DEBUG" = "1" ]; then
|
|
5
|
+
echo "husky (debug) - $1"
|
|
6
|
+
fi
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
readonly hook_name="$(basename "$0")"
|
|
10
|
+
debug "starting $hook_name..."
|
|
11
|
+
|
|
12
|
+
if [ "$HUSKY" = "0" ]; then
|
|
13
|
+
debug "HUSKY env variable is set to 0, skipping hook"
|
|
14
|
+
exit 0
|
|
15
|
+
fi
|
|
16
|
+
|
|
17
|
+
if [ -f ~/.huskyrc ]; then
|
|
18
|
+
debug "sourcing ~/.huskyrc"
|
|
19
|
+
. ~/.huskyrc
|
|
20
|
+
fi
|
|
21
|
+
|
|
22
|
+
export readonly husky_skip_init=1
|
|
23
|
+
sh -e "$0" "$@"
|
|
24
|
+
exitCode="$?"
|
|
25
|
+
|
|
26
|
+
if [ $exitCode != 0 ]; then
|
|
27
|
+
echo "husky - $hook_name hook exited with code $exitCode (error)"
|
|
28
|
+
fi
|
|
29
|
+
|
|
30
|
+
exit $exitCode
|
|
31
|
+
fi
|
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# @shelf/react-outside-click
|
|
2
|
+
|
|
3
|
+
React library for handling outside clicks of a specified element.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the library using npm:
|
|
8
|
+
|
|
9
|
+
```shell
|
|
10
|
+
$ npm install @shelf/react-outside-click
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Install the library using yarn:
|
|
14
|
+
|
|
15
|
+
```shell
|
|
16
|
+
$ yarn add @shelf/react-outside-click
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
### ClickOutsideProvider
|
|
21
|
+
`ClickOutsideProvider` is a component that wraps its children and detects clicks outside of its container element.
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { ClickOutsideProvider } from '@shelf/react-outside-click';
|
|
25
|
+
|
|
26
|
+
const App = () => {
|
|
27
|
+
const onOutsideClick = () => {
|
|
28
|
+
console.log('Clicked outside')
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<ClickOutsideProvider onOutsideClick={onOutsideClick}>
|
|
33
|
+
<span>Shelf.io</span>
|
|
34
|
+
</ClickOutsideProvider>
|
|
35
|
+
);
|
|
36
|
+
};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### Props
|
|
40
|
+
|
|
41
|
+
**onOutsideClick**
|
|
42
|
+
|
|
43
|
+
Type: `function (required)`
|
|
44
|
+
|
|
45
|
+
A function that will be called when an outside click is detected.
|
|
46
|
+
|
|
47
|
+
**disabled**
|
|
48
|
+
|
|
49
|
+
Type: `boolean (optional)`
|
|
50
|
+
|
|
51
|
+
If set to true, the outside click detection will be disabled.
|
|
52
|
+
|
|
53
|
+
**mouseEvent**
|
|
54
|
+
|
|
55
|
+
Type: `string (optional)`
|
|
56
|
+
|
|
57
|
+
The mouse event to listen for. Defaults to 'mousedown'
|
|
58
|
+
|
|
59
|
+
**listenerOptions**
|
|
60
|
+
|
|
61
|
+
Type: `boolean | AddEventListenerOptions (optional)`
|
|
62
|
+
|
|
63
|
+
Additional options for the event listener. By default, it uses capture phase.This behavior ensures that the click outside event is captured before any click events on the descendants, even if stopPropagation is used.
|
|
64
|
+
See https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
### useClickOutside
|
|
69
|
+
`useClickOutside` is a custom hook that attaches an outside click event listener to a specified element.
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import { useRef } from 'react'
|
|
73
|
+
import { useClickOutside } from '@shelf/react-outside-click';
|
|
74
|
+
|
|
75
|
+
const Component() {
|
|
76
|
+
const ref = useRef(null)
|
|
77
|
+
|
|
78
|
+
const handleClickInside = () => {
|
|
79
|
+
console.log('Clicked outside')
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
useOnClickOutside(ref, handleClickOutside, {mouseEvent: 'mouseup'})
|
|
83
|
+
|
|
84
|
+
return <span ref={ref}>Shelf.io</span>
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### Props
|
|
89
|
+
|
|
90
|
+
**ref**
|
|
91
|
+
|
|
92
|
+
Type: `RefObject<T> (required)`
|
|
93
|
+
|
|
94
|
+
A ref object that points to the target element.
|
|
95
|
+
|
|
96
|
+
**onOutsideClick**
|
|
97
|
+
|
|
98
|
+
Type: `function (required)`
|
|
99
|
+
|
|
100
|
+
A function that will be called when an outside click is detected.
|
|
101
|
+
|
|
102
|
+
**options:**
|
|
103
|
+
|
|
104
|
+
> **disabled**
|
|
105
|
+
|
|
106
|
+
Type: `boolean (optional)`
|
|
107
|
+
|
|
108
|
+
If set to true, the outside click detection will be disabled.
|
|
109
|
+
|
|
110
|
+
> **mouseEvent**
|
|
111
|
+
|
|
112
|
+
Type: `string (optional)`
|
|
113
|
+
|
|
114
|
+
The mouse event to listen for. Defaults to 'mousedown'
|
|
115
|
+
|
|
116
|
+
> **listenerOptions**
|
|
117
|
+
|
|
118
|
+
Type: `boolean | AddEventListenerOptions (optional)`
|
|
119
|
+
|
|
120
|
+
Additional options for the event listener. By default, it uses capture phase.This behavior ensures that the click outside event is captured before any click events on the descendants, even if stopPropagation is used.
|
|
121
|
+
See https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture
|
|
122
|
+
|
|
123
|
+
## Publish
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
$ git checkout master
|
|
127
|
+
$ yarn version
|
|
128
|
+
$ yarn publish
|
|
129
|
+
$ git push origin master --tags
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT © [Shelf](https://shelf.io)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useRef } from 'react';
|
|
3
|
+
import { useClickOutside } from './useClickOutside';
|
|
4
|
+
export const ClickOutsideProvider = ({ children, onOutsideClick, ...options }) => {
|
|
5
|
+
const ref = useRef(null);
|
|
6
|
+
useClickOutside(ref, onOutsideClick, options);
|
|
7
|
+
return _jsx("div", { ref: ref, children: children });
|
|
8
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
export const useClickOutside = (ref, onOutsideClick, options) => {
|
|
3
|
+
const { mouseEvent = 'mousedown', disabled, listenerOptions = true } = options || {};
|
|
4
|
+
const savedHandler = useRef(onOutsideClick);
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
savedHandler.current = onOutsideClick;
|
|
7
|
+
}, [onOutsideClick]);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
const handleClickOutside = (event) => {
|
|
10
|
+
if (ref.current && !ref.current.contains(event.target)) {
|
|
11
|
+
savedHandler.current(event);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
if (!disabled) {
|
|
15
|
+
// `useCapture` for `options` is set to true by default to handle the capture phase of the event.
|
|
16
|
+
// It ensures that the click outside event will be captured before any click events on the descendants,
|
|
17
|
+
// even if `stopPropagation` is used.
|
|
18
|
+
document.addEventListener(mouseEvent, handleClickOutside, listenerOptions);
|
|
19
|
+
}
|
|
20
|
+
return () => {
|
|
21
|
+
document.removeEventListener(mouseEvent, handleClickOutside, listenerOptions);
|
|
22
|
+
};
|
|
23
|
+
}, [mouseEvent, disabled, listenerOptions, ref]);
|
|
24
|
+
};
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ReactNode, RefObject } from 'react';
|
|
2
|
+
export type ClickOutsideHandler = (event: MouseEvent) => void;
|
|
3
|
+
export type Ref<T extends HTMLElement = HTMLElement> = RefObject<T>;
|
|
4
|
+
export type Options = {
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
mouseEvent?: 'mousedown' | 'mouseup';
|
|
7
|
+
listenerOptions?: boolean | AddEventListenerOptions;
|
|
8
|
+
};
|
|
9
|
+
export type ClickOutsideProviderProps = {
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
onOutsideClick: ClickOutsideHandler;
|
|
12
|
+
} & Options;
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Gemshelf Inc. (shelf.io)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shelf/react-outside-click",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React library for handling outside clicks of a specified element",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Andrii Butsa",
|
|
8
|
+
"email": "andrii.butsa@shelf.io",
|
|
9
|
+
"url": "https://shelf.io"
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": "./lib/index.js",
|
|
14
|
+
"main": "lib/index.js",
|
|
15
|
+
"types": "lib/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"lib"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "rm -rf lib/ && tsc",
|
|
21
|
+
"coverage": "yarn test --coverage",
|
|
22
|
+
"lint": "yarn lint:ci --fix",
|
|
23
|
+
"lint:ci": "eslint . --ext .js,.ts,.tsx,.json",
|
|
24
|
+
"prepack": "yarn build",
|
|
25
|
+
"size": "size-limit",
|
|
26
|
+
"test": "TZ=UTC jest src",
|
|
27
|
+
"type-check": "tsc --noEmit"
|
|
28
|
+
},
|
|
29
|
+
"lint-staged": {
|
|
30
|
+
"*.{html,md,yml}": "prettier --write",
|
|
31
|
+
"*.{ts,js,json,tsx,jsx}": "eslint --fix",
|
|
32
|
+
".circleci/config.yml": ".husky/validate-circleci-config.sh"
|
|
33
|
+
},
|
|
34
|
+
"prettier": "@shelf/prettier-config",
|
|
35
|
+
"dependencies": {},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@shelf/eslint-config": "2.29.3",
|
|
38
|
+
"@shelf/prettier-config": "1.0.0",
|
|
39
|
+
"@shelf/tsconfig": "0.0.11",
|
|
40
|
+
"@size-limit/esbuild-why": "8.2.4",
|
|
41
|
+
"@size-limit/preset-small-lib": "8.2.4",
|
|
42
|
+
"@testing-library/jest-dom": "5.16.5",
|
|
43
|
+
"@testing-library/react": "14.0.0",
|
|
44
|
+
"@types/jest": "29.5.2",
|
|
45
|
+
"@types/node": "16",
|
|
46
|
+
"@types/react": "18.2.13",
|
|
47
|
+
"eslint": "8.43.0",
|
|
48
|
+
"husky": "8.0.3",
|
|
49
|
+
"jest": "29.5.0",
|
|
50
|
+
"jest-environment-jsdom": "29.5.0",
|
|
51
|
+
"lint-staged": "13.2.2",
|
|
52
|
+
"prettier": "2.8.8",
|
|
53
|
+
"react": "18.2.0",
|
|
54
|
+
"react-dom": "18.2.0",
|
|
55
|
+
"size-limit": "8.2.4",
|
|
56
|
+
"ts-jest": "29.1.0",
|
|
57
|
+
"typescript": "5.1.3"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"react": "^17.0.0 || ^18.0.0",
|
|
61
|
+
"react-dom": "^17.0.0 || ^18.0.0"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=16"
|
|
65
|
+
},
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"access": "public"
|
|
68
|
+
},
|
|
69
|
+
"size-limit": [
|
|
70
|
+
{
|
|
71
|
+
"path": "lib/index.js",
|
|
72
|
+
"limit": "425 B"
|
|
73
|
+
}
|
|
74
|
+
]
|
|
75
|
+
}
|