Files
threshold-ui/addons/rose/addon/modifiers/on-click-outside.js
T
Zhihe Li bcf004b8a4 Reformat codebase with trailing commas in functions (#2006)
* style: 💄 Update trailing commas in API

* style: 💄 Update trailing commas in core

* style: 💄 Update trailing commas in rose

* style: 💄 Update trailing commas in admin

* style: 💄 Update trailing commas in desktop
2023-11-22 14:07:41 -05:00

29 lines
664 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { modifier } from 'ember-modifier';
/**
* Used to detect clicks occurring _outside_ of the modified element.
* This is useful especially for dropdowns to toggle content visibility
* when the user clicks away.
*/
export default modifier(
(element, [callback]) => {
function handleClick(event) {
if (!element.contains(event.target)) {
callback(element, event);
}
}
document.addEventListener('click', handleClick, false);
return () => {
document.removeEventListener('click', handleClick, false);
};
},
{ eager: false },
);