Closest Selector Mixin

This mixin lets CSS authors apply styles to the nearest element matching a CSS selector to another element matching a given CSS selector. You can use this to find the nearest matching ancestor.

Syntax

closest(selector, ancestor, rule)

Example

Input:

closest('#start', '.target', `border-color: lime`)

Output:

/* #start:closest(.target) */
[data-closest-unique="0"] {
  border-color: lime
}

Code


function closest(selector, ancestor, rule) {

  var tag = document.querySelectorAll(selector)
  var style = ''
  var count = 0

  for (var i = 0; i < tag.length; i++) {

    var attr = (selector+ancestor).replace(/\W+/g, '')

    tag[i].closest(ancestor).setAttribute('data-closest-' + attr, count)

    style += '\n/* ' + selector + ':closest(' + ancestor + ') */\n'
             + '[data-closest-' + attr + '="' + count + '"] {\n'
             + '  ' + rule + '\n'
             + '}\n'

    count++

  }

  return style

}

Techniques Capable