I recently built a new WordPress block to add a toggle between light and dark modes on your website, like I have on my blog.
Download on WordPress.org / View the code on Github
The block was initially designed as a dynamic block, similar to my recent Cards block, but I converted it to a static block to allow for better style support, so that the toggle can look differently across websites.
While dynamic blocks do support styles, like the WordPress search block, it becomes quite a bit of a lift on the PHP front to account for all style classes—if you’re not applying styles to a block’s parent element, like I needed to do.
I like it. It’s simple, but you can take it as far as you want with size, color, border styles—and four icon sets—like this here:

Also interesting, I used a CSS variable for calculating the size of elements throughout the block, which made it super simple to have multiple sizes without many additional lines of CSS.
All in all, I’d say the Dark Mode Toggle block is a nice example of a block affording creative expression with smart defaults. Maybe you’ll learn something new by checking out the code, or have suggestions to make it better. A win either way.
I’m also planning on adding it to the Block Directory as a block plugin. If you’re curious how that works, I have a primer on adding blocks to the block directory.

Leave a Reply