-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preview for color changes in profile edit (#4212)
- Loading branch information
1 parent
1cf125c
commit 81e3f65
Showing
3 changed files
with
122 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const colorField = { | ||
// input type="color" | ||
bgColor: document.getElementById('bg-color-colorfield'), | ||
textColor: document.getElementById('text-color-colorfield'), | ||
}; | ||
|
||
const textField = { | ||
// input type="text" | ||
bgColor: document.getElementById('bg-color-textfield'), | ||
textColor: document.getElementById('text-color-textfield'), | ||
}; | ||
|
||
const preview = document.getElementById('color-select-preview-logo'); | ||
|
||
// Assigns input[type='text'] values to input[type='color'] | ||
function updateColorFields() { | ||
colorField.bgColor.value = textField.bgColor.value; | ||
colorField.textColor.value = textField.textColor.value; | ||
} | ||
|
||
// Updating text fields when color fields are changed | ||
function updateTextFields() { | ||
textField.bgColor.value = colorField.bgColor.value; | ||
textField.textColor.value = colorField.textColor.value; | ||
} | ||
|
||
// Updates Preview Colors | ||
function updatePreview() { | ||
preview.style.backgroundColor = textField.bgColor.value; | ||
preview.style.fill = textField.textColor.value; | ||
} | ||
|
||
// Event Watchers | ||
// When color fields change -> updateTextField values and refresh preview | ||
function watchColorFields() { | ||
updateTextFields(); | ||
updatePreview(); | ||
} | ||
|
||
// When text fields change -> updateColorField values and refresh preview | ||
function watchTextFields(e) { | ||
if (e.target.value.match(/#[0-9a-f]{6}/gi)) { | ||
updateColorFields(); | ||
updatePreview(); | ||
} | ||
} | ||
|
||
function initPreview() { | ||
// Event Listeners | ||
colorField.bgColor.addEventListener('input', watchColorFields); | ||
colorField.textColor.addEventListener('input', watchColorFields); | ||
textField.bgColor.addEventListener('keyup', watchTextFields); | ||
textField.textColor.addEventListener('keyup', watchTextFields); | ||
|
||
// on init | ||
updateColorFields(); | ||
updatePreview(); | ||
preview.style.display = 'inline-block'; | ||
} | ||
|
||
initPreview(); | ||
window.InstantClick.on('change', initPreview); |
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