-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#202): Implement copy content and simplify copy popup notifications
- Loading branch information
Showing
4 changed files
with
58 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,28 @@ | ||
let clipboard = new ClipboardJS('.copy-button'); | ||
document.querySelectorAll('.copy-success, .copy-fail').forEach((element) => { | ||
element.style.display = 'none'; | ||
}); | ||
|
||
// Setup copy button icons | ||
const Icons = { | ||
Copy: { | ||
Class: 'icon-copy', | ||
ForegroundColor: '', | ||
BackgroundColor: '', | ||
}, | ||
Success: { | ||
Class: 'icon-check', | ||
ForegroundColor: '', | ||
BackgroundColor: '', | ||
}, | ||
Fail: { | ||
Class: 'icon-x', | ||
ForegroundColor: '', | ||
BackgroundColor: '', | ||
}, | ||
const Notify = { | ||
Success: '.copy-success', | ||
Fail: '.copy-fail', | ||
}; | ||
resetButtons(); | ||
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', resetButtons); | ||
|
||
function resetButtons() { | ||
getIconColors(); | ||
document.querySelectorAll('.copy-button').forEach((btn) => { | ||
setIcon(btn, Icons.Copy); | ||
}); | ||
} | ||
|
||
// Get icon color values from css | ||
function getIconColors() { | ||
console.log('getting colors'); | ||
let element = window.getComputedStyle(document.documentElement); | ||
Object.entries(Icons).forEach(([, icon]) => { | ||
icon.ForegroundColor = element.getPropertyValue(`--${icon.Class}-fg`); | ||
icon.BackgroundColor = element.getPropertyValue(`--${icon.Class}-bg`); | ||
}); | ||
} | ||
|
||
function setIcon(btn, icon) { | ||
for (let child of btn.children) { | ||
console.log(child.classList.contains(icon.Class)); | ||
if (child.classList.contains(icon.Class)) { | ||
child.style.display = 'inline-block'; | ||
btn.style.setProperty('--copy-button-fg', icon.ForegroundColor); | ||
btn.style.setProperty('--copy-button-bg', icon.BackgroundColor); | ||
} else { | ||
child.style.display = 'none'; | ||
} | ||
} | ||
} | ||
let clipboard = new ClipboardJS('.copy-button'); | ||
|
||
//TODO: Implement success callback | ||
clipboard.on('success', function (e) { | ||
console.info('Trigger:', e.trigger); | ||
const btn = e.trigger; | ||
|
||
console.info(Icons); | ||
showNotification(e.trigger, Notify.Success); | ||
}); | ||
|
||
setIcon(btn, Icons.Success); | ||
clipboard.on('error', function (e) { | ||
showNotification(e.trigger, Notify.Fail); | ||
}); | ||
|
||
function showNotification(btn, notify) { | ||
const notificationElement = btn.parentElement.querySelector(notify); | ||
notificationElement.style.display = 'flex'; | ||
setTimeout(() => { | ||
setIcon(btn, Icons.Copy); | ||
notificationElement.style.display = 'none'; | ||
}, 2000); | ||
}); | ||
|
||
//TODO: Implement failure callback | ||
clipboard.on('error', function (e) { | ||
console.log('Copy failed!'); | ||
console.error('Trigger:', e.trigger); | ||
}); | ||
} | ||
|
||
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