Last active
June 24, 2019 04:17
-
-
Save BarthesSimpson/1eb620aab68f95223802e6d945f446aa to your computer and use it in GitHub Desktop.
Hook for simulating some typing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from "react" | |
const TIMER_WINDOW = 20000 | |
const TYPING_DELAY = 20 | |
const MAX_LENGTH = 20 | |
export const useTyping = (setState) => | |
useEffect(() => { | |
const inputs = document.getElementsByClassName("input-letter") | |
Array.from(inputs).forEach((el, idx) => { | |
const delay = (1 + idx) * TYPING_DELAY | |
const isTyping = setInterval(() => { | |
const l = el.id | |
setState(_state => ({ | |
..._state, | |
[l]: | |
_state[l].length < MAX_LENGTH | |
? _state[l] + _state[l].slice(0, 1) | |
: _state[l].slice(0, 1) | |
})) | |
}, delay) | |
setTimeout(() => { | |
clearInterval(isTyping) | |
}, TIMER_WINDOW) | |
}) | |
}, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment