Skip to content

Commit 249201b

Browse files
authored
docs(cdk/layout): add BreakpointObserver example (#22828)
Also adds a layout page to the dev-app since it didn't have one
1 parent ffee818 commit 249201b

16 files changed

+171
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
/src/dev-app/grid-list/** @jelbourn
176176
/src/dev-app/icon/** @jelbourn
177177
/src/dev-app/input/** @mmalerba
178+
/src/dev-app/layout/** @jelbourn
178179
/src/dev-app/list/** @jelbourn @crisbeto @devversion
179180
/src/dev-app/live-announcer/** @jelbourn
180181
/src/dev-app/mdc-button/** @andrewseguin

src/cdk/layout/layout.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ size ranges between breakpoints correspond to different standard screen sizes.
88
`BreakpointObserver` lets you evaluate media queries to determine the current screen size and
99
react to changes when the viewport size crosses a breakpoint.
1010

11+
<!-- example(breakpoint-observer-overview) -->
12+
1113
#### Check the current viewport size
1214
You can use the `isMatched` method to evaluate one or more media queries against the current
1315
viewport size.

src/components-examples/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ ALL_EXAMPLES = [
5858
"//src/components-examples/cdk/drag-drop",
5959
"//src/components-examples/cdk/clipboard",
6060
"//src/components-examples/cdk/a11y",
61+
"//src/components-examples/cdk/layout",
6162
"//src/components-examples/cdk/overlay",
6263
"//src/components-examples/cdk-experimental/menu",
6364
"//src/components-examples/cdk-experimental/popover-edit",
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 = "layout",
7+
srcs = glob(["**/*.ts"]),
8+
assets = glob([
9+
"**/*.html",
10+
"**/*.css",
11+
]),
12+
module_name = "@angular/components-examples/cdk/layout",
13+
deps = [
14+
"//src/cdk/layout",
15+
],
16+
)
17+
18+
filegroup(
19+
name = "source-files",
20+
srcs = glob([
21+
"**/*.html",
22+
"**/*.css",
23+
"**/*.ts",
24+
]),
25+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** No CSS for this example */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<p>
2+
Resize your browser window to see the current screen size change.
3+
</p>
4+
<p>
5+
The current screen size is <strong>{{currentScreenSize}}</strong>
6+
</p>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
2+
import {Component, OnDestroy} from '@angular/core';
3+
import {Subject} from 'rxjs';
4+
import {takeUntil} from 'rxjs/operators';
5+
6+
/** @title Respond to viewport changes with BreakpointObserver */
7+
@Component({
8+
selector: 'breakpoint-observer-overview-example',
9+
templateUrl: 'breakpoint-observer-overview-example.html',
10+
styleUrls: ['breakpoint-observer-overview-example.css']
11+
})
12+
export class BreakpointObserverOverviewExample implements OnDestroy {
13+
destroyed = new Subject<void>();
14+
currentScreenSize: string;
15+
16+
// Create a map to display breakpoint names for demonstration purposes.
17+
displayNameMap = new Map([
18+
[Breakpoints.XSmall, 'XSmall'],
19+
[Breakpoints.Small, 'Small'],
20+
[Breakpoints.Medium, 'Medium'],
21+
[Breakpoints.Large, 'Large'],
22+
[Breakpoints.XLarge, 'XLarge'],
23+
]);
24+
25+
constructor(breakpointObserver: BreakpointObserver) {
26+
breakpointObserver.observe([
27+
Breakpoints.XSmall,
28+
Breakpoints.Small,
29+
Breakpoints.Medium,
30+
Breakpoints.Large,
31+
Breakpoints.XLarge,
32+
]).pipe(takeUntil(this.destroyed)).subscribe(result => {
33+
for (const query of Object.keys(result.breakpoints)) {
34+
if (result.breakpoints[query]) {
35+
this.currentScreenSize = this.displayNameMap.get(query) ?? 'Unknown';
36+
}
37+
}
38+
});
39+
}
40+
41+
ngOnDestroy() {
42+
this.destroyed.next();
43+
this.destroyed.complete();
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {LayoutModule} from '@angular/cdk/layout';
2+
import {NgModule} from '@angular/core';
3+
import {
4+
BreakpointObserverOverviewExample,
5+
} from './breakpoint-observer-overview/breakpoint-observer-overview-example';
6+
7+
export {BreakpointObserverOverviewExample};
8+
9+
const EXAMPLES = [
10+
BreakpointObserverOverviewExample,
11+
];
12+
13+
@NgModule({
14+
imports: [
15+
LayoutModule,
16+
],
17+
declarations: EXAMPLES,
18+
exports: EXAMPLES,
19+
entryComponents: EXAMPLES,
20+
})
21+
export class CdkLayoutExamplesModule {
22+
}
23+

src/dev-app/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ng_module(
4545
"//src/dev-app/grid-list",
4646
"//src/dev-app/icon",
4747
"//src/dev-app/input",
48+
"//src/dev-app/layout",
4849
"//src/dev-app/list",
4950
"//src/dev-app/live-announcer",
5051
"//src/dev-app/mdc-autocomplete",

src/dev-app/dev-app/dev-app-layout.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class DevAppLayout {
5656
{name: 'Icon', route: '/icon'},
5757
{name: 'Input', route: '/input'},
5858
{name: 'List', route: '/list'},
59+
{name: 'Layout', route: '/layout'},
5960
{name: 'Live Announcer', route: '/live-announcer'},
6061
{name: 'Menu', route: '/menu'},
6162
{name: 'Menubar', route: '/menubar'},

src/dev-app/dev-app/routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const DEV_APP_ROUTES: Routes = [
6161
{path: 'grid-list', loadChildren: 'grid-list/grid-list-demo-module#GridListDemoModule'},
6262
{path: 'icon', loadChildren: 'icon/icon-demo-module#IconDemoModule'},
6363
{path: 'input', loadChildren: 'input/input-demo-module#InputDemoModule'},
64+
{path: 'layout', loadChildren: 'layout/layout-demo-module#LayoutDemoModule'},
6465
{path: 'list', loadChildren: 'list/list-demo-module#ListDemoModule'},
6566
{
6667
path: 'live-announcer',

src/dev-app/layout/BUILD.bazel

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("//tools:defaults.bzl", "ng_module", "sass_binary")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ng_module(
6+
name = "layout",
7+
srcs = glob(["**/*.ts"]),
8+
assets = [
9+
"layout-demo.html",
10+
":layout_demo_scss",
11+
],
12+
deps = [
13+
"//src/cdk/layout",
14+
"//src/components-examples/cdk/layout",
15+
"@npm//@angular/router",
16+
],
17+
)
18+
19+
sass_binary(
20+
name = "layout_demo_scss",
21+
src = "layout-demo.scss",
22+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.io/license
7+
*/
8+
9+
import {CommonModule} from '@angular/common';
10+
import {NgModule} from '@angular/core';
11+
import {RouterModule} from '@angular/router';
12+
import {LayoutDemo} from './layout-demo';
13+
import {CdkLayoutExamplesModule} from '@angular/components-examples/cdk/layout';
14+
15+
@NgModule({
16+
imports: [
17+
CommonModule,
18+
CdkLayoutExamplesModule,
19+
RouterModule.forChild([{path: '', component: LayoutDemo}]),
20+
],
21+
declarations: [LayoutDemo],
22+
})
23+
export class LayoutDemoModule { }

src/dev-app/layout/layout-demo.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h2>Respond to viewport changes with BreakpointObserver</h2>
2+
<breakpoint-observer-overview-example></breakpoint-observer-overview-example>

src/dev-app/layout/layout-demo.scss

Whitespace-only changes.

src/dev-app/layout/layout-demo.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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.io/license
7+
*/
8+
9+
import {Component} from '@angular/core';
10+
11+
12+
@Component({
13+
selector: 'layout-demo',
14+
templateUrl: 'layout-demo.html',
15+
styleUrls: ['layout-demo.css'],
16+
})
17+
export class LayoutDemo { }

0 commit comments

Comments
 (0)