diff --git a/.gitignore b/.gitignore index 3e2e84b0..58800c0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build/ node_modules/ + +**/.DS_Store diff --git a/package-lock.json b/package-lock.json index 0ed269a6..b043cafc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "animejs": "^3.2.2", "clsx": "^1.1.1", "react": "^17.0.2", + "react-animate-height": "^3.2.3", "react-dom": "^17.0.2", "react-helmet": "^6.1.0", "react-mailchimp-subscribe": "^2.1.3", @@ -12367,6 +12368,18 @@ "node": ">=0.10.0" } }, + "node_modules/react-animate-height": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/react-animate-height/-/react-animate-height-3.2.3.tgz", + "integrity": "sha512-R6DSvr7ud07oeCixScyvXWEMJY/Mt2+GyOWC1KMaRc69gOBw+SsCg4TJmrp4rKUM1hyd6p+YKw90brjPH93Y2A==", + "engines": { + "node": ">= 12.0.0" + }, + "peerDependencies": { + "react": ">=16.8.0", + "react-dom": ">=16.8.0" + } + }, "node_modules/react-app-polyfill": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/react-app-polyfill/-/react-app-polyfill-3.0.0.tgz", diff --git a/package.json b/package.json index f8e388d2..20b7a26b 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "animejs": "^3.2.2", "clsx": "^1.1.1", "react": "^17.0.2", + "react-animate-height": "^3.2.3", "react-dom": "^17.0.2", "react-helmet": "^6.1.0", "react-mailchimp-subscribe": "^2.1.3", diff --git a/src/app/App.js b/src/app/App.js index ef94673a..527f427f 100644 --- a/src/app/App.js +++ b/src/app/App.js @@ -29,6 +29,7 @@ import Designathon from "./pages/Designathons"; import Hey from "./pages/Hey"; import { HOUSES_SORTING_FORM } from "./pages/Houses/sections/Join"; import { PROJECT_TEAMS_GOOGLE_FORM } from "./pages/ProjectTeams/ProjectTeams"; +import { ColorGame } from "./pages/ColorGame/ColorGame"; function App() { useScroll(); @@ -71,6 +72,8 @@ function App() { } /> + } /> + {/* Redirects */} { + setShow(false); + }, []); + if (location.pathname.includes("/designathon")) return null; return ( -
-

- - Join the DAUCI Houses - {" "} - to connect with your own design family! -

-
+ show && ( + <> +
+

+ + Join the DAUCI Houses + {" "} + to connect with your own design family! +

+ +
+ + ) ); } diff --git a/src/app/components/Banner/Banner.module.scss b/src/app/components/Banner/Banner.module.scss index f0f8600c..fe7873a2 100644 --- a/src/app/components/Banner/Banner.module.scss +++ b/src/app/components/Banner/Banner.module.scss @@ -9,4 +9,15 @@ .underline { text-decoration: underline; } + .close { + position: absolute; + right: 20px; + background-color: transparent; + border: none; + color: white; + top: 0; + height: 100%; + font-size: 1.3em; + cursor: pointer; + } } diff --git a/src/app/components/Nav/Nav.scss b/src/app/components/Nav/Nav.scss index efbfaa09..5e56a43d 100644 --- a/src/app/components/Nav/Nav.scss +++ b/src/app/components/Nav/Nav.scss @@ -1,5 +1,11 @@ @import "app/styles/variables"; +nav { + position: sticky; + top: 0; + z-index: 1000; +} + #navSpacer { height: 88px; display: block; diff --git a/src/app/pages/ColorGame/ColorGame.jsx b/src/app/pages/ColorGame/ColorGame.jsx new file mode 100644 index 00000000..70ce661e --- /dev/null +++ b/src/app/pages/ColorGame/ColorGame.jsx @@ -0,0 +1,78 @@ +import { memo, useCallback, useEffect, useRef, useState } from "react"; +import cn from "./ColorGame.module.scss"; +import { Text } from "app/components"; +import { Section, Space } from "app/Symbols"; +import clsx from "clsx"; +import { Leaderboard } from "./components/Leaderboard/Leaderboard"; +import { + DIGITS, + HexColorInput, +} from "./components/HexColorInput/HexColorInput"; +import { Results } from "./components/Results/Results"; +import Auto from "react-animate-height"; +const generateRandomHexString = () => { + const randomHex = []; + + for (let i = 0; i < 6; i++) { + randomHex.push(DIGITS[Math.floor(Math.random() * 16)]); + } + return randomHex; +}; + +const PADDING = 40; + +export const ColorGame = memo(function ColorGame() { + const [color, setColor] = useState(generateRandomHexString); + const [report, setReport] = useState(); + const [height, setHeight] = useState("auto"); + + const resetColor = useCallback( + () => setColor(generateRandomHexString()), + [] + ); + + useEffect(() => { + const element = contentRef.current; + + const resizeObserver = new ResizeObserver(() => { + setHeight(element.clientHeight + PADDING); + }); + + resizeObserver.observe(element); + + return () => resizeObserver.disconnect(); + }, []); + const contentRef = useRef(null); + + return ( +
+ + Guess the color + + + +
+
+ +
+ {report && } +
+ +
+ ); +}); diff --git a/src/app/pages/ColorGame/ColorGame.module.scss b/src/app/pages/ColorGame/ColorGame.module.scss new file mode 100644 index 00000000..2c0be4b8 --- /dev/null +++ b/src/app/pages/ColorGame/ColorGame.module.scss @@ -0,0 +1,48 @@ + +.centered { + text-align: center; +} +.page { + min-height: calc(100vh - 88px); + display: grid; + place-content: center; + box-sizing: border-box; + + grid-auto-flow: row; + gap: 20px; + margin: 0 auto; + + padding: 20px; + box-sizing: border-box; + + + .container { + padding: 20px; + border: 2px solid var(--silver); + border-radius: 18px; + + .split { + display: grid; + grid-template-columns: auto 1fr; + gap: 20px; + + .color { + height: 200px; + width: 200px; + border-radius: 8px; + margin-bottom: 0px; + } + + @media screen and (max-width: 900px) { + grid-template-columns: auto; + gap: 20px; + + .color { + width: 100%; + margin: 0 auto; + } + + } + } + } +} diff --git a/src/app/pages/ColorGame/components/HexColorInput/HexColorInput.jsx b/src/app/pages/ColorGame/components/HexColorInput/HexColorInput.jsx new file mode 100644 index 00000000..e50ec93d --- /dev/null +++ b/src/app/pages/ColorGame/components/HexColorInput/HexColorInput.jsx @@ -0,0 +1,138 @@ +import { createRef, memo, useMemo, useState } from "react"; +import cn from "./HexColorInput.module.scss"; +import clsx from "clsx"; +import { generateReport } from "./generateReport"; +import REFRESH_ICON from "./refresh.png"; + +export const DIGITS = [ + "0", + "1", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "A", + "B", + "C", + "D", + "E", + "F", +]; + +const EMPTY_GUESS = ["", "", "", "", "", ""]; + +const focusEndOfInput = (inputEl) => { + inputEl.focus(); + inputEl.setSelectionRange(-1, -1); +}; + +export const HexColorInput = memo(function HexColorInput({ + color, + setReport, + resetColor, +}) { + const [guess, setGuess] = useState(EMPTY_GUESS); + const [input] = useState(() => Array.from({ length: 6 }, createRef)); + + const guessIsValid = useMemo(() => guess.join("").length === 6, [guess]); + + const ignoreEvent = (event) => { + event.preventDefault(); + }; + + const handleSubmit = (event) => { + event.preventDefault(); + if (!guessIsValid) return; + + setReport(generateReport(color, guess)); + }; + + const refresh = () => { + resetColor(); + setGuess(EMPTY_GUESS); + setReport(); + focusEndOfInput(input[0].current); + }; + + const handleChange = (index) => (event) => { + const userKeyInput = event.key.toUpperCase(); + + switch (event.key) { + case "Backspace": + if (index > 0 && !guess[index].length) { + focusEndOfInput(input[index - 1].current); + } + setGuess((prev) => { + const next = [...prev]; + next[!guess[index].length ? index - 1 : index] = ""; + return next; + }); + return; + + case "ArrowLeft": + if (index === 0) return; + focusEndOfInput(input[index - 1].current); + return; + + case "ArrowRight": + if (index > 5) return; + focusEndOfInput(input[index + 1].current); + return; + + default: + if (!DIGITS.includes(userKeyInput)) return; + + setGuess((prev) => { + const next = [...prev]; + next[index] = userKeyInput; + return next; + }); + + if (index > 4) return; + focusEndOfInput(input[index + 1].current); + } + }; + return ( +
+
+ + {guess.map((digit, index) => ( + + ))} +
+
+ + +
+
+ ); +}); diff --git a/src/app/pages/ColorGame/components/HexColorInput/HexColorInput.module.scss b/src/app/pages/ColorGame/components/HexColorInput/HexColorInput.module.scss new file mode 100644 index 00000000..bb71bf4f --- /dev/null +++ b/src/app/pages/ColorGame/components/HexColorInput/HexColorInput.module.scss @@ -0,0 +1,87 @@ +.container { + display: grid; + place-content: center; + grid-template-columns: auto auto; + gap: 20px; + min-width: 600px; + + @media screen and (max-width: 1000px) { + grid-template-columns: auto; + min-width: 0; + + .actions { + height: 40px; + place-self: center; + gap: 10px; + } + + } + + .actions { + display: grid; + grid-template-columns: auto auto; + gap: 10px; + } + + .input { + display: grid; + grid-template-columns: repeat(7, 32px); + gap: 8px; + + input { + border: none; + border-bottom: 2px solid var(--gray); + text-align: center; + font-size: 34px; + + &::placeholder { + opacity: 0.1; + } + + &.underline { + color: var(--gray); + border: none; + } + } + } + + button{ + border: 2px solid var(--silver); + padding: 0 20px; + border-radius: 8px; + text-transform: uppercase; + transition: border-color 250ms, color 250ms, background-color 250ms; + background-color: transparent; + + &.submit { + color: var(--silver); + font-weight: bold; + cursor: not-allowed; + + &.allowed { + color: var(--green); + border-color: var(--green); + cursor: pointer; + + &:hover { + background-color: var(--green); + color: var(--white); + } + } + } + + &.retry { + padding: 0 10px; + cursor: pointer; + border-color: var(--gray); + + & img { + pointer-events: none; + height: 24px; + width: 24px; + object-fit: contain; + } + } + + } +} diff --git a/src/app/pages/ColorGame/components/HexColorInput/generateReport.js b/src/app/pages/ColorGame/components/HexColorInput/generateReport.js new file mode 100644 index 00000000..6169fbb5 --- /dev/null +++ b/src/app/pages/ColorGame/components/HexColorInput/generateReport.js @@ -0,0 +1,38 @@ +const RANGE_PER_PIXEL_COLOR = 255; +const TOTAL_RANGE_PER_PIXEL = RANGE_PER_PIXEL_COLOR * 3; + +export const generateReport = (actualHexCode, guessHexCode) => { + console.log(actualHexCode, guessHexCode); + + const actualRGBValues = [ + Number("0x" + actualHexCode.slice(0, 2).join("")), + Number("0x" + actualHexCode.slice(2, 4).join("")), + Number("0x" + actualHexCode.slice(4, 6).join("")), + ]; + const guessRGBValues = [ + Number("0x" + guessHexCode.slice(0, 2).join("")), + Number("0x" + guessHexCode.slice(2, 4).join("")), + Number("0x" + guessHexCode.slice(4, 6).join("")), + ]; + + const percentageDifference = [ + actualRGBValues[0] - guessRGBValues[0], + actualRGBValues[1] - guessRGBValues[1], + actualRGBValues[2] - guessRGBValues[2], + ]; + const similarityPercentage = + 100 - + (percentageDifference.reduce((acc, val) => acc + Math.abs(val), 0) / + TOTAL_RANGE_PER_PIXEL) * + 100; + + console.log(percentageDifference, similarityPercentage); + + return { + actual: actualHexCode.join(""), + guess: guessHexCode.join(""), + similarityPercentage, + timestamp: new Date().getTime(), + percentageDifference, + }; +}; diff --git a/src/app/pages/ColorGame/components/HexColorInput/refresh.png b/src/app/pages/ColorGame/components/HexColorInput/refresh.png new file mode 100644 index 00000000..9e06bbc4 Binary files /dev/null and b/src/app/pages/ColorGame/components/HexColorInput/refresh.png differ diff --git a/src/app/pages/ColorGame/components/Leaderboard/Leaderboard.jsx b/src/app/pages/ColorGame/components/Leaderboard/Leaderboard.jsx new file mode 100644 index 00000000..0d395f07 --- /dev/null +++ b/src/app/pages/ColorGame/components/Leaderboard/Leaderboard.jsx @@ -0,0 +1,70 @@ +import clsx from "clsx"; +import { memo, useEffect, useRef, useState } from "react"; +import cn from "./Leaderboard.module.scss"; +import Auto from "react-animate-height"; + +import { Text } from "app/components"; +import { ScoreItem } from "./components/ScoreItem"; + +const PADDING = 40; +const HIGHSCORE_LOCALSTORAGE_KEY = "@duci/color-game-highscore"; + +export const Leaderboard = memo(function Leaderboard({ report }) { + const [highscores, setHighScore] = useState(() => + JSON.parse(localStorage.getItem(HIGHSCORE_LOCALSTORAGE_KEY) ?? "[]") + ); + const contentRef = useRef(null); + const [height, setHeight] = useState("auto"); + + useEffect(() => { + if (!report) return; + + setHighScore((prev) => { + const next = [...prev, report] + .sort((a, b) => b.similarityPercentage - a.similarityPercentage) + .splice(0, 15); + + localStorage.setItem( + HIGHSCORE_LOCALSTORAGE_KEY, + JSON.stringify(next) + ); + + return next; + }); + }, [report]); + + useEffect(() => { + const element = contentRef.current; + + const resizeObserver = new ResizeObserver(() => { + setHeight(element.clientHeight + PADDING); + }); + + resizeObserver.observe(element); + return () => resizeObserver.disconnect(); + }, []); + + return ( + +
+ + High Scores + +
+ {!highscores.length && ( +
+ No previous scores to show. +
+ )} + {highscores.map((score) => ( + + ))} +
+
+
+ ); +}); diff --git a/src/app/pages/ColorGame/components/Leaderboard/Leaderboard.module.scss b/src/app/pages/ColorGame/components/Leaderboard/Leaderboard.module.scss new file mode 100644 index 00000000..7a6525ef --- /dev/null +++ b/src/app/pages/ColorGame/components/Leaderboard/Leaderboard.module.scss @@ -0,0 +1,24 @@ +.container { + padding: 20px; + border: 2px solid var(--silver); + border-radius: 18px; + + .title { + margin-bottom: 20px; + } + + .list { + background-color: var(--silver); + border-radius: 8px; + width: 100%; + display: grid; + padding: 8px; + + .empty { + display: grid; + place-content: center; + color: var(--gray); + min-height: 300px; + } + } +} \ No newline at end of file diff --git a/src/app/pages/ColorGame/components/Leaderboard/components/ColorDisplay.jsx b/src/app/pages/ColorGame/components/Leaderboard/components/ColorDisplay.jsx new file mode 100644 index 00000000..f69947a9 --- /dev/null +++ b/src/app/pages/ColorGame/components/Leaderboard/components/ColorDisplay.jsx @@ -0,0 +1,20 @@ +import { memo } from "react"; + +import cn from "./ScoreItem.module.scss"; + +export const ColorDisplay = memo(function ColorDisplay({ + color, + label, + isShort, +}) { + return ( +
+ {!isShort && ( + <> + {label}: {color} + + )} +
+
+ ); +}); diff --git a/src/app/pages/ColorGame/components/Leaderboard/components/ScoreItem.jsx b/src/app/pages/ColorGame/components/Leaderboard/components/ScoreItem.jsx new file mode 100644 index 00000000..2826e6c5 --- /dev/null +++ b/src/app/pages/ColorGame/components/Leaderboard/components/ScoreItem.jsx @@ -0,0 +1,44 @@ +import { memo, useEffect, useState } from "react"; +import cn from "./ScoreItem.module.scss"; +import { ColorDisplay } from "./ColorDisplay"; + +export const ScoreItem = memo(function ScoreItem({ report }) { + const [isShort, setIsShort] = useState(window.innerWidth < 900); + + useEffect(() => { + const onResize = () => { + setIsShort(window.innerWidth < 900); + }; + window.addEventListener("resize", onResize); + + return () => window.removeEventListener("resize", onResize); + }, []); + + return ( +
+
+ {report.similarityPercentage.toFixed(2)}% +
+ + +
+ {new Date(report.timestamp).toLocaleString("en-US", { + day: "numeric", + month: "numeric", + year: "2-digit", + + hour: "numeric", + minute: "numeric", + })} +
+
+ ); +}); diff --git a/src/app/pages/ColorGame/components/Leaderboard/components/ScoreItem.module.scss b/src/app/pages/ColorGame/components/Leaderboard/components/ScoreItem.module.scss new file mode 100644 index 00000000..7b397096 --- /dev/null +++ b/src/app/pages/ColorGame/components/Leaderboard/components/ScoreItem.module.scss @@ -0,0 +1,31 @@ +.container { + display: grid; + grid-template-columns: 70px auto auto 1fr; + padding: 8px; + gap: 8px; + + .percent { + font-weight: bold; + } + + .label { + display: grid; + grid-auto-flow: column; + gap: 8px; + color: var(--gray); + + span { + color: #151515; + } + + .box { + height: 16px; + width: 16px; + border-radius: 8px; + } + } + + .date { + text-align: right; + } +} diff --git a/src/app/pages/ColorGame/components/Results/Results.jsx b/src/app/pages/ColorGame/components/Results/Results.jsx new file mode 100644 index 00000000..a322db08 --- /dev/null +++ b/src/app/pages/ColorGame/components/Results/Results.jsx @@ -0,0 +1,16 @@ +import { Text } from "app/components"; +import { memo } from "react"; +import cn from "./Results.module.scss"; + +export const Results = memo(function Results({ report }) { + return ( +
+ + You were {report.similarityPercentage.toFixed(2)}% of the way + there! + +
answer: #{report.actual}
+
you guessed: #{report.guess}
+
+ ); +}); diff --git a/src/app/pages/ColorGame/components/Results/Results.module.scss b/src/app/pages/ColorGame/components/Results/Results.module.scss new file mode 100644 index 00000000..f2a35521 --- /dev/null +++ b/src/app/pages/ColorGame/components/Results/Results.module.scss @@ -0,0 +1,6 @@ +.container { + text-align: center; + margin: 40px; + display: grid; + gap: 8px; +} \ No newline at end of file diff --git a/src/app/pages/Resources/Resources.js b/src/app/pages/Resources/Resources.js index fdc9e6f6..445d4936 100644 --- a/src/app/pages/Resources/Resources.js +++ b/src/app/pages/Resources/Resources.js @@ -37,11 +37,18 @@ const Resources = () => ( }} className="wait dx show card flex left fill sky spaceChildrenSmall" > - + {card.title} - {card.desc} + + {card.desc} + ))}