Skip to content

Commit

Permalink
Feeds color back into modal component when editing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Andrews committed Aug 14, 2019
1 parent 7546da8 commit 6b458bb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/assets/components/color-block-component.js
Expand Up @@ -7,18 +7,29 @@ class ColorBlockComponent extends HTMLElement {
this._deleteButton = this.querySelector('.js-delete-button');
this._editButton = this.querySelector('.js-edit-button');
this._colorButton = this.querySelector('.js-color-button');
this._color = '#000000';
this._modalTemplate = document.body.querySelector('[tag="color-selector-modal"]');
}
removeColor() {
this.remove();
}
editColor() {
const modal = document.importNode(this._modalTemplate.content, true);
document.body.appendChild(modal);
const modalComponent = document.body.querySelector('color-modal-component');
modalComponent.setInitialColor(this._color);
}
activateColor() {
}
connectedCallback() {
this._deleteButton.addEventListener('click', this.handleDeleteClick);
this._editButton.addEventListener('click', this.handleEditClick);
this._colorButton.addEventListener('click', this.handleColorClick);
const colorPreview = this.querySelector('custom-color-preview');
const cleanValue = colorPreview.style.backgroundColor.replace(/[rgb\(\)\s]/gi, '');
const rgbValue = cleanValue.split(',');
this._color = `#${convert.rgb.hex(rgbValue)}`;
colorPreview.dataset.color = this._color;
}
disconnectedCallback() {
this._deleteButton.removeEventListener('click', this.handleDeleteClick);
Expand Down
10 changes: 10 additions & 0 deletions docs/assets/components/color-modal-component.js
Expand Up @@ -19,6 +19,16 @@ class ColorModalComponent extends HTMLElement {
this._hslInput = this.querySelector('input[data-type="hsl"]');
this._pallet = document.body.querySelector('color-pallet-component');
}
setInitialColor(color) {
const hex = color;
const rgb = convert.hex.rgb(color);
const hsl = convert.hex.hsl(color);
this._hexInput.value = hex;
this._rgbInput.value = `rgb(${rgb})`;
this._hslInput.value = `hsl(${hsl[0]}, ${hsl[1]}%, ${hsl[2]}%)`;
this._colorInput.value = `#${hex}`;
this._colorPreview.style.backgroundColor = `rgb(${rgb})`;
}
addColor(e) {
e.preventDefault();
const color = this._colorInput.value.replace('#', '');
Expand Down
15 changes: 14 additions & 1 deletion src/color-block-component.ts
Expand Up @@ -3,13 +3,17 @@ class ColorBlockComponent extends HTMLElement
private _deleteButton : HTMLButtonElement;
private _editButton : HTMLButtonElement;
private _colorButton : HTMLElement;
private _color : string;
private _modalTemplate : HTMLTemplateElement;

constructor()
{
super();
this._deleteButton = this.querySelector('.js-delete-button');
this._editButton = this.querySelector('.js-edit-button');
this._colorButton = this.querySelector('.js-color-button');
this._color = '#000000';
this._modalTemplate = document.body.querySelector('[tag="color-selector-modal"]');
}

private handleDeleteClick:EventListener = this.removeColor.bind(this);
Expand All @@ -23,7 +27,10 @@ class ColorBlockComponent extends HTMLElement

private editColor() : void
{

const modal = document.importNode(this._modalTemplate.content, true);
document.body.appendChild(modal);
const modalComponent = document.body.querySelector('color-modal-component') as ColorModalComponent;
modalComponent.setInitialColor(this._color);
}

private activateColor() : void
Expand All @@ -36,6 +43,12 @@ class ColorBlockComponent extends HTMLElement
this._deleteButton.addEventListener('click', this.handleDeleteClick);
this._editButton.addEventListener('click', this.handleEditClick);
this._colorButton.addEventListener('click', this.handleColorClick);

const colorPreview = this.querySelector('custom-color-preview') as HTMLElement;
const cleanValue = colorPreview.style.backgroundColor.replace(/[rgb\(\)\s]/gi, '');
const rgbValue = cleanValue.split(',');
this._color = `#${ convert.rgb.hex(rgbValue) }`;
colorPreview.dataset.color = this._color;
}

disconnectedCallback()
Expand Down
13 changes: 13 additions & 0 deletions src/color-modal-component.ts
Expand Up @@ -39,6 +39,19 @@ class ColorModalComponent extends HTMLElement
private handleInputKeyup:EventListener = this.detectColor.bind(this);
private handleColorChange:EventListener = this.changeColor.bind(this);

public setInitialColor(color:string) : void
{
const hex = color;
const rgb = convert.hex.rgb(color);
const hsl = convert.hex.hsl(color);

this._hexInput.value = hex;
this._rgbInput.value = `rgb(${ rgb })`;
this._hslInput.value = `hsl(${ hsl[0] }, ${ hsl[1] }%, ${ hsl[2] }%)`;
this._colorInput.value = `#${ hex }`;
this._colorPreview.style.backgroundColor = `rgb(${ rgb })`;
}

private addColor(e:Event) : void
{
e.preventDefault();
Expand Down
1 change: 0 additions & 1 deletion src/new-color-component.ts
@@ -1,6 +1,5 @@
class NewColorComponent extends HTMLElement
{

private _modalTemplate : HTMLTemplateElement;

constructor()
Expand Down

0 comments on commit 6b458bb

Please sign in to comment.