11import {
2- computed ,
32 Directive ,
43 effect ,
5- ElementRef ,
64 inject ,
75 input ,
8- Renderer2 ,
96} from '@angular/core' ;
107import {
118 ResizeConsumerService ,
@@ -15,47 +12,24 @@ import {
1512 * A directive that adjusts the height of an element based on resize messages from a specified channel.
1613 */
1714@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 ]
2021} )
2122export class ScalableDirective {
2223 /**
23- * The connection ID for the element, used as channel id backup
24+ * The connection ID for the element
2425 */
2526 public connect = input < string > ( ) ;
2627
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 ) ;
4629
4730 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) */
5431 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 ( ) ?? '' ;
5933 } ) ;
6034 }
6135}
0 commit comments