Skip to content

Commit c579cf4

Browse files
authored
Merge pull request #107 from jason89521/feat/implement-hide-cursor-when-done
Feat/implement hide cursor when done
2 parents 958cc90 + ef36397 commit c579cf4

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export type TypistProps = {
5454
cursor?: string | React.ReactElement;
5555
disabled?: boolean;
5656
restartKey?: any;
57+
hideCursorWhenDone?: boolean;
5758
};
5859
```
5960

@@ -156,6 +157,10 @@ If this value is `true`, the result will be displayed immediately without typing
156157

157158
`Typist` will restart the typing animation whenever `restartKey` changes.
158159

160+
#### `hideCursorWhenDone`
161+
162+
Hide the cursor when the typing animation is done.
163+
159164
### `Typist.Backspace`
160165

161166
```ts

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-typist-component",
3-
"version": "1.0.6",
3+
"version": "1.1.0",
44
"description": "A react component lets you create typewriter effect.",
55
"author": "Yu-Xuan Zheng",
66
"keywords": [

src/components/Main.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const Main = ({
2020
finishDelay = 0,
2121
loop = false,
2222
pause = false,
23+
hideCursorWhenDone = false,
2324
}: TypistProps) => {
2425
const [typedChildrenArray, setTypedChildrenArray] = useState<TypedChildren[]>(
2526
[],
@@ -28,6 +29,8 @@ const Main = ({
2829
const clearTimerRef = useRef(emptyFunc);
2930
const loopRef = useRef(loop);
3031
const pauseRef = useRef(pause);
32+
const hideCursorWhenDoneRef = useRef(hideCursorWhenDone);
33+
const [showCursor, setShowCursor] = useState(true);
3134

3235
const timeoutPromise = useCallback((delay: Delay) => {
3336
return new Promise((resolve, reject) => {
@@ -73,7 +76,8 @@ const Main = ({
7376
useEffect(() => {
7477
loopRef.current = loop;
7578
pauseRef.current = pause;
76-
}, [loop, pause]);
79+
hideCursorWhenDoneRef.current = hideCursorWhenDone;
80+
}, [loop, pause, hideCursorWhenDone]);
7781

7882
useEffect(() => {
7983
const typedChildrenArray = getTypedChildrenArray(children, splitter);
@@ -86,6 +90,7 @@ const Main = ({
8690
(async () => {
8791
try {
8892
do {
93+
setShowCursor(true);
8994
setCurrentIndex(-1);
9095
const actions = getActions(children, splitter);
9196
if (startDelay > 0) await timeoutPromise(startDelay);
@@ -107,6 +112,7 @@ const Main = ({
107112
}
108113
}
109114
onTypingDone?.();
115+
setShowCursor(false);
110116
if (finishDelay > 0) await timeoutPromise(finishDelay);
111117
if (!loopRef.current) await loopPromise();
112118
} while (loopRef.current);
@@ -123,7 +129,13 @@ const Main = ({
123129
}, [restartKey, disabled]);
124130

125131
const typedChildren = typedChildrenArray[currentIndex];
126-
return <>{cursor ? insertCursor(typedChildren, cursor) : typedChildren}</>;
132+
return (
133+
<>
134+
{cursor && showCursor
135+
? insertCursor(typedChildren, cursor)
136+
: typedChildren}
137+
</>
138+
);
127139
};
128140

129141
export default Main;

src/types/TypistProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export type TypistProps = {
1616
cursor?: string | React.ReactElement;
1717
disabled?: boolean;
1818
restartKey?: any;
19+
hideCursorWhenDone?: boolean;
1920
};

0 commit comments

Comments
 (0)