There is a hidden page you can visit at /you-are-awesome. Didn't want you to
miss out on the fun!
Content has not been updated for more than 2 years, proceed with
caution.
How to display alternate content in dark mode using Theme UI
Theme UI provides a great pattern for implementing dark mode on your Gatsby website. If your site is primarily text, this can be as straightforward as changing a few colors in your theme and off you go.
Most websites are not so minimal and include images, icons, background patterns, etc. If you are lucky, these will all look fine in dark mode but odds are they won’t. For example images that look good with a light background might visually clash against a darker background.
Here is a pattern to conditionally display alternate content such as images in dark mode using the useColorMode hook from theme-ui.
Import the hook from theme-ui
Check the current color mode
Conditionally display an image based on the color mode
Or conditionally apply a css filter
This will work best with images that are already single toned, e.g. a black and white logo. The code belowis using the sx prop from theme-ui.
A full example component switching images in dark mode