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

ngx-mode-watcher

v0.2.1

Published

Simple light/dark mode management for Angular apps. ๐ŸŒ‘ โ†โ†’ โ˜€๏ธ

Downloads

9

Readme

Mode Watcher

Simple utilities to manage light & dark mode in your Angular app.

Installation

Make sure you are using Angular 16 or greater.

npm install ngx-mode-watcher

Usage

Add provideModeWatcher() to your app config app.config.ts file.

import { provideModeWatcher } from 'ngx-mode-watcher';

export const appConfig: ApplicationConfig = {
  providers: [
    // ...
    provideModeWatcher(),
  ],
};

The provider will automatically detect the user's preferences and apply/remove the "dark" class, along with the corresponding color-scheme style attribute to the html element.

ModeWatcher will automatically track operating system preferences and apply these if no user preference is set. If you wish to disable this behavior, set the track config to false:

provideModeWatcher({
  track: false,
});

ModeWatcher can also be configured with a default mode instead of automatically detecting the user's preference.

To set a default mode, use the defaultMode config:

provideModeWatcher({
  defaultMode: 'dark',
});

ModeWatcher can manage the theme-color meta tag for you.

To enable this, set the themeColor config to your preferred colors:

provideModeWatcher({
  themeColor: { dark: 'black', light: 'white' },
});

ModeWatcherService

To manage the mode of your application, you can inject the ModeWatcherService.

import { ModeWatcherService } from './mode-watcher.service';

@Component({
  //...
})
export class MyComp {
  private readonly modeWatcher = inject(ModeWatcherService);
}

API

toggleMode

A function that toggles the current mode.

modeWatcher = inject(ModeWatcherService);

<button (click)="modeWatcher.toggleMode()">Toggle Mode</button>

setMode

A function that sets the current mode. It accepts a string with the value "light", "dark" or "system".

modeWatcher = inject(ModeWatcherService);

<button (click)="modeWatcher.setMode('light')">Set Light Mode</button>
<button (click)="modeWatcher.setMode('dark')">Set Dark Mode</button>

resetMode

A function that resets the mode to system preferences.

modeWatcher = inject(ModeWatcherService);

<button (click)="modeWatcher.resetMode()">System</button>

mode

A signal that contains the current mode. It can be "light" or "dark" or undefined if evaluated on the server.

modeWatcher = inject(ModeWatcherService);
handleModeChange() {
    if (modeWatcher.mode() === 'light') {
        modeWatcher.setMode('dark');
    } else {
        modeWatcher.setMode('light');
    }
}

<button (click)="handleModeChange()">{{ modeWatcher.mode() }}</button>

userPrefersMode

A signal that represents the user's mode preference. It can be "light", "dark" or "system".

systemPrefersMode

A signal that represents the operating system's mode preference. It can be "light", "dark" or undefined if evaluated on the server. Will automatically track changes to the operating system's mode preference unless this is disabled with the track config which takes a boolean.

Inspiration & credits

This project is an Angular port of svecosystem's mode-watcher.