Skip to content

Commit 64034f3

Browse files
committed
Make it work on mobile
1 parent 03cf8f9 commit 64034f3

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Diff for: pages/index.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { useCallback, useEffect, useReducer } from "react";
1+
import Head from "next/head";
2+
import { useCallback, useEffect, useReducer, useRef } from "react";
23

34
const nGuesses = 6;
45
const wordleLength = 5;
@@ -57,6 +58,7 @@ function reducer(state, action) {
5758
}
5859

5960
export default function Home() {
61+
const inputRef = useRef(null);
6062
const [state, dispatch] = useReducer(reducer, initialState);
6163
const { guess, guessError, history, outcome } = state;
6264

@@ -72,6 +74,7 @@ export default function Home() {
7274

7375
if (res.ok) {
7476
dispatch({ type: actionTypes.ACCEPT_RESPONSE, response: jsonResponse });
77+
inputRef.current.value = "";
7578
} else if (jsonResponse && "error" in jsonResponse) {
7679
dispatch({ type: actionTypes.ACCEPT_ERROR, response: jsonResponse });
7780
} else {
@@ -99,8 +102,22 @@ export default function Home() {
99102
return () => window.removeEventListener("keyup", onKeyUp);
100103
}, [outcome, submitWordle]);
101104

105+
useEffect(() => {
106+
inputRef.current.focus();
107+
}, []);
108+
102109
return (
103110
<div className="wordle-container">
111+
<Head>
112+
<title>Wordle with React</title>
113+
<meta
114+
name="viewport"
115+
content="width=device-width, initial-scale=1, maximum-scale=1"
116+
/>
117+
</Head>
118+
119+
<input className="fallback-input" type="text" ref={inputRef} />
120+
104121
<Grid guess={guess} guessError={guessError} history={history} />
105122
{outcome === "win" && (
106123
<div style={{ color: "green", textAlign: "center", padding: "1rem" }}>

Diff for: styles/globals.css

+11
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,14 @@ a {
7575
border-color: var(--bg-color);
7676
color: white;
7777
}
78+
79+
.fallback-input {
80+
text-transform: "uppercase";
81+
width: 100%;
82+
}
83+
84+
@media only screen and (min-width: 768px) {
85+
.fallback-input {
86+
display: none;
87+
}
88+
}

0 commit comments

Comments
 (0)