-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add typing animation component
- Loading branch information
1 parent
a3d13a8
commit 4a8f405
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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
51 changes: 51 additions & 0 deletions
51
docs/src/components/spark-ui/typingAnimation/typing-animation.vue
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<script setup lang='ts'> | ||
import { ref, watch } from 'vue' | ||
import { cn } from '../../../lib/utils' | ||
interface TypingAnimationProps { | ||
text: string | ||
duration?: number | ||
class?: string | ||
} | ||
const props = withDefaults(defineProps<TypingAnimationProps>(), { | ||
duration: 300, | ||
}) | ||
const displayedText = ref('') | ||
const i = ref(0) | ||
function handleTypingEffect(d: number, n: number) { | ||
const typingEffect = setInterval(() => { | ||
if (n < props.text.length) { | ||
displayedText.value = props.text.substring(0, n + 1) | ||
n = n + 1 | ||
} | ||
else { | ||
clearInterval(typingEffect) | ||
} | ||
}, d) | ||
return () => { | ||
clearInterval(typingEffect) | ||
} | ||
} | ||
watch(() => [props.duration, i.value], ([d, n]) => { | ||
handleTypingEffect(d, n) | ||
}, { | ||
deep: true, | ||
immediate: true, | ||
}) | ||
const className = cn( | ||
'font-display text-center text-4xl font-bold leading-[5rem] tracking-[-0.02em] drop-shadow-sm', | ||
props.class, | ||
) | ||
</script> | ||
|
||
<template> | ||
<h1 :class="className"> | ||
{{ displayedText ? displayedText : props.text }} | ||
</h1> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script setup lang='ts'> | ||
import TypingAnimation from './typing-animation.vue' | ||
</script> | ||
|
||
<template> | ||
<TypingAnimation class="text-4xl font-bold text-black" text="Typing Animation" /> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<script setup lang='ts'> | ||
import { ref, watch } from 'vue' | ||
import { cn } from '../../lib/utils' | ||
interface TypingAnimationProps { | ||
text: string | ||
duration?: number | ||
class?: string | ||
} | ||
const props = withDefaults(defineProps<TypingAnimationProps>(), { | ||
duration: 300, | ||
}) | ||
const displayedText = ref('') | ||
const i = ref(0) | ||
function handleTypingEffect(d: number, n: number) { | ||
const typingEffect = setInterval(() => { | ||
if (n < props.text.length) { | ||
displayedText.value = props.text.substring(0, n + 1) | ||
n = n + 1 | ||
} | ||
else { | ||
clearInterval(typingEffect) | ||
} | ||
}, d) | ||
return () => { | ||
clearInterval(typingEffect) | ||
} | ||
} | ||
watch(() => [props.duration, i.value], ([d, n]) => { | ||
handleTypingEffect(d, n) | ||
}, { | ||
deep: true, | ||
immediate: true, | ||
}) | ||
const className = cn( | ||
'font-display text-center text-4xl font-bold leading-[5rem] tracking-[-0.02em] drop-shadow-sm', | ||
props.class, | ||
) | ||
</script> | ||
|
||
<template> | ||
<h1 :class="className"> | ||
{{ displayedText ? displayedText : props.text }} | ||
</h1> | ||
</template> |