├── .editorconfig ├── .gitignore ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── angular.json ├── e2e ├── protractor.conf.js ├── src │ ├── app.e2e-spec.ts │ └── app.po.ts └── tsconfig.json ├── karma.conf.js ├── package-lock.json ├── package.json ├── src ├── app │ ├── app.component.html │ ├── app.component.scss │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── components │ │ ├── content │ │ │ └── content.component.ts │ │ └── header │ │ │ └── header.component.ts │ ├── sample-dialog │ │ ├── sample-dialog.component.html │ │ ├── sample-dialog.component.scss │ │ ├── sample-dialog.component.spec.ts │ │ └── sample-dialog.component.ts │ └── services │ │ └── theme.service.ts ├── assets │ └── .gitkeep ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.scss └── test.ts ├── tsconfig.app.json ├── tsconfig.json ├── tsconfig.spec.json └── tslint.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.ts] 12 | quote_type = single 13 | 14 | [*.md] 15 | max_line_length = off 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events*.json 15 | speed-measure-plugin*.json 16 | 17 | # IDEs and editors 18 | /.idea 19 | .project 20 | .classpath 21 | .c9/ 22 | *.launch 23 | .settings/ 24 | *.sublime-workspace 25 | 26 | # IDE - VSCode 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | .history/* 33 | 34 | # misc 35 | /.angular/cache 36 | /.sass-cache 37 | /connect.lock 38 | /coverage 39 | /libpeerconnection.log 40 | npm-debug.log 41 | yarn-error.log 42 | testem.log 43 | /typings 44 | 45 | # System Files 46 | .DS_Store 47 | Thumbs.db 48 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "angular.enable-strict-mode-prompt": false 3 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Zoaib 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Material Dark Mode 2 | 3 | This is a companion demo app showing dark mode in Angular material. 4 | 5 | Repo updated to material v19 with the new mat.theme() mixin - article due for an update. 6 | 7 | [Check out this article](https://zoaibkhan.com/blog/angular-material-dark-mode-in-3-steps/) on adding a dark mode in Angular material using custom themes. 8 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "angular-dark-mode": { 7 | "projectType": "application", 8 | "schematics": { 9 | "@schematics/angular:component": { 10 | "style": "scss", 11 | "inlineStyle": true, 12 | "inlineTemplate": true, 13 | "skipTests": true 14 | } 15 | }, 16 | "root": "", 17 | "sourceRoot": "src", 18 | "prefix": "app", 19 | "architect": { 20 | "build": { 21 | "builder": "@angular-devkit/build-angular:application", 22 | "options": { 23 | "outputPath": { 24 | "base": "dist/angular-dark-mode" 25 | }, 26 | "index": "src/index.html", 27 | "polyfills": ["src/polyfills.ts"], 28 | "tsConfig": "tsconfig.app.json", 29 | "assets": ["src/favicon.ico", "src/assets"], 30 | "styles": ["src/styles.scss"], 31 | "scripts": [], 32 | "extractLicenses": false, 33 | "sourceMap": true, 34 | "optimization": false, 35 | "namedChunks": true, 36 | "browser": "src/main.ts", 37 | "inlineStyleLanguage": "scss" 38 | }, 39 | "configurations": { 40 | "production": { 41 | "fileReplacements": [ 42 | { 43 | "replace": "src/environments/environment.ts", 44 | "with": "src/environments/environment.prod.ts" 45 | } 46 | ], 47 | "optimization": true, 48 | "outputHashing": "all", 49 | "sourceMap": false, 50 | "namedChunks": false, 51 | "extractLicenses": true, 52 | "budgets": [ 53 | { 54 | "type": "initial", 55 | "maximumWarning": "2mb", 56 | "maximumError": "5mb" 57 | }, 58 | { 59 | "type": "anyComponentStyle", 60 | "maximumWarning": "6kb", 61 | "maximumError": "10kb" 62 | } 63 | ] 64 | } 65 | }, 66 | "defaultConfiguration": "" 67 | }, 68 | "serve": { 69 | "builder": "@angular-devkit/build-angular:dev-server", 70 | "options": { 71 | "buildTarget": "angular-dark-mode:build" 72 | }, 73 | "configurations": { 74 | "production": { 75 | "buildTarget": "angular-dark-mode:build:production" 76 | } 77 | } 78 | }, 79 | "extract-i18n": { 80 | "builder": "@angular-devkit/build-angular:extract-i18n", 81 | "options": { 82 | "buildTarget": "angular-dark-mode:build" 83 | } 84 | }, 85 | "test": { 86 | "builder": "@angular-devkit/build-angular:karma", 87 | "options": { 88 | "main": "src/test.ts", 89 | "polyfills": "src/polyfills.ts", 90 | "tsConfig": "tsconfig.spec.json", 91 | "karmaConfig": "karma.conf.js", 92 | "assets": ["src/favicon.ico", "src/assets"], 93 | "styles": ["src/styles.scss"], 94 | "scripts": [], 95 | "inlineStyleLanguage": "scss" 96 | } 97 | }, 98 | "e2e": { 99 | "builder": "@angular-devkit/build-angular:protractor", 100 | "options": { 101 | "protractorConfig": "e2e/protractor.conf.js", 102 | "devServerTarget": "angular-dark-mode:serve" 103 | }, 104 | "configurations": { 105 | "production": { 106 | "devServerTarget": "angular-dark-mode:serve:production" 107 | } 108 | } 109 | } 110 | } 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | // Protractor configuration file, see link for more information 3 | // https://github.com/angular/protractor/blob/master/lib/config.ts 4 | 5 | const { SpecReporter, StacktraceOption } = require('jasmine-spec-reporter'); 6 | 7 | /** 8 | * @type { import("protractor").Config } 9 | */ 10 | exports.config = { 11 | allScriptsTimeout: 11000, 12 | specs: [ 13 | './src/**/*.e2e-spec.ts' 14 | ], 15 | capabilities: { 16 | browserName: 'chrome' 17 | }, 18 | directConnect: true, 19 | baseUrl: 'http://localhost:4200/', 20 | framework: 'jasmine', 21 | jasmineNodeOpts: { 22 | showColors: true, 23 | defaultTimeoutInterval: 30000, 24 | print: function() {} 25 | }, 26 | onPrepare() { 27 | require('ts-node').register({ 28 | project: require('path').join(__dirname, './tsconfig.json') 29 | }); 30 | jasmine.getEnv().addReporter(new SpecReporter({ 31 | spec: { 32 | displayStacktrace: StacktraceOption.PRETTY 33 | } 34 | })); 35 | } 36 | }; -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | import { browser, logging } from 'protractor'; 3 | 4 | describe('workspace-project App', () => { 5 | let page: AppPage; 6 | 7 | beforeEach(() => { 8 | page = new AppPage(); 9 | }); 10 | 11 | it('should display welcome message', () => { 12 | page.navigateTo(); 13 | expect(page.getTitleText()).toEqual('angular-dark-mode app is running!'); 14 | }); 15 | 16 | afterEach(async () => { 17 | // Assert that there are no errors emitted from the browser 18 | const logs = await browser.manage().logs().get(logging.Type.BROWSER); 19 | expect(logs).not.toContain(jasmine.objectContaining({ 20 | level: logging.Level.SEVERE, 21 | } as logging.Entry)); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo(): Promise { 5 | return browser.get(browser.baseUrl) as Promise; 6 | } 7 | 8 | getTitleText(): Promise { 9 | return element(by.css('app-root .content span')).getText() as Promise; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /e2e/tsconfig.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "../tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "../out-tsc/e2e", 6 | "module": "commonjs", 7 | "target": "es2018", 8 | "types": [ 9 | "jasmine", 10 | "jasminewd2", 11 | "node" 12 | ] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, './coverage/angular-dark-mode'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false, 30 | restartOnFileChange: true 31 | }); 32 | }; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-dark-mode", 3 | "version": "2.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e" 11 | }, 12 | "private": true, 13 | "dependencies": { 14 | "@angular/animations": "^19.0.4", 15 | "@angular/cdk": "^19.0.3", 16 | "@angular/common": "^19.0.4", 17 | "@angular/compiler": "^19.0.4", 18 | "@angular/core": "^19.0.4", 19 | "@angular/flex-layout": "^12.0.0-beta.34", 20 | "@angular/forms": "^19.0.4", 21 | "@angular/material": "^19.0.3", 22 | "@angular/platform-browser": "^19.0.4", 23 | "@angular/platform-browser-dynamic": "^19.0.4", 24 | "@angular/router": "^19.0.4", 25 | "rxjs": "~6.5.5", 26 | "tslib": "^2.0.0", 27 | "zone.js": "~0.15.0" 28 | }, 29 | "devDependencies": { 30 | "@angular-devkit/build-angular": "^19.0.5", 31 | "@angular/cli": "^19.0.5", 32 | "@angular/compiler-cli": "^19.0.4", 33 | "@types/node": "^12.11.1", 34 | "@types/jasmine": "~3.6.0", 35 | "@types/jasminewd2": "~2.0.3", 36 | "codelyzer": "^6.0.0", 37 | "jasmine-core": "~3.6.0", 38 | "jasmine-spec-reporter": "~5.0.0", 39 | "karma": "~6.3.4", 40 | "karma-chrome-launcher": "~3.1.0", 41 | "karma-coverage-istanbul-reporter": "~3.0.2", 42 | "karma-jasmine": "~4.0.0", 43 | "karma-jasmine-html-reporter": "^1.5.0", 44 | "protractor": "~7.0.0", 45 | "ts-node": "~8.3.0", 46 | "tslint": "~6.1.0", 47 | "typescript": "~5.6.3" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 | Angular Dark Mode 3 |
4 | 7 | 8 | @for (theme of themes(); track theme.name) { 9 | 17 | } 18 | 19 |
20 | 21 |
22 |
23 |

