From a2485392a569ef30cabaca3c4ad76d40fe42056c Mon Sep 17 00:00:00 2001 From: Konstantin Vyatkin Date: Mon, 28 Oct 2019 16:26:19 -0400 Subject: [PATCH] don't reimplement native `closest` (#696) --- README.md | 1 + src/scripts/lib/utils.js | 18 +++++------------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index dc7f5a2..2abb440 100644 --- a/README.md +++ b/README.md @@ -1053,6 +1053,7 @@ Choices is compiled using [Babel](https://babeljs.io/) to enable support for [ES - Array.prototype.indexOf - Object.assign - Element.prototype.classList +- Element.prototype.closest - window.requestAnimationFrame - CustomEvent diff --git a/src/scripts/lib/utils.js b/src/scripts/lib/utils.js index cf3207b..24dc844 100644 --- a/src/scripts/lib/utils.js +++ b/src/scripts/lib/utils.js @@ -39,19 +39,11 @@ export const wrap = (element, wrapper = document.createElement('div')) => { return wrapper.appendChild(element); }; -export const findAncestorByAttrName = (el, attr) => { - let target = el; - - while (target) { - if (target.hasAttribute(attr)) { - return target; - } - - target = target.parentElement; - } - - return null; -}; +/** + * @param {HTMLElement} el + * @param {string} attr + */ +export const findAncestorByAttrName = (el, attr) => el.closest(`[${attr}]`); export const getAdjacentEl = (startEl, className, direction = 1) => { if (!startEl || !className) {