# @kne/use-click-outside

> 用于捕获dom以外的元素点击事件

Latest version **0.2.1** (published 2022-08-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install @kne/use-click-outside
pnpm add @kne/use-click-outside
yarn add @kne/use-click-outside
bun add @kne/use-click-outside
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2022-08-05 |
| First published | 2020-05-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | linzp |
| Maintainers | yangwang123456, juliewang, lingtong |

## Links

- npm: https://www.npmjs.com/package/@kne/use-click-outside
- Repository: https://github.com/kne-union/use-click-outside
- Homepage: https://github.com/kne-union/use-click-outside#readme
- Issues: https://github.com/kne-union/use-click-outside/issues
- npm.io page: https://npm.io/package/@kne/use-click-outside

## Recent versions

- 0.2.1 (latest) — 2022-08-05
- 0.2.0 — 2022-03-08
- 0.1.2 — 2022-03-08
- 0.1.1 — 2021-06-18
- 0.1.0 — 2020-05-09

## README

# use-click-outside


### 描述

用于捕获dom以外的元素点击事件


### 安装

```shell
npm i --save @kne/use-click-outside
```


### 概述

实现了一个点击容器之外触发回调函数的hooks


### 示例


#### 示例样式

```scss
.container{
  width: 200px;
  height: 200px;
  border: solid 1px #ccc;
  border-radius: 6px;
  display: flex;
  justify-content: center;
  align-items: center;
}
```

#### 示例代码

- 基本示例
- 点击容器外部就弹窗提示
- _useClickOutside(@kne/use-click-outside),_message(antd/lib/message)

```jsx
const {default: useClickOutside} = _useClickOutside;
const {default: message} = _message;

const BaseExample = () => {
    const ref = useClickOutside(() => {
        message.success('点击了容器外部');
    });
    return <div ref={ref} className="container">
        这是组件内部，点击外部试试
    </div>;
};

render(<BaseExample/>);

```


### API

const ref = useClickOutside(callback, dom);

注意：返回的ref必须传给一个dom的ref

| 属性名      | 说明                  | 类型       | 默认值           |
|----------|---------------------|----------|---------------|
| callback | 模拟的失去焦点事件触发后执行的回调   | function | -             |
| dom      | 可以触发点击的外部区域，默认为body | dom      | document.body |

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