All Blog Articles

Force Light or Dark Mode in PayloadCMS | Tips & Tricks

josh
joshuaMay 19, 2025
Control your PayloadCMS backend appearance with forced light/dark mode implementation - no more inconsistent themes!

Hi everyone! Welcome to our comprehensive guide on forcing a light mode or dark mode in your Payload backend. In this tutorial, we will explore the step-by-step process to control theme settings, ensuring a consistent user experience in your Payload NextJS applications. Whether you're a novice or a seasoned developer, this guide is designed to help you understand how to enforce your desired theme and hide user settings effectively.

This article provides a detailed written step-by-step tutorial on forcing light or dark mode in PayloadCMS. For visual learners or those who prefer following along with video instruction, you can watch our comprehensive video guide on YouTube at https://www.youtube.com/watch?v=Gg_o1X8IBu4. The video covers the same content but offers additional context and real-time demonstrations of each step in the process, making it easier to implement theme control in your own PayloadCMS projects.

Table of Contents


Understanding the Importance of Theme Control

In modern web applications, user interface consistency is key to providing a seamless experience. By controlling the theme settings in your Payload NextJS project, you can ensure that your application maintains a specific aesthetic and usability standard. This is especially pertinent in scenarios where branding is essential or when user settings might conflict with your design goals.

In this guide, we'll focus on:

  • Hiding user settings for theme toggling.
  • Enforcing a specific theme regardless of user preferences.
  • Implementing a custom provider in your Payload config.
  • Manipulating the DOM using useEffect.
  • Styling adjustments to hide theme settings.

Hiding User Settings for Theme Toggle

To begin with, we need to hide the settings that allow users to toggle between light and dark modes. This is crucial because we want to enforce a specific theme, and allowing users to change it would contradict our goal.

As shown in the unmodified Payload backend, users typically have the option to select their theme preference. However, we will be overriding this functionality. To achieve this, we will modify the Payload configuration to hide the theme toggle options.


Enforcing Light Mode Regardless of User Settings

Next, let’s discuss how to enforce light mode in your Payload NextJS application. By default, the Payload backend may follow the system settings, which means if your system is set to dark mode, the backend could reflect that. Our goal is to ensure that light mode is always displayed.

In the HTML structure of the Payload backend, there is a data-theme attribute that we need to manipulate. This attribute determines whether the application is in light or dark mode. By setting this attribute to always reflect light mode, we can ensure that our theme is consistent.

Adding a Custom Provider in Payload Config

The first step to achieving this is to create a custom provider in your Payload configuration. Here’s how you can do it:

  1. Open your Payload configuration file.
  2. Navigate to the admin section and locate the components attribute.
  3. Add a new provider to the array of providers.

We will name our provider ForceThemeProvider. Currently, this provider does not exist, so we will create it next.

payloadcms-config-force-theme-provider.jpg


Creating the ForceThemeProvider Component

Now that we have defined our provider in the configuration, let’s create the actual component. Follow these steps:

  1. Create a new file named ForceThemeProvider.jsx.
  2. In this file, create a default component that will wrap around the children components.
  3. Implement the useEffect hook to manipulate the DOM.

Here’s a simple structure for your ForceThemeProvider.jsx:

import React, { useEffect } from 'react';

const ForceThemeProvider = ({ children }) => {
    useEffect(() => {
        document.documentElement.setAttribute('data-theme', 'light');
    }, []);

    return <>{children};
};

export default ForceThemeProvider;

In this code, we are using the useEffect hook to set the data-theme attribute of the documentElement to light. This will ensure that the Payload backend always displays in light mode.


Using useEffect to Manipulate the DOM

The useEffect hook is a powerful feature in React that allows you to perform side effects in function components. In our case, we are using it to manipulate the DOM directly.

When the component mounts, the effect will run, setting the data-theme attribute. This approach is ideal because it only runs once after the first render, ensuring that the theme is set appropriately without causing unnecessary re-renders.


Hiding the Theme Settings in CSS

Now, we need to hide the theme toggle settings from the user interface to prevent any confusion. To do this, we’ll add some CSS rules to ensure that the theme toggle input is not displayed.

We can achieve this by targeting the parent div of the theme input field. Here’s how you can hide it:

div#field-theme {
    display: none;
}

By adding this CSS rule, we effectively remove the theme toggle from view, ensuring that users cannot alter the theme settings.

payloadcms-theme-toggle-inspector-view.jpg


Conclusion

In conclusion, controlling the theme settings in your Payload NextJS application is essential for maintaining a consistent user experience. By following the steps outlined in this guide, you can easily enforce a specific theme and hide user settings that may interfere with your design objectives.

If you found this tutorial helpful, please consider sharing your thoughts in the comments below. Your feedback is invaluable to us, and we're eager to hear your suggestions for future tutorials. Don't forget to hit that like button and subscribe for more in-depth tech content!

FAQs

1. Can I switch between light and dark modes later?

Yes, you can modify the ForceThemeProvider component to switch between light and dark modes based on certain conditions or user preferences.

2. What if my Payload version is different?

This approach should work for various versions of Payload, but always refer to the official documentation for any version-specific changes.

3. Is it possible to customize the theme further?

Absolutely! You can enhance the theme by adding custom styles and components to your Payload configuration.

4. How does this affect performance?

Using useEffect for DOM manipulation is efficient, as it reduces unnecessary re-renders, thus maintaining good performance.

5. Where can I find more tutorials like this?

For more tutorials and updates, follow the AllAboutPayload channel on YouTube or check out their website.

Thank you for reading, and happy coding!