Skip to content

Commit dcd104c

Browse files
authored
Allow flipping the board layout on mobile (#184)
1 parent fa501b1 commit dcd104c

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

src/App.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ function App() {
3636
"keyboardLayout",
3737
"QWERTY",
3838
);
39+
const [layoutOrientation, setLayoutOrientation] = useStorage(
40+
"layout",
41+
"portrait",
42+
);
43+
const [cardOrientation, setCardOrientation] = useStorage(
44+
"orientation",
45+
"vertical",
46+
);
47+
48+
const toggleLayoutOrientation = () => {
49+
setLayoutOrientation((x) => (x === "portrait" ? "landscape" : "portrait"));
50+
};
51+
const toggleCardOrientation = () => {
52+
setCardOrientation((x) => (x === "vertical" ? "horizontal" : "vertical"));
53+
};
54+
3955
const [volume, setVolume] = useStorage("volume", "on");
4056

4157
useEffect(() => {
@@ -127,7 +143,16 @@ function App() {
127143
) : (
128144
<UserContext.Provider value={user}>
129145
<SettingsContext.Provider
130-
value={{ keyboardLayout, setKeyboardLayout, volume, setVolume }}
146+
value={{
147+
keyboardLayout,
148+
setKeyboardLayout,
149+
volume,
150+
setVolume,
151+
layoutOrientation,
152+
toggleLayoutOrientation,
153+
cardOrientation,
154+
toggleCardOrientation,
155+
}}
131156
>
132157
<ConnectionsTracker />
133158
<WelcomeDialog />

src/components/Game.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import ResponsiveSetCard from "../components/ResponsiveSetCard";
1111
import { SettingsContext } from "../context";
1212
import useDimensions from "../hooks/useDimensions";
1313
import useKeydown from "../hooks/useKeydown";
14-
import useStorage from "../hooks/useStorage";
1514
import { generateCards, standardLayouts } from "../util";
1615

1716
const gamePadding = 8;
@@ -27,15 +26,14 @@ function Game({
2726
answer,
2827
lastSet,
2928
}) {
30-
const [layoutOrientation, setLayoutOrientation] = useStorage(
31-
"layout",
32-
"portrait",
33-
);
34-
const [cardOrientation, setCardOrientation] = useStorage(
35-
"orientation",
36-
"vertical",
37-
);
38-
const { keyboardLayout, volume } = useContext(SettingsContext);
29+
const {
30+
keyboardLayout,
31+
volume,
32+
layoutOrientation,
33+
toggleLayoutOrientation,
34+
cardOrientation,
35+
toggleCardOrientation,
36+
} = useContext(SettingsContext);
3937
const keyboardLayoutDesc = standardLayouts[keyboardLayout];
4038
const isHorizontal = cardOrientation === "horizontal";
4139
const isLandscape = layoutOrientation === "landscape";
@@ -177,11 +175,11 @@ function Game({
177175
) {
178176
event.preventDefault();
179177
if (volume === "on") playLayout();
180-
setCardOrientation(isHorizontal ? "vertical" : "horizontal");
178+
toggleCardOrientation();
181179
} else if (key.toLowerCase() === keyboardLayoutDesc.layoutChangeKey) {
182180
event.preventDefault();
183181
if (volume === "on") playLayout();
184-
setLayoutOrientation(isLandscape ? "portrait" : "landscape");
182+
toggleLayoutOrientation();
185183
}
186184
}
187185
});

src/components/Navbar.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import Toolbar from "@mui/material/Toolbar";
1313
import Tooltip from "@mui/material/Tooltip";
1414
import Typography from "@mui/material/Typography";
1515
import { useContext, useState } from "react";
16+
import useSound from "use-sound";
1617

18+
import layoutSfx from "../assets/layoutChangeSound.mp3";
1719
import { version } from "../config";
1820
import { SettingsContext, UserContext } from "../context";
1921
import firebase from "../firebase";
@@ -33,6 +35,7 @@ function Navbar({
3335
}) {
3436
const user = useContext(UserContext);
3537
const settings = useContext(SettingsContext);
38+
const [playLayout] = useSound(layoutSfx);
3639
const [anchorEl, setAnchorEl] = useState(null);
3740
const [changeName, setChangeName] = useState(false);
3841
const [changeUserColor, setChangeUserColor] = useState(false);
@@ -181,6 +184,16 @@ function Navbar({
181184
>
182185
Change keyboard layout
183186
</MenuItem>
187+
<MenuItem
188+
onClick={() => {
189+
if (settings.volume === "on") playLayout();
190+
settings.toggleLayoutOrientation();
191+
settings.toggleCardOrientation();
192+
handleCloseMenu();
193+
}}
194+
>
195+
Flip board layout
196+
</MenuItem>
184197
<MenuItem
185198
onClick={() => {
186199
setShowOptions(true);

0 commit comments

Comments
 (0)