|
1 | | -import { booleanAttribute, Directive, HostBinding, Input } from '@angular/core'; |
| 1 | +import { booleanAttribute, computed, Directive, input, InputSignal, InputSignalWithTransform } from '@angular/core'; |
2 | 2 |
|
3 | 3 | @Directive({ |
4 | 4 | selector: '[cImg]', |
5 | | - standalone: true |
| 5 | + standalone: true, |
| 6 | + host: { |
| 7 | + '[class]': 'hostClasses()', |
| 8 | + '[style]': 'hostStyles()' |
| 9 | + } |
6 | 10 | }) |
7 | 11 | export class ImgDirective { |
8 | | - |
9 | 12 | /** |
10 | 13 | * Set the horizontal aligment. |
11 | 14 | * @type {'' | 'start' | 'end' | 'center'} |
12 | 15 | */ |
13 | | - @Input() align: '' | 'start' | 'end' | 'center' = ''; |
| 16 | + readonly align: InputSignal<'' | 'start' | 'end' | 'center'> = input<'' | 'start' | 'end' | 'center'>(''); |
14 | 17 |
|
15 | 18 | /** |
16 | 19 | * Make image responsive. |
17 | 20 | * @type boolean |
18 | 21 | */ |
19 | | - @Input({ transform: booleanAttribute }) fluid: string | boolean = false; |
| 22 | + readonly fluid: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute }); |
20 | 23 |
|
21 | 24 | /** |
22 | 25 | * Make image rounded. |
23 | 26 | * @type boolean |
24 | 27 | */ |
25 | | - @Input({ transform: booleanAttribute }) rounded: string | boolean = false; |
| 28 | + readonly rounded: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute }); |
26 | 29 |
|
27 | 30 | /** |
28 | 31 | * Give an image a rounded 1px border appearance. |
29 | 32 | * @type boolean |
30 | 33 | */ |
31 | | - @Input({ transform: booleanAttribute }) thumbnail: string | boolean = false; |
| 34 | + readonly thumbnail: InputSignalWithTransform<boolean, unknown> = input(false, { transform: booleanAttribute }); |
32 | 35 |
|
33 | 36 | /** |
34 | 37 | * Color for image placeholder. |
35 | 38 | */ |
36 | | - @Input() placeholderColor = 'transparent'; |
| 39 | + readonly placeholderColor = input('transparent'); |
37 | 40 |
|
38 | | - @HostBinding('style') |
39 | | - get getStyles(): any { |
40 | | - return { backgroundColor: this.placeholderColor }; |
41 | | - } |
| 41 | + readonly hostStyles = computed(() => { |
| 42 | + return { backgroundColor: this.placeholderColor() }; |
| 43 | + }); |
42 | 44 |
|
43 | | - @HostBinding('class') |
44 | | - get hostClasses(): any { |
45 | | - const align = this.align; |
| 45 | + readonly hostClasses = computed(() => { |
| 46 | + const align = this.align(); |
46 | 47 | return { |
47 | 48 | [`float-${align}`]: align === 'start' || align === 'end', |
48 | 49 | 'd-block': align === 'center', |
49 | 50 | 'mx-auto': align === 'center', |
50 | | - 'img-fluid': this.fluid, |
51 | | - 'rounded': this.rounded, |
52 | | - 'img-thumbnail': this.thumbnail |
53 | | - }; |
54 | | - } |
| 51 | + 'img-fluid': this.fluid(), |
| 52 | + rounded: this.rounded(), |
| 53 | + 'img-thumbnail': this.thumbnail() |
| 54 | + } as Record<string, boolean>; |
| 55 | + }); |
55 | 56 | } |
0 commit comments