Giter Club home page Giter Club logo

angular-material-css-vars's Introduction

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
17 6.x
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.

angular-material-css-vars's People

Contributors

adrigm avatar austenstone avatar cgatian avatar daskus1 avatar dependabot[bot] avatar dflor003 avatar fstrelczuk avatar johannesjo avatar json-derulo avatar literalpie avatar negue avatar priveraussi avatar vpetrusevici avatar wlada 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.