From 24ac47599b26f93664fbed26b1f450ba0d9f6383 Mon Sep 17 00:00:00 2001 From: jacquikeating Date: Sun, 14 Apr 2024 16:33:06 -0400 Subject: [PATCH] Modularized JS Separated code related to theme switching and copying colour codes to their own files. --- index.css | 31 ++++---------- index.html | 22 +++++----- index.js | 91 +++++++---------------------------------- modules/copy-code.js | 42 +++++++++++++++++++ modules/theme-switch.js | 32 +++++++++++++++ 5 files changed, 107 insertions(+), 111 deletions(-) create mode 100644 modules/copy-code.js create mode 100644 modules/theme-switch.js diff --git a/index.css b/index.css index dade2f4..909334b 100644 --- a/index.css +++ b/index.css @@ -135,7 +135,7 @@ select, input:not(.clr-input) { .input:focus, .input:active { - /* font-weight: 700; */ + font-weight: 700; outline: solid var(--clr-outline) 5px; border: 2px solid var(--clr-border); } @@ -154,7 +154,11 @@ select, input:not(.clr-input) { color: var(--clr-text); pointer-events: none; letter-spacing: 1.75px; - font-weight: 500; + font-weight: 600; +} + +#clr-input:hover ~ #clr-input-text { + font-weight: 800; } .scheme-select { @@ -192,26 +196,6 @@ select, input:not(.clr-input) { position: relative; } -#clr-0{ - background-color: #3EEBD8; -} - -#clr-1 { - background-color: #42CDED; -} - -#clr-2 { - background-color: #459CEE; -} - -#clr-3 { - background-color: #496CF0; -} - -#clr-4 { - background-color: #5B4DF1; -} - .clr-hex-code { width: 100px; padding: 10px; @@ -318,7 +302,6 @@ div > span { position: relative; bottom: 0; right: 0; - font-size: 1.15em; } @keyframes copied { @@ -363,7 +346,7 @@ div > span { border-radius: 6px; text-align: center; } - + button { line-height: 0; } diff --git a/index.html b/index.html index 1fcfb5d..b1c5f94 100644 --- a/index.html +++ b/index.html @@ -37,22 +37,22 @@

Palette

- - + +

+ -

#459CEE

- +

Click anywhere on a colour to copy its hex code!

@@ -64,32 +64,32 @@

Palette

-

#3EEBD8

+

-

#42CDED

+

-

#459CEE

+

-

#496CF0

+

-

#5B4DF1

+

- + diff --git a/index.js b/index.js index 66bc16c..7a5fedb 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,9 @@ -// ELEMENT REFERENCES ----------------------------------------------------------------- +// IMPORTS ---------------------------------------------------------------- +import { lightThemeBtn, darkThemeBtn, applyLightTheme, applyDarkTheme } from './modules/theme-switch.js' +import { copyCode } from './modules/copy-code.js' -const lightThemeBtn = document.getElementById("light-theme-btn") -const darkThemeBtn = document.getElementById("dark-theme-btn") + +// ELEMENT REFERENCES ---------------------------------------------------------------- const clrInput = document.querySelector("#clr-input") const clrInputText = document.querySelector("#clr-input-text") const schemeSelect = document.querySelector(".scheme-select") @@ -9,38 +11,20 @@ const getSchemeBtn = document.querySelector(".get-scheme-btn") const clrDivs = document.querySelectorAll(".clr") - // GLOBAL VARIABLES ----------------------------------------------------------------- - -let seedClrData let clrsArray -let theme = 'dark' let seedClr = clrInput.value.replace(/#/, "") -let schemeType - - -// LISTENERS ----------------------------------------------------------------- +// LISTENERS ------------------------------------------------------------------------ // Selecting dark or light mode "fills in" the corresponding icon and applies the theme -lightThemeBtn.addEventListener("click", function() { - theme = 'light' - lightThemeBtn.innerHTML = `` - darkThemeBtn.innerHTML = `` - applyLightTheme() -}) - -darkThemeBtn.addEventListener("click", function() { - theme = 'dark' - darkThemeBtn.innerHTML = `` - lightThemeBtn.innerHTML = `` - applyDarkTheme() -}) +lightThemeBtn.addEventListener("click", applyLightTheme) +darkThemeBtn.addEventListener("click", applyDarkTheme) // Calls updateClrInput() whenever user chooses a colour, not just when clicking getSchemeBtn clrInput.addEventListener("input", updateClrInput) -// getSchemeBtn triggers a small chain of events, starting with getColourScheme() +// getSchemeBtn triggers getColourScheme() followed by updatePalette() getSchemeBtn.addEventListener("click", getColourScheme) // Triggers copyCode() whenever any colour is clicked @@ -49,7 +33,7 @@ for (let i = 0; i < clrDivs.length; i++) { } -// FUNCTIONS +// FUNCTIONS ------------------------------------------------------------------------- // Displays the hex code of the seed colour each time the user changes it function updateClrInput() { clrInputText.textContent = clrInput.value.toUpperCase() @@ -57,15 +41,14 @@ function updateClrInput() { fetch(`https://www.thecolorapi.com/id?hex=${seedClr}`) .then(res => res.json()) .then(data => { - seedClrData = data - clrInputText.style.color = seedClrData.contrast.value - clrInput.setAttribute("data-contrast", `${seedClrData.contrast.value}`) + clrInputText.style.color = data.contrast.value + clrInput.setAttribute("data-contrast", `${data.contrast.value}`) }) } // Get colour scheme, step 1: Call API to fill clrsArray variable with data function getColourScheme() { - schemeType = document.querySelector(".scheme-select").value + let schemeType = document.querySelector(".scheme-select").value fetch(`https://www.thecolorapi.com/scheme?hex=${seedClr}&mode=${schemeType}&count=5`) .then(res => res.json()) .then(data => { @@ -88,49 +71,5 @@ function updatePalette(){ } } -// Code-copying function 1: Basically just sets up all the arguments the other functions need -function copyCode(e) { - let targetClrDiv = e.target - - if (e.target.tagName === "P") { - targetClrDiv = e.target.parentElement - } - - let targetClrDivId = targetClrDiv.id - let codeToCopy = document.querySelector(`#${targetClrDivId} p`).textContent - let contrastTextClr = targetClrDiv.getAttribute("data-contrast") - tempTextArea(codeToCopy, targetClrDivId, contrastTextClr) -} - -// code-copy 2: Cretes a temporary