Skip to content

Commit 90f184b

Browse files
committed
refactor: create new type and rename some variables
- Create `Delay` type - Rename `setUpProps` to `updateProps` - Default values of props moved to `TypistCore` - Using object destructuring to get coreProps instead of `useMemo`
1 parent 17b6fca commit 90f184b

5 files changed

Lines changed: 35 additions & 71 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Set to `true` if you want to pause the typing animation.
121121

122122
**Default**: `() => {return}`
123123

124-
This function will be called when the typing animation finishes.
124+
This function will be called when the typing animation finishes. It will be called before `finishDelay`.
125125

126126
#### `splitter`
127127

src/TypistCore.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import React from 'react';
22

3-
import type { Splitter, TypedLines, TypistProps } from './types/TypistProps';
4-
import { emptyFunc } from './utils/defaultFuncs';
3+
import type { Splitter, TypedLines, TypistProps, Delay } from './types/TypistProps';
4+
import { defaultSplitter, emptyFunc } from './utils/defaultFuncs';
55
import getActions from './utils/getActions';
66

7-
type T = Omit<TypistProps, 'cursor' | 'disabled' | 'restartKey'>;
8-
type CoreProps = Required<T>;
7+
type CoreProps = Omit<TypistProps, 'cursor' | 'disabled' | 'restartKey'>;
98
type SetTypedLines = React.Dispatch<React.SetStateAction<TypedLines>>;
109

