Created
May 7, 2024 20:18
-
-
Save inxilpro/b0ac74eafecfef4452f687569e39740a 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