-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.tsx
127 lines (108 loc) · 2.42 KB
/
Board.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React from 'react'
import styled, { keyframes } from 'styled-components'
import { compact } from 'lodash-es'
import { lighten } from 'polished'
const pop = keyframes`
0%{
transform: scale3d(1, 1, 1);
box-shadow: none;
}
50%{
transform: scale3d(1.2, 1.2, 1.2);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
100%{
transform: scale3d(1, 1, 1);
box-shadow: none;
}
`
const Wrap = styled.div`
display: flex;
flex-wrap: wrap;
width: 100%;
max-width: 500px;
`
const Cell = styled.div`
position: relative;
margin: 1px;
background-color: ${lighten(0.1, '#20202a')};
color: #fff;
&.clickable {
cursor: pointer;
:hover {
background-color: ${lighten(0.2, '#20202a')};
}
}
&.winning-cell-0,
&.winning-cell-1,
&.winning-cell-2 {
animation: 1500ms ${pop} ease-in-out;
}
&.winning-cell-1 {
animation-delay: 500ms;
}
&.winning-cell-2 {
animation-delay: 1000ms;
}
`
const XMark = styled.div`
position: absolute;
top: 10px;
right: 10px;
bottom: 10px;
left: 10px;
:before,
:after {
position: absolute;
top: 0;
bottom: 0;
left: calc(50% - 2px);
display: block;
content: '';
width: 4px;
border-radius: 2px;
background: #fff;
}
:before {
transform: rotate(-45deg);
}
:after {
transform: rotate(45deg);
}
`
const OMark = styled.div`
position: absolute;
top: 20px;
right: 20px;
bottom: 20px;
left: 20px;
border-radius: 100%;
border: 4px solid #fff;
`
interface IBoardProps {
cells: CellValue[]
winner?: { player: Player | null; combo: Combo } | null
onCellClick?(cell: CellIndex): void
}
const Board: React.FC<IBoardProps> = ({ cells, winner, onCellClick }) => {
const cellWidth = `calc(${(1 / Math.sqrt(cells.length)) * 100}% - 2px)`
return (
<Wrap>
{cells.map((cellValue, cellIndex) => (
<Cell
key={cellIndex}
style={{ width: cellWidth, paddingBottom: cellWidth }}
className={compact([
onCellClick && cellValue === null ? 'clickable' : null,
winner && winner.combo.includes(cellIndex) ? `winning-cell-${winner.combo.indexOf(cellIndex)}` : null
]).join(' ')}
onClick={() => onCellClick && cellValue === null && onCellClick(cellIndex)}
>
{cellValue === 'x' && <XMark />}
{cellValue === 'o' && <OMark />}
</Cell>
))}
</Wrap>
)
}
export default Board