Skip to content

Commit

Permalink
Trigger a "change" event when closing the color picker
Browse files Browse the repository at this point in the history
The change event wasn't triggered in case the color value input is still focused when the color picker is closed by clicking outside

Fixes #62
  • Loading branch information
mdbassit committed Sep 19, 2022
1 parent 9b741e0 commit 3a9fe9b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/coloris.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@
*/
function closePicker(revert) {
if (currentEl && !settings.inline) {
const prevEl = currentEl;

// Revert the color to the original value if needed
if (revert && oldColor !== currentEl.value) {
currentEl.value = oldColor;
Expand All @@ -421,9 +423,12 @@
currentEl.dispatchEvent(new Event('input', { bubbles: true }));
}

if (oldColor !== currentEl.value) {
currentEl.dispatchEvent(new Event('change', { bubbles: true }));
}
// Trigger a "change" event if needed
setTimeout(() => { // Add this to the end of the event loop
if (oldColor !== prevEl.value) {
prevEl.dispatchEvent(new Event('change', { bubbles: true }));
}
});

// Hide the picker dialog
picker.classList.remove('clr-open');
Expand Down

0 comments on commit 3a9fe9b

Please sign in to comment.