@libs-ui/components-color-picker 0.2.356-9 → 0.2.357-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,31 +1,31 @@
1
1
  # @libs-ui/components-color-picker
2
2
 
3
- > Component chọn màu sắc với giao diện trực quan, hỗ trợ nhiều định dạng màu và alpha channel
3
+ > Component chọn màu sắc canvas-based với hỗ trợ đầy đủ HSL, RGB, HEX và alpha channel.
4
4
 
5
5
  ## Giới thiệu
6
6
 
7
- `LibsUiComponentsColorPickerComponent` là một standalone Angular component để chọn màu sắc một cách trực quan thông qua giao diện canvas-based color picker.
7
+ `LibsUiComponentsColorPickerComponent` là một standalone Angular component cung cấp giao diện color picker trực quan dựa trên HTML5 Canvas. Component cho phép người dùng chọn màu qua thanh Hue, vùng Saturation/Lightness, thanh Alpha và các ô nhập liệu RGB/HSL/HEX. Mọi thay đổi màu sắc được emit theo thời gian thực ở nhiều định dạng khác nhau.
8
8
 
9
- ### Tính năng
9
+ ## Tính năng
10
10
 
11
- - Canvas-based color picker với Hue, Saturation/Lightness bars
12
- - Hỗ trợ Alpha channel (độ trong suốt)
13
- - Nhiều định dạng output (RGB, RGBA, HSL, HSLA, HEX, HEX with alpha)
14
- - Input fields cho từng giá trị màu (R, G, B, H, S, L, Alpha, HEX)
15
- - Preview màu real-time
16
- - Copy mã màu nhanh chóng
17
- - Tùy chỉnh kích thước bars hiển thị fields
18
- - OnPush Change Detection
19
- - Angular Signals
20
- - Standalone Component
11
+ - Canvas-based color picker với thanh Hue và vùng Saturation/Lightness
12
+ - Hỗ trợ Alpha channel (độ trong suốt 0–1)
13
+ - Emit màu ở nhiều định dạng: HEX, RGB, RGBA, HSL, HSLA, alpha
14
+ - Input fields cho từng kênh màu (R, G, B, H, S, L, Alpha, HEX)
15
+ - Preview màu real-time
16
+ - Nút copy mã màu HEX vào clipboard
17
+ - Tùy chỉnh kích thước các thanh color bar
18
+ - Bật/tắt từng nhóm input fields (RGB, HSL, HEX, Alpha)
19
+ - Kiểm soát có emit event khi khởi tạo component hay không
20
+ - OnPush Change Detection, Angular Signals, Standalone Component
21
21
 
22
22
  ## Khi nào sử dụng
23
23
 
24
24
  - Khi cần cho phép người dùng chọn màu sắc một cách trực quan
25
- - Khi cần hỗ trợ nhiều định dạng màu (RGB, HSL, HEX)
25
+ - Khi cần hỗ trợ nhiều định dạng màu (RGB, HSL, HEX) đồng thời
26
26
  - Khi cần điều chỉnh độ trong suốt (alpha channel)
27
- - Khi cần preview màu đã chọn real-time
28
- - Khi cần copy màu nhanh chóng
27
+ - Khi cần preview màu đã chọn real-time trước khi áp dụng
28
+ - Khi cần tùy chỉnh kích thước giao diện color picker để phù hợp với layout
29
29
 
30
30
  ## Cài đặt
31
31
 
@@ -33,148 +33,274 @@
33
33
  npm install @libs-ui/components-color-picker
34
34
  ```
35
35
 
36
- ## Sử dụng cơ bản
36
+ ## Import
37
37
 
38
38
  ```typescript
39
- import { Component } from '@angular/core';
39
+ import { LibsUiComponentsColorPickerComponent } from '@libs-ui/components-color-picker';
40
+
41
+ // Interfaces và types nếu cần
42
+ import {
43
+ IPickerCustomOptions,
44
+ IOutputColorChangeMultipleType,
45
+ } from '@libs-ui/components-color-picker';
46
+ ```
47
+
48
+ ## Ví dụ sử dụng
49
+
50
+ ### Cơ bản — Lắng nghe màu đã chọn dạng HEX
51
+
52
+ ```typescript
53
+ import { Component, signal } from '@angular/core';
40
54
  import { LibsUiComponentsColorPickerComponent } from '@libs-ui/components-color-picker';
41
55
 
