From ec3ae132dddfea0a944e5f1285267953acf3e598 Mon Sep 17 00:00:00 2001 From: Stefan Dragomir Date: Wed, 20 Sep 2023 15:03:13 +0300 Subject: [PATCH] Add labels to Color Picker swatches --- .../src/scripts/ColorInput.js | 2 ++ .../src/scripts/ColorInputSwatch.js | 10 ++++++++++ coral-component-colorinput/src/styles/index.styl | 15 ++++++++++++--- .../src/templates/base.html | 3 ++- .../src/templates/colorLabel.html | 1 + 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 coral-component-colorinput/src/templates/colorLabel.html diff --git a/coral-component-colorinput/src/scripts/ColorInput.js b/coral-component-colorinput/src/scripts/ColorInput.js index 9b9d3ccab6..1962d9fc30 100644 --- a/coral-component-colorinput/src/scripts/ColorInput.js +++ b/coral-component-colorinput/src/scripts/ColorInput.js @@ -814,6 +814,8 @@ const ColorInput = Decorator(class extends BaseFormField(BaseComponent(HTMLEleme this._elements.colorPreview.style.backgroundColor = currentColor ? currentColor.rgbaValue : ''; this.classList.toggle('_coral-ColorInput--novalue', isValueEmpty); + this._elements.colorPreviewLabel.textContent = currentColor ? this._color.value : ''; + // Update preview in overlay const preview = this._elements.overlay.querySelector('._coral-ColorInput-preview'); if (preview) { diff --git a/coral-component-colorinput/src/scripts/ColorInputSwatch.js b/coral-component-colorinput/src/scripts/ColorInputSwatch.js index 2247ceae35..67d0378066 100644 --- a/coral-component-colorinput/src/scripts/ColorInputSwatch.js +++ b/coral-component-colorinput/src/scripts/ColorInputSwatch.js @@ -15,6 +15,7 @@ import BaseColorInputAbstractSubview from './BaseColorInputAbstractSubview'; import Color from './Color'; import '../../../coral-component-button'; import colorButton from '../templates/colorButton'; +import colorLabel from '../templates/colorLabel'; import {commons, i18n, transform} from '../../../coral-utils'; import {Decorator} from '../../../coral-decorator'; @@ -39,6 +40,7 @@ const ColorInputSwatch = Decorator(class extends BaseColorInputAbstractSubview(B // Templates this._elements = {}; colorButton.call(this._elements); + colorLabel.call(this._elements); } /** @@ -107,10 +109,12 @@ const ColorInputSwatch = Decorator(class extends BaseColorInputAbstractSubview(B if (cssColorValue) { this._elements.colorButton.style.backgroundColor = cssColorValue; this._elements.colorButton.label.textContent = hexColorValue; + this._elements.colorLabel.textContent = hexColorValue; this.setAttribute('data-value', hexColorValue); } else { this._elements.colorButton.classList.add('_coral-ColorInput-swatch-novalue'); this._elements.colorButton.label.textContent = i18n.get('unset'); + this._elements.colorLabel.textContent = i18n.get('unset');; this.setAttribute('data-value', ''); } } @@ -192,7 +196,13 @@ const ColorInputSwatch = Decorator(class extends BaseColorInputAbstractSubview(B button.remove(); } + const label = this.querySelector('[handle="colorLabel"]'); + if (label) { + label.remove(); + } + this.appendChild(this._elements.colorButton); + this.appendChild(this._elements.colorLabel); } }); diff --git a/coral-component-colorinput/src/styles/index.styl b/coral-component-colorinput/src/styles/index.styl index 7b04ded686..0e4d3f77d8 100644 --- a/coral-component-colorinput/src/styles/index.styl +++ b/coral-component-colorinput/src/styles/index.styl @@ -50,6 +50,9 @@ $colorinput-button-size = 28px; $colorinput-input-swatch-size = 38px; +$colorinput-swatch-preview-width = 44px; +$colorinput-swatch-preview-height = 57px; + $colorinput-slider-width = 160px; $colorinput-slider-margin = 5px 16px 0 16px; @@ -141,6 +144,7 @@ coral-colorinput-item { ._coral-ColorInput-swatch { display: block; float: left; + text-align: center; padding: $colorinput-swatch-top-padding $colorinput-swatch-left-right-padding 0; /* All buttons inside the colorinput are square */ @@ -185,12 +189,17 @@ coral-colorinput-item { } } } + + label { + display block; + } } ._coral-ColorInput-buttonWrapper { position: absolute; right: $colorinput-button-wrapper-offset; top: $colorinput-button-wrapper-offset; + text-align: center; // set opacity < 1 to create new stacking context (for checker image to work) opacity: .999; @@ -233,11 +242,11 @@ coral-colorinput-item { // Swatch variant ._coral-ColorInput--swatch { - width: $colorinput-input-swatch-size; - height: $colorinput-input-swatch-size; + width: $colorinput-swatch-preview-width; + height: $colorinput-swatch-preview-height; // makes sure the width stays no matter the parent's configuration - max-width: $colorinput-input-swatch-size; + max-width: $colorinput-swatch-preview-width; ._coral-ColorInput-input { position: absolute; diff --git a/coral-component-colorinput/src/templates/base.html b/coral-component-colorinput/src/templates/base.html index 5e1eb767d3..0b6719eb99 100644 --- a/coral-component-colorinput/src/templates/base.html +++ b/coral-component-colorinput/src/templates/base.html @@ -21,6 +21,7 @@ - + diff --git a/coral-component-colorinput/src/templates/colorLabel.html b/coral-component-colorinput/src/templates/colorLabel.html new file mode 100644 index 0000000000..0aa47cf24c --- /dev/null +++ b/coral-component-colorinput/src/templates/colorLabel.html @@ -0,0 +1 @@ +