using dark mode in react-select

Viewed 2518

I think the best way to change color is using the theme props or in the doc look something like this :


import { flavourOptions } from '../data';
import Select from 'react-select';

export default () => (
  <Select
    defaultValue={flavourOptions[2]}
    label="Single select"
    options={flavourOptions}
    theme={theme => ({
      ...theme,
      borderRadius: 0,
      colors: {
        ...theme.colors,
        primary25: 'hotpink',
        primary: 'black',
      },
    })}
  />
);

but, I am not stuck at figuring out how to "connect" it dark mode togggle..

2 Answers

Here's an example with tailwind css making use of the className and classNamePrefix arguments:

React usage:

<Select
   className="my-react-select-container"
   classNamePrefix="my-react-select"
...

Tailwind CSS index.css

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
  /* .my-react-select-container {
  } */
  .my-react-select-container .my-react-select__control {
    @apply bg-white dark:bg-neutral-700 border-2 border-neutral-300 dark:border-neutral-700 hover:border-neutral-400 dark:hover:border-neutral-500;
  }

  .my-react-select-container .my-react-select__control--is-focused {
    @apply border-neutral-500 hover:border-neutral-500 dark:border-neutral-400 dark:hover:border-neutral-400 shadow-none;
  }

  .my-react-select-container .my-react-select__menu {
    @apply bg-neutral-100 dark:bg-neutral-700 border-2 border-neutral-300 dark:border-neutral-600;
  }

  .my-react-select-container .my-react-select__option {
    @apply text-neutral-600 dark:text-neutral-200 bg-neutral-100 hover:bg-neutral-200 dark:bg-neutral-700 dark:hover:bg-neutral-800;
  }
  /* .my-react-select-container .my-react-select__option--is-focused {
    @apply bg-neutral-200 dark:bg-neutral-800;
  } */

  .my-react-select-container .my-react-select__indicator-separator {
    @apply bg-neutral-400;
  }

  .my-react-select-container .my-react-select__input-container,
  .my-react-select-container .my-react-select__placeholder,
  .my-react-select-container .my-react-select__single-value {
    @apply text-neutral-600 dark:text-neutral-200;
  }
}

Result:

enter image description here

You should use CSS variables to change the colors for light and dark mode. Then you could apply dark mode styles separately using CSS or Sass, like this:

Using SCSS:

.react-select-container {
  .react-select__control {
    background-color: var(--bg-secondary);
    border-color: var(--border-color);
    transition: none;

    &:hover {
      border-color: var(--border-color);
    }
  }

  .react-select__menu {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
  }

  .react-select__option {
    background-color: var(--bg-secondary);

    &:hover {
      background-color: var(--bg-primary);
    }
  }

  .react-select__indicator-separator {
    background-color: var(--border-color);
  }

  .react-select__placeholder,
  .react-select__single-value {
    color: var(--text-primary);
  }
}

That's the way I did it, but if anyone find out a better way please contribute to this question!

Related