Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/implement hide cursor when done #107

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
Loading