MDA Dark Mode detector PCF for Custom Pages and Embedded Canvas Apps

MDA Dark Mode detector PCF for Custom Pages and Embedded Canvas Apps

šŸŽ‰ I Built a PCF Control: Meet the MDA Dark Mode Detector!

Hey folks, I’m absolutely buzzing to share something I’ve been tinkering with—a brand-new Power Apps Component Framework (PCF) control called the MDA Dark Mode Detector! If you’re into Power Apps and Model-Driven Apps, this one’s for you. Let me tell you the story of how it came to be and why it’s a handy little tool.

šŸ•µļøā€ā™‚ļø The Spark: A Sneaky Dark Mode Screenshot

Picture this: I’m sifting through my support inbox, and a user sends me a screenshot of a Model-Driven App. Bam—there’s Dark Mode staring me right in the face, with &flags=themeOptiondarkmode chilling in the URL. I was like, ā€œHold up, what?!ā€ Microsoft doesn’t officially support Dark Mode in Model-Driven Apps, but it’s out there in the wild, and users are finding it!

Here’s the kicker: if you’re building Custom Pages or embedded Canvas Apps, there’s 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—think dynamic colors, icons, or even conditional logic. But how?

šŸ’” The Inspiration: A Push from David Rivard

I’d just read this ace article by David Rivard about prepping PCF controls for Dark Mode, and it got my gears turning. David’s piece was all about making controls Dark Mode-ready, and I thought, ā€œRight, can I pull this off in a Custom Page or Canvas App?ā€ I was hyped to give it a go.

First hurdle: the context.fluentDesignLanguage.isDarkTheme property, which is supposed to tell you if Dark Mode is on, is a complete no-show in Canvas Apps. Gutted! I could’ve thrown in the towel, but I’m stubborn like that. If the Power Apps context wasn’t going to play ball, maybe the browser’s DOM could save the day.

šŸ› ļø The Solution: DOM Diving to the Rescue

I figured I’d 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—specifically the “—changes its background color like clockwork:

  • Dark Mode: rgb(10,10,10) (proper moody black).
  • Light Mode: rgb(240,240,240) (crisp, clean white).

I built the control to check this element’s background color and output true for Dark Mode or false for Light Mode. And guess what? It bloody works! šŸ˜Ž

āš™ļø How the MDA Dark Mode Detector Works

The MDA Dark Mode Detector is dead simple but does exactly what it says on the tin:

  • It looks at the nav bar’s background color via document.querySelector("#id-30[data-id='navbar-container']").
  • If it’s rgb(10,10,10), it outputs true (Dark Mode).
  • If it’s rgb(240,240,240), it outputs false (Light Mode).
  • If it can’t find the element (or just to be future-proof), it tries context.fluentDesignLanguage.isDarkTheme first, but that’s more of a backup plan.

The control is super lightweight—no fancy UI, just a clean string output you can bind to a Label or use in your app’s logic. It plays nice with Custom Pages, Canvas Apps, and Model-Driven App forms, so you can chuck it anywhere you need theme detection.

🌟 Why It’s Useful

With users sneaking Dark Mode into Model-Driven Apps via that &flags=themeOptiondarkmode trick, it’s a real-world problem-solver, the control allows you to adjust your Custom Page or Canvas based on the theme.

šŸš€ Get It and Give It a Go!

Ready to try it? Head over to my GitHub repo, where you’ll find the solution files and a straightforward README to get you started. It’s all there, ready to drop into your Power Apps environment:

šŸ‘‰ github.com/P3N-101/MDA-Dark-Mode-Detector

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *