@dazl/color-scheme

中文文档
1.3.0发布于 2026/06/16MITES Module零依赖近 30 天下载 2.4万

Color scheme utilities and tools

原始使用文档
文档目录7 节
  1. Features
  2. Installation
  3. Usage
  4. Client-side Setup
  5. React Integration
  6. CSS Styling
  7. License

@dazl/color-scheme

A simple color scheme management library for web applications with support for light/dark themes and automatic system preference detection.

Features

  • 🌓 Light/Dark/System modes - Support for light, dark, and automatic system preference detection
  • 📱 System preference detection - Automatically responds to OS-level theme changes
  • ⚛️ React integration - Ready-to-use React hook for seamless integration
  • 💾 Persistent storage - Remembers user preferences using localStorage
  • 🎨 CSS class management - Automatically applies theme classes to document root
  • 🏷️ Automatic style injection - Injects the color-scheme CSS property on the document root for native browser theming
  • 📦 Zero dependencies - Lightweight with no external dependencies
  • 🔧 TypeScript support - Full TypeScript definitions included

Installation

npm install @dazl/color-scheme

Usage

Client-side Setup

Import the client module to initialize color scheme management:

Note: This import should only be used in client-side code and must run before the <body> is rendered. For best results, include it in an inline script.

import '@dazl/color-scheme/client';

// The color scheme API is now available globally
const currentScheme = window.colorSchemeApi.current;
console.log(currentScheme); // { config: 'system', resolved: 'dark' }

// Change the color scheme
window.colorSchemeApi.config = 'light';

// Subscribe to changes
const unsubscribe = window.colorSchemeApi.subscribe(({ config, resolved }) => {
  console.log(`Color scheme changed: ${config} (resolved: ${resolved})`);
});

Color-scheme API

  • config (light|dark|system) - Get or set the current color scheme configuration
  • currentState ({ config: light|dark|system, resolved: light|dark }) - Get the current state including both config and resolved values
  • resolvedSystem (light|dark) - Get the resolved theme based on system preferences
  • subscribe ((handler: (currentState) => void) => () => void) - Subscribe to color scheme changes and return an unsubscribe function
  • getRootCssClass ((resolved?: light|dark) => string) - CSS class applied to the document root based on the resolved theme or passed value

Override root CSS class

Override the default CSS classes applied to the document root by specifying data-dark-class and data-light-class attributes in the client script tag:

<script src="@dazl/color-scheme/client" data-dark-class="dark-theme" data-light-class="light-theme"></script>

React Integration

Use the provided React hook for easy integration:

import { useColorScheme } from '@dazl/color-scheme/react';

function ThemeToggle() {
  const { configScheme, resolvedScheme, setColorScheme, isLight, isDark } = useColorScheme();

  return (
    <div>
      <p>Current config: {configScheme}</p>
      <p>Resolved theme: {resolvedScheme}</p>
      <p>Is light theme: {isLight}</p>
      <p>Is dark theme: {isDark}</p>

      <button onClick={() => setColorScheme('light')}>Light Theme</button>
      <button onClick={() => setColorScheme('dark')}>Dark Theme</button>
      <button onClick={() => setColorScheme('system')}>System Theme</button>
    </div>
  );
}

Hook API

  • resolvedScheme (light|dark) - Resolved theme (light/dark)
  • configScheme (light|dark|system) - Current configuration (light/dark/system)
  • setColorScheme ((config: light|dark|system) => void) - Function to change the color scheme
  • isLight (boolean) - Whether the current resolved theme is light
  • isDark (boolean) - Whether the current resolved theme is dark
  • rootCssClass (string) - CSS class applied to the document root based on the resolved theme

CSS Styling

The library automatically applies CSS classes to the document root. Style your application accordingly:

/* Dark theme styles */
:root.dark-theme {
  --bg-color: #000000;
  --text-color: #ffffff;
}

/* Light theme styles (explicit) */
:root.light-theme {
  --bg-color: #ffffff;
  --text-color: #000000;
}

License

MIT © Dazl

版本与发布记录

@dazl/color-scheme 共发布 9 个版本,首次发布于 2025/07/02;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.3.0latest2026/06/164 个月前
1.2.12025/11/021 年前
1.2.02025/08/241.2 年前
1.1.22025/08/151.2 年前
1.1.12025/07/131.3 年前
1.1.02025/07/071.3 年前

关于 @dazl/color-scheme 的常见问题

@dazl/color-scheme 是什么?

Color scheme utilities and tools

如何安装 @dazl/color-scheme?

在项目目录运行 npm install @dazl/color-scheme,或使用 pnpm add / yarn add / bun add。

@dazl/color-scheme 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/dazl__color-scheme 是否存在。

@dazl/color-scheme 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dazl/color-scheme 可以商用吗?

@dazl/color-scheme 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dazl/color-scheme 的下载量有多少?

根据 npm 官方下载统计,@dazl/color-scheme 在 2026/10/12 – 2026/11/10 这 30 天内被下载 24,051 次,日均约 802 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dazl/color-scheme 还在维护吗?

最新版本 1.3.0 发布于 2026/06/16(4 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/07/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。