Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds color controls and pause button #14

Merged
merged 4 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Visit the [life demo](sirpixiejerry.github.io/dop-eye/) to try it yourself.
- [x] change element color
- [x] rewind to original color
- [x] scroll steps
- [ ] improve scroll steps feature and cleanup event listeners
- [ ] add user menu
- [x] add pause button
- [x] add undo button
- [x] add redo button
- [x] add info button
- [ ] add save button
- [ ] optimization for mobile devices
60 changes: 60 additions & 0 deletions src/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ body {
width: 300px;
height: 300px;
background-color: rgb(53, 45, 106);
transform: translateX(30px);
}

svg {
Expand Down Expand Up @@ -125,3 +126,62 @@ path {
transform: translate(-45px, -45px);
background-color: yellow;
}

button {
width: 40px;
height: 40px;
border-radius: 8px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
margin-bottom: 4px;
border: none;
background-color: white;
position: relative;
overflow: hidden;
}

button:hover {
background-color: rgb(242, 242, 242);
}

img {
height: 25px;
width: 25px;
}

#menu {
display: flex;
flex-direction: column;
transform: translate(40px, -150px);
}

button span {
position: absolute;
border-radius: 8px;
background-color: rgba(0, 0, 0, 0.2);
width: 40px;
height: 40px;
animation: ripple 1s;
opacity: 0;
}

@keyframes ripple {
from {
opacity: 1;
transform: scale(0);
}
to {
opacity: 0;
transform: scale(10);
}
}

.active {
background-color: rgb(236, 236, 236);
}

.active:hover {
background-color: rgb(226, 226, 226);
}
13 changes: 13 additions & 0 deletions src/icons/square-arrow-left-svgrepo-com.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/icons/square-arrow-right-svgrepo-com.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/icons/square-pause-svgrepo-com.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
</div>
</div>
</div>
<div id="menu">
<button id="pause">
<img src="./icons/square-pause-svgrepo-com.svg" alt="pause" />
</button>
<button id="redo">
<img src="./icons/square-arrow-right-svgrepo-com.svg" alt="redo" />
</button>
<button id="undo">
<img src="./icons/square-arrow-left-svgrepo-com.svg" alt="undo" />
</button>
</div>
<script type="module" src="ts/index.ts"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions src/modules/addRippleEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const addRippleEffect = (button: HTMLButtonElement): void => {
const ripple = document.createElement('span');
ripple.classList.add('ripple');
button.appendChild(ripple);
setTimeout(() => {
ripple.remove();
}, 600);
};
3 changes: 2 additions & 1 deletion src/modules/applyColorFromStep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('applyColorFromStep', () => {
it('should apply color to the specified element', () => {
const mockTarget = document.getElementById(mockStep.target);
applyColorFromStep(mockStep);
expect(mockTarget!.style.backgroundColor).to.equal(mockStep.color);
if (!mockTarget) throw new Error('mockTarget not found');
expect(mockTarget.style.backgroundColor).to.equal(mockStep.color);
});
});
2 changes: 1 addition & 1 deletion src/modules/applyColorFromStep.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { colorReflection } from './colorReflection';
import { colorSVG } from './colorSVG';
import type { StepType } from './handleClickableElement';
import type { StepType } from './rewindColors';

export const applyColorFromStep = (step: StepType | undefined): void => {
if (step === undefined) return;
Expand Down
6 changes: 0 additions & 6 deletions src/modules/handleClickableElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import { recordColorChange } from './recordColorChange';
import { resetTimer, setStopRewind } from './rewindColors';
import { scrollThroughSteps } from './scrollThroughSteps';

export type StepType = {
target: string;
color: string;
};

let isScroll = false;
export const steps: StepType[] = [];

type ClickableElement = HTMLElement & { id: string };

Expand Down
26 changes: 26 additions & 0 deletions src/modules/handleMenuButtons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { addRippleEffect } from './addRippleEffect';
import {
redoColorChange,
setPauseRewind,
undoColorChange,
} from './rewindColors';

export const handleMenuButtons = (): void => {
document.querySelectorAll('button').forEach((button) => {
button.addEventListener('click', (evt) => {
const button = evt.currentTarget as HTMLButtonElement;
addRippleEffect(button);

if (button.id === 'pause') {
button.classList.toggle('active');
setPauseRewind();
}
if (button.id === 'redo') {
redoColorChange();
}
if (button.id === 'undo') {
undoColorChange();
}
});
});
};
1 change: 1 addition & 0 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './colorSVG';
export * from './colorReflection';
export * from './handleClickableElement';
export * from './recordColorChange';
export * from './handleMenuButtons';
4 changes: 3 additions & 1 deletion src/modules/recordColorChange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type StepType, steps } from './handleClickableElement';
import { type StepType, steps, updateStepIndex } from './rewindColors';

const pushStep = (step: StepType): void => {
steps.push(step);
Expand All @@ -11,12 +11,14 @@ export const recordColorChange = (target: Element): void => {
target: target.id,
color: computedStyle.backgroundColor,
});
updateStepIndex();
}
if (target instanceof SVGElement) {
const computedStyle = window.getComputedStyle(target);
pushStep({
target: target.id,
color: computedStyle.fill,
});
updateStepIndex();
}
};
34 changes: 31 additions & 3 deletions src/modules/rewindColors.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import { steps } from './handleClickableElement';
import { applyColorFromStep } from './applyColorFromStep';

export type StepType = {
target: string;
color: string;
};

export const steps: StepType[] = [];
let stepIndex = steps.length - 1;

const DELAY = 1000;
const TIMEOUT = 5000;

let idleTime: NodeJS.Timeout;
let interval: NodeJS.Timeout;
let stopRewind = true;
let pauseRewind = false;
let initialDelay: number;

export const updateStepIndex = (): void => {
stepIndex = stepIndex + 1;
};

export const undoColorChange = (): void => {
if (stepIndex < 0) return;
applyColorFromStep(steps[stepIndex]);
stepIndex = stepIndex - 1;
};

export const redoColorChange = (): void => {
if (stepIndex > steps.length - 1) return;
applyColorFromStep(steps[stepIndex]);
stepIndex = stepIndex + 1;
};

const rewind = (): void => {
const removeStep = (): void => {
if (stopRewind) return;
if (stopRewind || pauseRewind) return;
if (steps.length > 0) {
console.log(steps.length);
const removedStep = steps.pop();
Expand All @@ -29,8 +53,12 @@ export const setStopRewind = (arg: boolean): void => {
stopRewind = arg;
};

export const setPauseRewind = (): void => {
pauseRewind = !pauseRewind;
};

export const resetTimer = (): void => {
if (stopRewind) return;
if (stopRewind || pauseRewind) return;
clearTimeout(idleTime);
clearTimeout(interval);
initialDelay = DELAY;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/scrollThroughSteps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { applyColorFromStep } from './applyColorFromStep';
import { steps } from './handleClickableElement';
import { steps } from './rewindColors';

let scrollIndex = 0;

Expand Down
3 changes: 2 additions & 1 deletion src/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleClickableElement } from '../modules';
import { handleClickableElement, handleMenuButtons } from '../modules';

((): void => {
handleClickableElement();
handleMenuButtons();
})();