{"id":871,"date":"2025-05-08T19:03:27","date_gmt":"2025-05-08T19:03:27","guid":{"rendered":"https:\/\/pensplace.co.uk\/?p=871"},"modified":"2025-05-08T19:17:13","modified_gmt":"2025-05-08T19:17:13","slug":"mda-dark-mode-detector-pcf-for-custom-pages-and-embedded-canvas-apps","status":"publish","type":"post","link":"https:\/\/pensplace.co.uk\/index.php\/2025\/05\/08\/mda-dark-mode-detector-pcf-for-custom-pages-and-embedded-canvas-apps\/","title":{"rendered":"MDA Dark Mode detector PCF for Custom Pages and Embedded Canvas Apps"},"content":{"rendered":"<div id=\"bsf_rt_marker\"><\/div>\n<h2 class=\"wp-block-heading\" id=\"\ud83c\udf89-i-built-a-pcf-control-meet-the-mda-dark-mode-detector\">\ud83c\udf89 I Built a PCF Control: Meet the MDA Dark Mode Detector!<\/h2>\n\n\n\n<p>Hey folks, I\u2019m absolutely buzzing to share something I\u2019ve been tinkering with\u2014a brand-new Power Apps Component Framework (PCF) control called the <strong>MDA Dark Mode Detector<\/strong>! If you\u2019re into Power Apps and Model-Driven Apps, this one\u2019s for you. Let me tell you the story of how it came to be and why it\u2019s a handy little tool.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\ud83d\udd75\ufe0f\u2642\ufe0f-the-spark-a-sneaky-dark-mode-screenshot\">\ud83d\udd75\ufe0f\u200d\u2642\ufe0f The Spark: A Sneaky Dark Mode Screenshot<\/h2>\n\n\n\n<p>Picture this: I\u2019m sifting through my support inbox, and a user sends me a screenshot of a Model-Driven App. Bam\u2014there\u2019s Dark Mode staring me right in the face, with <code class=\"\" data-line=\"\">&amp;flags=themeOptiondarkmode<\/code> chilling in the URL. I was like, \u201cHold up, what?!\u201d Microsoft doesn\u2019t officially support Dark Mode in Model-Driven Apps, but it\u2019s out there in the wild, and users are finding it!<\/p>\n\n\n\n<p>Here\u2019s the kicker: if you\u2019re building Custom Pages or embedded Canvas Apps, there\u2019s no built-in way to check if the host app is rocking Dark Mode. That really bugged me. I wanted to make apps that could adapt their style or logic based on the theme\u2014think dynamic colors, icons, or even conditional logic. But how?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\ud83d\udca1-the-inspiration-a-push-from-david-rivard\">\ud83d\udca1 The Inspiration: A Push from <a href=\"https:\/\/itmustbecode.com\/how-to-get-pcf-controls-ready-for-dark-mode-in-model-driven-apps\/\">David Rivard<\/a><\/h2>\n\n\n\n<p>I\u2019d just read this ace article by David Rivard about prepping PCF controls for Dark Mode, and it got my gears turning. David\u2019s piece was all about making controls Dark Mode-ready, and I thought, \u201cRight, can I pull this off in a Custom Page or Canvas App?\u201d I was hyped to give it a go.<\/p>\n\n\n\n<p>First hurdle: the <code class=\"\" data-line=\"\">context.fluentDesignLanguage.isDarkTheme<\/code> property, which is supposed to tell you if Dark Mode is on, is a complete no-show in Canvas Apps. Gutted! I could\u2019ve thrown in the towel, but I\u2019m stubborn like that. If the Power Apps context wasn\u2019t going to play ball, maybe the browser\u2019s DOM could save the day.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\ud83d\udee0\ufe0f-the-solution-dom-diving-to-the-rescue\">\ud83d\udee0\ufe0f The Solution: DOM Diving to the Rescue<\/h2>\n\n\n\n<p>I figured I\u2019d poke around the DOM to find an element that flips when Dark Mode kicks in. After a bit of faffing about in Developer Tools (you know, the usual inspect-element chaos), I struck gold. The navigation bar\u2014specifically the &#8220;\u2014changes its background color like clockwork:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Dark Mode<\/strong>: <code class=\"\" data-line=\"\">rgb(10,10,10)<\/code> (proper moody black).<\/li>\n\n\n\n<li><strong>Light Mode<\/strong>: <code class=\"\" data-line=\"\">rgb(240,240,240)<\/code> (crisp, clean white).<\/li>\n<\/ul>\n\n\n\n<p>I built the control to check this element\u2019s background color and output <code class=\"\" data-line=\"\">true<\/code> for Dark Mode or <code class=\"\" data-line=\"\">false<\/code> for Light Mode. And guess what? It bloody works! \ud83d\ude0e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u2699\ufe0f-how-the-mda-dark-mode-detector-works\">\u2699\ufe0f How the MDA Dark Mode Detector Works<\/h2>\n\n\n\n<p>The <strong>MDA Dark Mode Detector<\/strong> is dead simple but does exactly what it says on the tin:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It looks at the nav bar\u2019s background color via <code class=\"\" data-line=\"\">document.querySelector(&quot;#id-30[data-id=&#039;navbar-container&#039;]&quot;)<\/code>.<\/li>\n\n\n\n<li>If it\u2019s <code class=\"\" data-line=\"\">rgb(10,10,10)<\/code>, it outputs <code class=\"\" data-line=\"\">true<\/code> (Dark Mode).<\/li>\n\n\n\n<li>If it\u2019s <code class=\"\" data-line=\"\">rgb(240,240,240)<\/code>, it outputs <code class=\"\" data-line=\"\">false<\/code> (Light Mode).<\/li>\n\n\n\n<li>If it can\u2019t find the element (or just to be future-proof), it tries <code class=\"\" data-line=\"\">context.fluentDesignLanguage.isDarkTheme<\/code> first, but that\u2019s more of a backup plan.<\/li>\n<\/ul>\n\n\n\n<p>The control is super lightweight\u2014no fancy UI, just a clean string output you can bind to a Label or use in your app\u2019s logic. It plays nice with Custom Pages, Canvas Apps, and Model-Driven App forms, so you can chuck it anywhere you need theme detection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\ud83c\udf1f-why-its-useful\">\ud83c\udf1f Why It\u2019s Useful<\/h2>\n\n\n\n<p>With users sneaking Dark Mode into Model-Driven Apps via that <code class=\"\" data-line=\"\">&amp;flags=themeOptiondarkmode<\/code> trick, it\u2019s a real-world problem-solver, the control allows you to adjust your Custom Page or Canvas based on the theme.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\ud83d\ude80-get-it-and-give-it-a-go\">\ud83d\ude80 Get It and Give It a Go!<\/h2>\n\n\n\n<p>Ready to try it? Head over to my GitHub repo, where you\u2019ll find the solution files and a straightforward <code class=\"\" data-line=\"\">README<\/code> to get you started. It\u2019s all there, ready to drop into your Power Apps environment:<\/p>\n\n\n\n<p>\ud83d\udc49 <a href=\"https:\/\/github.com\/P3N-101\/MDA-Dark-Mode-Detector\">github.com\/P3N-101\/MDA-Dark-Mode-Detector<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udf89 I Built a PCF Control: Meet the MDA Dark Mode Detector! Hey folks, I\u2019m absolutely buzzing to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":872,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[30,31],"tags":[53,52],"class_list":["post-871","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","category-power-apps","tag-darkmode","tag-pcf"],"featured_image_src":"https:\/\/pensplace.co.uk\/wp-content\/uploads\/2025\/05\/detectorinaction.png","author_info":{"display_name":"Pen","author_link":"https:\/\/pensplace.co.uk\/index.php\/author\/pen\/"},"_links":{"self":[{"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=871"}],"version-history":[{"count":3,"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/871\/revisions"}],"predecessor-version":[{"id":877,"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/871\/revisions\/877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/media\/872"}],"wp:attachment":[{"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pensplace.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}