-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a32d724
commit a50794f
Showing
15 changed files
with
169 additions
and
622 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,41 @@ | ||
import { FC } from 'react'; | ||
import ChessBoard from './ChessBoard'; | ||
import { useState } from 'react'; | ||
import styles from './chess-board.module.scss'; | ||
import { Box } from './box'; | ||
|
||
const gridSize = 8; | ||
|
||
function App() { | ||
const [selectedTile, setSelectedTile] = useState<{ row: number; col: number } | null>(null); | ||
|
||
function colorDiagonally(e: React.MouseEvent<HTMLButtonElement>) { | ||
const target = e.target as HTMLButtonElement; | ||
setSelectedTile({ | ||
row: Number(target.dataset.row), | ||
col: Number(target.dataset.col), | ||
}); | ||
} | ||
|
||
const App: FC = () => { | ||
return ( | ||
<main className={styles.app}> | ||
<ChessBoard /> | ||
</main> | ||
<> | ||
<p className={styles.instruction}>Click on any cell to color diagonally</p> | ||
|
||
<div className={styles.board}> | ||
{Array.from({ length: gridSize }, (_, row) => ( | ||
<div key={row} className={styles.row}> | ||
{Array.from({ length: gridSize }, (_, col) => ( | ||
<Box | ||
key={col} | ||
row={row} | ||
col={col} | ||
selectedTile={selectedTile} | ||
onClick={colorDiagonally} | ||
/> | ||
))} | ||
</div> | ||
))} | ||
</div> | ||
</> | ||
); | ||
}; | ||
} | ||
|
||
export default App; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import classes from './chess-board.module.scss'; | ||
|
||
interface Props { | ||
row: number; | ||
col: number; | ||
selectedTile: { row: number; col: number } | null; | ||
onClick: (e: React.MouseEvent<HTMLButtonElement>) => void; | ||
} | ||
|
||
export function Box({ row, col, selectedTile, onClick }: Props) { | ||
let className = classes.box; | ||
if (selectedTile) { | ||
if ( | ||
row - col === selectedTile.row - selectedTile.col || | ||
row + col === selectedTile.row + selectedTile.col | ||
) { | ||
className += ' ' + classes.selected; | ||
} | ||
if (row === selectedTile.row && col === selectedTile.col) { | ||
className += ' ' + classes.clicked; | ||
} | ||
} | ||
|
||
return <button key={col} className={className} data-row={row} data-col={col} onClick={onClick} />; | ||
} |
91 changes: 39 additions & 52 deletions
91
apps/react/src/challenges/chess-board/chess-board.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,48 @@ | ||
//variables | ||
$white: #ffffff; | ||
$black: #151417; | ||
$gray: #333642; | ||
$shadow: rgba(0, 0, 0, 0.2) 0px 0px 25px; | ||
$base-font-size: 16px; | ||
|
||
.app { | ||
width: 100%; | ||
height: 85dvh; | ||
background-color: red; | ||
.instruction { | ||
text-align: center; | ||
} | ||
|
||
//mixins | ||
@mixin flex($direction: row, $justify: center, $align: center, $wrap: nowrap, $gap: 0) { | ||
.board { | ||
display: flex; | ||
align-items: $align; | ||
justify-content: $justify; | ||
flex-wrap: $wrap; | ||
gap: $gap; | ||
} | ||
flex-direction: column; | ||
flex-grow: 1; | ||
margin: auto; | ||
border: 2px solid black; | ||
width: min(90vw, 100vh - 160px); | ||
height: min(90vw, 100vh - 160px); | ||
|
||
@mixin grid($columsn, $rows, $gap: 0) { | ||
display: grid; | ||
grid-template-columns: $columsn; | ||
grid-template-rows: $rows; | ||
gap: $gap; | ||
} | ||
div { | ||
display: flex; | ||
width: 100%; | ||
height: 100%; | ||
|
||
//styles | ||
.app { | ||
height: 85dvh; | ||
width: 100%; | ||
background-color: $gray; | ||
@include flex(); | ||
} | ||
button.box { | ||
display: flex; | ||
flex-grow: 1; | ||
cursor: pointer; | ||
border: none; | ||
} | ||
|
||
.chess { | ||
height: 100%; | ||
min-height: 2.5rem; | ||
aspect-ratio: 1; | ||
background-color: $black; | ||
@include grid(repeat(8, 1fr), repeat(8, 1fr)); | ||
box-shadow: $shadow; | ||
overflow: hidden; | ||
background-color: $black; | ||
color: $white; | ||
} | ||
button:focus-visible { | ||
outline: 4px solid yellowgreen; | ||
z-index: 1; | ||
} | ||
} | ||
|
||
.tile { | ||
@include flex; | ||
width: 100%; | ||
height: 100%; | ||
font-size: 3.5em; | ||
} | ||
div:nth-child(even) button:nth-child(odd) { | ||
background-color: black; | ||
} | ||
|
||
div:nth-child(odd) button:nth-child(even) { | ||
background-color: black; | ||
} | ||
|
||
div button.box.selected { | ||
transition: background-color 0.33s; | ||
background-color: red; | ||
} | ||
|
||
.tile_color { | ||
background-color: $white; | ||
overflow: hidden; | ||
color: $black; | ||
div button.box.clicked { | ||
background-color: darkred; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.