-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated code related to theme switching and copying colour codes to their own files.
- Loading branch information
1 parent
2d51373
commit 24ac475
Showing
5 changed files
with
107 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// EXPORT ----------------------------------------------------------------------------- | ||
export { copyCode, tempTextArea, showCopiedSpan } | ||
|
||
|
||
// FUNCTIONS -------------------------------------------------------------------------- | ||
// 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 <textarea> so we can put our text in it and then copy from that. | ||
// navigator.clipboard.writeText would be better, but it wasn't working in the Scrimba editor | ||
function tempTextArea(codeToCopy, targetClrDivId, contrastTextClr) { | ||
const textArea = document.createElement('textarea') | ||
textArea.value = codeToCopy | ||
textArea.style.opacity = 0 | ||
document.body.appendChild(textArea) | ||
textArea.focus() | ||
textArea.select() | ||
document.execCommand('copy') | ||
document.body.removeChild(textArea) | ||
|
||
showCopiedSpan(codeToCopy, targetClrDivId, contrastTextClr) | ||
} | ||
|
||
// Code-copy step 3: display feedback to user | ||
function showCopiedSpan(codeToCopy, targetClrDivId, contrastTextClr) { | ||
const copiedSpan = document.querySelector(`#${targetClrDivId}-span`) | ||
copiedSpan.classList.add("copied") | ||
copiedSpan.style.color = contrastTextClr | ||
copiedSpan.innerHTML = `${codeToCopy} <br> copied!` | ||
setTimeout(function(){copiedSpan.classList.remove('copied')}, 650) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// EXPORT ----------------------------------------------------------------------------- | ||
export { lightThemeBtn, darkThemeBtn, applyLightTheme, applyDarkTheme } | ||
|
||
|
||
// ELEMENT REFERENCES ----------------------------------------------------------------- | ||
const lightThemeBtn = document.getElementById("light-theme-btn") | ||
const darkThemeBtn = document.getElementById("dark-theme-btn") | ||
|
||
|
||
// GLOBAL VARIABLE(S) ---------------------------------------------------------------- | ||
let theme = 'dark' | ||
|
||
|
||
// LISTENERS -------------------------------------------------------------------------- | ||
lightThemeBtn.addEventListener("click", applyLightTheme) | ||
darkThemeBtn.addEventListener("click", applyDarkTheme) | ||
|
||
|
||
// FUNCTIONS -------------------------------------------------------------------------- | ||
function applyLightTheme() { | ||
theme = 'light' | ||
lightThemeBtn.innerHTML = `<i class="fa-solid fa-sun"></i>` | ||
darkThemeBtn.innerHTML = `<i class="fa-regular fa-moon"></i>` | ||
document.querySelector("html").classList.add("light-theme") | ||
} | ||
|
||
function applyDarkTheme() { | ||
theme = 'dark' | ||
darkThemeBtn.innerHTML = `<i class="fa-solid fa-moon"></i>` | ||
lightThemeBtn.innerHTML = `<i class="fa-regular fa-sun"></i>` | ||
document.querySelector("html").classList.remove("light-theme") | ||
} |