Last active
May 15, 2023 09:13
-
-
Save manabuyasuda/8e601f3291e11692ace19175839f3751 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * 指定された要素がイベントのターゲット要素に含まれているかどうかをチェックします。 | |
| * @param {Event} event チェックしたい`click`などのイベント | |
| * @param {Array<string>} elements `DOMString`で指定した要素名の配列 | |
| * @returns {boolean} ターゲット要素が指定された要素を含む場合はtrue、そうでない場合はfalse | |
| * @example | |
| * import { containsTargetElements } from '@utility/containsTargetElements' | |
| * document.addEventListener('click', (event) => { | |
| * const elements = ['.foo', '#bar', '[data-baz]'] | |
| * const hasElements = containsTargetElements(event, elements) // => `true` or `false` | |
| * }) | |
| */ | |
| const containsTargetElements = (event, elements) => { | |
| const target = event.target | |
| const clickedElement = element => !!target.closest(element) | |
| const containsElements = elements.some(clickedElement) | |
| return containsElements | |
| } | |
| export { containsTargetElements } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment