Skip to content

Commit 82a4411

Browse files
committed
[fix] Tetris unmount
1 parent 470fbcf commit 82a4411

File tree

7 files changed

+29
-17
lines changed

7 files changed

+29
-17
lines changed

src/components/atoms/GameModal.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ import { ModalClose } from '../modules/Modal';
33

44
type GameModalOptions = {
55
columns: number;
6-
rows: number;
76
copyWidth?: number;
87
children: any;
98
};
109

1110
export function GameModal({
1211
columns,
13-
rows,
1412
copyWidth = 40,
1513
children,
1614
}: GameModalOptions) {
1715
return (
1816
<div
1917
className={css.use('f', 'fh', 'fj_sb', 'panel', {
2018
width: `calc(${columns * CFG.SIZE}px + ${copyWidth}rem)`,
21-
height: `${rows * CFG.SIZE + 40}px`,
2219
position: 'relative',
2320
padding: css.$v.spaceL,
2421
})}

src/components/atoms/GameSidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function GameSidebar({
2020
return (
2121
<div className={cls}>
2222
{children}
23-
<GameStartButton eventOver={evtEnd} eventStart={evtStart} timer={60} />
23+
<GameStartButton eventOver={evtEnd} eventStart={evtStart} timer={60} style={{marginTop: 'calc(' + css.$v.spaceL + ' * 2)'}} />
2424
</div>
2525
);
2626
}

src/components/atoms/GameStartButton.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ type GameStartButtonOptions = {
55
timer: number;
66
eventStart: keyof RelayEvents;
77
eventOver: keyof RelayEvents;
8+
style?: Record<string, unknown>;
89
};
910

1011
export function GameStartButton(data: GameStartButtonOptions) {
1112
return (
12-
<button type="button" className={css.use('button', { width: '100%' })}>
13+
<button type="button" className={css.use('button', {
14+
width: '100%',
15+
[css.attr('data-hidden')]: {
16+
visibility: 'hidden',
17+
pointerEvents: 'none',
18+
cursor: 'none',
19+
},
20+
}, data.style || {})}>
1321
Start Now (<span>{data.timer}</span>s)
1422
<Script data={data}>
1523
{({ el, data, $ }) => {
@@ -49,11 +57,10 @@ export function GameStartButton(data: GameStartButtonOptions) {
4957
el.$subscribe(data.eventOver, () => {
5058
$.clear(el);
5159
el.textContent = 'Play again?';
52-
el.style.display = 'inline-block';
5360
});
5461

5562
/* On game start, hide element */
56-
el.$subscribe(data.eventStart, () => (el.style.display = 'none'));
63+
el.$subscribe(data.eventStart, () => (el.setAttribute('data-hidden', '')));
5764

5865
/* Start the clock on game boot */
5966
el.$subscribeOnce('game:evt:boot', setTimer);

src/routes/breakout/Game.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function BreakoutGame() {
4343
const gameBoardId = css.cid();
4444

4545
return (
46-
<GameModal columns={BREAKOUT_CFG.COLS} rows={BREAKOUT_CFG.ROWS}>
46+
<GameModal columns={BREAKOUT_CFG.COLS}>
4747
<GameConfig preview={PREVIEW} />
4848
<GameSidebar
4949
evtStart="breakout:start"

src/routes/snake/constants.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export const PREVIEW = {
1010
};
1111

1212
export const CFG = {
13-
COLS: 20 /* Board Columns */,
14-
ROWS: 20 /* Board Rows */,
13+
COLS: 15 /* Board Columns */,
14+
ROWS: 15 /* Board Rows */,
1515
};

src/routes/tetris/Game.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export type TetrisGameEvents = {
3434

3535
export function TetrisGame() {
3636
return (
37-
<GameModal columns={TETRIS_CFG.COLS} rows={TETRIS_CFG.ROWS} copyWidth={45}>
37+
<GameModal columns={TETRIS_CFG.COLS} copyWidth={45}>
3838
<GameConfig preview={PREVIEW} />
3939
<GameSidebar evtStart="tetris:start" evtEnd="tetris:gameover" width={35}>
4040
<GameExplanation
@@ -118,8 +118,13 @@ export function TetrisGame() {
118118
render();
119119
}
120120

121-
function setTimer() {
121+
function clearTimer() {
122122
if (tick) clearInterval(tick);
123+
tick = null;
124+
}
125+
126+
function setTimer() {
127+
clearTimer();
123128
tick = setInterval(() => {
124129
if (!isPaused) {
125130
el.$publish('tetris:evt:move', 'down');
@@ -226,12 +231,9 @@ export function TetrisGame() {
226231

227232
el.$subscribe('tetris:pause', () => (isPaused = !isPaused));
228233

229-
el.$subscribe('tetris:gameover', () => {
230-
if (tick) clearInterval(tick);
231-
tick = null;
232-
});
234+
el.$subscribe('tetris:gameover', () => clearTimer());
233235

234-
$.on(document, 'keydown', (e) => {
236+
const keydownhandler = $.on(document, 'keydown', (e) => {
235237
if (isPaused || !tick) return;
236238
let dir: TetrisGameEvents['tetris:evt:move'] | null = null;
237239
switch (e.key) {
@@ -253,6 +255,11 @@ export function TetrisGame() {
253255
}
254256
if (dir) el.$publish('tetris:evt:move', dir);
255257
});
258+
259+
el.$unmount = () => {
260+
keydownhandler();
261+
clearTimer();
262+
};
256263
}}
257264
</Script>
258265
</Game>

src/routes/tetris/atoms/Pieces.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function Block({ color, matrix }: BlockOptions) {
3434
<Script data={{ x, y, matrix, color }}>
3535
{({ el, data, $ }) => {
3636
let { x, y } = data;
37+
const { matrix } = data;
3738
let matrix_idx = 0;
3839
let isFirstCheck = true;
3940
const CELLS = $.queryAll(el, '[data-index]');

0 commit comments

Comments
 (0)