Last active
February 24, 2022 16:37
-
-
Save jhades/6e2945497d38b1d794172bd728e47bba to your computer and use it in GitHub Desktop.
Angular Responsive Design: Complete Guide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<side-menu *ngIf="!hideSideMenu"> ... </side-menu> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container" [ngClass]="{'is-phone-portrait': isPhonePortrait}"> | |
.... | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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