npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-theme-detector

v1.2.0

Published

快速适配深色模式的工具

Downloads

31

Readme

react-theme-detector

Npm package version Npm package total downloads Npm package license

介绍

安装

$ npm i react-theme-detector

$ pnpm i react-theme-detector

$ yarn add react-theme-detector

推荐使用的 node 版本和 npm 版本

{
  "node": ">=14.x.x",
  "npm": ">=6.x.x"
}

要求的 react 版本

{
  "react": ">=16.8.0"
}

使用教程

index.tsx

import { Theme, useTheme } from 'react-theme-detector';

import './index.scss';

/**
 * 原来的根组件
 */
function Home() {
  /** 是否为深色模式 */
  const isDarkMode = useTheme();
  /** 对应模式的文案 */
  const text = isDarkMode ? '深色' : '浅色';

  return <div className="home-page">{`当前为 “${text}” 模式`}</div>;
}

/**
 * 需要将根组件传入 `Theme` 组件中,否则 `useTheme` 无法使用
 */
export default function HomePage() {
  return (
    <Theme>
      <Home />
    </Theme>
  );
}

index.scss

/* 浅色模式 */
.home-page {
  color: blue;
}

/* 暗黑模式(根据自己项目中的CSS来选择对应的那一个) */
/* 在普通 CSS 中使用 */
body[dark-mode] {
  .home-page {
    color: green;
  }
}

/* 在 CSS Modules 中使用 */
:global(body[dark-mode]) {
  .home-page {
    color: green;
  }
}

如果是轻度使用的话,可以采用下方的这种方式引入并使用

import { useDarkMode } from 'react-theme-detector';

import './index.scss';

/**
 * 标题组件
 */
export default function Title() {
  /** 是否为深色模式 */
  const isDarkMode = useDarkMode();
  /** 对应模式的标题 */
  const text = isDarkMode ? '天黑请闭眼' : '天亮请睁眼';

  return <div>{text}</div>;
}

useDarkMode 可接收回调函数作为参数:

/**
 * 回调函数,返回当前是否为深色模式
 */
type Callback = (isDarkMode: boolean) => void;

declare function useDarkMode(callback?: Callback): boolean;

Theme 组件的 props 如下:

| 参数 | 类型 | 默认值 | 说明 | | :-------: | :-----: | :---------: | :------: | | disable | boolean | false | 禁用检测 | | tagName | string | "dark-mode" | 标记名称 |