42
56
  @Component({
43
- selector: 'app-example',
57
+ selector: 'app-basic-color-picker',
44
58
  standalone: true,
59
+ changeDetection: ChangeDetectionStrategy.OnPush,
45
60
  imports: [LibsUiComponentsColorPickerComponent],
46
61
  template: `
47
- <libs_ui-components-color_picker (outColorChange)="onColorChange($event)" />
62
+ <libs_ui-components-color_picker
63
+ (outColorChange)="handlerColorChange($event)"
64
+ />
65
+ <p>Màu đã chọn: <code>{{ selectedColor() }}</code></p>
48
66
  `,
49
67
  })
50
- export class ExampleComponent {
51
- onColorChange(color: string): void {
52
- console.log('Selected color:', color); // "#3b82f6"
68
+ export class BasicColorPickerComponent {
69
+ protected selectedColor = signal<string>('#ff0000');
70
+
71
+ protected handlerColorChange(event: string): void {
72
+ event.stopPropagation?.();
73
+ this.selectedColor.set(event);
53
74
  }
54
75
  }
55
76
  ```
56
77
 
57
- ## API
58
-
59
- ### Inputs
60
-
61
- | Property | Type | Default | Description |
62
- | ------------------------------------- | ---------------------- | ----------- | --------------------------------------- |
63
- | `[customOptions]` | `IPickerCustomOptions` | `undefined` | Tùy chỉnh options cho color picker |
64
- | `[noEmitEventColorWhenInitComponent]` | `boolean` | `true` | Không emit event khi khởi tạo component |
78
+ ### Với custom options — Alpha channel và HSL ẩn
65
79
 
66
- ### Outputs
67
-
68
- | Property | Type | Description |
69
- | ------------------------------ | -------------------------------- | ------------------------------------- |
70
- | `(outColorChange)` | `string` | Emit màu đã chọn dưới dạng HEX string |
71
- | `(outColorChangeMultipleType)` | `IOutputColorChangeMultipleType` | Emit màu với nhiều format |
72
-
73
- ## Interfaces
80
+ ```typescript
81
+ import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
82
+ import {
83
+ IOutputColorChangeMultipleType,
84
+ IPickerCustomOptions,
85
+ LibsUiComponentsColorPickerComponent,
86
+ } from '@libs-ui/components-color-picker';
74
87
 
75
- ### IPickerCustomOptions
88
+ @Component({
89
+ selector: 'app-custom-options-color-picker',
90
+ standalone: true,
91
+ changeDetection: ChangeDetectionStrategy.OnPush,
92
+ imports: [LibsUiComponentsColorPickerComponent],
93
+ template: `
94
+ <libs_ui-components-color_picker
95
+ [customOptions]="pickerOptions()"
96
+ (outColorChange)="handlerColorChange($event)"
97
+ (outColorChangeMultipleType)="handlerColorChangeMultiple($event)"
98
+ />
99
+ @if (colorOutput()) {
100
+ <ul>
101
+ <li>HEX: {{ colorOutput()!.hex }}</li>
102
+ <li>RGB: {{ colorOutput()!.rgb }}</li>
103
+ <li>RGBA: {{ colorOutput()!.rgba }}</li>
104
+ <li>HSL: {{ colorOutput()!.hsl }}</li>
105
+ <li>Alpha: {{ colorOutput()!.alpha }}</li>
106
+ </ul>
107
+ }
108
+ `,
109
+ })
110
+ export class CustomOptionsColorPickerComponent {
111
+ protected pickerOptions = signal<IPickerCustomOptions>({
112
+ color: '#ff6b6b',
113
+ showAlpha: true,
114
+ showHex: true,
115
+ showRgb: true,
116
+ showHsl: false,
117
+ format: 'hex',
118
+ });
76
119
 
77
- ```typescript
78
- export interface IPickerCustomOptions {
79
- slBarSize?: Array<number>; // Kích thước Saturation/Lightness bar [width, height]
80
- hueBarSize?: Array<number>; // Kích thước Hue bar [width, height]
81
- alphaBarSize?: Array<number>; // Kích thước Alpha bar [width, height]
82
- showHsl?: boolean; // Hiển thị HSL input fields
83
- showRgb?: boolean; // Hiển thị RGB input fields
84
- showHex?: boolean; // Hiển thị HEX input field
85
- showAlpha?: boolean; // Hiển thị Alpha slider
86
- color?: string | Array<number>; // Màu mặc định
87
- format?: 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hex' | 'color';
88
- }
89
- ```
120
+ protected colorOutput = signal<IOutputColorChangeMultipleType | null>(null);
90
121
 
91
- ### IOutputColorChangeMultipleType
122
+ protected handlerColorChange(event: string): void {
123
+ event.stopPropagation?.();
124
+ }
92
125
 
93
- ```typescript
94
- export interface IOutputColorChangeMultipleType {
95
- rgba: string; // "rgba(59, 130, 246, 1)"
96
- rgb: string; // "rgb(59, 130, 246)"
97
- hex: string; // "#3b82f6" hoặc "#3b82f6ff" (nếu có alpha)
98
- hsl: string; // "hsl(217, 91, 60)"
99
- hsla: string; // "hsla(217, 91, 60, 1)"
100
- alpha: number; // 0-1
126
+ protected handlerColorChangeMultiple(event: IOutputColorChangeMultipleType): void {
127
+ this.colorOutput.set(event);
128
+ }
101
129
  }
102
130
  ```
103
131
 
104
- ## dụ nâng cao
105
-
106
- ### Với Custom Options
132
+ ### Tùy chỉnh kích thước bars — Picker nhỏ hơn
107
133
 
108
134
  ```typescript
109
- import { Component, signal } from '@angular/core';
110
- import { LibsUiComponentsColorPickerComponent, IPickerCustomOptions, IOutputColorChangeMultipleType } from '@libs-ui/components-color-picker';
135
+ import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
136
+ import {
137
+ IPickerCustomOptions,
138
+ LibsUiComponentsColorPickerComponent,
139
+ } from '@libs-ui/components-color-picker';
111
140
 
112
141
  @Component({
142
+ selector: 'app-compact-color-picker',
113
143
  standalone: true,
144
+ changeDetection: ChangeDetectionStrategy.OnPush,
114
145
  imports: [LibsUiComponentsColorPickerComponent],
115
146
  template: `
116
147
  <libs_ui-components-color_picker
117
- [customOptions]="pickerOptions()"
118
- (outColorChange)="onColorChange($event)"
119
- (outColorChangeMultipleType)="onColorChangeMultiple($event)" />
148
+ [customOptions]="compactOptions()"
149
+ (outColorChange)="handlerColorChange($event)"
150
+ />
120
151
  `,
121
152
  })
122
- export class ExampleComponent {
123
- readonly pickerOptions = signal<IPickerCustomOptions>({
124
- color: '#ff6b6b',
153
+ export class CompactColorPickerComponent {
154
+ protected compactOptions = signal<IPickerCustomOptions>({
155
+ slBarSize: [300, 150],
156
+ hueBarSize: [300, 16],
157
+ alphaBarSize: [300, 16],
125
158
  showAlpha: true,
126
159
  showHex: true,
127
160
  showRgb: true,
128
161
  showHsl: false,
129
- format: 'hex',
162
+ color: '#3b82f6',
130
163
  });
131
164
 
132
- onColorChange(color: string): void {
133
- console.log('HEX:', color);
165
+ protected handlerColorChange(event: string): void {
166
+ event.stopPropagation?.();
134
167
  }
168
+ }
169
+ ```
170
+
171
+ ### Emit màu ngay khi khởi tạo component
172
+
173
+ ```typescript
174
+ import { ChangeDetectionStrategy, Component } from '@angular/core';
175
+ import { LibsUiComponentsColorPickerComponent } from '@libs-ui/components-color-picker';
135
176
 
136
- onColorChangeMultiple(colors: IOutputColorChangeMultipleType): void {
137
- console.log('RGB:', colors.rgb);
138
- console.log('HSL:', colors.hsl);
139
- console.log('Alpha:', colors.alpha);
177
+ @Component({
178
+ selector: 'app-emit-on-init-color-picker',
179
+ standalone: true,
180
+ changeDetection: ChangeDetectionStrategy.OnPush,
181
+ imports: [LibsUiComponentsColorPickerComponent],
182
+ template: `
183
+ <libs_ui-components-color_picker
184
+ [noEmitEventColorWhenInitComponent]="false"
185
+ (outColorChange)="handlerColorChange($event)"
186
+ />
187
+ `,
188
+ })
189
+ export class EmitOnInitColorPickerComponent {
190
+ protected handlerColorChange(event: string): void {
191
+ event.stopPropagation?.();
192
+ // Sẽ được gọi ngay khi component khởi tạo
140
193
  }
141
194
  }
142
195
  ```
143
196
 
144
- ### Custom Bar Sizes
197
+ ## @Input()
198
+
199
+ | Input | Type | Default | Mô tả | Ví dụ |
200
+ |---|---|---|---|---|
201
+ | `[customOptions]` | `IPickerCustomOptions` | `undefined` | Tùy chỉnh toàn bộ options của color picker: kích thước bars, màu mặc định, format parse, hiển thị các nhóm input | `[customOptions]="{ color: '#ff0000', showAlpha: true }"` |
202
+ | `[noEmitEventColorWhenInitComponent]` | `boolean` | `true` | Không emit event `outColorChange` và `outColorChangeMultipleType` khi component khởi tạo lần đầu. Set `false` để emit ngay khi init | `[noEmitEventColorWhenInitComponent]="false"` |
203
+
204
+ ## @Output()
205
+
206
+ | Output | Type | Mô tả | Handler TS | Binding HTML |
207
+ |---|---|---|---|---|
208
+ | `(outColorChange)` | `string` | Emit màu đã chọn dưới dạng HEX string (có alpha nếu `showAlpha: true`). Ví dụ: `"#3b82f6"` hoặc `"#3b82f6ff"` | `handlerColorChange(event: string): void { event.stopPropagation?.(); this.color.set(event); }` | `(outColorChange)="handlerColorChange($event)"` |
209
+ | `(outColorChangeMultipleType)` | `IOutputColorChangeMultipleType` | Emit màu đã chọn ở tất cả các định dạng cùng lúc: hex, rgb, rgba, hsl, hsla và giá trị alpha số | `handlerColorChangeMultiple(event: IOutputColorChangeMultipleType): void { event.stopPropagation?.(); this.colorOutput.set(event); }` | `(outColorChangeMultipleType)="handlerColorChangeMultiple($event)"` |
210
+
211
+ ## Types & Interfaces
145
212
 
146
213
  ```typescript
147
- readonly customSizeOptions = signal<IPickerCustomOptions>({
148
- slBarSize: [300, 200], // Larger SL bar
149
- hueBarSize: [300, 20], // Wider hue bar
150
- alphaBarSize: [300, 20], // Wider alpha bar
151
- showAlpha: true
152
- });
214
+ import {
215
+ IPickerCustomOptions,
216
+ IOutputColorChangeMultipleType,
217
+ } from '@libs-ui/components-color-picker';
153
218
  ```
154
219
 
155
- ## Important Notes
220
+ ### IPickerCustomOptions
156
221
 
157
- ⚠️ **Canvas Rendering**: Component sử dụng HTML5 Canvas để render color picker, cần browser hỗ trợ Canvas API.
222
+ Interface để tùy chỉnh giao diện hành vi của color picker. Tất cả các field đều là optional — chỉ truyền những gì muốn override so với giá trị mặc định.
158
223
 
159
- ⚠️ **Signal-based**: Component sử dụng Angular Signals để quản lý state, đảm bảo reactivity tốt.
224
+ ```typescript
225
+ interface IPickerCustomOptions {
226
+ /** Kích thước vùng Saturation/Lightness [width, height]. Default: [420, 200] */
227
+ slBarSize?: Array<number>;
160
228
 
161
- ⚠️ **Multiple Formats**: Hỗ trợ nhiều format output (RGB, RGBA, HSL, HSLA, HEX, HEX with alpha).
229
+ /** Kích thước thanh Hue [width, height]. Default: [420, 20] */
230
+ hueBarSize?: Array<number>;
162
231
 
163
- ⚠️ **Custom Options**: thể customize kích thước bars, hiển thị/ẩn các input fields.
232
+ /** Kích thước thanh Alpha [width, height]. Default: [420, 20] */
233
+ alphaBarSize?: Array<number>;
164
234
 
165
- ⚠️ **Event Timing**: Mặc định không emit event khi khởi tạo component (`noEmitEventColorWhenInitComponent = true`). Set thành `false` nếu muốn emit event ngay khi init.
235
+ /** Hiển thị nhóm input HSL (H, S, L). Default: false */
236
+ showHsl?: boolean;
166
237
 
167
- ## Dependencies
238
+ /** Hiển thị nhóm input RGB (R, G, B). Default: true */
239
+ showRgb?: boolean;
168
240
 
169
- - `@angular/core` >= 18.0.0
170
- - `@libs-ui/components-inputs-input`
171
- - `@libs-ui/services-notification`
172
- - `@libs-ui/utils`
241
+ /** Hiển thị ô nhập HEX. Default: true */
242
+ showHex?: boolean;
173
243
 
174
- ## Demo
244
+ /** Hiển thị thanh và ô nhập Alpha. Default: false */
245
+ showAlpha?: boolean;
246
+
247
+ /** Màu khởi tạo ban đầu — nhận HEX string, CSS color name, hoặc mảng [r, g, b] / [r, g, b, a]. Default: '#ff0000' */
248
+ color?: string | Array<number>;
175
249
 
176
- Xem demo tại: [http://localhost:4500/color-picker](http://localhost:4500/color-picker)
250
+ /**
251
+ * Format dùng để parse giá trị color input ban đầu khi gọi handlerColorChange nội bộ.
252
+ * - 'color': Tự động parse từ nhiều format (mặc định)
253
+ * - 'rgb' / 'rgba': Parse từ mảng [r, g, b] / [r, g, b, a]
254
+ * - 'hsl' / 'hsla': Parse từ mảng [h, s, l] / [h, s, l, a]
255
+ * - 'hex': Parse từ HEX string
256
+ * Default: 'color'
257
+ */
258
+ format?: 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hex' | 'color';
259
+ }
260
+ ```
177
261
 
178
- ## License
262
+ ### IOutputColorChangeMultipleType
263
+
264
+ Interface của object được emit qua `outColorChangeMultipleType`. Chứa màu đã chọn ở mọi định dạng phổ biến.
265
+
266
+ ```typescript
267
+ interface IOutputColorChangeMultipleType {
268
+ /** HEX string. Ví dụ: "#3b82f6" hoặc "#3b82f6ff" khi có alpha */
269
+ hex: string;
270
+
271
+ /** CSS rgb(). Ví dụ: "rgb(59,130,246)" */
272
+ rgb: string;
273
+
274
+ /** CSS rgba(). Ví dụ: "rgba(59,130,246,1)" */
275
+ rgba: string;
276
+
277
+ /** CSS hsl(). Ví dụ: "hsl(217,91,60)" */
278
+ hsl: string;
279
+
280
+ /** CSS hsla(). Ví dụ: "hsla(217,91,60,1)" */
281
+ hsla: string;
282
+
283
+ /** Giá trị alpha trong khoảng 0–1. Ví dụ: 0.75 */
284
+ alpha: number;
285
+ }
286
+ ```
287
+
288
+ ## Lưu ý quan trọng
289
+
290
+ ⚠️ **Canvas API bắt buộc**: Component dùng HTML5 Canvas để render vùng màu và các thanh bar. Cần chạy trên môi trường browser hỗ trợ Canvas API (không dùng được trong SSR/Node context).
291
+
292
+ ⚠️ **Không emit khi init mặc định**: Mặc định `noEmitEventColorWhenInitComponent = true`, nghĩa là `outColorChange` và `outColorChangeMultipleType` sẽ không fired khi component mount lần đầu. Nếu cần nhận giá trị màu ngay khi hiển thị, truyền `[noEmitEventColorWhenInitComponent]="false"`.
293
+
294
+ ⚠️ **Alpha chỉ hoạt động khi bật**: Thanh alpha và ô nhập alpha chỉ render khi `showAlpha: true` trong `customOptions`. Khi `showAlpha` là `false` (mặc định), giá trị alpha luôn là `1` và HEX output không kèm alpha.
295
+
296
+ ⚠️ **HEX output có alpha khi showAlpha bật**: Khi `showAlpha: true`, `outColorChange` emit HEX 8 ký tự dạng `#rrggbbaa` thay vì `#rrggbb` thông thường.
297
+
298
+ ⚠️ **customOptions không reactive sau init**: `customOptions` được đọc một lần trong `ngOnInit`. Thay đổi object sau khi component đã mount sẽ không cập nhật giao diện picker. Nếu cần thay đổi dynamic, hãy destroy và tạo lại component.
299
+
300
+ ## Demo
301
+
302
+ ```bash
303
+ npx nx serve core-ui
304
+ ```
179
305
 
180
- MIT
306
+ Truy cập: http://localhost:4500/components/color-picker
@@ -13,7 +13,7 @@ export declare class LibsUiComponentsColorPickerComponent implements OnInit, Aft
13
13
  protected g: import("@angular/core").WritableSignal<number>;
14
14
  protected b: import("@angular/core").WritableSignal<number>;
15
15
  protected a: import("@angular/core").WritableSignal<number>;
16
- private slBarHelper;
16
+ private readonly slBarHelper;
17
17
  readonly customOptions: import("@angular/core").InputSignal<IPickerCustomOptions | undefined>;
18
18
  readonly noEmitEventColorWhenInitComponent: import("@angular/core").InputSignalWithTransform<boolean, boolean | undefined>;
19
19
  readonly outColorChange: import("@angular/core").OutputEmitterRef<string>;
@@ -25,8 +25,8 @@ export declare class LibsUiComponentsColorPickerComponent implements OnInit, Aft
25
25
  private readonly alphaEl;
26
26
  private readonly alphaPointerEl;
27
27
  private readonly previewEl;
28
- private destroyRef;
29
- private notificationService;
28
+ private readonly destroyRef;
29
+ private readonly notificationService;
30
30
  constructor();
31
31
  ngOnInit(): void;
32
32
  ngAfterViewInit(): void;