Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Last active February 9, 2021 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetanelBasal/a6008c6cbf4d90e5a11e80286d3e1dd3 to your computer and use it in GitHub Desktop.
Save NetanelBasal/a6008c6cbf4d90e5a11e80286d3e1dd3 to your computer and use it in GitHub Desktop.
import { untilDestoryed, UntilDestroy } from '@ngneat/until-destroy';
const breakpoints = { ... };
@UntilDestroy()
@Directive({
selector: '[matches]'
})
export class MatchesDirective implements OnInit {
@Input() matches: keyof typeof breakpoints;
constructor(private tpl: TemplateRef<any>,
private breakpointObserver: BreakpointObserver,
private vcr: ViewContainerRef) {
}
ngOnInit() {
const breakpoint = breakpoints[this.matches];
fromMediaQuery(breakpoint).pipe(untilDestroyed(this)).subscribe(matches => {
matches ? this.vcr.createEmbeddedView(this.tpl) : this.vcr.clear();
});
}
}
@gjungb
Copy link

gjungb commented Feb 9, 2021

Very nice solution, indeed.
I guess untilDestoryed should be untilDestroyed :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment