Skip to content

Commit

Permalink
Merge pull request #107 from jason89521/feat/implement-hide-cursor-wh…
Browse files Browse the repository at this point in the history
…en-done

Feat/implement hide cursor when done
  • Loading branch information
jason89521 authored Jun 21, 2024
2 parents 958cc90 + ef36397 commit c579cf4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type TypistProps = {
cursor?: string | React.ReactElement;
disabled?: boolean;
restartKey?: any;
hideCursorWhenDone?: boolean;
};
```

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

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

#### `hideCursorWhenDone`

Hide the cursor when the typing animation is done.

### `Typist.Backspace`

```ts
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-typist-component",
"version": "1.0.6",
"version": "1.1.0",
"description": "A react component lets you create typewriter effect.",
"author": "Yu-Xuan Zheng",
"keywords": [
Expand Down
16 changes: 14 additions & 2 deletions src/components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Main = ({
finishDelay = 0,
loop = false,
pause = false,
hideCursorWhenDone = false,
}: TypistProps) => {
const [typedChildrenArray, setTypedChildrenArray] = useState<TypedChildren[]>(
[],
Expand All @@ -28,6 +29,8 @@ const Main = ({
const clearTimerRef = useRef(emptyFunc);
const loopRef = useRef(loop);
const pauseRef = useRef(pause);
const hideCursorWhenDoneRef = useRef(hideCursorWhenDone);
const [showCursor, setShowCursor] = useState(true);

const timeoutPromise = useCallback((delay: Delay) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -73,7 +76,8 @@ const Main = ({
useEffect(() => {
loopRef.current = loop;
pauseRef.current = pause;
}, [loop, pause]);
hideCursorWhenDoneRef.current = hideCursorWhenDone;
}, [loop, pause, hideCursorWhenDone]);

useEffect(() => {
const typedChildrenArray = getTypedChildrenArray(children, splitter);
Expand All @@ -86,6 +90,7 @@ const Main = ({
(async () => {
try {
do {
setShowCursor(true);
setCurrentIndex(-1);
const actions = getActions(children, splitter);
if (startDelay > 0) await timeoutPromise(startDelay);
Expand All @@ -107,6 +112,7 @@ const Main = ({
}
}
onTypingDone?.();
setShowCursor(false);
if (finishDelay > 0) await timeoutPromise(finishDelay);
if (!loopRef.current) await loopPromise();
} while (loopRef.current);
Expand All @@ -123,7 +129,13 @@ const Main = ({
}, [restartKey, disabled]);

const typedChildren = typedChildrenArray[currentIndex];
return <>{cursor ? insertCursor(typedChildren, cursor) : typedChildren}</>;
return (
<>
{cursor && showCursor
? insertCursor(typedChildren, cursor)
: typedChildren}
</>
);
};

export default Main;
1 change: 1 addition & 0 deletions src/types/TypistProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export type TypistProps = {
cursor?: string | React.ReactElement;
disabled?: boolean;
restartKey?: any;
hideCursorWhenDone?: boolean;
};

0 comments on commit c579cf4

Please sign in to comment.