What is @angular/flex-layout?
@angular/flex-layout is a powerful layout library for Angular that provides a sophisticated API using Flexbox CSS and mediaQuery. It allows developers to create responsive and adaptive layouts with ease.
What are @angular/flex-layout's main functionalities?
Responsive Layouts
This feature allows you to create responsive layouts that adapt to different screen sizes. The example shows a row layout that changes to a column layout on extra-small screens.
<div fxLayout="row" fxLayout.xs="column">
<div fxFlex>Item 1</div>
<div fxFlex>Item 2</div>
<div fxFlex>Item 3</div>
</div>
Grid Layouts
This feature allows you to create grid layouts with flexible spacing and alignment. The example shows a row layout with items taking 25%, 50%, and 25% of the available space, respectively.
<div fxLayout="row" fxLayoutGap="10px">
<div fxFlex="25">Item 1</div>
<div fxFlex="50">Item 2</div>
<div fxFlex="25">Item 3</div>
</div>
Alignment and Justification
This feature allows you to align and justify content within a layout. The example shows a row layout with content centered both horizontally and vertically.
<div fxLayout="row" fxLayoutAlign="center center">
<div fxFlex>Centered Item</div>
</div>
Show/Hide Elements
This feature allows you to conditionally show or hide elements based on screen size. The example shows text that is hidden on screens larger than small.
<div fxShow fxHide.gt-sm>
This text is hidden on screens larger than small.
</div>
Other packages similar to @angular/flex-layout
ngx-flexible-layout
ngx-flexible-layout is another Angular library that provides a flexible layout system using CSS Flexbox. It offers similar functionalities to @angular/flex-layout but with a different API and potentially fewer features.
Flex Layout
)
Angular Flex Layout provides a sophisticated layout API using FlexBox CSS + mediaQuery.
This module provides Angular (v2.x and higher) developers with component layout features using a
custom Layout API, mediaQuery observables,and injected DOM flexbox-2016 css stylings.
The Layout engine intelligently automates the process of applying appropriate FlexBox CSS to
browser view hierarchies. This automation also addresses many of the complexities and workarounds
encountered with the traditional, manual, CSS-only application of Flexbox CSS.
The real power of Flex Layout, however, is its responsive engine. The Responsive API
enables developers to easily specify different layouts, sizing, visibilities for different
viewport sizes and display devices.
The sources for this package are in the Flex-Layout repository.
Please file issues and pull requests against that repo.
License: MIT
Installation
The latest release of Angular Flex-Layout can be installed from npm
npm install @angular/flex-layout
Playing with the latest changes from master is also possible
npm install https://github.com/angular/flex-layout-builds.git
IMPORTANT API CHANGE
Directive selectors are now camelCase (instead of dash-case); as specified in the Angular Style Guide (Q4, 2016):
- @Component selectors always use dash-case
- @Directive selectors always use camelCase
Except when the directive is pretending to be a component (i.e, if it had a template it would be '').
- @Directive properties are prefixed with namespace info
@Component properties are not
<div class="flex-container"
fxLayout="row"
fxLayout.xs="column"
fxLayoutAlign="center center"
fxLayoutAlign.xs="start">
<div class="flex-item" fxFlex="20%" fxFlex.xs="40%"> </div>
<div class="flex-item" fxFlex> </div>
<div class="flex-item" fxFlex="25px"> </div>
</div>
Quick Links
Developers
Demos
Templates
Why choose Flex-Layout
While other Flexbox CSS libraries are implementations of:
- pure CSS-only implementations, or
- the JS+CSS Stylesheets implementation of Angular Material v1.x Layouts.
Angular Flex Layout - in contrast - is a pure-Typescript UI Layout engine with an implementation that:
- uses HTML attributes (aka Layout API) to specify the layout configurations
- is currently only available for Angular (v2.x or higher) Applications.
- is independent of Angular Material (v1 or v2).
- requires no external stylesheets.
- requires Angular v2.x or higher.
Browser Support
2.0.0-beta.5 (2017-02-09)
Bug Fixes
- breakpoints: resolve 1px hole between lg -> xl breakpoints (#159) (d78527c), closes #149
- FlexLayoutModule: remove use of static ngModule.forRoot() (#167) (86010bf)
- FlexLayoutModule: add observable-media-service to exported barrel (#139) (b7dffaa)
- fxFlex: fix use of values with 'auto' (#122) (04d24d5), closes #120
- fxFlex: prevent setting min/max-size when grow/shrink is zero (#160) (942939e), closes #153
- fxHide,fxShow: restore orig display mode and more... (#143) (d269d73), closes #140 #141
- fxHide,fxShow: fix standalone breakpoint selectors (#121) (0ca7d07), closes #62 #59 #105
- fxLayoutGap: add gaps to dynamic content (#124) (6482c12), closes #95
- fxLayoutGap: fxLayoutWrap to apply gap logic for reverse directions (#148) (9f7137e), closes #108
- fxLayoutGap: skip hidden element nodes (#145) (6c45b35), closes #136
- fxClass,fxStyle: enable raw input caching (#173) (d5b283c)
- ObservableMedia: expose
asObservable()
for rxjs operators (#133) (6e46561), closes #125
Features
- API: use protected access to allow API directives to be easily extended (#163) (e6bc451)
- fxClass,fxStyle: add responsive support for ngClass and ngStyle (#170) (f57a63d)
- ObservableMedia: use ObservableMedia class as provider token (#158) (dad69fe)
BREAKING CHANGES
- ObservableMedia: Deprecated use of
ObservableMediaService
opaque token. Developers now simply use the ObservableMedia class to inject the service. - FlexLayoutModule: Previously releases used FlexLayoutModule.forRoot(); This has been deprecated.
before
constructor( @Inject(ObserverableMediaService) media:any ) { ... }
after
constructor(private media:ObservableMedia) { ... }
- ObservableMedia: use class
ObservableMedia
to inject instance of service - use
MediaService::asObservable()
to get instance of observable
// RxJS
import 'rxjs/add/operator/map';
import {ObservableMedia} from '@angular/flex-layout';
@Component({ ... })
export class MyComponent {
constructor( media:ObservableMedia ) {
media.asObservable()
.map( (change:MediaChange) => change.mqAlias == 'md' )
.subscribe((change:MediaChange) => {
let state = change ? `'${change.mqAlias}' = (${change.mediaQuery})` : "";
console.log( state );
});
}
}
Previously releases used FlexLayoutModule.forRoot().
This has been deprecated and will output a console.warn()
if used.
-before-
@NgModule({
declarations : [...],
imports : [
CommonModule,
FlexLayoutModule.forRoot()
]
})
export class DemosResponsiveLayoutsModule { }
-after-
@NgModule({
declarations : [...],
imports : [ CommonModule, FlexLayoutModule ]
})
export class DemosResponsiveLayoutsModule { }
<a name="2.0.0-beta.4"></a>