-
-
Save subbe/bb31cc68ede7e95eb0bec33a98f5417d to your computer and use it in GitHub Desktop.
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
Alpine.directive('typed', (el, { expression, modifiers }, { evaluateLater, effect, cleanup }) => { | |
const getStrings = evaluateLater(expression); | |
const modifierValue = (key, fallback) => { | |
if (-1 === modifiers.indexOf(key)) { | |
return fallback; | |
} | |
const value = modifiers[modifiers.indexOf(key) + 1]; | |
if (value && ! isNaN(fallback)) { | |
return parseInt(value); | |
} | |
return value ? value : fallback; | |
} | |
effect(() => getStrings(strings => { | |
const instance = new Typed(el, { | |
strings, | |
startDelay: modifierValue('delay', 750), | |
typeSpeed: modifierValue('speed',150), | |
backSpeed: modifierValue('backspace', 100), | |
showCursor: ! modifiers.includes('cursorless'), | |
loop: modifiers.includes('loop'), | |
cursorChar: '_', | |
}); | |
cleanup(() => instance.destroy()); | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment