-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
mouse and touch actions refactor
1 parent
c4d9dc7
commit 153de41
Showing
5 changed files
with
241 additions
and
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { CellElement } from '../models/interfaces'; | ||
|
||
type CellDetails = | ||
| { | ||
isValidCell: false; | ||
selectedCell?: null; | ||
} | ||
| { | ||
isValidCell: true; | ||
selectedCell: CellElement; | ||
}; | ||
|
||
export function isTouchDevice() { | ||
return 'ontouchstart' in window || navigator.maxTouchPoints > 0; | ||
} | ||
|
||
export function getCellDetails(element: HTMLElement | null): CellDetails { | ||
if (!element) { | ||
return { isValidCell: false }; | ||
} | ||
|
||
if (element.tagName !== 'BUTTON') { | ||
return { isValidCell: false }; | ||
} | ||
|
||
const row = +(element.dataset.row ?? -1); | ||
const col = +(element.dataset.col ?? -1); | ||
const cellType = +(element.dataset.cellType ?? -1); | ||
|
||
if (row === -1 || col === -1 || cellType === -1) { | ||
return { isValidCell: false }; | ||
} | ||
|
||
return { | ||
isValidCell: true, | ||
selectedCell: { row, col, cellType }, | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { CellElement, CellType } from '../models/interfaces'; | ||
import useMouseActions from './use-mouse-actions.hook'; | ||
import { getCellDetails } from '../helpers/action.helper'; | ||
|
||
export function useMouse({ | ||
isMobile, | ||
ref, | ||
}: { | ||
isMobile: boolean; | ||
ref: React.RefObject<HTMLDivElement>; | ||
}) { | ||
const [cell, setCell] = useState<CellElement | null>(null); | ||
const { element, isMouseDown } = useMouseActions({ isMobile, ref }); | ||
const { isValidCell, selectedCell } = getCellDetails(element); | ||
const previousCellRef = useRef<CellElement | null>(null); | ||
const isEntryOrExitRef = useRef(false); | ||
|
||
useEffect(() => { | ||
if (!isValidCell) { | ||
return; | ||
} | ||
|
||
const isNew = !( | ||
previousCellRef.current?.col === selectedCell.col && | ||
previousCellRef.current?.row === selectedCell.row | ||
); | ||
|
||
if (!isNew) { | ||
return; | ||
} | ||
|
||
if (isEntryOrExitRef.current && previousCellRef.current) { | ||
if ( | ||
![CellType.entry, CellType.exit, CellType.wall].includes( | ||
selectedCell.cellType | ||
) | ||
) { | ||
setCell({ | ||
...selectedCell, | ||
cellType: previousCellRef.current.cellType, | ||
}); | ||
previousCellRef.current = { | ||
...selectedCell, | ||
cellType: previousCellRef.current.cellType, | ||
}; | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (![CellType.entry, CellType.exit].includes(selectedCell.cellType)) { | ||
setCell({ | ||
row: selectedCell.row, | ||
col: selectedCell.col, | ||
cellType: | ||
selectedCell.cellType === CellType.wall | ||
? CellType.clear | ||
: CellType.wall, | ||
}); | ||
previousCellRef.current = selectedCell; | ||
} | ||
}, [isValidCell, selectedCell]); | ||
|
||
useEffect(() => { | ||
if (isMouseDown && selectedCell && !previousCellRef.current) { | ||
previousCellRef.current = selectedCell; | ||
isEntryOrExitRef.current = [CellType.entry, CellType.exit].includes( | ||
selectedCell.cellType | ||
); | ||
} | ||
|
||
if (!isMouseDown) { | ||
previousCellRef.current = null; | ||
isEntryOrExitRef.current = false; | ||
} | ||
}, [isMouseDown, selectedCell]); | ||
|
||
return cell; | ||
} |
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,79 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
import { CellElement, CellType } from '../models/interfaces'; | ||
import { getCellDetails } from '../helpers/action.helper'; | ||
|
||
export function useTouch({ | ||
isMobile, | ||
ref, | ||
}: { | ||
isMobile: boolean; | ||
ref: React.RefObject<HTMLDivElement>; | ||
}) { | ||
const [cell, setCell] = useState<CellElement | null>(null); | ||
const previousCellRef = useRef<CellElement | null>(null); | ||
|
||
const onClick = useCallback( | ||
(e: MouseEvent | TouchEvent) => { | ||
if (!isMobile) { | ||
return; | ||
} | ||
|
||
const target = e.target as HTMLElement; | ||
if (!target) { | ||
return; | ||
} | ||
|
||
const { isValidCell, selectedCell } = getCellDetails(target); | ||
if (!isValidCell) { | ||
return; | ||
} | ||
|
||
const cellType = selectedCell.cellType; | ||
const isEntryOrExit = [CellType.entry, CellType.exit].includes(cellType); | ||
if (isEntryOrExit) { | ||
previousCellRef.current = selectedCell; | ||
return; | ||
} | ||
|
||
if ( | ||
![CellType.entry, CellType.exit, CellType.wall].includes( | ||
selectedCell.cellType | ||
) && | ||
previousCellRef.current | ||
) { | ||
setCell({ | ||
...selectedCell, | ||
cellType: previousCellRef.current.cellType, | ||
}); | ||
previousCellRef.current = null; | ||
return; | ||
} | ||
|
||
setCell({ | ||
...selectedCell, | ||
cellType: cellType === CellType.wall ? CellType.clear : CellType.wall, | ||
}); | ||
previousCellRef.current = null; | ||
}, | ||
[isMobile] | ||
); | ||
|
||
useEffect(() => { | ||
if (!isMobile) { | ||
return; | ||
} | ||
|
||
const referenceEl = ref.current; | ||
if (!referenceEl) { | ||
return; | ||
} | ||
|
||
referenceEl.addEventListener('click', onClick); | ||
|
||
return () => { | ||
referenceEl.removeEventListener('mousedown', onClick); | ||
}; | ||
}, [isMobile, onClick, ref]); | ||
|
||
return cell; | ||
} |