-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (85 loc) · 3.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// relations to DOM
const mainEl = document.getElementsByTagName("main")[0]
const footerEl = document.getElementsByTagName("footer")[0]
const eyeDropperEl = document.getElementById("eye-dropper")
const modeEl = document.getElementById("mode")
const getColorBtn = document.getElementById("get-color")
//handleColor function fetches data from The Color Api, calls for display/removeSpinner functions and renderColors function with fetched data
// in case when it doesn't get the data, catches the error and displays error message on page; "getColor" button event handler
async function handleGetColor() {
try{
mainEl.innerHTML = ""
displaySpinner()
const res = await fetch(`https://www.thecolorapi.com/scheme?hex=${eyeDropperEl.value.substring(1)}&format=json&mode=${modeEl.value}&count=${count}`)
const data = await res.json()
removeSpinner()
renderColors(data.colors)
} catch (err) {
console.error(err)
removeSpinner()
mainEl.innerHTML = `
<div class = "error-container"
<p class = "error-message">Something went wrong!😿</br>Please try again later.</p>
</div>
`
}
}
// amount of colors to displayed; declared as const in case of further development
const count = 5;
// renderColors displays the color palette with colors' codes in hex and rgb;
// the colors are being taken from thecolorapi
function renderColors(data) {
let html = ""
data.forEach(color => {html += `
<div class = "color-container"
style = "background-color:${color.hex.value}">
<div class = "color-value-container">
<p class = "color-value">${color.hex.value}</p>
<p class = "color-value">${color.rgb.value}</p>
</div>
</div>
`
mainEl.innerHTML = html
})
}
// util functions displaySpinner and removeSpinner displays or removes spinner
// by adding or removing class to main element in DOM
function displaySpinner() {
mainEl.classList.add("loading")
}
function removeSpinner() {
mainEl.classList.remove("loading")
}
//copyToClipboard function copies the event target (in this case hex or rgb color code on click)
//and notifies user if it succeeded or failed
async function copyToClipboard(e) {
try{
await navigator.clipboard.writeText(e.target.textContent)
footerEl.innerHTML = `
<div class = "alert-box">
<p>Copied to clipboard!</p>
</div>
`
setTimeout(() => footerEl.innerHTML = "", 2000)
} catch(err) {
console.error("Failed to copy: " + err)
footerEl.innerHTML = `
<div class = "alert-box">
<p>Failed to copy. Please verify clipboard access permission.</p>
</div>
`
setTimeout(() => footerEl.innerHTML = "", 2000)
}
}
// event listener calls for handleGetColor function and adds event listeners for each color code
// to make them copiable with onclick
getColorBtn.addEventListener("click", ()=>{
handleGetColor().then(() => {
const colorCodeArray = Array.from(document.querySelectorAll(".color-value"))
for(element of colorCodeArray){
element.addEventListener("click", (e)=>{
copyToClipboard(e)
})
}
})
})