How should I apply a custom angular material theme? - angular-material

I have a problem trying to setup a custom Angular Material theme. I'm trying to follow the basic example given in the docs (https://material.angular.io/guide/theming) but keep hitting an error during startup of app : "SassError: Missing argument $accent".
This appears to be trying to highlight the mat-light-theme() call, but as far as I can see I'm following the example file given in docs.
Does this look about right... (I'm hoping to be confident the theme file is right so I can start tracking down issues introduced elsewhere)?
#import '~#angular/material/theming';
#import 'mixins/definitions'; // just another scss file
// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
#include mat-core();
$my-app-primary: mat-palette($mat-indigo);
$my-app-accent: mat-palette($mat-pink, A200, A100, A400);
$my-app-warn: mat-palette($mat-red, A200, A100, A400);
// The warn palette is optional (defaults to red).
//$candy-app-warn: mat-palette($mat-red);
// Create the theme object. A theme consists of configurations for individual
// theming systems such as `color` or `typography`.
//$candy-app-theme: mat-light-theme((
$my-app-theme: mat-light-theme((
color: (
primary: $my-app-primary,
accent: $my-app-accent,
warn: $my-app-warn
)
));
#include angular-material-theme($my-app-theme);
#include mat-core-theme($my-app-theme);
#include mat-checkbox-theme($my-app-theme);
Some bits that may be relevant from my package.json:
"dependencies": {
...
"#angular/animations": "~9.1.9",
"#angular/cdk": "^9.2.4",
"#angular/common": "~9.1.9",
"#angular/compiler": "~9.1.9",
"#angular/core": "~9.1.9",
"#angular/material": "^9.2.4",
...
}
"devDependencies": {
"#angular-devkit/build-angular": "~0.901.7",
"#angular/cli": "~9.1.7",
"#angular/compiler-cli": "~9.1.9",
...
}

Working example, pay attention to the 'mat-light-theme' line:
#import '~#angular/material/theming';
#include mat-core();
$app-primary: mat-palette($mat-indigo);
$app-accent: mat-palette($mat-amber, A200, A100, A400);
$app-warn: mat-palette($mat-red);
$app-theme: mat-light-theme($app-primary, $app-accent, $app-warn);
#include angular-material-theme($app-theme);

Related

Angular Material Styles Not Appearing in Storybook

I am trying to create a Storybook using the Angular Material library in an nx app but I can't get the styles to appear that come along with the library. So the component renders but there are no styles along with it. I've added the component to Storybook like so:
export default {
title: 'Buttons',
component: ButtonComponent,
decorators: [
moduleMetadata({
imports: [
MatButtonModule
],
}),
]
} as Meta<ButtonComponent>;
This screenshot shows the Primary Button but doesn't get the correct purple styling.
How do I go about getting something like this import into Storybook so the theme comes through?
#import '#angular/material/prebuilt-themes/deeppurple-amber.css';
This is also an nx app so there is no angular.json, but a project.json and a workspace.json. I have try adding the theme to project.json as I have below but doesn't work, I would assume it needs to passed to storybook directly somehow and not inside of the project.json (which would apply to the app itself and not storybook)?
"build": {
"options": {
"styles": [
"node_modules/#angular/material/prebuilt-themes/deeppurple-amber.css"
],
"scripts": []
}
},
Using Angular Material "13.2.3", "#nrwl/storybook": "13.8.3", and then these additional libraries:
"#storybook/addon-essentials": "6.5.0-alpha.41",
"#storybook/addon-knobs": "6.4.0",
"#storybook/addons": "^6.5.0-alpha.41",
"#storybook/angular": "6.5.0-alpha.41",
"#storybook/api": "6.5.0-alpha.41",
"#storybook/builder-webpack5": "6.5.0-alpha.41",
"#storybook/core-server": "6.5.0-alpha.41",
"#storybook/manager-webpack5": "6.5.0-alpha.41",
Any help is much appreciated!
In your project.json there should be a storybook target. That's where you need to add styles.
build only affects the build target.
e.g.
"storybook": {
"executor": "#storybook/angular:start-storybook",
"options": {
"port": 4400,
"configDir": "libs/shared/form-controls-ui/.storybook",
"browserTarget": "shared-form-controls-ui:build-storybook",
"compodoc": false,
"styles": [
"./node_modules/#angular/material/prebuilt-themes/deeppurple-amber.css"
]
},
Maybe is a little bit too late but here is what I've done.
I've created a styles.scss file in the root of the project, inside it I've imported
#import '~#angular/material/theming';
#include mat-core();
and then I've imported it into scss of the component.
You can skip the scss in the root and 1just import material into the component's scss.
Cheers!

CSS Styling issue in Cumulocity server

I've deployed my angular 7 project in cumulocity server. No errors while building and deploying the application. But, Bootstrap CSS styling is not working properly.
In package.json I've mentioned these dependencies.
"#c8y/ngx-components": "1004.2.0",
"#c8y/style": "1004.2.0",
"#ng-bootstrap/ng-bootstrap": "^4.1.0",
In the new Angular Web SDK the Cumulocity Team switched to ngx-bootstrap. It is included in ngx-components, so no need to add it.
Your issue might be that some styles are missing as Cumulocity just comes with a subset of bootstrap. You might need to add bootstrap CSS to your app to make it working:
Add a branding entry in the package.json:
"c8y": {
"application": {
"name": "tutorial application",
"contextPath": "tutorial-application",
"key": "tutorial-application-key",
"brandingEntry": "./branding/branding.less",
"globalTitle": "Pied Piper IoT",
"tabsHorizontal": true
}
}
Then add the #c8y styles plus bootstrap in that file:
#import '~#c8y/style/extend.less';
#import '~yourpatt/to/boostrap.css';

Access primary color of prebuilt schemes

I set up a new project using
ng new app
ng add #angular/material
I included the following line in styles.css:
#import '~#angular/material/prebuilt-themes/deeppurple-amber.css';
I want to apply the primary color of the deeppurple-amber theme to a <p> element.
How can I extract the primary color of a prebuilt scheme and apply it to my own component in Angular 6??
Instead of importing the pre-built theme, include the content of the pre-built theme instead.
#import '~#angular/material/theming';
// Include non-theme styles for core.
#include mat-core();
// Define a theme.
$primary: mat-palette($mat-deep-purple);
$accent: mat-palette($mat-amber, A200, A100, A400);
$theme: mat-light-theme($primary, $accent);
// Include all theme styles for the components.
#include angular-material-theme($theme);
You can then access the palettes and colors and theme your own components.

Bourbon's #font-face mixin

I'm new to Bourbon & SASS and trying to use the #font-face mixin to add a font I downloaded (Museo Sans) to my Rails 3 app.
Bourbon provides the following examples:
#include font-face(SourceSansPro, '/fonts/Source_Sans_Pro/SourceSansPro-Regular');
#include font-face(SourceSansPro, '/fonts/Source_Sans_Pro/SourceSansPro-Bold', bold);
#include font-face(SourceSansPro, '/fonts/Source_Sans_Pro/SourceSansPro-Italic', normal, italic);
// Rails asset-pipeline - place fonts in app/assets/fonts/
#include font-face(SourceSansPro, 'Source_Sans_Pro/SourceSansPro-Regular', normal, $asset-pipeline: true);
What I did:
// application.css.scss
#import "bourbon";
#import "fonts";
// fonts.css.scss
#include font-face(MuseoSans, '/fonts/MuseoSans/MuseoSans_500-webfont', normal, $asset-pipeline: true);
* {
font-family: MuseoSans;
}
The fonts are in assets/fonts/MuseoSans/ with filenames like MuseoSans_500-webfont.eot, .ttf, etc. I get the impression you can leave off the extension and Bourbon is supposed to pick up all the files.
I've tried a lot of different variants of the above to no avail. I know that Bourbon and the files are working because when I set the font-family to $helvetica I see a change on the page.
If anyone can provide the proper code, or a GitHub project that I could look at, I'd be much obliged.
Try removing the leading "/fonts" in your in your path like:
#include font-face(MuseoSans, 'MuseoSans/MuseoSans_500-webfont', normal, $asset-pipeline: true);
I had some issues working with this mixin as well - I would get IOerrors if the following files were not present in the fonts directory: "myfont.eot?#iefix" and "myfont.svg#myfont."
However, when I added those files, I found that they were not getting precompiled (i.e. they lacked a MD5 thumb print and weren't present in the asset manifest), so I decided to override this mixin and rewrite it using modified asset-path methods.

Sharing mixins between scss files in rails 3.1

I'm trying to refer to a mixin defined in one file (application.css.scss) in another (home.css.scss). I have tried importing application into home but still get an "Undefined mixin" error.
Is there a way to automatically import all of my files, or what is the best way to manage imports between files?
I haven't been able to make the jump to 3.1 yet, but using Compass & Sass for quite a while, I've found it's best to try to manage mixin/definition sass separately from your actual CSS generating sass. In this way, the mixin files can be treated freely like code libraries, included wherever necessary, without them repeatedly generating CSS rules.
So you might have:
/* my-mixin-concern.scss */
$default_foo: 123px !default;
#mixin some-concern($foo: $default_foo) {
// do something
}
/* application.scss */
$default_foo: 321px; // optionally, pre-set the default value before import.
#import 'my-mixin-concern';
p { #include some-concern; }
/* home.scss */
#import 'my-mixin-concern';
body.home p { #include some-concern(9000px); }
In this way you are explicitly importing all requirements for each scss file, similarly to how you would do so in a code library.
Pure Rails Solution
Step 1. Rename
application.css -> application.css.scss
Step 2. Refactor
// application.css.scss
/*
*= require_self
*/
#import "mixins.css.csss"
#import "project.css.scss"

Resources