Skip to content

Commit 2154647

Browse files
committed
refactor(editor): apply current shade styles to host element
1 parent 65f08ac commit 2154647

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

src/app/editor/editor.component.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<article
2-
#editor
32
class="mx-auto flex w-full max-w-md flex-col gap-4 rounded bg-neutral-100 p-4 shadow-md dark:border dark:border-neutral-600 dark:bg-neutral-700 dark:shadow-none"
43
>
54
<h2 class="font-semibold">{{ color().name }}</h2>

src/app/editor/editor.component.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DIALOG_DATA, DialogRef } from '@angular/cdk/dialog';
22
import { DecimalPipe } from '@angular/common';
3-
import { Component, ElementRef, computed, effect, inject, signal, viewChild } from '@angular/core';
3+
import { Component, computed, effect, inject, signal } from '@angular/core';
44
import { TranslateModule, TranslateService } from '@ngx-translate/core';
55
import { ColorService } from '../shared/data-access/color.service';
66
import { Color, Shade } from '../shared/model';
@@ -33,19 +33,24 @@ export enum UpdateType {
3333
selector: 'rp-editor',
3434
standalone: true,
3535
imports: [ColorInputComponent, TranslateModule, DecimalPipe, ColorRangeSliderComponent],
36-
templateUrl: './editor.component.html'
36+
templateUrl: './editor.component.html',
37+
host: {
38+
'[style.--editor-saturation]': 'shade().hsl.S + "%"',
39+
'[style.--editor-lightness]': 'shade().hsl.L + "%"',
40+
'[style.--editor-hue]': 'shade().hsl.H'
41+
}
3742
})
3843
export class EditorComponent {
3944
protected readonly UpdateType = UpdateType;
4045
protected readonly textColor = textColor;
4146

42-
private readonly _data = inject<EditorData>(DIALOG_DATA);
43-
private readonly _dialogRef = inject(DialogRef);
44-
private readonly _colorService = inject(ColorService);
45-
private readonly _translateService = inject(TranslateService);
47+
readonly #data = inject<EditorData>(DIALOG_DATA);
48+
readonly #dialogRef = inject(DialogRef);
49+
readonly #colorService = inject(ColorService);
50+
readonly #translateService = inject(TranslateService);
4651

47-
protected readonly color = signal(this._data.color.copy());
48-
protected readonly shadeIndex = signal(this._data.shadeIndex ?? 0);
52+
protected readonly color = signal(this.#data.color.copy());
53+
protected readonly shadeIndex = signal(this.#data.shadeIndex ?? 0);
4954

5055
protected readonly shade = computed<Shade>(() => {
5156
const selectedShade = this.color().shades.find((shade) => shade.index === this.shadeIndex());
@@ -62,20 +67,12 @@ export class EditorComponent {
6267
});
6368

6469
protected readonly hasUnsavedChanges = computed<boolean>(
65-
() => this._data.color.toString() !== this.color().toString()
70+
() => this.#data.color.toString() !== this.color().toString()
6671
);
6772

68-
private readonly _editor = viewChild.required<ElementRef<HTMLElement>>('editor');
69-
7073
public constructor() {
7174
effect(() => {
72-
this._editor().nativeElement.style.setProperty('--editor-hue', `${this.shade().hsl.H}`);
73-
this._editor().nativeElement.style.setProperty('--editor-saturation', `${this.shade().hsl.S}%`);
74-
this._editor().nativeElement.style.setProperty('--editor-lightness', `${this.shade().hsl.L}%`);
75-
});
76-
77-
effect(() => {
78-
this._dialogRef.disableClose = this.hasUnsavedChanges();
75+
this.#dialogRef.disableClose = this.hasUnsavedChanges();
7976
});
8077
}
8178

@@ -100,7 +97,7 @@ export class EditorComponent {
10097
}
10198

10299
shade.fixed = false;
103-
this._updateColor();
100+
this.updateColor();
104101
}
105102

106103
public update(type: UpdateType, value: string | number): void {
@@ -122,36 +119,36 @@ export class EditorComponent {
122119
break;
123120
}
124121

125-
this._updateColor();
122+
this.updateColor();
126123

127124
const editedShade = this.color().shades.find((s) => s.hex === shade.hex);
128125
this.shadeIndex.set(editedShade?.index ?? -1);
129126
}
130127

131128
protected cancel(): void {
132-
this._dialogRef.close(undefined);
133-
this.color.set(this._data.color.copy());
129+
this.#dialogRef.close(undefined);
130+
this.color.set(this.#data.color.copy());
134131
}
135132

136133
protected save(): void {
137-
this._dialogRef.close(this.color());
134+
this.#dialogRef.close(this.color());
138135
}
139136

140-
private _updateColor(): void {
137+
private updateColor(): void {
141138
const updatedColor = this.color().copy();
142-
this._colorService.regenerateShades(updatedColor);
139+
this.#colorService.regenerateShades(updatedColor);
143140
this.color.set(updatedColor);
144141
}
145142

146143
protected getTooltip(shade: Shade, selected: boolean): string {
147144
const tooltips: Array<string> = [];
148145

149146
if (!selected) {
150-
tooltips.push(this._translateService.instant('editor.shades'));
147+
tooltips.push(this.#translateService.instant('editor.shades'));
151148
}
152149

153150
if (shade.fixed) {
154-
tooltips.push(this._translateService.instant('editor.unfix'));
151+
tooltips.push(this.#translateService.instant('editor.unfix'));
155152
}
156153

157154
return tooltips.join('\n');

0 commit comments

Comments
 (0)