1110
export default class TypistCore {
1211
#children: React.ReactNode;
13-
#typingDelay!: number | (() => number);
14-
#backspaceDelay!: number | (() => number);
12+
#typingDelay!: Delay;
13+
#backspaceDelay!: Delay;
1514
#loop!: boolean;
1615
#pause!: boolean;
1716
#startDelay!: number;
@@ -24,7 +23,7 @@ export default class TypistCore {
2423
#setTypedLines: SetTypedLines;
2524

2625
constructor(props: CoreProps, setTypedLines: SetTypedLines) {
27-
this.setUpProps(props);
26+
this.updateProps(props);
2827
this.#setTypedLines = setTypedLines;
2928
}
3029

@@ -36,16 +35,16 @@ export default class TypistCore {
3635
};
3736
};
3837

39-
setUpProps = ({
38+
updateProps = ({
4039
children,
41-
typingDelay,
42-
backspaceDelay,
43-
loop,
44-
pause,
45-
startDelay,
46-
finishDelay,
47-
onTypingDone,
48-
splitter,
40+
typingDelay = 75,
41+
backspaceDelay = 75,
42+
loop = false,
43+
pause = false,
44+
startDelay = 0,
45+
finishDelay = 0,
46+
onTypingDone = emptyFunc,
47+
splitter = defaultSplitter,
4948
}: CoreProps) => {
5049
this.#children = children;
5150
this.#typingDelay = typingDelay;
@@ -82,9 +81,10 @@ export default class TypistCore {
8281
}
8382
};
8483

85-
#timeoutPromise = (delay: number) => {
84+
#timeoutPromise = (delay: Delay) => {
8685
return new Promise<void>((resolve, reject) => {
87-
const timeoutId = setTimeout(resolve, delay);
86+
const ms = typeof delay === 'number' ? delay : delay();
87+
const timeoutId = setTimeout(resolve, ms);
8888
this.#clearTimer = () => {
8989
clearTimeout(timeoutId);
9090
reject();
@@ -123,14 +123,13 @@ export default class TypistCore {
123123
};
124124

125125
/**
126-
* Each async token should be executed after the pause promise resolve.
126+
* Each token should be executed after the pause promise resolve.
127127
* @param callback
128-
* @param timeoutDelay
128+
* @param delay
129129
*/
130-
#executeAsyncToken = async (callback: () => void, timeoutDelay: number | (() => number)) => {
130+
#executeToken = async (callback: () => void, delay: Delay) => {
131131
await this.#pausePromise();
132132
callback();
133-
const delay = typeof timeoutDelay === 'number' ? timeoutDelay : timeoutDelay();
134133
await this.#timeoutPromise(delay);
135134
};
136135

@@ -147,7 +146,7 @@ export default class TypistCore {
147146
const splittedLine = this.#splitter(line);
148147
const lastIdx = this.#typedLines.length;
149148
for (let charIdx = 1; charIdx <= splittedLine.length; charIdx++) {
150-
await this.#executeAsyncToken(() => {
149+
await this.#executeToken(() => {
151150
const newLine = splittedLine.slice(0, charIdx).join('');
152151
const newTypedLines = [...this.#typedLines];
153152
newTypedLines[lastIdx] = newLine;
@@ -157,14 +156,14 @@ export default class TypistCore {
157156
};
158157

159158
#typeElement = async (el: React.ReactElement) => {
160-
await this.#executeAsyncToken(() => {
159+
await this.#executeToken(() => {
161160
this.#updateTypedLines([...this.#typedLines, el]);
162161
}, this.#typingDelay);
163162
};
164163

165164
#backspace = async (amount: number) => {
166165
while (amount--) {
167-
await this.#executeAsyncToken(() => {
166+
await this.#executeToken(() => {
168167
const typedLines = [...this.#typedLines];
169168
let lineIndex = typedLines.length - 1;
170169
let line = typedLines[lineIndex];

src/components/Typist.tsx

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,20 @@
1-
import React, { useEffect, useMemo, useRef, useState } from 'react';
1+
import React, { useEffect, useRef, useState } from 'react';
22

33
import type { TypedLines, TypistProps } from '../types/TypistProps';
44
import getTypedChildren from '../utils/getTypedChildren';
55
import getFinalTypedLines from '../utils/getFinalTypedLines';
66
import insertCursor from '../utils/insertCursor';
7-
import { defaultSplitter, emptyFunc } from '../utils/defaultFuncs';
7+
import { defaultSplitter } from '../utils/defaultFuncs';
88
import Backspace from './Backspace';
99
import Delay from './Delay';
1010
import Paste from './Paste';
1111

1212
import TypistCore from '../TypistCore';
1313

14-
const Typist = ({
15-
children,
16-
typingDelay = 75,
17-
backspaceDelay = 75,
18-
loop = false,
19-
pause = false,
20-
startDelay = 0,
21-
finishDelay = 0,
22-
onTypingDone = emptyFunc,
23-
splitter = defaultSplitter,
24-
cursor,
25-
disabled = false,
26-
restartKey,
27-
}: TypistProps) => {
14+
const Typist = ({ cursor, disabled = false, restartKey, ...coreProps }: TypistProps) => {
15+
const { children, splitter = defaultSplitter } = coreProps;
2816
const [typedLines, setTypedLines] = useState<TypedLines>([]);
2917
const typistCoreRef = useRef<TypistCore>();
30-
const coreProps = useMemo(
31-
() => ({
32-
children,
33-
typingDelay,
34-
backspaceDelay,
35-
loop,
36-
pause,
37-
startDelay,
38-
finishDelay,
39-
onTypingDone,
40-
splitter,
41-
}),
42-
[
43-
children,
44-
typingDelay,
45-
backspaceDelay,
46-
loop,
47-
pause,
48-
startDelay,
49-
finishDelay,
50-
onTypingDone,
51-
splitter,
52-
]
53-
);
5418

5519
useEffect(() => {
5620
// If disable is true, show the final result immediately.
@@ -77,7 +41,7 @@ const Typist = ({
7741

7842
// Update the typistCore's props whenever component's props change
7943
useEffect(() => {
80-
typistCoreRef.current?.setUpProps(coreProps);
44+
typistCoreRef.current?.updateProps(coreProps);
8145
}, [coreProps]);
8246

8347
const typedChildren = getTypedChildren(children, typedLines);

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { TypistProps, Splitter } from './types/TypistProps';
1+
import type { TypistProps, Splitter, Delay } from './types/TypistProps';
22
import Typist from './components/Typist';
33

44
export default Typist;
5-
export type { TypistProps, Splitter };
5+
export type { TypistProps, Splitter, Delay };

src/types/TypistProps.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
export type TypedChildren = (string | React.ReactElement)[] | null | undefined;
22
export type TypedLines = (string | React.ReactElement | null)[];
3+
export type Delay = number | (() => number);
34
export type Splitter = (str: string) => string[];
45

56
export type TypistProps = {
67
children: React.ReactNode;
7-
typingDelay?: number | (() => number);
8-
backspaceDelay?: number | (() => number);
8+
typingDelay?: Delay;
9+
backspaceDelay?: Delay;
910
loop?: boolean;
1011
pause?: boolean;
1112
startDelay?: number;

0 commit comments

Comments
 (0)