Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active February 24, 2022 16:37
Show Gist options
  • Save jhades/6e2945497d38b1d794172bd728e47bba to your computer and use it in GitHub Desktop.
Save jhades/6e2945497d38b1d794172bd728e47bba to your computer and use it in GitHub Desktop.
Angular Responsive Design: Complete Guide
export declare const Breakpoints: {
XSmall: string;
Small: string;
Medium: string;
Large: string;
XLarge: string;
Handset: string;
Tablet: string;
Web: string;
HandsetPortrait: string;
TabletPortrait: string;
WebPortrait: string;
HandsetLandscape: string;
TabletLandscape: string;
WebLandscape: string;
};
import {Breakpoints} from '@angular/cdk/layout';
console.log('Web ' + Breakpoints.Web);
console.log('WebLandscape ' + Breakpoints.WebLandscape);
console.log('WebPortrait ' + Breakpoints.WebPortrait);
console.log('Tablet ' + Breakpoints.Tablet);
console.log('TabletPortrait ' + Breakpoints.TabletPortrait);
console.log('TabletLandscape ' + Breakpoints.TabletLandscape);
console.log('Handset ' + Breakpoints.Handset);
console.log('HandsetLandscape ' + Breakpoints.HandsetLandscape);
console.log('HandsetPortrait ' + Breakpoints.HandsetPortrait);
console.log('XSmall ' + Breakpoints.XSmall);
console.log('Small ' + Breakpoints.Small);
console.log('Medium ' + Breakpoints.Medium);
console.log('Large ' + Breakpoints.Large);
console.log('XLarge ' + Breakpoints.XLarge);
@Component()
export class ResponsiveComponent implements OnInit {
constructor(private responsive: BreakpointObserver) {
}
ngOnInit() {
this.responsive.observe(Breakpoints.HandsetLandscape)
.subscribe(result => {
if (result.matches) {
console.log("screens matches HandsetLandscape");
}
});
}
}
this.responsive.observe([
Breakpoints.TabletPortrait,
Breakpoints.HandsetLandscape])
.subscribe(result => {
const breakpoints = result.breakpoints;
if (breakpoints[Breakpoints.TabletPortrait]) {
console.log("screens matches TabletPortrait");
}
else if (breakpoints[Breakpoints.HandsetLandscape]) {
console.log("screens matches HandsetLandscape");
}
});
@Component()
export class ResponsiveComponent implements OnInit {
hideSideMenu = false;
constructor(private responsive: BreakpointObserver) {
}
ngOnInit() {
this.responsive.observe([
Breakpoints.HandsetLandscape,
Breakpoints.TableLandscape,
])
.subscribe(result => {
this.hideSideMenu = false;
if (result.matches) {
this.hideSideMenu = true;
}
});
}
}
<side-menu *ngIf="!hideSideMenu"> ... </side-menu>
@Component()
export class ResponsiveComponent implements OnInit {
isPhonePortrait = false;
constructor(private responsive: BreakpointObserver) {
}
ngOnInit() {
this.responsive.observe(Breakpoints.HandsetPortrait)
.subscribe(result => {
this.isPhonePortrait = false;
if (result.matches) {
this.isPhonePortrait = true;
}
});
}
}
<div class="container" [ngClass]="{'is-phone-portrait': isPhonePortrait}">
....
</div>
container.is-phone-portrait {
margin: 0;
padding:0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment