v3.1.0 - Sparkles!
This release introduces a elementBehaviors.whenDefined(name)
method on the BehaviorRegistry
class, similar to the customElements.whenDefined(name)
method on the CustomElementRegistry
class. The method can be used to wait for a behavior class to be defined before performing logic.
Previous code ordering issues would lead to behaviors not being instantiated if the has=""
attribute was defined before the behaviors were defined. Now the has=""
attribute uses the new elementBehaviors.whenDefined()
method internally to fix this problem by waiting for definitions to exist instead of returning if a definition doesn't exist.
Example:
<script src="https://unpkg.com/element-behaviors@3.1.0/dist/global.js"></script>
<h1 has="sparkles">Sparkles!</h1>
<script>
elementBehaviors.whenDefined('sparkles').then(() => {
console.log('Sparkles are ready!')
})
setTimeout(() => {
// if the sparkles behavior class gets defined later, then it will be
// instantiated as expected, and the above console.log will run:
elementBehaviors.define('sparkles', class {
// ... add sparkles to host element ...
})
})
</script>
Full Sparkles demo on CodePen: https://codepen.io/trusktr/pen/MWzzNdV?editors=1000