-
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 success / failure popup notifications
- Loading branch information
Showing
4 changed files
with
97 additions
and
6 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,5 +1,72 @@ | ||
var clipboard = new ClipboardJS('.copy-button'); | ||
let clipboard = new ClipboardJS('.copy-button'); | ||
|
||
// Setup copy button icons | ||
const Icons = { | ||
Copy: { | ||
Class: 'icon-copy', | ||
ForegroundColor: '', | ||
BackgroundColor: '', | ||
}, | ||
Success: { | ||
Class: 'icon-check', | ||
ForegroundColor: '', | ||
BackgroundColor: '', | ||
}, | ||
Fail: { | ||
Class: 'icon-x', | ||
ForegroundColor: '', | ||
BackgroundColor: '', | ||
}, | ||
}; | ||
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'; | ||
} | ||
} | ||
} | ||
|
||
//TODO: Implement success callback | ||
clipboard.on('success', function (e) { | ||
console.info('Trigger:', e.trigger); | ||
const btn = e.trigger; | ||
|
||
console.info(Icons); | ||
|
||
setIcon(btn, Icons.Success); | ||
|
||
setTimeout(() => { | ||
setIcon(btn, Icons.Copy); | ||
}, 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