{"id":3669,"date":"2024-08-27T13:39:43","date_gmt":"2024-08-27T18:39:43","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=3669"},"modified":"2024-08-27T13:39:44","modified_gmt":"2024-08-27T18:39:44","slug":"closewatcher","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/closewatcher\/","title":{"rendered":"CloseWatcher"},"content":{"rendered":"\n<p>I&#8217;m first hearing about the <code>CloseWatcher<\/code> API after running across <a href=\"https:\/\/logaretm.com\/blog\/fix-your-annoying-popups-with-the-closewatcher-api\/\">Abdelrahman Awad&#8217;s blog post<\/a> about it. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CloseWatcher\">The MDN docs are quite direct<\/a>, making the purpose clear:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Some UI components have &#8220;close behavior&#8221;, meaning that the component appears, and the user can close it when they are finished with it. For example: sidebars, popups, dialogs, or notifications.<\/p>\n\n\n\n<p>Users generally expect to be able to use a particular mechanism to close these elements, and the mechanism tends to be device-specific. For example, on a device with a keyboard it might be the&nbsp;<kbd>Esc<\/kbd>&nbsp;key, but Android might use the back button.<\/p>\n<\/blockquote>\n\n\n\n<p>So rather than bind an closing action to the <code>Esc<\/code> key, use this API, which normalizes that user action across platforms. <\/p>\n\n\n\n<p>I also like how it automatically creates a &#8220;stack&#8221; of things to close. So if there are multiple closeable things open, a close event only closes the most recently open one. <a href=\"https:\/\/codepen.io\/chriscoyier\/pen\/MWMXMwX\">I made a demo to play with this<\/a> (<strong>note the Chrome-n-friends only support!<\/strong>).<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_MWMXMwX\" src=\"\/\/codepen.io\/anon\/embed\/MWMXMwX?height=450&amp;theme-id=47434&amp;slug-hash=MWMXMwX&amp;default-tab=result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed MWMXMwX\" title=\"CodePen Embed MWMXMwX\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>A video in case it doesn&#8217;t work on your browser:<\/p>\n\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player\" style=\"\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='387' src='https:\/\/videopress.com\/embed\/a44ZpwAp?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script><\/div>\n\t\t\t\n\t\t\t\n\t\t<\/figure>\n\t\t","protected":false},"excerpt":{"rendered":"<p>I&#8217;m first hearing about the CloseWatcher API after running across Abdelrahman Awad&#8217;s blog post about it. The MDN docs are quite direct, making the purpose clear: Some UI components have &#8220;close behavior&#8221;, meaning that the component appears, and the user can close it when they are finished with it. For example: sidebars, popups, dialogs, or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3673,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29],"tags":[228,98,3,120,190],"class_list":["post-3669","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-beat","tag-closewatcher","tag-dialog","tag-javascript","tag-popover","tag-popup"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/08\/pexels-photo-942304.jpeg?fit=1880%2C1253&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=3669"}],"version-history":[{"count":1,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3669\/revisions"}],"predecessor-version":[{"id":3672,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3669\/revisions\/3672"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/3673"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=3669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=3669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=3669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}