Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 7x 7x 2x 2x 2x 7x 7x 6x 6x 6x 1x 4x 6x 6x 3x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { CdkConnectedOverlay, CdkOverlayOrigin } from '@angular/cdk/overlay'; import { booleanAttribute, ChangeDetectionStrategy, Component, computed, ElementRef, input, model, signal, viewChild, viewChildren } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { isRTL } from '@siemens/element-ng/common'; import { addIcons, elementOk, SiIconComponent } from '@siemens/element-ng/icon'; import { SiTranslatePipe, TranslatableString } from '@siemens/element-translate-ng/translate'; /** * The Element data color palette is used as default. * Note: This array needs to be kept in sync with the design system data color tokens. */ const defaultDataColors: string[] = [ 'element-data-1', 'element-data-2', 'element-data-3', 'element-data-4', 'element-data-5', 'element-data-6', 'element-data-7', 'element-data-8', 'element-data-9', 'element-data-10', 'element-data-11', 'element-data-12', 'element-data-13', 'element-data-14', 'element-data-15', 'element-data-16' ]; @Component({ selector: 'si-color-picker', imports: [SiIconComponent, SiTranslatePipe, CdkConnectedOverlay, CdkOverlayOrigin], templateUrl: './si-color-picker.component.html', styleUrl: './si-color-picker.component.scss', providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: SiColorPickerComponent, multi: true } ], changeDetection: ChangeDetectionStrategy.OnPush }) export class SiColorPickerComponent implements ControlValueAccessor { // eslint-disable-next-line defaultValue/tsdoc-defaultValue-annotation /** * The color palette to choose the colors from. As colors, only valid CSS * variable names omitting the `--` prefix or Element color tokens omitting * the `$` prefix are supported. * * Note: If custom CSS variables are provided, they need to be defined for * both light and dark mode. * * @defaultValue The first 16 colors of the Element data color palette. */ readonly colorPalette = input<string[]>(defaultDataColors); /** * The selected color. */ readonly color = model<string>(); /** * Specifies whether the color popup should automatically close on a color selection. * * @defaultValue false */ readonly autoClose = input(false, { transform: booleanAttribute }); /** * Specifies whether the color picker component is disabled. * * @defaultValue false */ // eslint-disable-next-line @angular-eslint/no-input-rename readonly disabledInput = input(false, { alias: 'disabled' }); /** * Aria label for the color input button. */ readonly ariaLabel = input<TranslatableString>(); private onChange: (value: string) => void = () => {}; private onTouched: () => void = () => {}; private readonly colorInputRef = viewChild.required<ElementRef<HTMLInputElement>>('colorInputBox'); private readonly swatchInputs = viewChildren<ElementRef<HTMLInputElement>>('swatchInput'); private readonly selectedSwatchInput = computed(() => this.swatchInputs().find(swatchInput => swatchInput.nativeElement.checked) ); private readonly disabledNgControl = signal(false); private readonly numberOfColumns = 4; protected readonly disabled = computed(() => this.disabledInput() || this.disabledNgControl()); protected readonly isOverlayOpen = signal(false); protected readonly icons = addIcons({ elementOk }); protected blur(): void { if (!this.autoClose()) { this.onTouched(); } } protected arrowDown(index: number, event: Event): void { const nextIndex = index + this.numberOfColumns; this.focusLabel(nextIndex); event.preventDefault(); } protected arrowUp(index: number, event: Event): void { const prevIndex = index - this.numberOfColumns; this.focusLabel(prevIndex); event.preventDefault(); } protected arrowLeft(index: number, event: Event): void { const prevIndex = index + (isRTL() ? 1 : -1); this.focusLabel(prevIndex); event.preventDefault(); } protected arrowRight(index: number, event: Event): void { const prevIndex = index + (isRTL() ? -1 : +1); this.focusLabel(prevIndex); event.preventDefault(); } private focusLabel(index: number): void { const labels = this.swatchInputs(); const totalSwatches = labels.length; const normalizedIndex = (index + totalSwatches) % totalSwatches; labels[normalizedIndex].nativeElement.focus(); } protected openOverlay(): void { this.isOverlayOpen.set(true); this.focusSelectedColor(); } protected overlayDetach(): void { this.isOverlayOpen.set(false); setTimeout(() => { this.colorInputRef().nativeElement?.focus(); }); } private focusSelectedColor(): void { setTimeout(() => { this.selectedSwatchInput()?.nativeElement.focus(); }); } protected selectColor(color: string): void { this.color.set(color); this.onChange(color!); if (this.autoClose()) { this.overlayDetach(); } } writeValue(value: string): void { this.color.set(value); } registerOnChange(fn: (value: string) => void): void { this.onChange = fn; } registerOnTouched(fn: () => void): void { this.onTouched = fn; } setDisabledState(isDisabled: boolean): void { this.disabledNgControl.set(isDisabled); } } |