Files
2026-08-30 16:15:14 +02:00

37 lines
604 B
JavaScript

/**
* Module Dependencies
*/
try {
var matches = require('matches-selector')
} catch (err) {
var matches = require('component-matches-selector')
}
/**
* Export `closest`
*/
module.exports = closest
/**
* Closest
*
* @param {Element} el
* @param {String} selector
* @param {Element} scope (optional)
*/
function closest (el, selector, scope) {
scope = scope || document.documentElement;
// walk up the dom
while (el && el !== scope) {
if (matches(el, selector)) return el;
el = el.parentNode;
}
// check scope for match
return matches(el, selector) ? el : null;
}