# Getting Started

## Requirements

* npm or Yarn
* Node.js
* React 16.8+

## Installation

```bash
npm install react-color-mode

# OR

yarn add react-color-mode
```

## Usage

### Setup your styles

`react-color-mode` will set a special attribute on your `<html>` tag, allowing you to style your site based on it.

```css
/* Set default light mode colors on <html> */
:root {
  background-color: white;
  color: black;
}

/* User is in light mode, but has selected dark mode */
:root[react-color-mode="dark"] {
  background-color: black;
  color: white;
}

/* User is in dark mode and HAS NOT has selected light mode */
@media (prefers-color-scheme: dark) {
  :root:not([react-color-mode="light"]) {
    background-color: black;
    color: white;
  }
}
```

### Install the script

`react-color-mode` exports a `<script>` tag to be injected into your `<head>`. This script allows `react-color-mode` to setup the color mode before the rest of the page is loaded, preventing the page from flickering on page load. This component should be rendered *as early as possible.* See our recipes section for the best way to accomplish this in your app.

```javascript
import { ColorModeScript } from 'react-color-mode'
import ReactDOM from 'react-dom'
import App from './App'

ReactDOM.render(
  (
    <>
      <ColorModeScript />
      <App />
    </>
  ),
  document.querySelector('#react-root')
)
```

### Install the Context Provider

The easiest way to use and update the color mode is via the Context API. This can be done with `<ColorModeContextProvider>` and the `useColorMode` hook.

```javascript
// index.js
import {
  ColorModeScript,
  ColorModeContextProvider,
} from 'react-color-mode'
import ReactDOM from 'react-dom'
import App from './App'

ReactDOM.render(
  (
    <>
      <ColorModeScript />
      <ColorModeContextProvider>
        <App />
      </ColorModeContextProvider>
    </>
  ),
  document.querySelector('#react-root')
)
```

```javascript
// App.js
import { useColorMode } from 'react-color-mode'

export default function App() {
  const {
    colorMode,
    updateColorMode,
  } = useColorMode()

  return (
    <>
      <p>The current color mode is: {colorMode}</p>
      <button onClick={() => updateColorMode('dark')}>Dark mode</button>
      <button onClick={() => updateColorMode('light')}>Light mode</button>
      <button onClick={() => updateColorMode('system')}>System mode</button>
    </>
  )
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://react-color-mode.trezy.com/getting-started.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
