|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + Directive, |
| 11 | + input, |
| 12 | + ElementRef, |
| 13 | + inject, |
| 14 | + contentChildren, |
| 15 | + afterRenderEffect, |
| 16 | + signal, |
| 17 | + booleanAttribute, |
| 18 | + computed, |
| 19 | +} from '@angular/core'; |
| 20 | +import {Directionality} from '@angular/cdk/bidi'; |
| 21 | +import {AccordionGroupPattern, AccordionTriggerPattern} from '@angular/aria/private'; |
| 22 | +import {AccordionTrigger} from './accordion-trigger'; |
| 23 | +import {AccordionPanel} from './accordion-panel'; |
| 24 | +import {ACCORDION_GROUP} from './accordion-tokens'; |
| 25 | + |
| 26 | +/** |
| 27 | + * A container for a group of accordion items. It manages the overall state and |
| 28 | + * interactions of the accordion, such as keyboard navigation and expansion mode. |
| 29 | + * |
| 30 | + * The `ngAccordionGroup` serves as the root of a group of accordion triggers and panels, |
| 31 | + * coordinating the behavior of the `ngAccordionTrigger` and `ngAccordionPanel` elements within it. |
| 32 | + * It supports both single and multiple expansion modes. |
| 33 | + * |
| 34 | + * ```html |
| 35 | + * <div ngAccordionGroup [multiExpandable]="true" [(expandedPanels)]="expandedItems"> |
| 36 | + * <div class="accordion-item"> |
| 37 | + * <h3> |
| 38 | + * <button ngAccordionTrigger panelId="item-1">Item 1</button> |
| 39 | + * </h3> |
| 40 | + * <div ngAccordionPanel panelId="item-1"> |
| 41 | + * <ng-template ngAccordionContent> |
| 42 | + * <p>Content for Item 1.</p> |
| 43 | + * </ng-template> |
| 44 | + * </div> |
| 45 | + * </div> |
| 46 | + * <div class="accordion-item"> |
| 47 | + * <h3> |
| 48 | + * <button ngAccordionTrigger panelId="item-2">Item 2</button> |
| 49 | + * </h3> |
| 50 | + * <div ngAccordionPanel panelId="item-2"> |
| 51 | + * <ng-template ngAccordionContent> |
| 52 | + * <p>Content for Item 2.</p> |
| 53 | + * </ng-template> |
| 54 | + * </div> |
| 55 | + * </div> |
| 56 | + * </div> |
| 57 | + * ``` |
| 58 | + * |
| 59 | + * @developerPreview 21.0 |
| 60 | + */ |
| 61 | +@Directive({ |
| 62 | + selector: '[ngAccordionGroup]', |
| 63 | + exportAs: 'ngAccordionGroup', |
| 64 | + host: { |
| 65 | + '(keydown)': '_pattern.onKeydown($event)', |
| 66 | + '(pointerdown)': '_pattern.onPointerdown($event)', |
| 67 | + '(focusin)': '_pattern.onFocus($event)', |
| 68 | + }, |
| 69 | + providers: [{provide: ACCORDION_GROUP, useExisting: AccordionGroup}], |
| 70 | +}) |
| 71 | +export class AccordionGroup { |
| 72 | + /** A reference to the group element. */ |
| 73 | + private readonly _elementRef = inject(ElementRef); |
| 74 | + |
| 75 | + /** A reference to the group element. */ |
| 76 | + readonly element = this._elementRef.nativeElement as HTMLElement; |
| 77 | + |
| 78 | + /** The AccordionTriggers nested inside this group. */ |
| 79 | + private readonly _triggers = contentChildren(AccordionTrigger, {descendants: true}); |
| 80 | + |
| 81 | + /** The AccordionTrigger patterns nested inside this group. */ |
| 82 | + private readonly _triggerPatterns = computed(() => this._triggers().map(t => t._pattern)); |
| 83 | + |
| 84 | + /** The AccordionPanels nested inside this group. */ |
| 85 | + private readonly _panels = contentChildren(AccordionPanel, {descendants: true}); |
| 86 | + |
| 87 | + /** The text direction (ltr or rtl). */ |
| 88 | + readonly textDirection = inject(Directionality).valueSignal; |
| 89 | + |
| 90 | + /** Whether the entire accordion group is disabled. */ |
| 91 | + readonly disabled = input(false, {transform: booleanAttribute}); |
| 92 | + |
| 93 | + /** Whether multiple accordion items can be expanded simultaneously. */ |
| 94 | + readonly multiExpandable = input(true, {transform: booleanAttribute}); |
| 95 | + |
| 96 | + /** |
| 97 | + * Whether to allow disabled items to receive focus. When `true`, disabled items are |
| 98 | + * focusable but not interactive. When `false`, disabled items are skipped during navigation. |
| 99 | + */ |
| 100 | + readonly softDisabled = input(true, {transform: booleanAttribute}); |
| 101 | + |
| 102 | + /** Whether keyboard navigation should wrap around from the last item to the first, and vice-versa. */ |
| 103 | + readonly wrap = input(false, {transform: booleanAttribute}); |
| 104 | + |
| 105 | + /** The UI pattern instance for this accordion group. */ |
| 106 | + readonly _pattern: AccordionGroupPattern = new AccordionGroupPattern({ |
| 107 | + ...this, |
| 108 | + activeItem: signal(undefined), |
| 109 | + items: this._triggerPatterns, |
| 110 | + // TODO(ok7sai): Investigate whether an accordion should support horizontal mode. |
| 111 | + orientation: () => 'vertical', |
| 112 | + getItem: e => this._getItem(e), |
| 113 | + element: () => this.element, |
| 114 | + }); |
| 115 | + |
| 116 | + constructor() { |
| 117 | + // Effect to link triggers with their corresponding panels and update the group's items. |
| 118 | + afterRenderEffect(() => { |
| 119 | + const triggers = this._triggers(); |
| 120 | + const panels = this._panels(); |
| 121 | + |
| 122 | + for (const trigger of triggers) { |
| 123 | + const panel = panels.find(p => p.panelId() === trigger.panelId()); |
| 124 | + trigger._accordionPanelPattern.set(panel?._pattern); |
| 125 | + if (panel) { |
| 126 | + panel._accordionTriggerPattern.set(trigger._pattern); |
| 127 | + } |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + /** Expands all accordion panels if multi-expandable. */ |
| 133 | + expandAll() { |
| 134 | + this._pattern.expansionBehavior.openAll(); |
| 135 | + } |
| 136 | + |
| 137 | + /** Collapses all accordion panels. */ |
| 138 | + collapseAll() { |
| 139 | + this._pattern.expansionBehavior.closeAll(); |
| 140 | + } |
| 141 | + |
| 142 | + /** Gets the trigger pattern for a given element. */ |
| 143 | + private _getItem(element: Element | null | undefined): AccordionTriggerPattern | undefined { |
| 144 | + let target = element; |
| 145 | + |
| 146 | + while (target) { |
| 147 | + const pattern = this._triggerPatterns().find(t => t.element() === target); |
| 148 | + if (pattern) { |
| 149 | + return pattern; |
| 150 | + } |
| 151 | + |
| 152 | + target = target.parentElement?.closest('[ngAccordionTrigger]'); |
| 153 | + } |
| 154 | + |
| 155 | + return undefined; |
| 156 | + } |
| 157 | +} |
0 commit comments