• Stars
    star
    193
  • Rank 201,081 (Top 4 %)
  • Language SCSS
  • License
    MIT License
  • Created over 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Little library to use css variables with @angular/material

logo

You want to style your angular material dynamically with all the colors in the rainbow? Look no further!

Check out the Demo!

Setup

  1. Install:
    npm i angular-material-css-vars -S
  2. If @angular/material is already configured remove @include mat.core() from your main stylesheet file if present.
  3. Add this to your main stylesheet instead:
    @use 'angular-material-css-vars' as mat-css-vars;
    
    @include mat-css-vars.init-material-css-vars();
  4. Add to your main module:
import {MaterialCssVarsModule} from 'angular-material-css-vars';

@NgModule({
  imports: [
    MaterialCssVarsModule.forRoot({
      // all optional
      isAutoContrast: true,
      primary: '#3f51b5',
      // ...
    }),
  ],
})
export class AppModule {
}
  1. If you want to adjust the theme at runtime, you can use MaterialCssVarsService:
    import {MaterialCssVarsService} from 'angular-material-css-vars';
    
    export class SomeComponentOrService {
      constructor(public materialCssVarsService: MaterialCssVarsService) {
        const hex = '#3f51b5';
        this.materialCssVarsService.setDarkTheme(true);
        this.materialCssVarsService.setPrimaryColor(hex);
        this.materialCssVarsService.setAccentColor('#333');
      }
    }

Additional Features

  • Auto or manually set contrast color via
    • setAutoContrastEnabled(bool)
    • setContrastColorThreshold(hueVal: HueValue)
  • Helper to set all variables
    • setVariable(cssVarName: MaterialCssVariables, value: string)
    • You can use the MaterialCssVariables enum from here to make sure you get the variable name right.
  • Rudimentary dark theme support via body class
    • setDarkTheme(isDark: boolean)

Utility

There are also several utility functions and mixins.

@use 'angular-material-css-vars' as mat-css-vars;

.with-color {
  border-color: mat-css-vars.mat-css-color-primary(300);
}

.color-and-contrast {
  @include mat-css-vars.mat-css-color-and-contrast(300);
}

.with-bg {
  @include mat-css-vars.mat-css-bg(300);
}

There are also some additional hacks (e.g. adding a color to the elevation shadow) available in case you need them.

Initialization Options

You can provide different options before initialization to change the body class used for the dark theme and to provide different default styles:

...
@use 'angular-material-css-vars' as mat-css-vars with (
  $dark-theme-selector: '.isDarkTheme',
  $light-theme-selector: '.isLightTheme'
);
...

@include mat-css-vars.init-material-css-vars();

Set default (fallback palettes)

There are two ways to set the default fallback theme. One is using the mat-css-palette-defaults mixin.

@use 'angular-material-css-vars' as mat-css-vars;
@use '@angular/material' as mat;

@include mat-css-vars.init-material-css-vars();

@include mat-css-vars.mat-css-set-palette-defaults(mat.$light-blue-palette, 'primary');
@include mat-css-vars.mat-css-set-palette-defaults(mat.$pink-palette, 'accent');
@include mat-css-vars.mat-css-set-palette-defaults(mat.$red-palette, 'warn');

The other is to include your own variables for $default-light-theme.

