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

angular-dark-mode

v3.1.0

Published

Add dark mode to your Angular applications with ease!

Downloads

1,113

Readme

GitHub Codecov Travis (.com) semantic-release npm bundle size npm npm

angular-dark-mode is a zero-dependency library that helps you integrate dark mode into you Angular applications with ease!

👉🏻 Live Demo 👈🏻

Inspired by the awesome use-dark-mode library

Installation

To use angular-dark-mode in your project install it via npm:

npm i angular-dark-mode

or if you are using yarn:

yarn add angular-dark-mode

and add angular-dark-mode.js file to angular.json scripts section:

{
  "scripts": ["./node_modules/angular-dark-mode/angular-dark-mode.js"]
}

if you are using custom configuration see angular-dark-mode.js

Usage

In order to use angular-dark-mode you need to inject the service somewhere in your applications - presumably where you hold the dark mode toggle, and get the dark mode value from the exported darkMode$ Observable:

// dark-mode-toggle.component.ts

@Component({
  selector: 'app-dark-mode-toggle',
  template: `<input
    type="checkbox"
    [checked]="darkMode$ | async"
    (change)="onToggle()"
  />`,
})
export class DarkModeToggle {
  darkMode$: Observable<boolean> = this.darkModeService.darkMode$;

  constructor(private darkModeService: DarkModeService) {}

  onToggle(): void {
    this.darkModeService.toggle();
  }
}

Next, include global styles and some text to reflect the mode:

/* styles.css */

body.dark-mode {
  background-color: #2d3436;
  color: #dfe6e9;
}

body.light-mode {
  background-color: #dfe6e9;
  color: #2d3436;
}
// app.component.ts

@Component({
  selector: 'app-root',
  template: `
    <h1>angular-dark-mode</h1>
    <p>Toggle to see magic happens!</p>
    <app-dark-mode-toggle></app-dark-mode-toggle>
  `,
})
export class AppComponent {}

You're all set!
Save and run your application, play with the toggle button to change between modes.

Options

angular-dark-mode ships with the following options:

| Option | Description | Default Value | | --------------- | :----------------------------------------------------------: | -----------------------: | | darkModeClass | dark mode css class name | 'dark-mode' | | lightModeClass | light mode css class name | 'light-mode' | | preloadingClass | css class name to flag that element is in preloading state | 'dark-mode-preloading' | | storageKey | localStorage key to persist dark mode | 'dark-mode' | | element | target HTMLElement to set given css classes | document.body |

All options are set to default and can be configured via the DARK_MODE_OPTIONS InjectionToken:

import { DARK_MODE_OPTIONS } from 'angular-dark-mode';

@NgModule({
    ...
    providers: [
        {
            provide: DARK_MODE_OPTIONS,
            useValue: {
                darkModeClass: 'my-dark-mode',
                lightModeClass: 'my-light-mode'
            }
        }
    ]
    ...
})
export class AppModule {}

angular-dark-mode.js

This file has multiple purposes:

  1. Persistence - retrieve previous dark mode state from localStorage if empty falls back to (prefers-color-scheme: dark) media query
  2. Preloading - set a preloadingClass to document.body which can be used to prevent initial transitioning

If you are using the default configurations, adding the angular-dark-mode.js file bundled with this library is enough.

In any other case, copy angular-dark-mode.js locally, make the necessary changes and load it instead of the bundled one.

Transitioning

It is often useful to transition the changes between dark and light modes, and most of the time we would want to skip the initial transition, in order to achieve this use the preloadingClass option like so:

/* styles.css */
...

body:not(.dark-mode-preloading) {
  transition: all 0.3s linear;
}

...

Contributors

Thanks goes to these wonderful people emoji key: