Skip to content

Commit acb7cbc

Browse files
committed
fix(*): finish app
1 parent 686d65e commit acb7cbc

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

css/style.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ main {
7777
justify-content: center;
7878
}
7979

80-
main div {
80+
.color-container {
8181
width: 20%;
8282
display: flex;
8383
flex-direction: column;
8484
justify-content: end;
8585
}
8686

87+
.color-container div {
88+
height: 100%;
89+
}
90+
8791
main div p {
8892
background-color: white;
8993
text-align: center;

index.html

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,33 @@
4444

4545
<main id="container">
4646

47+
<div class="color-container">
48+
<div id="color-0" style="background-color: white;"></div>
49+
<p id="color-name-0">''</p>
50+
</div>
51+
52+
<div class="color-container">
53+
<div id="color-1" style="background-color: white;"></div>
54+
<p id="color-name-1">''</p>
55+
</div>
56+
57+
<div class="color-container">
58+
<div id="color-2" style="background-color: white;"></div>
59+
<p id="color-name-2">''</p>
60+
</div>
61+
62+
<div class="color-container">
63+
<div id="color-3" style="background-color: white;"></div>
64+
<p id="color-name-3">''</p>
65+
</div>
66+
67+
<div class="color-container">
68+
<div id="color-4" style="background-color: white;"></div>
69+
<p id="color-name-4">''</p>
70+
</div>
4771

4872
</main>
4973

50-
5174
</body>
5275
</html>
5376

js/main.js

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,67 @@ const getColorSchemeBtn = document.getElementById("get-color-scheme");
33
const mainEl = document.getElementById("container");
44

55

6+
const color0El = document.getElementById("color-0");
7+
const color1El = document.getElementById("color-1");
8+
const color2El = document.getElementById("color-2");
9+
const color3El = document.getElementById("color-3");
10+
const color4El = document.getElementById("color-4");
11+
12+
const color0NameEl = document.getElementById("color-name-0");
13+
const color1NameEl = document.getElementById("color-name-1");
14+
const color2NameEl = document.getElementById("color-name-2");
15+
const color3NameEl = document.getElementById("color-name-3");
16+
const color4NameEl = document.getElementById("color-name-4");
17+
618
function fetchColors() {
719

8-
let mainHTML = '';
920
const mode = document.getElementById("mode").value;
1021
const cleanHex = colorPickerEl.value.split('#')[1];
1122
const url = `https://www.thecolorapi.com/scheme?hex=${cleanHex}&mode=${mode.toLowerCase()}`;
1223

1324
fetch(url, { method: "GET" })
1425
.then(res => res.json())
1526
.then(data => {
16-
data.colors.forEach(element => {
17-
const color = element.hex.value;
18-
mainHTML += `
19-
<div style="background-color: ${color};">
20-
<p>${color}</p>
21-
</div>
22-
`;
23-
});
24-
mainEl.innerHTML = mainHTML;
27+
for (let i = 0; i < data.colors.length; i++){
28+
const color = data.colors[i].hex.value;
29+
30+
const colorEl = document.getElementById(`color-${i}`);
31+
colorEl.style.backgroundColor = color;
32+
33+
const colorNameEl = document.getElementById(`color-name-${i}`);
34+
colorNameEl.textContent = color;
35+
}
2536
});
2637
}
2738

39+
function copy(color) {
40+
navigator.clipboard.writeText(color);
41+
alert(`Copied ${color} to clipboard`);
42+
}
2843

29-
getColorSchemeBtn.addEventListener("click", fetchColors);
44+
function copyColorfromContainer(event) {
45+
const color = event.target.style.backgroundColor;
46+
console.log(color);
47+
copy(color);
48+
}
3049

50+
function copyColorfromName(event) {
51+
const color = event.target.textContent;
52+
copy(color);
53+
}
54+
55+
56+
color0El.addEventListener("click", (e) => { copyColorfromContainer(e); });
57+
color1El.addEventListener("click", (e) => { copyColorfromContainer(e); });
58+
color2El.addEventListener("click", (e) => { copyColorfromContainer(e); });
59+
color3El.addEventListener("click", (e) => { copyColorfromContainer(e); });
60+
color4El.addEventListener("click", (e) => { copyColorfromContainer(e); });
61+
62+
color0NameEl.addEventListener("click", (e) => { copyColorfromName(e); });
63+
color1NameEl.addEventListener("click", (e) => { copyColorfromName(e); });
64+
color2NameEl.addEventListener("click", (e) => { copyColorfromName(e); });
65+
color3NameEl.addEventListener("click", (e) => { copyColorfromName(e); });
66+
color4NameEl.addEventListener("click", (e) => { copyColorfromName(e); });
67+
68+
getColorSchemeBtn.addEventListener("click", fetchColors);
3169
fetchColors();

0 commit comments

Comments
 (0)