Bir akordeon, ilgili içeriği genişletilebilir ve daraltılabilir bölümlere düzenleyerek sayfa kaydırmayı azaltır ve kullanıcıların ilgili bilgilere odaklanmasına yardımcı olur. Her bölümün bir tetikleyici butonu ve bir içerik paneli vardır. Bir tetikleyiciye tıklamak, ilişkili panelin görünürlüğünü değiştirir.
<divngAccordionGroup class="basic-accordion" [multiExpandable]="false"> <h3> <spanngAccordionTrigger [panel]="panel1" #trigger1="ngAccordionTrigger" [expanded]="true"> Which attribute tells assistive tech whether the panel is open or closed? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger1.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel1="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use <code>aria-expanded</code> on the button element. Set it to "true" when the content panel is visible and "false" when the content is hidden. This is a crucial state indicator for screen reader users. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel2" #trigger2="ngAccordionTrigger"> How do you link the button to the content it controls? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger2.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel2="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use the <code>aria-controls</code> attribute on the button, and set its value to match the id of the related content panel. This establishes a programmatic relationship, allowing assistive technologies to jump directly to the relevant content. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel3" #trigger3="ngAccordionTrigger"> What role should the heading element containing the accordion button have? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger3.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel3="ngAccordionPanel"> <ng-template ngAccordionContent> <p> The element containing the button should typically have <code>role="heading"</code> with an appropriate <code>aria-level</code> to define the structure. This ensures the accordion section is recognized as a section header, making the page structure navigable for users. </p> </ng-template> </div></div>
Aynı anda yalnızca bir panelin açık olmasına izin vermek için [multiExpandable]="false" ayarlayın. Yeni bir panel açmak, daha önce açık olan paneli otomatik olarak kapatır.
<divngAccordionGroup class="basic-accordion" [multiExpandable]="false"> <h3> <spanngAccordionTrigger [panel]="panel1" #trigger1="ngAccordionTrigger" [expanded]="true"> Which attribute tells assistive tech whether the panel is open or closed? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger1.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel1="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use <code>aria-expanded</code> on the button element. Set it to "true" when the content panel is visible and "false" when the content is hidden. This is a crucial state indicator for screen reader users. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel2" #trigger2="ngAccordionTrigger"> How do you link the button to the content it controls? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger2.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel2="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use the <code>aria-controls</code> attribute on the button, and set its value to match the id of the related content panel. This establishes a programmatic relationship, allowing assistive technologies to jump directly to the relevant content. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel3" #trigger3="ngAccordionTrigger"> What role should the heading element containing the accordion button have? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger3.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel3="ngAccordionPanel"> <ng-template ngAccordionContent> <p> The element containing the button should typically have <code>role="heading"</code> with an appropriate <code>aria-level</code> to define the structure. This ensures the accordion section is recognized as a section header, making the page structure navigable for users. </p> </ng-template> </div></div>
<divngAccordionGroup class="material-accordion" [multiExpandable]="false"> <h3> <spanngAccordionTrigger [panel]="panel1" #trigger1="ngAccordionTrigger"> Which attribute tells assistive tech whether the panel is open or closed? <span aria-hidden="true" class="material-symbols-outlined expand-icon" [class.expand-icon__expanded]="trigger1.expanded()" translate="no" >keyboard_arrow_up</span > </span> </h3> <div ngAccordionPanel #panel1="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use <code>aria-expanded</code> on the button element. Set it to "true" when the content panel is visible and "false" when the content is hidden. This is a crucial state indicator for screen reader users. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel2" #trigger2="ngAccordionTrigger" [expanded]="true"> How do you link the button to the content it controls? <span aria-hidden="true" class="material-symbols-outlined expand-icon" [class.expand-icon__expanded]="trigger2.expanded()" translate="no" >keyboard_arrow_up</span > </span> </h3> <div ngAccordionPanel #panel2="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use the <code>aria-controls</code> attribute on the button, and set its value to match the id of the related content panel. This establishes a programmatic relationship, allowing assistive technologies to jump directly to the relevant content. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel3" #trigger3="ngAccordionTrigger"> What role should the heading element containing the accordion button have? <span aria-hidden="true" class="material-symbols-outlined expand-icon" [class.expand-icon__expanded]="trigger3.expanded()" translate="no" >keyboard_arrow_up</span > </span> </h3> <div ngAccordionPanel #panel3="ngAccordionPanel"> <ng-template ngAccordionContent> <p> The element containing the button should typically have <code>role="heading"</code> with an appropriate <code>aria-level</code> to define the structure. This ensures the accordion section is recognized as a section header, making the page structure navigable for users. </p> </ng-template> </div></div>
Birden fazla panelin aynı anda açık olmasına izin vermek için [multiExpandable]="true" ayarlayın. Kullanıcılar diğerlerini kapatmadan ihtiyaç duyduğu kadar panel genişletebilir.
<divngAccordionGroup class="basic-accordion"> <h3> <spanngAccordionTrigger [panel]="panel1" #trigger1="ngAccordionTrigger" [expanded]="true"> Which attribute tells assistive tech whether the panel is open or closed? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger1.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel1="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use <code>aria-expanded</code> on the button element. Set it to "true" when the content panel is visible and "false" when the content is hidden. This is a crucial state indicator for screen reader users. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel2" #trigger2="ngAccordionTrigger"> How do you link the button to the content it controls? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger2.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel2="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use the <code>aria-controls</code> attribute on the button, and set its value to match the id of the related content panel. This establishes a programmatic relationship, allowing assistive technologies to jump directly to the relevant content. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel3" #trigger3="ngAccordionTrigger" [expanded]="true"> What role should the heading element containing the accordion button have? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger3.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel3="ngAccordionPanel"> <ng-template ngAccordionContent> <p> The element containing the button should typically have <code>role="heading"</code> with an appropriate <code>aria-level</code> to define the structure. This ensures the accordion section is recognized as a section header, making the page structure navigable for users. </p> </ng-template> </div></div>
<divngAccordionGroup class="material-accordion"> <h3> <spanngAccordionTrigger [panel]="panel1" #trigger1="ngAccordionTrigger" [expanded]="true"> Which attribute tells assistive tech whether the panel is open or closed? <span aria-hidden="true" class="material-symbols-outlined expand-icon" [class.expand-icon__expanded]="trigger1.expanded()" translate="no" >keyboard_arrow_up</span > </span> </h3> <div ngAccordionPanel #panel1="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use <code>aria-expanded</code> on the button element. Set it to "true" when the content panel is visible and "false" when the content is hidden. This is a crucial state indicator for screen reader users. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel2" #trigger2="ngAccordionTrigger"> How do you link the button to the content it controls? <span aria-hidden="true" class="material-symbols-outlined expand-icon" [class.expand-icon__expanded]="trigger2.expanded()" translate="no" >keyboard_arrow_up</span > </span> </h3> <div ngAccordionPanel #panel2="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use the <code>aria-controls</code> attribute on the button, and set its value to match the id of the related content panel. This establishes a programmatic relationship, allowing assistive technologies to jump directly to the relevant content. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel3" #trigger3="ngAccordionTrigger" [expanded]="true"> What role should the heading element containing the accordion button have? <span aria-hidden="true" class="material-symbols-outlined expand-icon" [class.expand-icon__expanded]="trigger3.expanded()" translate="no" >keyboard_arrow_up</span > </span> </h3> <div ngAccordionPanel #panel3="ngAccordionPanel"> <ng-template ngAccordionContent> <p> The element containing the button should typically have <code>role="heading"</code> with an appropriate <code>aria-level</code> to define the structure. This ensures the accordion section is recognized as a section header, making the page structure navigable for users. </p> </ng-template> </div></div>
disabled girdisini kullanarak belirli tetikleyicileri devre dışı bırakın. Devre dışı bırakılan öğelerin klavye navigasyonu sırasındaki davranışını akordeon grubundaki softDisabled girdisi ile kontrol edin.
<divngAccordionGroup class="basic-accordion"> <h3> <spanngAccordionTrigger [panel]="panel1" #trigger1="ngAccordionTrigger" [expanded]="true"> Which attribute tells assistive tech whether the panel is open or closed? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger1.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel1="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use <code>aria-expanded</code> on the button element. Set it to "true" when the content panel is visible and "false" when the content is hidden. This is a crucial state indicator for screen reader users. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel2" #trigger2="ngAccordionTrigger" disabled> How do you link the button to the content it controls? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger2.expanded()" ></span> </span> </h3> <div ngAccordionPanel #panel2="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use the <code>aria-controls</code> attribute on the button, and set its value to match the id of the related content panel. This establishes a programmatic relationship, allowing assistive technologies to jump directly to the relevant content. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel3" #trigger3="ngAccordionTrigger"> What role should the heading element containing the accordion button have? <span aria-hidden="true" class="expand-icon" [class.expand-icon__expanded]="trigger3.expanded()" ></span> </span> </h3> <div ngAccordionPanel panelId="q3" #panel3="ngAccordionPanel"> <ng-template ngAccordionContent> <p> The element containing the button should typically have <code>role="heading"</code> with an appropriate <code>aria-level</code> to define the structure. This ensures the accordion section is recognized as a section header, making the page structure navigable for users. </p> </ng-template> </div></div>
<divngAccordionGroup class="material-accordion"> <h3> <spanngAccordionTrigger [panel]="panel1" #trigger1="ngAccordionTrigger" [expanded]="true"> Which attribute tells assistive tech whether the panel is open or closed? <span aria-hidden="true" class="material-symbols-outlined expand-icon" [class.expand-icon__expanded]="trigger1.expanded()" translate="no" >keyboard_arrow_up</span > </span> </h3> <div ngAccordionPanel #panel1="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use <code>aria-expanded</code> on the button element. Set it to "true" when the content panel is visible and "false" when the content is hidden. This is a crucial state indicator for screen reader users. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel2" #trigger2="ngAccordionTrigger" disabled> How do you link the button to the content it controls? <span aria-hidden="true" class="material-symbols-outlined expand-icon" [class.expand-icon__expanded]="trigger2.expanded()" translate="no" >keyboard_arrow_up</span > </span> </h3> <div ngAccordionPanel #panel2="ngAccordionPanel"> <ng-template ngAccordionContent> <p> Use the <code>aria-controls</code> attribute on the button, and set its value to match the id of the related content panel. This establishes a programmatic relationship, allowing assistive technologies to jump directly to the relevant content. </p> </ng-template> </div> <h3> <spanngAccordionTrigger [panel]="panel3" #trigger3="ngAccordionTrigger"> What role should the heading element containing the accordion button have? <span aria-hidden="true" class="material-symbols-outlined expand-icon" [class.expand-icon__expanded]="trigger3.expanded()" translate="no" >keyboard_arrow_up</span > </span> </h3> <div ngAccordionPanel #panel3="ngAccordionPanel"> <ng-template ngAccordionContent> <p> The element containing the button should typically have <code>role="heading"</code> with an appropriate <code>aria-level</code> to define the structure. This ensures the accordion section is recognized as a section header, making the page structure navigable for users. </p> </ng-template> </div></div>
[softDisabled]="true" olduğunda (varsayılan), devre dışı bırakılan öğeler odak alabilir ancak etkinleştirilemez. [softDisabled]="false" olduğunda, devre dışı bırakılan öğeler klavye navigasyonu sırasında tamamen atlanır.
Panel ilk kez genişletilene kadar içerik render etmeyi ertelemek için bir ng-template üzerinde ngAccordionContent yönergesini kullanın. Bu, resimler, grafikler veya karmaşık bileşenler gibi ağır içerikli akordeonlar için performansı iyileştirir.
<divngAccordionGroup> <div> <buttonngAccordionTrigger [panel]="panel1">Trigger Text</button> <div ngAccordionPanel #panel1="ngAccordionPanel"> <ng-template ngAccordionContent> <!-- Bu içerik yalnızca panel ilk açıldığında render edilir --> <img src="large-image.jpg" alt="Description" /> <app-expensive-component /> </ng-template> </div> </div></div>
Varsayılan olarak, panel daraltıldıktan sonra içerik DOM'da kalır. Panel kapatıldığında içeriği DOM'dan kaldırmak için [preserveContent]="false" ayarlayın.
Angular Aria, akordeon bileşenlerini test etmek için bileşen harness'leri sağlar.
İşte harness'lerin bir bileşen testinde nasıl kullanılacağına dair bir örnek:
import {ComponentFixture, TestBed} from '@angular/core/testing';import {HarnessLoader} from '@angular/cdk/testing';import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';import {AccordionGroupHarness} from '@angular/aria/accordion/testing';import {MyAccordionComponent} from './my-accordion'; // Sizin bileşeninizdescribe('MyAccordionComponent', () => { let fixture:ComponentFixture<MyAccordionComponent>; let loader:HarnessLoader; beforeEach(async () => {TestBed.configureTestingModule({ imports: [MyAccordionComponent], }); fixture =TestBed.createComponent(MyAccordionComponent); await fixture.whenStable(); loader =TestbedHarnessEnvironment.loader(fixture); }); it('should allow expanding panels', async () => { // Akordeon grubu harness'ini yükle const group = await loader.getHarness(AccordionGroupHarness); // Gruptaki tüm bireysel akordeonları (öğeleri) al const accordions = await group.getAccordions(); expect(accordions.length).toBe(3); // Başlangıç durumunu doğrula (ilki genişletilmiş, diğerleri daraltılmış) expect(await accordions[0].isExpanded()).toBe(true); expect(await accordions[1].isExpanded()).toBe(false); // İkinci paneli genişlet await accordions[1].expand(); // Güncellenmiş durumu doğrula expect(await accordions[1].isExpanded()).toBe(true); // multiExpandable false ise, ilki artık daraltılmış olmalı expect(await accordions[0].isExpanded()).toBe(false); });});