@assortment/darkmodejs

中文文档
2.0.0发布于 2025/06/07MITTypeScriptCommonJS零依赖近 30 天下载 2452

Utility package for managing Dark Mode on the web

原始使用文档
文档目录11 节
  1. 📝 Prerequisites
  2. 🏁 Install
  3. 🎈 Usage
  4. ⚙ API
  5. config
  6. returned
  7. 🎬 Examples
  8. MacOS
  9. Windows 10
  10. No preference
  11. ✍️ License

darkmodejs

Status License npm


Utility package for managing Dark Mode on the web.

Dark Mode is a feature in modern Operating Systems which allows you to change your default UI from a light to dark theme. On the web we can take advantage of this to control the theme of our website.

Utilises the matchMedia API and its listeners, in combination with the prefers-color-scheme media query, in order to fire functions whenever you're in Dark Mode.

Example application using darkmodejs in Windows 10 Firefox Example application using darkmodejs in Windows 10 Firefox

📝 Prerequisites

Requires an Operating System which supports Dark Mode:

  • macOS 10.14
  • iOS 13.0
  • iPadOS 13.0
  • Windows 10
  • ... or greater

Also requires support for the prefers-color-scheme media query. A complete list of supported browsers can be found on caniuse.

⚠️ Breaking change in v2+: whilst the base API exposed by darkmodejs has remained the same, under the hood MediaQueryList's addListener and removeListener methods are marked as deprecated in the Web Standard, and have thus been replaced with addEventListener and removeEventListener respectfully. There is a slight difference in browser compatibility for very old browsers which were never explicitly supported, but for that reason a major version bump was created.

🏁 Install

$ npm install @assortment/darkmodejs

🎈 Usage

import darkmodejs from '@assortment/darkmodejs';

const config = { onChange: (activeTheme, themes) => {} };

darkmodejs(config);

If you need ES5 support, you can require the package instead.

const darkmodejs = require('@assortment/darkmodejs');

An example could be logging to console when a theme is active:

import darkmodejs from '@assortment/darkmodejs';

const onChange = (activeTheme, themes) => {
  switch (activeTheme) {
    case themes.DARK:
      console.log('darkmode enabled');
      break;
    case themes.LIGHT:
      console.log('lightmode enabled');
      break;
    case themes.NO_PREF:
      console.log('no preference enabled');
      break;
    case themes.NO_SUPP:
      console.log('no support sorry');
      break;
  }
};

darkmodejs({ onChange });

You can also do a spot of cleanup by calling the removeListeners function that is returned from darkmodejs. It removes both DARK and LIGHT query listeners.

const dmjs = darkmodejs({ onChange });

dmjs.removeListeners();

This can be useful when unmounting components or pages that use dynamic routing techniques.

⚙ API

config

darkmodejs accepts a config object, which in turn accepts a single function of onChange.

onChange(activeTheme, themes)

Type: Function. Required: No.

This function is called when darkmodejs is executed to check:

  • if there is support for prefers-color-scheme;
  • if the dark, light or no-preference theme is active.

The function is also bound to MediaQueryList.addListener to listen for changes.

activeTheme

Type: String. Required: No.

Returns the current active theme.

themes

Type: Object{String}. Required: No.

Returns all available theme states:

{
  DARK: 'dark',
  LIGHT: 'light',
  NO_PREF: 'no-preference',
  NO_SUPP: 'no-support'
}

returned

Once initiated, you also have access to the removeListeners function for cleanup purposes.

removeListeners

Type: Function.

Removes both DARK and LIGHT query listeners.

🎬 Examples

The following examples are taken from darkmodejs-demo, a demo application created to show how you can use @assortment/darkmodejs in conjunction with Emotion Theming to control your website's theme based on a user's OS preference.

MacOS

Safari (supported)

Supported in MacOS with Safari

Firefox (supported)

Supported in MacOS with Firefox

Windows 10

Firefox (supported)

Supported in Windows 10 with Firefox

Chrome (not supported until Chrome 76)

Not supported in Windows 10 with Chrome 75

No preference

Special mention to no-preference. To my knowledge I don't believe any Operating System currently allows for a no preference option, so there's no current circumstance where this returns true from a prefers-color-scheme media query. That said, as its part of the specification I've included it as an option in the demo app if you ever want to do anything specific.

Example no preference theme set

✍️ License

MIT © Luke Whitehouse

版本与发布记录

@assortment/darkmodejs 共发布 8 个版本,首次发布于 2019/06/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2025/06/071.4 年前
1.2.12021/02/025 年前
1.2.02019/12/086 年前
1.1.02019/07/167 年前
1.0.12019/07/137 年前
1.0.02019/07/137 年前

关于 @assortment/darkmodejs 的常见问题

@assortment/darkmodejs 是什么?

Utility package for managing Dark Mode on the web

如何安装 @assortment/darkmodejs?

在项目目录运行 npm install @assortment/darkmodejs,或使用 pnpm add / yarn add / bun add。

@assortment/darkmodejs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@assortment/darkmodejs 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@assortment/darkmodejs 可以商用吗?

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

@assortment/darkmodejs 的下载量有多少?

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

@assortment/darkmodejs 还在维护吗?

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