@@ -10,28 +10,28 @@ import {
1010 Directive ,
1111 input ,
1212 ElementRef ,
13+ OnInit ,
1314 inject ,
14- signal ,
1515 model ,
1616 booleanAttribute ,
1717 computed ,
18- WritableSignal ,
1918} from '@angular/core' ;
2019import { _IdGenerator } from '@angular/cdk/a11y' ;
21- import { AccordionPanelPattern , AccordionTriggerPattern } from '../private' ;
20+ import { AccordionTriggerPattern } from '../private' ;
2221import { ACCORDION_GROUP } from './accordion-tokens' ;
22+ import { AccordionPanel } from './accordion-panel' ;
2323
2424/**
2525 * The trigger that toggles the visibility of its associated `ngAccordionPanel`.
2626 *
27- * This directive requires a `panelId` that must match the `panelId` of the `ngAccordionPanel` it
28- * controls. When clicked, it will expand or collapse the panel. It also handles keyboard
27+ * This directive requires the `panel` input be set to the template reference of the `ngAccordionPanel`
28+ * it controls. When clicked, it will expand or collapse the panel. It also handles keyboard
2929 * interactions for navigation within the `ngAccordionGroup`. It applies `role="button"` and manages
3030 * `aria-expanded`, `aria-controls`, and `aria-disabled` attributes for accessibility.
3131 * The `disabled` input can be used to disable the trigger.
3232 *
3333 * ```html
34- * <button ngAccordionTrigger panelId="unique-id-1 ">
34+ * <button ngAccordionTrigger panel="panel ">
3535 * Accordion Trigger Text
3636 * </button>
3737 * ```
@@ -45,15 +45,15 @@ import {ACCORDION_GROUP} from './accordion-tokens';
4545 host : {
4646 '[attr.data-active]' : 'active()' ,
4747 'role' : 'button' ,
48- '[id]' : '_pattern. id()' ,
48+ '[id]' : 'id()' ,
4949 '[attr.aria-expanded]' : 'expanded()' ,
5050 '[attr.aria-controls]' : '_pattern.controls()' ,
5151 '[attr.aria-disabled]' : '_pattern.disabled()' ,
5252 '[attr.disabled]' : '_pattern.hardDisabled() ? true : null' ,
5353 '[attr.tabindex]' : '_pattern.tabIndex()' ,
5454 } ,
5555} )
56- export class AccordionTrigger {
56+ export class AccordionTrigger implements OnInit {
5757 /** A reference to the trigger element. */
5858 private readonly _elementRef = inject ( ElementRef ) ;
5959
@@ -63,32 +63,33 @@ export class AccordionTrigger {
6363 /** The parent AccordionGroup. */
6464 private readonly _accordionGroup = inject ( ACCORDION_GROUP ) ;
6565
66+ /** The associated AccordionPanel. */
67+ readonly panel = input . required < AccordionPanel > ( ) ;
68+
6669 /** A unique identifier for the widget. */
6770 readonly id = input ( inject ( _IdGenerator ) . getId ( 'ng-accordion-trigger-' , true ) ) ;
6871
69- /** A local unique identifier for the trigger, used to match with its panel's `panelId`. */
70- readonly panelId = input . required < string > ( ) ;
71-
7272 /** Whether the trigger is disabled. */
7373 readonly disabled = input ( false , { transform : booleanAttribute } ) ;
7474
7575 /** Whether the corresponding panel is expanded. */
7676 readonly expanded = model < boolean > ( false ) ;
7777
7878 /** Whether the trigger is active. */
79- readonly active = computed ( ( ) => this . _pattern . active ( ) ) ;
80-
81- /** The accordion panel pattern controlled by this trigger. This is set by AccordionGroup. */
82- readonly _accordionPanelPattern : WritableSignal < AccordionPanelPattern | undefined > =
83- signal ( undefined ) ;
79+ readonly active = computed ( ( ) => this . _pattern ! . active ( ) ) ;
8480
8581 /** The UI pattern instance for this trigger. */
86- readonly _pattern : AccordionTriggerPattern = new AccordionTriggerPattern ( {
87- ...this ,
88- accordionGroup : computed ( ( ) => this . _accordionGroup . _pattern ) ,
89- accordionPanel : this . _accordionPanelPattern ,
90- element : ( ) => this . element ,
91- } ) ;
82+ _pattern ! : AccordionTriggerPattern ;
83+
84+ ngOnInit ( ) {
85+ this . _pattern = new AccordionTriggerPattern ( {
86+ ...this ,
87+ accordionGroup : computed ( ( ) => this . _accordionGroup . _pattern ) ,
88+ accordionPanelId : ( ) => this . panel ( ) . id ( ) ,
89+ element : ( ) => this . element ,
90+ } ) ;
91+ this . panel ( ) . _accordionTriggerPattern . set ( this . _pattern ) ;
92+ }
9293
9394 /** Expands this item. */
9495 expand ( ) {
0 commit comments