Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding "Reveal" button #18

Closed
wants to merge 16 commits into from
Next Next commit
wip
  • Loading branch information
Andrew committed Apr 5, 2022
commit 29dc9212612406b49f7943f4c714a4858f99d88f
34 changes: 20 additions & 14 deletions components/answer.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
import { useEffect, useRef, useState } from "react";
import { AnswerProp } from "./types";
import { AnswerProp } from './types';
import { useEffect, useRef, useState } from 'react';

function Answer({ word, wasFound }: AnswerProp) {
function Answer({ word, wasFound, wasRevealed = false }: AnswerProp) {
const labelRef = useRef<HTMLLabelElement>(null);

let [wasScrolled, setWasScrolled] = useState(false);

useEffect(() => {
if (wasFound && !wasScrolled && labelRef?.current) {
labelRef.current.scrollIntoView({
behavior: "smooth",
inline: "center",
});
if (!wasRevealed && wasFound && !wasScrolled) {
useEffect(() => {
if (labelRef?.current) {
labelRef.current.scrollIntoView({
behavior: 'smooth',
inline: 'center',
});

setWasScrolled(true);
}
});
setWasScrolled(true);
}
});
}

return (
<label ref={labelRef}>
{wasFound ? word : "-".repeat(word.length)}
<label className={wasRevealed ? 'revealed' : ''} ref={labelRef}>
{wasFound ? word : '-'.repeat(word.length)}
<style jsx>{`
label {
margin: 0 8px;
text-transform: uppercase;
white-space: nowrap;
}

label.revealed {
opacity: 0.35;
}
`}</style>
</label>
);
Expand Down
9 changes: 6 additions & 3 deletions components/tile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ function Tile({ value, onTap }: Props) {
</button>
<style jsx>{`
div {
align-items: center;
display: flex;
flex: 1 1 0;
justify-content: center;
}

div.letter {
Expand All @@ -23,14 +26,14 @@ function Tile({ value, onTap }: Props) {
}

button {
background: #333030e8;
background: transparent;
color: white;
cursor: pointer;
display: inline-block;
font-size: 170%;
font-size: 200%;
font-weight: bold;
height: 50px;
padding: 4px;
padding-bottom: 2px;
text-transform: uppercase;
width: calc(100% - 8px);
}
Expand Down
1 change: 1 addition & 0 deletions components/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface AnswerProp {
shouldScroll: boolean;
wasFound: boolean;
wasRevealed: boolean;
word: string;
}

Expand Down
42 changes: 27 additions & 15 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Answer from "../components/answer";
import GameManager from "../lib/gameManager";
import Grade from "../components/grade";
import Head from "next/head";
import InteractiveWord from "../components/interactiveWord";
import Score from "../components/score";
import Tile from "../components/tile";
import { useState } from "react";
import { AnswerProp, TileProp } from "../components/types";
import Answer from '../components/answer';
import GameManager from '../lib/gameManager';
import Grade from '../components/grade';
import Head from 'next/head';
import InteractiveWord from '../components/interactiveWord';
import Score from '../components/score';
import Tile from '../components/tile';
import { AnswerProp, TileProp } from '../components/types';
import { useState } from 'react';

let game = GameManager.nextGame();

Expand Down Expand Up @@ -42,6 +42,17 @@ function Home() {
setScramble(scramble.concat(tile).sort((a, b) => a.index - b.index));
}

function revealAnswers() {
answers.forEach(answer => {
if (!answer.wasFound) {
answer.wasFound = true;
answer.wasRevealed = true;
}
});

setAnswers(answers);
}

function tryEnterWord() {
const word = entry.map((m) => m.letter).join("");
const answerIndex = answers.findIndex((f) => f.word === word && !f.wasFound);
Expand Down Expand Up @@ -72,7 +83,7 @@ function Home() {
<main>
<div className="score-container">
<Score label="Score" score={score}></Score>
<Grade max={maxScore} score={0}></Grade>
<Grade max={maxScore} score={score}></Grade>
<Score label="Max" score={maxScore} align="right"></Score>
</div>

Expand All @@ -88,6 +99,7 @@ function Home() {
</div>

<div className="enter-container">
<Tile value="Reveal" onTap={revealAnswers}></Tile>
<Tile value="Enter" onTap={tryEnterWord}></Tile>
</div>
</main>
Expand Down Expand Up @@ -139,7 +151,7 @@ function Home() {
display: flex;
flex-grow: 1;
font-size: 60px;
padding: 16px 0;
padding: 16px 15%;
overflow-x: auto;
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
Expand All @@ -164,7 +176,7 @@ function Home() {
}

.entry-container > * {
height: 100px;
height: 75px;
}

.main-container > * {
Expand All @@ -175,16 +187,16 @@ function Home() {
.enter-container {
align-items: center;
display: flex;
justify-content: flex-end;
padding: 4px 4px 16px 4px;
justify-content: space-between;
padding: 32px 6px 16px 6px;
}

footer {
align-items: center;
display: flex;
font-variant: small-caps;
justify-content: space-between;
padding: 16px;
padding: 16px 10px;
width: 100%;
}

Expand Down