Created
July 14, 2022 12:05
-
-
Save seriousManual/a28e24bc7ccdd228a723cf2586de465f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function clickOutside(node, handler) { | |
function handleClick(event) { | |
if (node && !node.contains(event.target) && !event.defaultPrevented) { | |
handler() | |
} | |
} | |
document.addEventListener('click', handleClick, true) | |
return { | |
destroy() { | |
document.removeEventListener('click', handleClick, true) | |
} | |
} | |
} | |
export default clickOutside |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="ts"> | |
import onClickOutside from '../clickOutside' | |
</script> | |
<div use:onClickOutside={() => console.log('click')}> | |
modal | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment