{"id":1344,"date":"2023-08-21T05:00:00","date_gmt":"2023-08-21T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1344"},"modified":"2023-08-18T15:06:14","modified_gmt":"2023-08-18T22:06:14","slug":"displaying-an-overlay-with-angular","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/displaying-an-overlay-with-angular\/","title":{"rendered":"Displaying an overlay with Angular"},"content":{"rendered":"\n<p>We covered <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/creating-a-dialog-in-two-lines-of-code\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to display a dialog in two lines of code<\/a> earlier. Today, I want to illustrate how we can show an overlay on any portion of the screen using the <a href=\"https:\/\/material.angular.io\/cdk\/categories\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular CDK<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>First, it&#8217;s important to mention that Angular CDK is NOT part of <a href=\"https:\/\/material.angular.io\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular Material<\/a>, and you can use the CDK independently from any component library.<\/p>\n\n\n\n<p>To install the CDK: <code>npm install @angular\/cdk<\/code><\/p>\n\n\n\n<p>Then, you would import the <code><a href=\"https:\/\/material.angular.io\/cdk\/overlay\/overview\" target=\"_blank\" rel=\"noopener\" title=\"\">OverlayModule<\/a><\/code> wherever you need it (here in a standalone component):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"133\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-23.png\" alt=\"\" class=\"wp-image-1345\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-23.png 393w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-23-300x102.png 300w\" sizes=\"auto, (max-width: 393px) 100vw, 393px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This module allows you to inject an <a href=\"https:\/\/material.angular.io\/cdk\/overlay\/api#Overlay\" target=\"_blank\" rel=\"noopener\" title=\"\">Overlay service<\/a> that can be used to create an overlay and display a component in it:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"533\" height=\"338\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-24.png\" alt=\"\" class=\"wp-image-1346\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-24.png 533w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-24-300x190.png 300w\" sizes=\"auto, (max-width: 533px) 100vw, 533px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>By default, overlays have a transparent background, so I added a panelClass for my overlay and then gave it a background color &#8211; Note that you also need to import the CSS styles from the CDK in your global CSS file:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"129\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-25.png\" alt=\"\" class=\"wp-image-1347\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-25.png 432w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-25-300x90.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>There are plenty of configuration options, and if you want to see a different approach, look at this <a href=\"https:\/\/blog.angulartraining.com\/how-to-display-an-overlay-with-angular-38345414fdad\" target=\"_blank\" rel=\"noopener\" title=\"\">tutorial for a dropdown built with an overlay<\/a>. The <a href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-umqdbm?description=An%20angular-cli%20project%20based%20on%20@angular\/animations,%20@angular\/common,%20@angular\/compiler,%20@angular\/core,%20@angular\/forms,%20@angular\/platform-browser,%20@angular\/platform-browser-dynamic,%20@angular\/router,%20core-js,%20rxjs,%20tslib%20and%20zone.js&amp;file=src%2Fglobal_styles.css,src%2Fhello%2Fhello.component.ts,src%2Fhello%2Fhello.component.html,src%2Fmain.ts&amp;title=Angular%20Starter\" target=\"_blank\" rel=\"noopener\" title=\"\">source code for my basic example is here on Stackblitz<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We covered how to display a dialog in two lines of code earlier. Today, I want to illustrate how we can show an overlay on any portion of the screen using the Angular CDK. First, it&#8217;s important to mention that Angular CDK is NOT part of Angular Material, and you can use the CDK independently [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,34],"tags":[],"class_list":["post-1344","post","type-post","status-publish","format-standard","hentry","category-angular","category-cdk"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1344"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1344\/revisions"}],"predecessor-version":[{"id":1349,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1344\/revisions\/1349"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}