Skip to content

Commit

Permalink
feat(hooks): move typing logic to useTypingEffect hook
Browse files Browse the repository at this point in the history
  • Loading branch information
sabertazimi committed Aug 9, 2021
1 parent 530616e commit 74838a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export { default as useBuildTime } from './useBuildTime';
export { default as useDisqus } from './useDisqus';
export { default as usePostsMetadata } from './usePostsMetadata';
export { default as useSiteMetadata } from './useSiteMetadata';
export { default as useTypingEffect } from './useTypingEffect';
export { default as useVisibility } from './useVisibility';
40 changes: 40 additions & 0 deletions src/hooks/useTypingEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { RefObject, useEffect, useRef } from 'react';
import Typed from 'typed.js';

interface TypingOptions {
titles: string[];
speed: number;
delay: number;
loop: boolean;
}

const useTypingEffect = (
ref: RefObject<HTMLElement>,
options: TypingOptions
): void => {
const { titles, speed, delay, loop } = options;
const typed = useRef<Typed | null>(null);

useEffect(() => {
const options = {
strings: [...titles],
smartBackspace: true,
typeSpeed: speed,
backSpeed: speed,
backDelay: delay,
loop,
};

if (ref.current) {
typed.current = new Typed(ref.current, options);
}

return () => {
if (typed.current) {
typed.current.destroy();
}
};
}, [ref, titles, speed, delay, loop]);
};

export default useTypingEffect;

0 comments on commit 74838a9

Please sign in to comment.