mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-19 05:59:44 -04:00
Refactor hex colour preview Javascript
This commit improves the code that previews a hex colour when setting up or changing an email branding. Specifically it: - refactors the Javascript to conform to our patterns (module pattern, preprocessed with Gulp) - makes the code work when there are multiple colour previews on one page It also does some visual prettifying, because I couldn’t help myself…
This commit is contained in:
26
app/assets/javascripts/colourPreview.js
Normal file
26
app/assets/javascripts/colourPreview.js
Normal file
@@ -0,0 +1,26 @@
|
||||
(function(Modules) {
|
||||
"use strict";
|
||||
|
||||
let isSixDigitHex = value => value.match(/^#[0-9A-F]{6}$/i);
|
||||
let colourOrWhite = value => isSixDigitHex(value) ? value : '#FFFFFF';
|
||||
|
||||
Modules.ColourPreview = function() {
|
||||
|
||||
this.start = component => {
|
||||
|
||||
this.$input = $('input', component);
|
||||
this.$preview = $('.textbox-colour-preview', component);
|
||||
|
||||
this.$input
|
||||
.on('change keyup', this.update)
|
||||
.trigger('change');
|
||||
|
||||
};
|
||||
|
||||
this.update = () => this.$preview.css(
|
||||
'background', colourOrWhite(this.$input.val())
|
||||
);
|
||||
|
||||
};
|
||||
|
||||
})(window.GOVUK.Modules);
|
||||
@@ -62,11 +62,13 @@
|
||||
|
||||
.textbox-colour-preview {
|
||||
@include media(desktop) {
|
||||
width: 34px;
|
||||
height: 34px;
|
||||
width: 38px;
|
||||
height: 38px;
|
||||
margin-left: 5px;
|
||||
border: 2px solid #0B0C0C;
|
||||
border-radius: 50%;
|
||||
box-shadow: inset 0 0 0 1px rgba($black, 0.2);
|
||||
display: inline-block;
|
||||
vertical-align: top;
|
||||
transition: background 0.3s ease-out;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user