Skip to content

Commit

Permalink
new update
Browse files Browse the repository at this point in the history
  • Loading branch information
z1shivam authored Oct 27, 2023
1 parent e3ed27f commit 9966057
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
7 changes: 6 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</header>
<div class="colorHistory" id="colorHistory">
<ul id="colorHistoryUL">
<li>#212121</li>

</ul>
</div>
<div class="colorViewer" id="colorViewer"></div>
Expand All @@ -30,6 +30,11 @@
<button id="copy" data-clipboard-target="#colCode">
<span class="material-symbols-rounded">content_copy</span>
</button>
<button id="clearHistory">
<span class="material-symbols-rounded">
delete_history
</span>
</button>
</div>
</body>
<script src="script.js"></script>
Expand Down
54 changes: 44 additions & 10 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
let clipboard = new ClipboardJS("#copy");

let colorViewer = document.getElementById("colorViewer");
let colCode = document.getElementById("colCode");
let clipboard = new ClipboardJS("#copy");
let generatedColour = "#212121";
let colorHistoryUL = document.getElementById("colorHistoryUL");

let colorHistory = [];
if (localStorage.getItem("colorHistory") !== null) {
colorHistory = JSON.parse(localStorage.getItem("colorHistory"));
} else {
colorHistory = ["#212121"];
}

const setAsCurrent = (e) => {
colorViewer.style.backgroundColor = e;
colCode.textContent = e;
};

const createElement = (color) => {
colorViewer.style.backgroundColor = color[0];
colCode.textContent = color[0];
colorHistoryUL.innerHTML = "";
colorHistory.forEach((color) => {
let createdEl = document.createElement("li");
createdEl.textContent = color;
createdEl.style.backgroundColor = color;
colorHistoryUL.appendChild(createdEl);
});
};
createElement(colorHistory);

let refresh = document
.getElementById("refresh")
.addEventListener("click", (e) => {
Expand All @@ -12,14 +38,22 @@ let refresh = document
for (let i = 1; i <= 6; i++) {
generatedColour += hexString.charAt(Math.floor(Math.random() * 16));
}
colorViewer.style.backgroundColor = generatedColour;
colCode.innerText = generatedColour;

let createdEl = document.createElement("li");
createdEl.textContent = generatedColour;
createdEl.style.backgroundColor = generatedColour;
if (colorHistoryUL.children.length >= 9) {
colorHistoryUL.removeChild(colorHistoryUL.lastChild);
colorHistory.unshift(generatedColour);
if (colorHistory.length > 10) {
colorHistory.pop();
}
colorHistoryUL.insertBefore(createdEl, colorHistoryUL.firstChild);
createElement(colorHistory);
localStorage.setItem("colorHistory", JSON.stringify(colorHistory));

document.querySelectorAll("li").forEach((li) => {
li.addEventListener("click", (e) => {
setAsCurrent(e.target.textContent);
});
});
});

document.getElementById("clearHistory").addEventListener("click", (e) => {
colorHistory = ["#212121"];
createElement(colorHistory);
localStorage.setItem("colorHistory", JSON.stringify(colorHistory));
});
5 changes: 3 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*{
padding: 0;
margin:0;
-webkit-tap-highlight-color: transparent;
}

.mainHeader{
Expand All @@ -13,9 +14,9 @@
display: flex;
justify-content: space-between;
align-items: center;

}


.logo{
text-align: center;
color: #fffff0;
Expand Down Expand Up @@ -99,7 +100,7 @@ ul li{
background-color: #212121;
border-radius: 20px;
color: #fffff0;

cursor: pointer;
}

.colCode{
Expand Down

0 comments on commit 9966057

Please sign in to comment.