Skip to content

Commit 77cbf2d

Browse files
committed
docs(cdk/accordion): add overview
Adds an overview doc for the `cdk/accordion` package, including a sample implementation of an accordion using the CDK. Fixes #14298.
1 parent a0ad9e5 commit 77cbf2d

File tree

8 files changed

+151
-1
lines changed

8 files changed

+151
-1
lines changed

src/cdk/accordion/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ng_web_test_suite(
4242

4343
markdown_to_html(
4444
name = "overview",
45-
srcs = [],
45+
srcs = [":accordion.md"],
4646
)
4747

4848
filegroup(

src/cdk/accordion/accordion.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
An accordion is a component with one or more expandable sections. CDK accordion provides a
2+
foundation upon which you can build your own custom accordion component. CDK accordion provides
3+
logic for the accordion interaction pattern without any styles. You can customize the accordion's
4+
appearance based on your application's needs.
5+
6+
<!-- example(cdk-accordion-overview) -->
7+
8+
### Accessibility
9+
The CDK accordion doesn't come with any accessibility treatment, because it doesn't have control
10+
over its markup. We recommend to set the accordion trigger element as a `role="button"` while
11+
the body container as a `role="region"`. Furthermore, the trigger should have `aria-controls`
12+
pointing to the body and `aria-expanded` based on the expanded state, while the body should have
13+
an `aria-labelledby` that points to the header. See the example above for a sample implementation.

src/components-examples/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ALL_EXAMPLES = [
5454
"//src/components-examples/cdk/stepper",
5555
"//src/components-examples/cdk/scrolling",
5656
"//src/components-examples/cdk/portal",
57+
"//src/components-examples/cdk/accordion",
5758
"//src/components-examples/cdk/platform",
5859
"//src/components-examples/cdk/drag-drop",
5960
"//src/components-examples/cdk/clipboard",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load("//tools:defaults.bzl", "ng_module")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ng_module(
6+
name = "accordion",
7+
srcs = glob(["**/*.ts"]),
8+
assets = glob([
9+
"**/*.html",
10+
"**/*.css",
11+
]),
12+
module_name = "@angular/components-examples/cdk/accordion",
13+
deps = [
14+
"//src/cdk/accordion",
15+
],
16+
)
17+
18+
filegroup(
19+
name = "source-files",
20+
srcs = glob([
21+
"**/*.html",
22+
"**/*.css",
23+
"**/*.ts",
24+
]),
25+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.example-accordion {
2+
display: block;
3+
max-width: 500px;
4+
}
5+
6+
.example-accordion-item {
7+
display: block;
8+
border: solid 1px #ccc;
9+
}
10+
11+
.example-accordion-item + .example-accordion-item {
12+
border-top: none;
13+
}
14+
15+
.example-accordion-item-header {
16+
display: flex;
17+
align-content: center;
18+
justify-content: space-between;
19+
}
20+
21+
.example-accordion-item-description {
22+
font-size: 0.85em;
23+
color: #999;
24+
}
25+
26+
.example-accordion-item-header,
27+
.example-accordion-item-body {
28+
padding: 16px;
29+
}
30+
31+
.example-accordion-item-header:hover {
32+
cursor: pointer;
33+
background-color: #eee;
34+
}
35+
36+
.example-accordion-item:first-child {
37+
border-top-left-radius: 4px;
38+
border-top-right-radius: 4px;
39+
}
40+
41+
.example-accordion-item:last-child {
42+
border-bottom-left-radius: 4px;
43+
border-bottom-right-radius: 4px;
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<cdk-accordion class="example-accordion">
2+
<cdk-accordion-item
3+
*ngFor="let item of items; let index = index;"
4+
#accordionItem="cdkAccordionItem"
5+
class="example-accordion-item"
6+
role="button"
7+
tabindex="0"
8+
[attr.id]="'accordion-header-' + index"
9+
[attr.aria-expanded]="accordionItem.expanded"
10+
[attr.aria-controls]="'accordion-body-' + index">
11+
<div class="example-accordion-item-header" (click)="accordionItem.toggle()">
12+
{{ item }}
13+
<span class="example-accordion-item-description">
14+
Click to {{ accordionItem.expanded ? 'close' : 'open' }}
15+
</span>
16+
</div>
17+
<div
18+
class="example-accordion-item-body"
19+
role="region"
20+
[style.display]="accordionItem.expanded ? '' : 'none'"
21+
[attr.id]="'accordion-body-' + index"
22+
[attr.aria-labelledby]="'accordion-header-' + index">
23+
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Perferendis
24+
excepturi incidunt ipsum deleniti labore, tempore non nam doloribus blanditiis
25+
veritatis illo autem iure aliquid ullam rem tenetur deserunt velit culpa?
26+
</div>
27+
</cdk-accordion-item>
28+
</cdk-accordion>
29+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Accordion overview
5+
*/
6+
@Component({
7+
selector: 'cdk-accordion-overview-example',
8+
templateUrl: 'cdk-accordion-overview-example.html',
9+
styleUrls: ['cdk-accordion-overview-example.css'],
10+
})
11+
export class CdkAccordionOverviewExample {
12+
items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'];
13+
expandedIndex = 0;
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {CommonModule} from '@angular/common';
2+
import {CdkAccordionModule} from '@angular/cdk/accordion';
3+
import {NgModule} from '@angular/core';
4+
import {
5+
CdkAccordionOverviewExample,
6+
} from './cdk-accordion-overview/cdk-accordion-overview-example';
7+
8+
export {CdkAccordionOverviewExample};
9+
10+
const EXAMPLES = [
11+
CdkAccordionOverviewExample,
12+
];
13+
14+
@NgModule({
15+
imports: [
16+
CommonModule,
17+
CdkAccordionModule
18+
],
19+
declarations: EXAMPLES,
20+
exports: EXAMPLES,
21+
entryComponents: EXAMPLES,
22+
})
23+
export class CdkAccordionExamplesModule {
24+
}

0 commit comments

Comments
 (0)