react-color-mode
main
main
  • Introduction
  • Getting Started
  • Recipes
    • Next.js
  • API
    • <ColorModeScript>
    • <ColorModeContextProvider>
    • ColorModeContext
    • useColorMode
  • Other Important Stuff
    • Code of Conduct
Powered by GitBook
On this page

Was this helpful?

  1. API

useColorMode

PreviousColorModeContextNextCode of Conduct

Last updated 4 years ago

Was this helpful?

useColorMode is a hook that provides access to all of the data made available by .

Example

import { useColorMode } from 'react-color-mode'

export function DarkModeComponent() {
  const {
    colorMode,
    updateColorMode,
  } = useColorMode()
  const isDarkMode = colorMode === 'dark'

  return (
    <div style={{
      backgroundColor: isDarkMode ? 'black' : 'white',
      color: isDarkMode ? 'white' : 'black',
    }}>
      Hello!
      <button
        disabled={colorMode === 'dark'}
        onClick={() => updateColorMode('dark')}>
        Dark mode
      </button>
      <button
        disabled={colorMode === 'light'}
        onClick={() => updateColorMode('light')}>
        Light mode
      </button>
      <button
        disabled={colorMode === 'system'}
        onClick={() => updateColorMode('system')}>
        System mode
      </button>
    </div>
  )
}
<ColorModeContextProvider>