Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
react-onclickoutside
Advanced tools
The react-onclickoutside npm package is a React component wrapper that provides functionality to detect clicks outside of the component it wraps. It is commonly used to handle scenarios like closing dropdown menus, modals, or any floating elements when a user clicks outside of these components.
Detecting clicks outside a component
This feature allows you to wrap any React component with the react-onclickoutside higher-order component (HOC) to detect and handle clicks outside of it. In the code sample, `MyComponent` is wrapped with `onClickOutside`, enabling it to handle clicks that occur outside of its bounds.
import React, { Component } from 'react';
import onClickOutside from 'react-onclickoutside';
class MyComponent extends Component {
handleClickOutside = evt => {
// handle click outside logic here
};
render() {
return <div>My Component</div>;
}
}
export default onClickOutside(MyComponent);
Similar to react-onclickoutside, this package provides functionality to detect clicks outside of a component. It differs in implementation, as it uses a mixin approach for React classes or a decorator for React components, which might not be as straightforward as the HOC approach used by react-onclickoutside.
This package offers a similar functionality to react-onclickoutside, with the added benefit of being able to handle clicks outside of multiple elements. It wraps components in a div to detect outside clicks, which can be more flexible but might introduce additional markup into the DOM.
This is a React mixin that you can add to your React components if you want to have them listen for clicks that occur somewhere in the document, outside of the element itself (for instance, if you need to hide a menu when people click anywhere else on your page).
Note that this mixin relies on the .classList
property, which is supported by all modern browsers, but not by no longer supported browsers like IE9 or even older. For setups that need to support deprecated browsers, using a classlist-polyfill will be necessary.
There are two ways to install this mixin, depending on your development process.
If you have Node.js needs, you can install this mixin via npm
, using:
npm install react-onclickoutside --save
(or --save-dev
depending on your needs). You then use it in your components as:
var Component = React.createClass({
mixins: [
require('react-onclickoutside')
],
handleClickOutside: function(evt) {
// ...handling code goes here...
}
});
If you have plain-old-browser needs and for some reason are unable to use the modern browserify/webpack approach to building your JS payloads, you can install this mixin via bower
, using:
bower install react-onclickoutside
and then include it as script via:
<script src="bower_components/react-onclickoutside/index.js"></script>
Then use it as:
var Component = React.createClass({
mixins: [
OnClickOutside
],
handleClickOutside: function(evt) {
// ...handling code goes here...
}
});
When using this mixin, a component has two functions that can be used to explicitly listen for, or do nothing with, outside clicks
enableOnClickOutside()
- Enables outside click listening by setting up the event listening bindings.disableOnClickOutside()
- Disables outside click listening by explicitly removing the event listening bindings.In addition, you can create a component that uses this mixin such that it has the code set up and ready to go, but not listening for outside click events until you explicitly issue its enableOnClickOutside()
, by passing in a properly called disableOnClickOutside
:
var Component = React.createClass({
mixins: [ ... ],
handleClickOutside: function(evt) {
// ...
}
});
var Container = React.createClass({
render: function(evt) {
return <Component disableOnClickOutside={true} />
}
});
If you want the mixin to ignore certain elements, then add the class ignore-react-onclickoutside
to that element and the callback won't be invoked when the click happens inside elements with that class. This class can be changed by setting the outsideClickIgnoreClass
property on the component.
Since mixins can't be used with ES6/2015 class React components a Higher-Order Component (HOC) and ES7 decorator are bundled with the mixin:
import listensToClickOutside from 'react-onclickoutside/decorator';
class Component extends React.Component {
handleClickOutside = (event) => {
// ...
}
}
export default listensToClickOutside(Component);
// OR
import listensToClickOutside from 'react-onclickoutside/decorator';
@listensToClickOutside()
class Component extends React.Component {
handleClickOutside = (event) => {
// ...
}
}
export default Component;
One difference when using the HOC/decorator compared to the mixin is that the enableOnClickOutside()
and disableOnClickOutside()
methods are not available as class methods, but rather on the props
;
so instead of this.enableOnClickOutside()
you would call this.props.enableOnClickOutside()
.
In every other respect the the mixin and HOC/decorator provides the same functionality.
For bugs and enhancements hit up https://github.com/Pomax/react-onclickoutside/issues
If you still use React 0.13 or 0.12, any version up to and including 2.4 will work. Any version v4.* or above will not work due to relying on modules not introduced until React 0.14.
If you use React 0.14 or above, use v2.5 or higher, as that specifically uses react-DOM
for the necessary DOM event bindings.
This is true, but also an edge-case problem that needs to be fixed in IE, not in individual libraries so that IE can keep getting away with not implementing proper support for all HTML5 elements. If you rely on this, I fully expect you to have already filed this as a feature request, to be added to MS Edge, or to have voted on getting it implemented.
If you haven't, and you just want this library fixed, then you already have the power to completely fix this problem yourself without needing to file any PRs: simply add a shim for classList
to your page(s), loaded before you load your React code, and you'll have instantly fixed every library that you might remotely rely on that makes use of the classList
property. You can find several shims quite easily, the usualy "first try" shim is the one given over on https://developer.mozilla.org/en/docs/Web/API/Element/classList
A note on PRs for this issue: I will not accept PRs to fix this issue. You already have the power to fix it, and I expect you to take responsibility as a fellow developer to let Microsoft know you need them to implement this.
FAQs
An onClickOutside wrapper for React components
The npm package react-onclickoutside receives a total of 418,607 weekly downloads. As such, react-onclickoutside popularity was classified as popular.
We found that react-onclickoutside demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.