@use 'angular-material-css-vars' as mat-css-vars with (
  $default-light-theme: (
    --palette-primary-50: mat-css-vars.hex-to-rgb(#e1f5fe),
    --palette-primary-100: mat-css-vars.hex-to-rgb(#b3e5fc),
    --palette-primary-200: mat-css-vars.hex-to-rgb(#81d4fa),
    --palette-primary-300: mat-css-vars.hex-to-rgb(#4fc3f7),
    --palette-primary-400: mat-css-vars.hex-to-rgb(#29b6f6),
    --palette-primary-500: mat-css-vars.hex-to-rgb(#03a9f4),
    // ...
  )
);

@include mat-css-vars.init-material-css-vars();

Set global density

To set the global density level, just pass the $density variable to the init-material-css-vars() mixin like the following:

@use 'angular-material-css-vars' as mat-css-vars;

@include mat-css-vars.init-material-css-vars($density: -2);

App Theme Mixins

The init-material-css-vars mixin allows content to be passed into it. This allows you to create app themes that can take advantage of the dynamic theme created inside this mixin. It may be possible to do all theming using the utility mixins outlined above, but in other cases, you may need access to the theme palette, including foreground and background palettes.

See the Material guide on Theming your custom component for more information.

Font config

If needed the typography can be adjusted as well. You can introduce your own CSS variables, if you wish.

@use 'angular-material-css-vars' as mat-css-vars;
@use '@angular/material' as mat;

// example
$custom-typography: mat.define-typography-config(
  // optionally, you introduce your own CSS variables: `$font-family: var(--my-custom-font-family)`
  $font-family: 'Roboto, monospace',
  $headline: mat.define-typography-level(32px, 48px, 700),
  $body-1: mat.define-typography-level(16px, 24px, 500)
);

@include mat-css-vars.init-material-css-vars($typography-config: $custom-typography) using($mat-css-theme) {
  @include app-theme($mat-css-theme);
};

@mixin app-theme($theme) {
  // Your app theme
}

Legacy components support

Angular Material v15 introduces MDC based components, which is basically a re-write for a lot of the available components. angular-material-css-vars v5+ only supports MDC components.

In case you are still using the legacy components, you can use the package angular-material-css-vars-legacy.

Angular compatibility table

Angular angular-material-css-vars
16 5.x
15 4.x
13/14 3.x
12 2.x
11 1.x

Credit...

...goes to @zbirizdo project on which parts of this are based which is in turn supposedly based on this gist.

...and to @pedrojrivera without whom there would be no support for @angular/material v12.

Development server

Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

More Repositories

1

super-productivity

Super Productivity is an advanced todo list app with integrated Timeboxing and time tracking capabilities. It also comes with integrations for Jira, Gitlab, GitHub and Open Project.
TypeScript
11,214
star
2

linux-window-session-manager

A tool to store and reload open windows and window positions for x11 desktops like unity and gnome.
TypeScript
394
star
3

ng-fab-form

Convenient forms for Angular with no extra markup? Fabulous!
JavaScript
172
star
4

angular-promise-buttons

Chilled loading buttons for AngularJS
JavaScript
153
star
5

super-productivity-android

Android App for Super Productivity (https://super-productivity.com/)
Kotlin
105
star
6

gnome-shell-extension-window-session-manager

An indicator that let's you save and restore your open apps and the window positions and arrangements over multiple real and virtual displays.
JavaScript
91
star
7

angular2-promise-buttons

Chilled loading buttons for angular2
TypeScript
86
star
8

generator-angular-auto-admin-loopback

Generator for automatic CRUD angular backend for loopback apps and apis
JavaScript
32
star
9

generator-modular-angular

A truly modular yeoman generator for AngularJS all device apps.
JavaScript
21
star
10

angular-material-sass-files

The sass files of the angular-material repository
CSS
20
star
11

super-productivity-themes

The repository for user made themes for Super Productivity <3
SCSS
18
star
12

sp2

Super Productivity2
TypeScript
11
star
13

generator-angular2-lib

A generator for angular 2 libraries with all the basic stuff setup to get you started quickly.
TypeScript
9
star
14

rainbow-bash-unicorn

Awesome happy rainbow bash theme with unicorn inside!!!!
8
star
15

super-productivity-ios

Repository of the iOS app for Super Productivity
7
star
16

super-productivity-chrome-extension

Allows communication with jira and idle time tracking for super productivity inside the browser.
JavaScript
7
star
17

linux-window-session-manger-indicator

TypeScript
6
star
18

angular-auto-forms

Auto form markup helper
JavaScript
6
star
19

bumcss

Scalable css architecture for web apps and websites
6
star
20

procrastinate-somewhere-else

A little program to keep you away from the computer from a certain point of time, via a message, shutting down or going to sleep mode.
JavaScript
5
star
21

gps-recorder

Records the GPS position over time
JavaScript
5
star
22

direct-video

Chrome extension to directly play videos in a new window in the native video player.
JavaScript
2
star
23

pods-wp-cli

Pods WP-CLI package
PHP
2
star
24

angular-auto-admin-looopback

JavaScript
2
star
25

clone-rename

Little script to clone a directory and rename all files with a certain pattern as well as all variable names in it.
JavaScript
2
star
26

chrome-extension-simple-vocabulary-trainer

Simple vocabulary trainer using chrome notifications. Uses a custom vocabulary list to remind you in a given interval of your vocabulary.
JavaScript
2
star
27

gnome-shell-extension-super-productivity

The companion gnome shell extension for super productivity
JavaScript
2
star
28

johannesjo.github.io

my github pages
1
star
29

cover-img

Simple jQuery Cover IMG plugin
JavaScript
1
star
30

git-count

...
1
star