Angular Flex Layout is very useful while working with Angular Material, Is there any way to consume the flex layout API in the TypeScript file?
For example from this URL, is there any way to get the MediaQueries values in the TypeScript file?
breakpoint mediaQuery
xs 'screen and (max-width: 599px)'
sm 'screen and (min-width: 600px) and (max-width: 959px)'
md 'screen and (min-width: 960px) and (max-width: 1279px)'
lg 'screen and (min-width: 1280px) and (max-width: 1919px)'
xl 'screen and (min-width: 1920px) and (max-width: 5000px)'
lt-sm 'screen and (max-width: 599px)'
lt-md 'screen and (max-width: 959px)'
lt-lg 'screen and (max-width: 1279px)'
lt-xl 'screen and (max-width: 1919px)'
gt-xs 'screen and (min-width: 600px)'
gt-sm 'screen and (min-width: 960px)'
gt-md 'screen and (min-width: 1280px)'
gt-lg 'screen and (min-width: 1920px)'
I mean in the Angular TypeScript file, I can consume the media query for SMALL
, LARGE
and MEDIUM
screen.
Yes, there is a way to use these media queries inside TypeScript files within Angular, but it's a bit hidden.
You can use the Breakpoint Observer API from Angular Material which provides you with the same media queries as used in Angular's FlexLayout API, specified in Material Design.
Predefined list of breakpoints (the widths used may not be completely obvious, but together with the media queries in Material Design it can be traced back):
This list can be expanded to your needs. A simple example can be found here as a StackBlitz.
Steps to follow:
LayoutModule
into yourapp.module.ts
and add it to imports.BreakpointObserver
,Breakpoints
andBreakpointState
inside your component.BreakpointObserver
to the constructor.ngOnInit
, add the observing query for a specific viewport (list), as specified inside the Breakpoints list. The list is an array and can hold multiple values:Code combined:
Final code on StackBlitz. Feel free to play around to your needs.