Coder Social home page Coder Social logo

light-dark-theme-for-angular-material's Introduction

Light-Dark theme for Angular Material

This is a custom theme made for Material in Angular. It used Twitter color palletes by default, feel free to make changes as you want.

Download & Integration

  1. Download/clone this repository
  2. Copy both light-theme.scss and dark-theme.scss to src directory.
  3. Import those files in your styles.scss.
@import "light-theme.scss";
@import "dark-theme.scss";
  1. Add these files in all angular.json styles sections as well
...
"styles": [
        ...,
        "src/light-theme.scss",
        "src/dark-theme.scss",
        "src/styles.scss",
        ...
    ],
...
  1. Copy the theming-service folder into the desired module(s) directories.
  2. Import the service in module(s)
...
import { ThemingService } from './theming-service/themes.service';
import { OverlayContainer } from '@angular/cdk/overlay';
...

Add it providers section

...
providers: [
    ...,
    ThemingService,
    ...
]
...

Add classes to export class

...
export class YourModule {
  constructor(overlayContainer: OverlayContainer, theming: ThemesService) {
    theming.defaultTheme.subscribe((theme: string) => {
      let themeToRemove = 'dark-theme';
      if (theme === 'dark-theme') {
        themeToRemove = 'light-theme';
        document.querySelector("body").style.background = '#060b0f';
      } else {
        document.querySelector("body").style.background = '#fff';
      }
      overlayContainer.getContainerElement().classList.remove(themeToRemove);
      overlayContainer.getContainerElement().classList.add(theme);
    });
  }
}
...
  1. In root html template (or any you want), create a container div with class to take the current theme value.
<div [className]="theme">
... 
ALL YOUR COMPNENTS HERE
...
</div>

IN .ts

theme: string = 'light-theme'; //default to light

contructor(private theming: ThemingService) {}

ngOnInit() {
    ...
    //Get the current theme

    this.theming.defaultTheme.subscribe((theme: string) => {
        this.theme = theme;
    });

    ...

}

//Change theme theme function to be called accordingly

changeTheme(theme: string) {
    this.theming.toggleTheme(theme);
}

light-dark-theme-for-angular-material's People

Contributors

ngabovictor avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.