Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

feat(highlighting): show which letters are correct #5

Open
wants to merge 1 commit into
base: 02-21-chore_scss_Migrate_css_-_scss
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions wordle-tutorial/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@types/node": "^16.7.13",
"@types/react": "^17.0.20",
"@types/react-dom": "^17.0.9",
"classnames": "^2.3.1",
"node-sass": "^7.0.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
66 changes: 35 additions & 31 deletions wordle-tutorial/src/App.scss
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
.guesses {
margin: 0 auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
padding: 40px 16px;

.App-link {
color: #61dafb;
}
.word {
display: flex;
flex-direction: row;
margin-bottom: 8px;

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
.letter {
display: flex;
align-items: center;
justify-content: center;

border: 2px solid #999999;
height: 56px;
width: 56px;
font-size: 32px;
text-transform: uppercase;
font-weight: bold;

margin-right: 8px;

&:last-child {
margin-right: 0;
}

&.letter--almost-correct {
background-color: #FFFFCC;
border-color: #CCCC99;
}

&.letter--exactly-correct {
background-color: #CCFFCC;
border-color: #99CC99;
}
}
}
}
47 changes: 44 additions & 3 deletions wordle-tutorial/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import classnames from "classnames";
import { useEffect, useState } from "react";
import "./App.scss";

const LETTERS = "abcdefghijklmnopqrstuvwxyz";

function App() {
// The word the user is trying to guess
const [actualWord] = useState("magic");
// The current word
const [buffer, setBuffer] = useState("");
// All previous guesses
Expand Down Expand Up @@ -32,13 +35,51 @@ function App() {
}, [buffer]);

return (
<div>
<div className="guesses">
{history.map((pastGuess, idx) => {
return <div key={idx}>{pastGuess}</div>;
return (
<Word key={idx} guess={pastGuess} highlight actualWord={actualWord} />
);
})}
<div>{buffer}</div>
<Word guess={buffer} actualWord={actualWord} />
</div>
);
}

function Word(props: {
guess: string;
actualWord: string;
highlight?: boolean;
}) {
const guessWithPadding = props.guess.padEnd(5, " ");

return (
<div className="word">
{Array.from(guessWithPadding).map((letter, idx) => {
return (
<Letter
key={idx}
guess={letter}
idx={idx}
actualWord={props.actualWord}
highlight={props.highlight}
/>
);
})}
</div>
);
}

function Letter(props: {
guess: string;
idx: number;
actualWord: string;
highlight?: boolean;
}) {
return <div className={classnames("letter", {
"letter--exactly-correct": props.highlight && props.actualWord[props.idx] === props.guess,
"letter--almost-correct": props.highlight && props.actualWord.indexOf(props.guess) !== -1
})}>{props.guess}</div>;
}

export default App;
5 changes: 5 additions & 0 deletions wordle-tutorial/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3013,6 +3013,11 @@ cjs-module-lexer@^1.0.0:
resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-1.2.2.tgz#9f84ba3244a512f3a54e5277e8eef4c489864e40"
integrity sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA==

classnames@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.3.1.tgz#dfcfa3891e306ec1dad105d0e88f4417b8535e8e"
integrity sha512-OlQdbZ7gLfGarSqxesMesDa5uz7KFbID8Kpq/SxIoNGDqY8lSYs0D+hhtBXhcdB3rcbXArFr7vlHheLk1voeNA==

clean-css@^5.2.2:
version "5.2.4"
resolved "https://registry.yarnpkg.com/clean-css/-/clean-css-5.2.4.tgz#982b058f8581adb2ae062520808fb2429bd487a4"
Expand Down