Last active
February 23, 2017 01:38
-
-
Save treshugart/fbc37ab7d19028b9fe22311bd1b634f4 to your computer and use it in GitHub Desktop.
Setting context props at specific DOM tree levels on elements.
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
const _context = Symbol(); | |
let currentContext = null; | |
function getContext (elem) { | |
elem.dispatchEvent(new Event('__context', { | |
bubbles: true, | |
cancelable: true, | |
composed: true, | |
scoped: true | |
})); | |
const returnContext = currentContext; | |
currentContext = null; | |
return returnContext; | |
} | |
function setContext (elem, context) { | |
if (!elem.hasOwnProperty(_context)) { | |
elem.addEventListener('__context', function (e) { | |
e.stopPropagation(); | |
currentContext = this[_context]; | |
}); | |
} | |
elem[_context] = context; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment