All Projects → kentor → React Click Outside

kentor / React Click Outside

Licence: mit
Higher Order Component that provides click outside functionality

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Click Outside

React Laag
Hooks to build things like tooltips, dropdown menu's and popovers in React
Stars: ✭ 568 (+113.53%)
Mutual labels:  component, dropdown
Ngx Treeview
An Angular treeview component with checkbox
Stars: ✭ 312 (+17.29%)
Mutual labels:  component, dropdown
React Select Search
⚡️ Lightweight select component for React
Stars: ✭ 379 (+42.48%)
Mutual labels:  component, dropdown
Vue Multiselect
Universal select/multiselect/tagging component for Vue.js
Stars: ✭ 5,988 (+2151.13%)
Mutual labels:  component, dropdown
Ngx Select Dropdown
Custom Dropdown for Angular 4+ with multiple and single selection options
Stars: ✭ 91 (-65.79%)
Mutual labels:  component, dropdown
Autocomplete
Blazing fast and lightweight autocomplete widget without dependencies. Only 1KB gzipped. Demo:
Stars: ✭ 244 (-8.27%)
Mutual labels:  component, dropdown
Vue Treeselect
A multi-select component with nested options support for Vue.js
Stars: ✭ 2,347 (+782.33%)
Mutual labels:  component, dropdown
React Nprogress
⌛️ A React primitive for building slim progress bars.
Stars: ✭ 173 (-34.96%)
Mutual labels:  hoc, component
react-stripe-dropdown
React Stripe Inspired Dropdown
Stars: ✭ 22 (-91.73%)
Mutual labels:  dropdown
autoops for win
Window自动化运维,模拟鼠标移动、单击、模拟输入等,可用来实现复杂GUI应用的操作
Stars: ✭ 36 (-86.47%)
Mutual labels:  click
react-native-double-click
A Component Wrapper for Double Click/Tap
Stars: ✭ 42 (-84.21%)
Mutual labels:  click
click-help-colors
Colorization of help messages in Click 🎨
Stars: ✭ 67 (-74.81%)
Mutual labels:  click
Antue
🌟 A set of enterprise-class Vue UI components, following the Ant Design specification.
Stars: ✭ 254 (-4.51%)
Mutual labels:  component
Utlyz-CLI
Let's you to access your FB account from the command line and returns various things number of unread notifications, messages or friend requests you have.
Stars: ✭ 30 (-88.72%)
Mutual labels:  click
Material Ui Superselectfield
multiselection autocomplete dropdown component for Material-UI
Stars: ✭ 260 (-2.26%)
Mutual labels:  dropdown
focus-outside
📦 一个很棒的 clickOutside 库,它解决了 iframe 无法触发 clickOutside 的问题,并且它支持分组绑定处理。A good clickOutside library, which solves the problem that iframe cannot trigger clickOutside, and it supports grouping binding processing.
Stars: ✭ 74 (-72.18%)
Mutual labels:  click
JHTapTextView
Tap TextView,Text Tap,文本点击
Stars: ✭ 23 (-91.35%)
Mutual labels:  click
Switzerland
🇨🇭Switzerland takes a functional approach to Web Components by applying middleware to your components. Supports Redux, attribute mutations, CSS variables, React-esque setState/state, etc… out-of-the-box, along with Shadow DOM for style encapsulation and Custom Elements for interoperability.
Stars: ✭ 261 (-1.88%)
Mutual labels:  component
Unmaskforugui
Reverse mask for uGUI element in Unity.
Stars: ✭ 259 (-2.63%)
Mutual labels:  component
create-flask-app
Package for Initializing a Flask Project Structure
Stars: ✭ 16 (-93.98%)
Mutual labels:  click

React Click Outside

Build Status npm

Enhance a React component with a Higher Order Component that provides click outside detection.

Note: React 0.14 required for version >= 2.x. This assumes react and react-dom is installed in your project. Continue using version 1.x for React 0.13 support.

Note: Use version >= 2.3.0 to get rid of React.createClass warnings in React 15.5.

Usage

Installation:

npm install react-click-outside

Some component that you wish to enhance with click outside detection:

const createReactClass = require('create-react-class');
const enhanceWithClickOutside = require('react-click-outside');
const React = require('react');

const Dropdown = createReactClass({
  getInitialState() {
    return {
      isOpened: false,
    };
  },

  handleClickOutside() {
    this.toggle();
  },

  toggle() {
    this.setState({ isOpened: !this.state.isOpened });
  },

  render() {
    ...
  },
});

module.exports = enhanceWithClickOutside(Dropdown);

Note: There will be no error thrown if handleClickOutside is not implemented.

wrappedRef prop

Use the wrappedRef prop to get access to the wrapped component instance. For example:

// Inside a component's render method
<Dropdown
  wrappedRef={instance => { this.toggle = instance.toggle; }}
/>

// Now you can call toggle externally
this.toggle();

Details

The enhanceWithClickOutside function wraps the provided component in another component that registers a click handler on document for the event capturing phase. Using the event capturing phase prevents elements with a click handler that calls stopPropagation from cancelling the click event that would eventually trigger the component's handleClickOutside function.

Why not a mixin?

There are some mixins that provide click outside detection functionality, but they prevent the component from implementing the componentDidMount and componentWillUnmount life cycle hooks. I recommend not using a mixin for this case.

Limitations

  • IE9+ due to the usage of the event capturing phase.

Not working on iOS?

If the handleClickOutside handler is not firing on iOS, try adding the cursor: pointer css style to the <body> element. There are many ways to achieve this, here is just one example:

if ('ontouchstart' in document.documentElement) {
  document.body.style.cursor = 'pointer';
}

If your app already has a way for mobile detection (e.g. Modernizr), you may want to use that instead.

See issue #4 for a discussion.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].