在react-select中使用深色模式

Muh*_*dan 7 javascript reactjs react-select

我认为改变颜色的最好方法是使用主题道具或在文档中看起来像这样:


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',
      },
    })}
  />
);
Run Code Online (Sandbox Code Playgroud)

但是,我并没有被困在弄清楚如何“连接”它的黑暗模式切换上。

jos*_*ias 14

这是一个使用classNameclassNamePrefix参数的 tailwind css 示例:

反应用法:

<Select
   className="my-react-select-container"
   classNamePrefix="my-react-select"
...
Run Code Online (Sandbox Code Playgroud)

顺风 CSSindex.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;
  }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述


Bre*_*sta 5

您应该使用 CSS 变量来更改浅色和深色模式的颜色。然后你可以使用 CSS 或 Sass 单独应用暗模式样式,如下所示:

使用 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

这就是我所做的,但如果有人找到更好的方法,请为这个问题做出贡献!