Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
feat: added ClickOutside action
Browse files Browse the repository at this point in the history
  • Loading branch information
TheComputerM committed Aug 24, 2020
1 parent 170481c commit 76c98ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/svelte-materialify/src/actions/ClickOutside/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Click Outside
* @param {Node} node
*/
export default (node, options = {}) => {
const settings = { exclude: [], ...options };

function checkIfNotClickedOnNode(target) {
for (let el = target; el && el !== document; el = el.parentNode) {
if (node.isSameNode(el) || settings.exclude.some((x) => x.isSameNode(el))) return false;
}
return true;
}

function detect({ target }) {
const check = checkIfNotClickedOnNode(target);
if (check) {
node.dispatchEvent(new CustomEvent('clickOutside'));
}
}
document.addEventListener('click', detect, { passive: true });
return {
destroy() {
document.removeEventListener('click', detect);
},
};
};
2 changes: 2 additions & 0 deletions packages/svelte-materialify/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as ClickOutside } from './actions/ClickOutside';
export { default as Ripple } from './actions/Ripple';
export { default as Intersect } from './actions/Intersect';
export { default as Jump } from './actions/Jump';
Expand Down Expand Up @@ -27,6 +28,7 @@ export {
} from './components/Card';
export { default as List, ListGroup, ListItem } from './components/List';
export { default as VirtualList } from './components/VirtualList';
export { default as Menu } from './components/Menu';
export { default as Overlay } from './components/Overlay';
export { default as NavigationDrawer } from './components/NavigationDrawer';
export { default as Subheader } from './components/Subheader';
Expand Down

0 comments on commit 76c98ab

Please sign in to comment.