-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (40 loc) · 1.91 KB
/
app.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
// Target Elements
const displayColorContainers = document.querySelectorAll('.color-display')
const displayHexColorContainers = document.querySelectorAll('.hex-display')
const colorInput = document.getElementById('color-input')
const colorSchemeInput = document.getElementById('color-scheme')
const getColorSchemeBtn = document.getElementById('get-color-scheme')
const copyMessage = document.querySelector('.message')
// Initialisations
let currentScheme
let currentColor
// Event Listeners
colorInput.addEventListener('change', () => currentColor = (colorInput.value).slice(1))
colorSchemeInput.addEventListener('change', () => currentScheme = colorSchemeInput.value)
/* Fetch color scheme
Base URL: https://www.thecolorapi.com/
Endpoints: /scheme
Query Strings Example: hex=0047AB&format=html&mode=analogic&count=6 */
getColorSchemeBtn.addEventListener('click', function() {
fetch(`https://www.thecolorapi.com/scheme?hex=${currentColor}&mode=${currentScheme}&count=5`)
.then(response => response.json())
.then(data => {
copyMessage.style.display = 'initial'
for(let i = 0; i < 5; i++) {
let currentSubColor = data.colors[i].hex.value
displayColorContainers[i].style.backgroundColor = currentSubColor
displayHexColorContainers[i].textContent =currentSubColor
// Copy to clipboard feature
displayColorContainers[i].addEventListener('click', function() {
navigator.clipboard.writeText(currentSubColor).then(function() {
copyMessage.textContent = `${currentSubColor} copied to clipboard`
});
})
displayHexColorContainers[i].addEventListener('click', function() {
navigator.clipboard.writeText(currentSubColor).then(function() {
copyMessage.textContent = `${currentSubColor} copied to clipboard`
});
})
}
})
})