Skip to content

Commit 0a09ade

Browse files
committed
add navigation mobile
1 parent 12c3ace commit 0a09ade

File tree

10 files changed

+86
-27
lines changed

10 files changed

+86
-27
lines changed

src/App.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import PlayIcon from "./components/icon/PlayIcon";
1010
import PauseIcon from "./components/icon/PauseIcon";
1111
import ResumePlay from "./components/icon/ResumePlay";
1212
import Player from "./components/Player/Player";
13+
import { disableScroll } from "./utils/disableScroll";
1314

1415
// type AlgState = 'started' | 'notStarted' | 'paused' | 'finished'
1516

@@ -147,6 +148,7 @@ const App = () => {
147148
};
148149

149150
const barWidth = window.screen.width / array.length;
151+
disableScroll()
150152

151153
return (
152154
<section>
@@ -160,7 +162,6 @@ const App = () => {
160162
history={history}
161163
currentStep={currentStep}
162164
/>
163-
{/* <Select selectedAlgorithm={selectedAlgorithm} onChange={selectAlgorithm}/> */}
164165
<Player
165166
selectAlgorithm={selectAlgorithm}
166167
goToNextStep={goToNextStep}

src/components/Array/Array.css

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
justify-content: center;
66
align-items: end;
77
box-sizing: border-box;
8-
/* gap: 0.2rem; */
98
}
109
@media (max-width: 768px) {
1110
.arr {
12-
height: 85vh;
11+
height: 84vh;
1312
}
1413
}

src/components/Button/Button.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import './Button.css'
22

33

4-
const Button = ({onClick, children, className}) => {
4+
const Button = ({onClick, children, className,onMouseDown,onMouseUp,onMouseLeave, onTouchCancel, onTouchStart, onTouchEnd}) => {
55
return (
6-
<button className={`button ${className}`} onClick={onClick}>{children}</button>
6+
<button className={`button ${className}`} onMouseDown={onMouseDown} onMouseUp={onMouseUp} onMouseLeave={onMouseLeave} onClick={onClick} onTouchEnd={onTouchEnd} onTouchCancel={onTouchCancel} onTouchStart={onTouchStart}>{children}</button>
77
)
88
}
99

src/components/Player/Player.css

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
.player-container {
2-
width: 400px;
2+
width: 700px;
33
height: auto;
44
position: fixed;
5-
inset: auto 0 0 0;
5+
inset: auto 0 0 30%;
66
z-index: 1000;
77
background-color: black;
88
opacity: 80%;
99
border-top-right-radius: 20px;
10+
border-top-left-radius: 20px;
11+
1012
}
1113
.controls {
1214
display: flex;
@@ -24,3 +26,13 @@
2426
display: flex;
2527
color: wheat;
2628
}
29+
30+
@media(max-width: 768px){
31+
.player-container{
32+
width:100%;
33+
inset: auto 0 0 0;
34+
border-top-right-radius: 0px;
35+
border-top-left-radius: 0px;
36+
}
37+
38+
}

src/components/Player/Player.jsx

+45-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { NextStepIcon } from "../icon/NextStepIcon";
44
import { ResetIcon } from "../icon/ResetIcon";
55
import DropDown from "../DropDown/DropDown";
66
import Button from "../Button/Button";
7+
import { useRef } from "react";
78

89
const Player = ({
910
selectAlgorithm,
@@ -16,6 +17,32 @@ const Player = ({
1617
handleAlgorithmRun,
1718
selectedAlgorithm,
1819
}) => {
20+
21+
const intervalRef = useRef(null)
22+
23+
const startInterval = (action) => {
24+
if(intervalRef.current === null){
25+
intervalRef.current = setInterval(()=>{
26+
action()
27+
},100)
28+
}
29+
}
30+
31+
const clearTimer = () => {
32+
if(intervalRef.current !== null){
33+
clearInterval(intervalRef.current)
34+
}
35+
intervalRef.current = null
36+
}
37+
38+
const handleMouseDown = (action) =>{
39+
action()
40+
startInterval(action)
41+
}
42+
const handleMouseUpOrLeave = () =>{
43+
clearTimer()
44+
}
45+
1946
return (
2047
<div className="player-container">
2148
<div className="controls">
@@ -24,11 +51,26 @@ const Player = ({
2451
selectedAlgorithm={selectedAlgorithm}
2552
/>
2653
<div className="step-buttons">
27-
<Button onClick={goToPreviousStep}>
54+
<Button
55+
onMouseDown={() => handleMouseDown(goToPreviousStep)}
56+
onMouseUp={handleMouseUpOrLeave}
57+
onMouseLeave={handleMouseUpOrLeave}
58+
onTouchStart={()=> handleMouseDown(goToPreviousStep)}
59+
onTouchEnd={handleMouseUpOrLeave}
60+
onTouchCancel = {handleMouseUpOrLeave}
61+
62+
>
2863
<PreviousStepIcon />
2964
</Button>
3065
<Button onClick={handleAlgorithmRun}>{getButtonText()}</Button>
31-
<Button onClick={goToNextStep}>
66+
<Button
67+
onMouseDown={()=>handleMouseDown(goToNextStep)}
68+
onMouseUp = {handleMouseUpOrLeave}
69+
onMouseLeave = {handleMouseUpOrLeave}
70+
onTouchStart={()=> handleMouseDown(goToNextStep)}
71+
onTouchEnd={handleMouseUpOrLeave}
72+
onTouchCancel = {handleMouseUpOrLeave}
73+
>
3274
<NextStepIcon />
3375
</Button>
3476
</div>
@@ -39,7 +81,7 @@ const Player = ({
3981
<div className="controls-speed">
4082
<div>
4183
<input
42-
style={{ width: "320px" }}
84+
style={{ width: "320px", opacity: '70%' }}
4385
type="range"
4486
id="speed"
4587
value={speed}

src/index.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
body {
77
background-color: rgb(98, 97, 97);
88
}
9+
910
section {
1011
display: flex;
1112
height: 100vh;
@@ -14,7 +15,7 @@ section {
1415
}
1516

1617
.title-algorithm {
17-
color: white;
18+
color: rgb(255, 253, 253);
1819
text-align: center;
1920
margin-top: 50px;
2021
font-size: xx-large;

src/utils/disableScroll.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const disableScroll = () =>{
2+
document.body.style.position = 'fixed'
3+
document.body.style.width = '100%'
4+
}

src/utils/generateRandomArray.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export const generateRandomArray = (length, min, max) => {
2-
const array = Array.from({length}, () => Math.floor(Math.random () * ( max - min + 1 )) + min)
3-
return array
4-
}
5-
2+
const array = Array.from(
3+
{ length },
4+
() => Math.floor(Math.random() * (max - min + 1)) + min,
5+
);
6+
return array;
7+
};

src/utils/pause.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { sleep } from "./sleep";
22

33
export const pause = async (evalStateRef) => {
4-
5-
if(evalStateRef.current === 'paused'){
6-
await sleep(100)
7-
return pause(evalStateRef)
8-
} else {
9-
return undefined
10-
}
11-
}
4+
if (evalStateRef.current === "paused") {
5+
await sleep(100);
6+
return pause(evalStateRef);
7+
} else {
8+
return undefined;
9+
}
10+
};

src/utils/sleep.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export const sleep = (ms,speedFactor) => {
2-
return new Promise((resolve) =>
3-
setTimeout((resolve), ms / speedFactor ))
4-
}
1+
export const sleep = (ms, speedFactor) => {
2+
return new Promise((resolve) => setTimeout(resolve, ms / speedFactor));
3+
};

0 commit comments

Comments
 (0)