Skip to main content
/
/
/
useApplyColorMode()

useApplyColorMode()

import useApplyColorMode from "@intility/bifrost-react/hooks/useApplyColorMode";

You might not need this hook

CSS color mode class names

In most apps you don't need to force a color mode, you can safely just let bifrost apply light or dark mode based on the prefers-color-scheme media query.

If you want to always force either light or dark mode you can use a class name on <html> element.

If you're using server-side rendering (like Next.js) you can programatically control the <html> root element directly, and don't need this hook.

Apply a color mode

For single-page apps we supply the useApplyColorMode() hook which will apply appropriate class name to the root element for you.

import useApplyColorMode from "@intility/bifrost-react/hooks/useApplyColorMode"; function MyApp() { // apply "bf-darkmode" class to <html> useApplyColorMode("dark"); }
import useApplyColorMode from "@intility/bifrost-react/hooks/useApplyColorMode"; function MyApp() { // apply "bf-darkmode" class to <html> useApplyColorMode("dark"); }

Valid values are:

  • "light", equivalent to <html class="bf-lightmode">
  • "dark", equivalent to <html class="bf-darkmode">
  • "system", equivalent to <html>

Color mode picker

If you want a color mode picker in your app, see the following example:

Color mode picker example