1
1
import {
2
- computed ,
3
2
Directive ,
4
3
effect ,
5
- ElementRef ,
6
4
inject ,
7
5
input ,
8
- Renderer2 ,
9
6
} from '@angular/core' ;
10
7
import {
11
8
ResizeConsumerService ,
@@ -15,47 +12,24 @@ import {
15
12
* A directive that adjusts the height of an element based on resize messages from a specified channel.
16
13
*/
17
14
@Directive ( {
18
- selector : '[scalable]' ,
19
- standalone : true
15
+ selector : '[connect][scalable]' ,
16
+ standalone : true ,
17
+ host : {
18
+ '[style.height.px]' : 'resizeConsumer.heightPx()'
19
+ } ,
20
+ providers : [ ResizeConsumerService ]
20
21
} )
21
22
export class ScalableDirective {
22
23
/**
23
- * The connection ID for the element, used as channel id backup
24
+ * The connection ID for the element
24
25
*/
25
26
public connect = input < string > ( ) ;
26
27
27
- /**
28
- * The channel id
29
- */
30
- public scalable = input < string > ( ) ;
31
-
32
- private readonly resizeHandler = inject ( ResizeConsumerService ) ;
33
-
34
- /**
35
- * This signal checks if the current channel requesting the resize matches the channel ID from the resize handler.
36
- * If they match, it returns the new height information; otherwise, it returns undefined.
37
- */
38
- private readonly newHeightFromChannel = computed ( ( ) => {
39
- const channelAskingResize = this . scalable ( ) || this . connect ( ) ;
40
- const newHeightFromChannel = this . resizeHandler . newHeightFromChannel ( ) ;
41
- if ( channelAskingResize && newHeightFromChannel ?. channelId === channelAskingResize ) {
42
- return newHeightFromChannel ;
43
- }
44
- return undefined ;
45
- } ) ;
28
+ protected readonly resizeConsumer = inject ( ResizeConsumerService ) ;
46
29
47
30
constructor ( ) {
48
- const elem = inject ( ElementRef ) ;
49
- const renderer = inject ( Renderer2 ) ;
50
-
51
- this . resizeHandler . start ( ) ;
52
-
53
- /** When a new height value is received set the height of the host element (in pixels) */
54
31
effect ( ( ) => {
55
- const newHeightFromChannel = this . newHeightFromChannel ( ) ;
56
- if ( newHeightFromChannel ) {
57
- renderer . setStyle ( elem . nativeElement , 'height' , `${ newHeightFromChannel . height } px` ) ;
58
- }
32
+ this . resizeConsumer . from = this . connect ( ) ?? '' ;
59
33
} ) ;
60
34
}
61
35
}
0 commit comments