-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathHangmanWord.tsx
More file actions
56 lines (51 loc) · 1.08 KB
/
HangmanWord.tsx
File metadata and controls
56 lines (51 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React from 'react';
type HangmanWordProps = {
guessedLetters: string[];
wordToGuess: string;
reveal?: boolean;
};
const renderLetter = (
letter: string,
index: number,
guessedLetters: string[],
reveal: boolean
) => {
const isGuessed = guessedLetters.includes(letter);
const isHidden = !isGuessed && reveal;
const textColor = isHidden ? 'red' : 'black';
return (
<span
key={index}
style={{
borderBottom: '.1em solid black',
visibility: isHidden ? 'visible' : 'hidden',
color: textColor,
}}
>
{letter}
</span>
);
};
const HangmanWord: React.FC<HangmanWordProps> = ({
guessedLetters,
wordToGuess,
reveal = false,
}) => {
return (
<div
style={{
display: 'flex',
gap: '.25em',
fontSize: '6rem',
fontWeight: 'bold',
textTransform: 'uppercase',
fontFamily: 'monospace',
}}
>
{wordToGuess.split('').map((letter, index) =>
renderLetter(letter, index, guessedLetters, reveal)
)}
</div>
);
};
export default HangmanWord;