Code Monkey home page Code Monkey logo

use-click-outside's Introduction

use-click-outside

描述

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

安装

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

概述

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

示例

示例样式

.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)
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

use-click-outside's People

Contributors

zhipenglin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.