Buttons

24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
32 | 33 |

34 | 35 |

Input

36 | 37 | 38 | 39 | 40 | 41 |

42 | 43 |

Checkbox

44 | 45 | Check this box! 46 | 47 |

48 | 49 |

Expansion panel

50 | 51 | 52 | 53 | Expansion title 54 | 55 | This is a summary of the content 56 | 57 | 58 |

This is the primary content of the panel.

59 |
60 | 61 |

62 |
63 |
64 |

A card

65 | 66 | 67 |
68 | Shiba Inu 69 | Dog Breed 70 |
71 | Photo of a Shiba Inu 76 | 77 |

78 | The Shiba Inu is the smallest of the six original and distinct spitz 79 | breeds of dog from Japan. A small, agile dog that copes very well with 80 | mountainous terrain, the Shiba Inu was originally bred for hunting. 81 |

82 |
83 | 84 | 85 | 86 | 87 |
88 |
89 |
90 | -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, waitForAsync } from '@angular/core/testing'; 2 | import { AppComponent } from './app.component'; 3 | 4 | describe('AppComponent', () => { 5 | beforeEach(waitForAsync(() => { 6 | TestBed.configureTestingModule({ 7 | imports: [AppComponent], 8 | }).compileComponents(); 9 | })); 10 | 11 | it('should create the app', () => { 12 | const fixture = TestBed.createComponent(AppComponent); 13 | const app = fixture.componentInstance; 14 | expect(app).toBeTruthy(); 15 | }); 16 | 17 | it(`should have as title 'angular-dark-mode'`, () => { 18 | const fixture = TestBed.createComponent(AppComponent); 19 | const app = fixture.componentInstance; 20 | expect(app.title).toEqual('angular-dark-mode'); 21 | }); 22 | 23 | it('should render title', () => { 24 | const fixture = TestBed.createComponent(AppComponent); 25 | fixture.detectChanges(); 26 | const compiled = fixture.nativeElement; 27 | expect(compiled.querySelector('.content span').textContent).toContain('angular-dark-mode app is running!'); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | import { HeaderComponent } from './components/header/header.component'; 3 | import { ContentComponent } from './components/content/content.component'; 4 | 5 | @Component({ 6 | selector: 'app-root', 7 | standalone: true, 8 | imports: [HeaderComponent, ContentComponent], 9 | template: ` 10 | 11 | 12 | `, 13 | styles: ``, 14 | }) 15 | export class AppComponent {} 16 | -------------------------------------------------------------------------------- /src/app/components/content/content.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, inject } from '@angular/core'; 2 | import { MatButtonModule } from '@angular/material/button'; 3 | import { MatCardModule } from '@angular/material/card'; 4 | import { MatFormFieldModule } from '@angular/material/form-field'; 5 | import { MatInputModule } from '@angular/material/input'; 6 | import { MatCheckboxModule } from '@angular/material/checkbox'; 7 | import { MatExpansionModule } from '@angular/material/expansion'; 8 | import { MatDialog } from '@angular/material/dialog'; 9 | import { SampleDialogComponent } from '../../sample-dialog/sample-dialog.component'; 10 | 11 | @Component({ 12 | selector: 'app-content', 13 | standalone: true, 14 | imports: [ 15 | MatButtonModule, 16 | MatCardModule, 17 | MatFormFieldModule, 18 | MatInputModule, 19 | MatCheckboxModule, 20 | MatExpansionModule, 21 | ], 22 | template: ` 23 |
24 |
25 |

Buttons

26 | 27 |
28 | 29 | 30 | 31 |
32 | 33 |

34 | 35 |

Input

36 | 37 | 38 | 39 | 40 | 41 |

42 | 43 |

Checkbox

44 | 45 | Check this box! 46 | 47 |

48 | 49 |

Expansion panel

50 | 51 | 52 | 53 | Expansion title 54 | 55 | This is a summary of the content 56 | 57 | 58 |

This is the primary content of the panel.

59 |
60 | 61 |

62 |
63 |
64 |

A card

65 | 66 | 67 |
68 | Shiba Inu 69 | Dog Breed 70 |
71 | Photo of a Shiba Inu 76 | 77 |

78 | The Shiba Inu is the smallest of the six original and distinct 79 | spitz breeds of dog from Japan. A small, agile dog that copes very 80 | well with mountainous terrain, the Shiba Inu was originally bred 81 | for hunting. 82 |

83 |
84 | 85 | 86 | 87 | 88 |
89 |
90 |
91 | `, 92 | styles: ` 93 | .content { 94 | padding: 32px; 95 | min-height: 100vh; 96 | 97 | background-color: var(--mat-sys-background); 98 | color: var(--mat-sys-on-background); 99 | } 100 | 101 | .responsive-grid { 102 | display: grid; 103 | grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); 104 | grid-gap: 1rem; 105 | } 106 | 107 | .flex-row { 108 | display: flex; 109 | flex-direction: row; 110 | gap: 1rem; 111 | } 112 | 113 | mat-card { 114 | max-width: 400px; 115 | } 116 | 117 | h3 { 118 | font-weight: 400; 119 | } 120 | `, 121 | }) 122 | export class ContentComponent { 123 | private dialog = inject(MatDialog); 124 | 125 | showDialog(): void { 126 | this.dialog.open(SampleDialogComponent, { 127 | width: '500px', 128 | }); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/app/components/header/header.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, inject } from '@angular/core'; 2 | import { MatToolbar } from '@angular/material/toolbar'; 3 | import { MatIconModule } from '@angular/material/icon'; 4 | import { MatButtonModule } from '@angular/material/button'; 5 | import { MatMenuModule } from '@angular/material/menu'; 6 | import { TitleCasePipe } from '@angular/common'; 7 | import { ThemeService } from '../../services/theme.service'; 8 | 9 | @Component({ 10 | selector: 'app-header', 11 | standalone: true, 12 | imports: [ 13 | MatToolbar, 14 | MatIconModule, 15 | MatButtonModule, 16 | MatMenuModule, 17 | TitleCasePipe, 18 | ], 19 | template: ` 20 | 21 | Angular Dark Mode 22 |
23 | 26 | 27 | @for (theme of themeService.getThemes(); track theme.name) { 28 | 38 | } 39 | 40 |
41 | `, 42 | styles: ` 43 | @use '@angular/material' as mat; 44 | 45 | mat-toolbar { 46 | display: flex; 47 | align-items: center; 48 | 49 | @include mat.toolbar-overrides( 50 | ( 51 | container-background-color: var(--mat-sys-primary), 52 | container-text-color: var(--mat-sys-on-primary), 53 | ) 54 | ); 55 | 56 | @include mat.icon-button-overrides( 57 | ( 58 | icon-color: var(--mat-sys-on-primary), 59 | ) 60 | ); 61 | } 62 | 63 | .selected-theme { 64 | @include mat.menu-overrides( 65 | ( 66 | item-icon-color: var(--mat-sys-primary), 67 | item-label-text-color: var(--mat-sys-primary), 68 | ) 69 | ); 70 | } 71 | 72 | .flex-stretch { 73 | flex: 1 0 auto; 74 | } 75 | `, 76 | }) 77 | export class HeaderComponent { 78 | protected themeService = inject(ThemeService); 79 | } 80 | -------------------------------------------------------------------------------- /src/app/sample-dialog/sample-dialog.component.html: -------------------------------------------------------------------------------- 1 |

Dialog title

2 | 3 | 4 | This is just a sample dialog. Click close to dismiss! 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/app/sample-dialog/sample-dialog.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thisiszoaib/angular-dark-mode/606b94ceb73d3f1de96c2ff492e534d1dfe46b31/src/app/sample-dialog/sample-dialog.component.scss -------------------------------------------------------------------------------- /src/app/sample-dialog/sample-dialog.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; 2 | 3 | import { SampleDialogComponent } from './sample-dialog.component'; 4 | 5 | describe('SampleDialogComponent', () => { 6 | let component: SampleDialogComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(waitForAsync(() => { 10 | TestBed.configureTestingModule({ 11 | imports: [SampleDialogComponent] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(SampleDialogComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/sample-dialog/sample-dialog.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { MatButtonModule } from '@angular/material/button'; 3 | import { MatDialogModule } from '@angular/material/dialog'; 4 | 5 | @Component({ 6 | selector: 'app-sample-dialog', 7 | templateUrl: './sample-dialog.component.html', 8 | styleUrls: ['./sample-dialog.component.scss'], 9 | standalone: true, 10 | imports: [MatDialogModule, MatButtonModule], 11 | }) 12 | export class SampleDialogComponent implements OnInit { 13 | constructor() {} 14 | 15 | ngOnInit(): void {} 16 | } 17 | -------------------------------------------------------------------------------- /src/app/services/theme.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, computed, effect, inject, signal } from '@angular/core'; 2 | 3 | export interface AppTheme { 4 | name: string; 5 | icon: string; 6 | } 7 | 8 | @Injectable({ 9 | providedIn: 'root', 10 | }) 11 | export class ThemeService { 12 | private appTheme = signal<'light' | 'dark' | 'system'>('system'); 13 | 14 | private themes: AppTheme[] = [ 15 | { name: 'light', icon: 'light_mode' }, 16 | { name: 'dark', icon: 'dark_mode' }, 17 | { name: 'system', icon: 'desktop_windows' }, 18 | ]; 19 | 20 | selectedTheme = computed(() => 21 | this.themes.find((t) => t.name === this.appTheme()) 22 | ); 23 | 24 | getThemes() { 25 | return this.themes; 26 | } 27 | 28 | setTheme(theme: 'light' | 'dark' | 'system') { 29 | this.appTheme.set(theme); 30 | } 31 | 32 | constructor() { 33 | effect(() => { 34 | const appTheme = this.appTheme(); 35 | const colorScheme = appTheme === 'system' ? 'light dark' : appTheme; 36 | document.body.style.setProperty('color-scheme', colorScheme); 37 | }); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thisiszoaib/angular-dark-mode/606b94ceb73d3f1de96c2ff492e534d1dfe46b31/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/plugins/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thisiszoaib/angular-dark-mode/606b94ceb73d3f1de96c2ff492e534d1dfe46b31/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | AngularDarkMode 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode, importProvidersFrom } from '@angular/core'; 2 | 3 | import { environment } from './environments/environment'; 4 | import { AppComponent } from './app/app.component'; 5 | import { provideAnimations } from '@angular/platform-browser/animations'; 6 | import { BrowserModule, bootstrapApplication } from '@angular/platform-browser'; 7 | 8 | if (environment.production) { 9 | enableProdMode(); 10 | } 11 | 12 | bootstrapApplication(AppComponent, { 13 | providers: [importProvidersFrom(BrowserModule), provideAnimations()], 14 | }).catch((err) => console.error(err)); 15 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** 22 | * By default, zone.js will patch all possible macroTask and DomEvents 23 | * user can disable parts of macroTask/DomEvents patch by setting following flags 24 | * because those flags need to be set before `zone.js` being loaded, and webpack 25 | * will put import in the top of bundle, so user need to create a separate file 26 | * in this directory (for example: zone-flags.ts), and put the following flags 27 | * into that file, and then add the following code before importing zone.js. 28 | * import './zone-flags'; 29 | * 30 | * The flags allowed in zone-flags.ts are listed here. 31 | * 32 | * The following flags will work for all browsers. 33 | * 34 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 35 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 36 | * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 37 | * 38 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 39 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 40 | * 41 | * (window as any).__Zone_enable_cross_context_check = true; 42 | * 43 | */ 44 | 45 | /*************************************************************************************************** 46 | * Zone JS is required by default for Angular itself. 47 | */ 48 | import 'zone.js'; // Included with Angular CLI. 49 | 50 | 51 | /*************************************************************************************************** 52 | * APPLICATION IMPORTS 53 | */ 54 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | @use "@angular/material" as mat; 2 | 3 | html { 4 | @include mat.theme( 5 | ( 6 | color: mat.$azure-palette, 7 | typography: Roboto, 8 | ) 9 | ); 10 | } 11 | 12 | .accent { 13 | @include mat.theme( 14 | ( 15 | color: mat.$magenta-palette, 16 | ) 17 | ); 18 | } 19 | 20 | .warn { 21 | @include mat.theme( 22 | ( 23 | color: mat.$red-palette, 24 | ) 25 | ); 26 | } 27 | 28 | html, 29 | body { 30 | height: 100%; 31 | } 32 | body { 33 | margin: 0; 34 | font-family: Roboto, "Helvetica Neue", sans-serif; 35 | } 36 | 37 | h2, 38 | p { 39 | padding: 16px; 40 | } 41 | 42 | .mr-8 { 43 | margin-right: 8px; 44 | } 45 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | // First, initialize the Angular testing environment. 11 | getTestBed().initTestEnvironment( 12 | BrowserDynamicTestingModule, 13 | platformBrowserDynamicTesting(), { 14 | teardown: { destroyAfterEach: false } 15 | } 16 | ); 17 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/app", 6 | "types": [] 7 | }, 8 | "files": [ 9 | "src/main.ts", 10 | "src/polyfills.ts" 11 | ], 12 | "include": [ 13 | "src/**/*.d.ts" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "compileOnSave": false, 4 | "compilerOptions": { 5 | "baseUrl": "./", 6 | "outDir": "./dist/out-tsc", 7 | "sourceMap": true, 8 | "esModuleInterop": true, 9 | "declaration": false, 10 | "experimentalDecorators": true, 11 | "moduleResolution": "node", 12 | "importHelpers": true, 13 | "target": "ES2022", 14 | "module": "es2020", 15 | "lib": [ 16 | "es2018", 17 | "dom" 18 | ], 19 | "useDefineForClassFields": false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/spec", 6 | "types": [ 7 | "jasmine" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts", 12 | "src/polyfills.ts" 13 | ], 14 | "include": [ 15 | "src/**/*.spec.ts", 16 | "src/**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rules": { 4 | "align": { 5 | "options": [ 6 | "parameters", 7 | "statements" 8 | ] 9 | }, 10 | "array-type": false, 11 | "arrow-return-shorthand": true, 12 | "curly": true, 13 | "deprecation": { 14 | "severity": "warning" 15 | }, 16 | "component-class-suffix": true, 17 | "contextual-lifecycle": true, 18 | "directive-class-suffix": true, 19 | "directive-selector": [ 20 | true, 21 | "attribute", 22 | "app", 23 | "camelCase" 24 | ], 25 | "component-selector": [ 26 | true, 27 | "element", 28 | "app", 29 | "kebab-case" 30 | ], 31 | "eofline": true, 32 | "import-blacklist": [ 33 | true, 34 | "rxjs/Rx" 35 | ], 36 | "import-spacing": true, 37 | "indent": { 38 | "options": [ 39 | "spaces" 40 | ] 41 | }, 42 | "max-classes-per-file": false, 43 | "max-line-length": [ 44 | true, 45 | 140 46 | ], 47 | "member-ordering": [ 48 | true, 49 | { 50 | "order": [ 51 | "static-field", 52 | "instance-field", 53 | "static-method", 54 | "instance-method" 55 | ] 56 | } 57 | ], 58 | "no-console": [ 59 | true, 60 | "debug", 61 | "info", 62 | "time", 63 | "timeEnd", 64 | "trace" 65 | ], 66 | "no-empty": false, 67 | "no-inferrable-types": [ 68 | true, 69 | "ignore-params" 70 | ], 71 | "no-non-null-assertion": true, 72 | "no-redundant-jsdoc": true, 73 | "no-switch-case-fall-through": true, 74 | "no-var-requires": false, 75 | "object-literal-key-quotes": [ 76 | true, 77 | "as-needed" 78 | ], 79 | "quotemark": [ 80 | true, 81 | "single" 82 | ], 83 | "semicolon": { 84 | "options": [ 85 | "always" 86 | ] 87 | }, 88 | "space-before-function-paren": { 89 | "options": { 90 | "anonymous": "never", 91 | "asyncArrow": "always", 92 | "constructor": "never", 93 | "method": "never", 94 | "named": "never" 95 | } 96 | }, 97 | "typedef": [ 98 | true, 99 | "call-signature" 100 | ], 101 | "typedef-whitespace": { 102 | "options": [ 103 | { 104 | "call-signature": "nospace", 105 | "index-signature": "nospace", 106 | "parameter": "nospace", 107 | "property-declaration": "nospace", 108 | "variable-declaration": "nospace" 109 | }, 110 | { 111 | "call-signature": "onespace", 112 | "index-signature": "onespace", 113 | "parameter": "onespace", 114 | "property-declaration": "onespace", 115 | "variable-declaration": "onespace" 116 | } 117 | ] 118 | }, 119 | "variable-name": { 120 | "options": [ 121 | "ban-keywords", 122 | "check-format", 123 | "allow-pascal-case" 124 | ] 125 | }, 126 | "whitespace": { 127 | "options": [ 128 | "check-branch", 129 | "check-decl", 130 | "check-operator", 131 | "check-separator", 132 | "check-type", 133 | "check-typecast" 134 | ] 135 | }, 136 | "no-conflicting-lifecycle": true, 137 | "no-host-metadata-property": true, 138 | "no-input-rename": true, 139 | "no-inputs-metadata-property": true, 140 | "no-output-native": true, 141 | "no-output-on-prefix": true, 142 | "no-output-rename": true, 143 | "no-outputs-metadata-property": true, 144 | "template-banana-in-box": true, 145 | "template-no-negated-async": true, 146 | "use-lifecycle-interface": true, 147 | "use-pipe-transform-interface": true 148 | }, 149 | "rulesDirectory": [ 150 | "codelyzer" 151 | ] 152 | } --------------------------------------------------------------------------------