-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (50 loc) · 1.81 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
//grab dom elements, seedColor, schemeMode, form
//add submit event listener to form
//create global fetch and trigger fetch
const form = document.getElementById('form');
const colorPicker = document.getElementById('colorPicker');
const modePicker = document.getElementById('modePicker');
const colorScheme = document.getElementById('colorScheme');
let html = '';
const copyColorAndCodes = () => {
const displayedColor = document.querySelectorAll('.color');
const displayedColorCodes = document.querySelectorAll('.color-code');
displayedColor.forEach((e) => {
e.addEventListener('click', () => {
navigator.clipboard.writeText(e.style.backgroundColor);
alert(`Color code: ${e.style.backgroundColor} has been copied`);
});
});
displayedColorCodes.forEach((e) => {
e.addEventListener('click', () => {
navigator.clipboard.writeText(e.textContent);
alert(`Color code: ${e.textContent} has been copied`);
});
});
};
const getColorScheme = () => {
let seedColor = colorPicker.value.replace('#', '');
let schemeMode = modePicker.value;
fetch(
`https://www.thecolorapi.com/scheme?hex=${seedColor}&mode=${schemeMode}`
)
.then((res) => res.json())
.then((data) => {
let colors = '';
data.colors.forEach((e) => {
colors += `
<div class="bg-color">
<div class="color" style="background-color:${e.hex.value}"></div>
<p class="color-code">${e.hex.value}</p>
</div>
`;
});
html = colors;
colorScheme.innerHTML = html;
copyColorAndCodes();
});
};
form.addEventListener('submit', (e) => {
e.preventDefault();
getColorScheme();
});