{"id":1549,"date":"2023-09-29T05:00:00","date_gmt":"2023-09-29T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1549"},"modified":"2023-09-25T16:06:53","modified_gmt":"2023-09-25T23:06:53","slug":"using-the-cdk-listbox-for-custom-list-selection","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/using-the-cdk-listbox-for-custom-list-selection\/","title":{"rendered":"Using the CDK ListBox for custom list selection"},"content":{"rendered":"\n<p>The <a href=\"https:\/\/material.angular.io\/cdk\/categories\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular CDK<\/a> is an important toolkit for building Angular applications. We&#8217;ve already covered <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/displaying-an-overlay-with-angular\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to display an overlay using the CDK<\/a>. Today, let&#8217;s create a custom list selection feature using <a href=\"https:\/\/material.angular.io\/cdk\/listbox\/overview\" target=\"_blank\" rel=\"noopener\" title=\"\">CDK ListBox<\/a>.<\/p>\n\n\n\n<p>Often, HTML dropdowns are too basic to allow for meaningful customization of what gets rendered in the dropdown. This has led to the creation of several custom components, such as <a href=\"https:\/\/material.angular.io\/components\/select\/overview\" target=\"_blank\" rel=\"noopener\" title=\"\">Material Select<\/a>.<\/p>\n\n\n\n<p>But what if you&#8217;re not using a library and still need to customize a list of items for your users? Something like this, for instance:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"289\" height=\"159\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-53.png\" alt=\"\" class=\"wp-image-1550\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Implementing such a UI with HTML is not very difficult. The complexity of the task comes from getting all the expected behaviors for an accessible experience, such as keyboard interaction and focus management.<\/p>\n\n\n\n<p>This is where the <code>cdkListbox<\/code> directive shines. Here&#8217;s an example:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"330\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-54.png\" alt=\"\" class=\"wp-image-1551\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-54.png 908w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-54-300x109.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-54-768x279.png 768w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The few lines of code in this example add the following select-dropdown-like features to these basic HTML list items:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A numerical value is associated with the user selection (1, 0, or -1) thanks to <code><a href=\"https:\/\/material.angular.io\/cdk\/listbox\/overview#option-values\" target=\"_blank\" rel=\"noopener\" title=\"\">cdkOption<\/a><\/code><\/li>\n\n\n\n<li>Typeahead is available. If I click on the list and then type &#8220;o&#8221; on my keyboard, the &#8220;Okay&#8221; option gets selected.<\/li>\n\n\n\n<li>The current selection can be changed using the keyboard&#8217;s up and down keys.<\/li>\n<\/ul>\n\n\n\n<p>And there are <a href=\"https:\/\/material.angular.io\/cdk\/listbox\/overview\" target=\"_blank\" rel=\"noopener\" title=\"\">many more possibilities to explore<\/a>. For instance, enabling multiple selections in the list is as easy as adding a <code><a href=\"https:\/\/material.angular.io\/cdk\/listbox\/overview#single-vs-multiple-selection\" target=\"_blank\" rel=\"noopener\" title=\"\">cdkListboxMultiple<\/a><\/code> attribute on our list element:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"842\" height=\"53\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-55.png\" alt=\"\" class=\"wp-image-1552\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-55.png 842w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-55-300x19.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-55-768x48.png 768w\" sizes=\"auto, (max-width: 842px) 100vw, 842px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And now I can do this:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-56.png\" alt=\"\" class=\"wp-image-1553\" style=\"width:292px;height:154px\" width=\"292\" height=\"154\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-56.png 416w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-56-300x159.png 300w\" sizes=\"auto, (max-width: 292px) 100vw, 292px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can see that <a href=\"https:\/\/stackblitz.com\/edit\/akktnt?file=src%2Fexample%2Fcdk-listbox-custom-typeahead-example.html\" target=\"_blank\" rel=\"noopener\" title=\"\">example in action on Stackblitz here<\/a>. Another nice feature of the Angular CDK is that all its directives are standalone, so we can import just what we need in our components:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"556\" height=\"302\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-57.png\" alt=\"\" class=\"wp-image-1555\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-57.png 556w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-57-300x163.png 300w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The Angular CDK is an important toolkit for building Angular applications. We&#8217;ve already covered how to display an overlay using the CDK. Today, let&#8217;s create a custom list selection feature using CDK ListBox. Often, HTML dropdowns are too basic to allow for meaningful customization of what gets rendered in the dropdown. This has led to [&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,11],"tags":[],"class_list":["post-1549","post","type-post","status-publish","format-standard","hentry","category-angular","category-cdk","category-directives"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1549","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=1549"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1549\/revisions"}],"predecessor-version":[{"id":1557,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1549\/revisions\/1557"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}