Skip to content

Commit

Permalink
Merge pull request #13 from JamesManningR/feature/notYetTyped
Browse files Browse the repository at this point in the history
Added remaining text computed property
  • Loading branch information
JamesManningR committed Sep 28, 2022
2 parents 86930e4 + 85a883a commit 808df74
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ interface UseTypewriterReturns {
* The current string being typed out.
*/
text: Computed<string>;
/**
* The remaining text not yet typed out.
*/
remainingText: Computed<string>;
/**
* The array of strings to be typed out.
* @note This is reactive and can be changed at anytime, however,
Expand Down
3 changes: 3 additions & 0 deletions demo/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const exampleReplacementStrings = [
const {
text,
remainingText,
currentAction,
currentString,
stringIndex,
Expand Down Expand Up @@ -136,6 +137,8 @@ const {
<details>
<summary>Debug</summary>
<AppDebug
:text="text"
:remaining-text="remainingText"
:current-string="currentString"
:current-action="currentAction"
:typed-length="typedLength"
Expand Down
34 changes: 28 additions & 6 deletions demo/src/components/app/AppDebug.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { TypewriterStates } from '../../../../src/useTypewriter'
type Props = {
text: string;
remainingText: string;
currentAction: TypewriterStates;
currentString: string;
strings: string[];
Expand Down Expand Up @@ -33,6 +35,8 @@ defineProps<Props>()
dynamically.
</blockquote>

<p>Computed properties are marked with a *</p>

<div class="vars">
<section>
<h3>Strings in the list</h3>
Expand All @@ -48,7 +52,13 @@ defineProps<Props>()
currentAction === TypewriterStates.Deleting,
}"
>
{{ string }}
<template v-if="index === stringIndex">
<span class="typed">{{ text }}</span>
<span class="not-typed">{{ currentString.slice(typedLength) }}</span>
</template>
<span v-else>
{{ string }}
</span>
</li>
</ul>
</section>
Expand All @@ -57,7 +67,15 @@ defineProps<Props>()
<h3>State</h3>
<table>
<tr>
<td>Current string(*)</td>
<td>Text *</td>
<td>{{ text }}</td>
</tr>
<tr>
<td>Remaining Text *</td>
<td>{{ remainingText }}</td>
</tr>
<tr>
<td>Current string *</td>
<td>{{ currentString }}</td>
</tr>
<tr>
Expand All @@ -81,11 +99,11 @@ defineProps<Props>()
<td>{{ isPaused }}</td>
</tr>
<tr>
<td>Is at last letter</td>
<td>Is at last letter *</td>
<td>{{ isAtLastLetter }}</td>
</tr>
<tr>
<td>Is last iteration</td>
<td>Is last iteration *</td>
<td>{{ isLastIteration }}</td>
</tr>
<tr>
Expand Down Expand Up @@ -143,10 +161,14 @@ defineProps<Props>()
}
.active {
color: green;
color: #5f5;
}
.not-typed {
opacity: .3;
}
.deleting {
color: red;
color: #f45;
}
</style>
8 changes: 7 additions & 1 deletion src/useTypewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function useTypewriter (
)

reset()
return
return ''
}
if (string.length < typedLength.value) {
// eslint-disable-next-line no-console
Expand All @@ -156,6 +156,11 @@ export function useTypewriter (
return string.slice(0, typedLength.value)
})

/**
* The text that has not yet been typed
*/
const remainingText = computed(() => currentString.value.slice(typedLength.value))

/**
* Types the next letter, or sets up the waiting state if at the end of the word.
*/
Expand Down Expand Up @@ -432,6 +437,7 @@ export function useTypewriter (
// expose managed state as return value
return {
text,
remainingText,
strings,
currentString,
currentAction,
Expand Down

0 comments on commit 808df74

Please sign in to comment.