Angular Material Font Family Switch - angularjs

I can't seem to find any documentation regarding using a different font in Angular Materials. Is this possible through the framework?

This is the official documentation on the subject, but it doesn't specify how to provide backup family classes if a font can't load:
https://material.angular.io/guide/typography
#import '~#angular/material/theming';
// Define a custom typography config that overrides the font-family as well as the
// `headlines` and `body-1` levels.
$custom-typography: mat-typography-config(
$font-family: monospace,
$headline: mat-typography-level(32px, 48px, 700),
$body-1: mat-typography-level(16px, 24px, 500)
);
// Override typography for all Angular Material, including mat-base-typography and all components.
#include angular-material-typography($custom-typography);
Edit; for backup font-families (notice the quoting):
$font-family: '"Sintony", Arial, sans-serif'
Edit; if you had a custom font in /assets
#font-face {
font-family: 'MyWebFont';
src: url('/assets/webfont.eot');
}
$font-family: '"MyWebFont", Arial, sans-serif'

Angular Material is implementation of Material Design and Material Design is strongly linked to Roboto font and as you mentioned, there is nothing about it in the documentation, so I think it is not possible to do that through the framework.
But you can easily change font family in css file, which needs to be included in your build after your Angular Material dependencies. Here's an example:
body {
font-family: "Comic Sans MS";
}
input,
button,
select,
textarea {
font-family: inherit;
font-size: inherit;
line-height: inherit;
}
And Demo: http://codepen.io/mattdanna/pen/pgwVzX
Borrowed from here: https://github.com/angular/material/issues/6561#issuecomment-170837189

Related

How To Change the Color and Font Of Labels of Bar Chart Using ChartistJs Library with ReactJs

I am working with React-Js Application and using ChartistJs Library to show the Charts.
I am trying to change the color and Font of Lables in Bar Chart but unable to find any options to achieve that.
I think you can do something like this in your CSS file:
.ct-label {
color: red;
font-size: 15px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
I hope it helps.

Nextjs using rsuitejs and css modules no css in build directory

I use nextjs with rsuitejs.
in dev everything works fine.
after build the library css are not in the build directory,
so ugly page with no css.
i use css module with custom webpack config.
you can find the project here :
https://github.com/Temkit/rsuitejs-nextjs-example
i imported rsuitejs in the less file and it worked,
+ #import '~rsuite/styles/less/index';
h1 {
font-size: 25px;
font-weight: 600;
color: #424242cf !important;
padding-left: 12px;
}
the repository is correct now, if someone is facing the same problem check this example https://github.com/Temkit/rsuitejs-nextjs-example
thank you to simonguo

google material icons loading slowly, flicker from text to icon

Installing Google Material icons using Setup Method 2 self hosting for our React project the ligatures associated with the icon is sometimes displayed before the material icon.
<i class="material-icons">face</i> {/* shows text "face" on site prior to proper material icon load */}
For example the above line would display "face" for a second before showing a face. How can we delay the UI rendering until the file references are fully loaded?
/*material icons file references loaded locally */
#font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(../node_modules/material-design-icons/iconfont/MaterialIcons-Regular.eot); /* For IE6-8 */
src: local('Material Icons'), local('MaterialIcons-Regular'), url(../node_modules/material-design-icons/iconfont/MaterialIcons-Regular.woff2) format('woff2'), url(../node_modules/material-design-icons/iconfont/MaterialIcons-Regular.woff) format('woff'), url(../node_modules/material-design-icons/iconfont/MaterialIcons-Regular.ttf) format('truetype');
}
Create a file called preload.js in your src folder.
document.fonts.load('10pt "Material Icons"').then(function () {
console.log('Material Icons font has been preloaded.');
});
add the following script tag at the end of the head section in index.html
<script defer src="%PUBLIC_URL%/preload.js"></script>
user in your React component like this
import Icon from '#material-ui/core/Icon';
function MyComponent() {
return <Icon>home</Icon>;
}
I hope it's working
Answer from How to prevent material icon text from showing up when Google's JS fails to convert them?:
you can use font-display: block;, just add this CSS to your HTML head:
<style>
#font-face {
font-family: 'Material Icons';
font-display: block;
}
</style>
for more information font-display

MDC-Web Typography: Font Customization

Is the MDC typography specific to the Roboto font, or can we implement with other Google fonts and if so, is the recommended way simply to apply the font-family CSS to body?
Lastly, it appears that all header elements are tied to the <h1> element which seems to break the semantic nature of HTML5, i.e. h1 normally has higher significance than h5.
MDC-Web is a customizable library, and given the fact that Google doesn't prohibit using your brand styles along with Material Design framework, you're free to use any font, not just "Roboto".
If you're using CSS-only approach, adding font-family to body is not enough: MDC-Web applies default typography styles (including font-family) to different components (e.g., mdc-button, mdc-list, mdc-card) and typography classes, and they still will have “Roboto” font applied. So, if you’re going to use such MDC-Web components and classes, you need to manually specify font-family for each of them:
.mdc-button {
font-family: "Open Sans", sans-serif;
}
.mdc-list {
font-family: "Open Sans", sans-serif;
}
.mdc-card__supporting-text {
font-family: “Open Sans”, sans-serif;
}
But this might be tedious, so the recommended way to generate MDC-Web styling is to use Sass. Specifically, you need to override MDC-Web’s typography variable in your .scss file before importing the component:
$mdc-typography-font-family: "Open Sans", sans-serif;
#import "#material/typography/mdc-typography";
Another method is described in the MDCv2 developer documentation:
#use "#material/typography" with (
$font-family: unquote("Arial, Helvetica, sans-serif")
);
This method leverages Sass module variables.
If you don't want to use SASS, you can do some basic customisation by setting variables in your css. For example:
<link href="https://fonts.googleapis.com/css?family=Nunito+Sans:200,300,400,600,700" rel="stylesheet">
<link rel="stylesheet" href="https://unpkg.com/material-components-web#latest/dist/material-components-web.min.css">
<link rel="stylesheet" href="/assets/css/site.css" />
site.css
:root {
--mdc-typography-font-family: 'Nunito Sans', sans-serif;
}
body {
font-family: var(--mdc-typography-font-family);
}
You can also override the default theme colours using variables like this:
--mdc-theme-primary: #0d46a0;
More info can be found here: https://material.io/develop/web/docs/theming

How to use material-ui (alpha) with styeld-components properly?

I've been trying to use styled-components with the alpha version of material-ui
According to the documentation, this should work out of the box.
This code:
const StyledButton = styled(Button)`
color: red;
text-transform: uppercase;
`;
return <StyledButton>Button</StyledButton>;
will generate something like this:
<button tabindex="0" class="MuiButtonBase-root-3177716317 sc-bdVaJa sxRGN" type="button" role="button">
...
</button>
It looks good.
However, the only problem I have is the order of the injected CSS styles (pic). Styles from styled-components are injected before MUI's styles which make their priority lower.
Is there any way to solve this without using !important?
In the current release (i.e. non-alpha) version, what you've asked would indeed require !important basis:
"Note that CSS properties defined inline are given priority over those defined in a CSS class. You need to use !important to take precedence over the inline style."
Ref: http://www.material-ui.com/#/customization/styles
Perhaps the alpha hasn't quite moved away from this inline requirement yet or it is still a work-in-progress.
What I've done to overcome this sort of thing myself is to (unfortunately) recreate the entire CSS on a standard <button> element when I need such a solution. Here's an example of how I'm doing that with a react-photonkit "theme"
// #flow
import styled from 'styled-components';
const PhotonStyledButton = styled.button`
font-family: Arial, Roboto, Helvetica, sans-serif;
height: 30px;
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 12px !important;
line-height: 1.4;
text-align: center;
white-space: nowrap;
vertical-align: middle;
cursor: default;
background-image: none;
border: 1px solid transparent;
border-radius: $default-border-radius;
box-shadow: 0 1px 1px rgba(0,0,0,.06);
-webkit-app-region: no-drag;
&:focus {
outline: none;
box-shadow: none;
}
color: #333;
border-top-color: #c2c0c2;
border-right-color: #c2c0c2;
border-bottom-color: #a19fa1;
border-left-color: #c2c0c2;
background-color: #fcfcfc;
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#fcfcfc), color-stop(100%,#f1f1f1));
background-image: -webkit-linear-gradient(top, #fcfcfc 0%, #f1f1f1 100%);
background-image: linear-gradient(to bottom, #fcfcfc 0%, #f1f1f1 100%);
&:active {
background-color: #ddd;
background-image: none;
}
`;
export default PhotonStyledButton;
styled-components in general is compatible with any component library. When you write styled(AnotherComponent) we take that component and inject an automatically generated class name. This means essentially it's the same thing as writing <AnotherComponent className="sc-asdf123" />!
The current version of material-ui specifically is a bit difficult to custom style because it uses inline styles. From the MaterialUI documentation:
Note that CSS properties defined inline are given priority over those defined in a CSS class. You need to use !important to take precedence over the inline style.
This means simply using styled(MaterialButton) won't work as the passed-in styles will mostly just be ignored. You need to bump the specificity of your styles to override the inline styles that material-ui ships with. (this article is a great primer on specificity if you're not familiar with the details)
Answer for the alpha version of material-ui
The current alpha version of material-ui has switched to using JSS under the hood. (which is CSS in JS not inline styles, like styled-components) This means the issue is likely to be that the styled-components styles are injected after the default material-ui styles. (which are injected by JSS)
JSS supports custom injection points so you might be able to add a <!-- jss --> comment to the HEAD of your HTML to make sure JSS injects its CSS before the styled-components injected CSS?
Answer for the current version of material-ui
There are two ways to bump the specificity of the styled-components injected styles, one more tedious and one a bit more "hacky". The first one is adding !important at the end of all of your styles:
const Button = styled(MaterialButton)`
color: blue!important;
`
While this works in most cases it gets tedious very fast when you have lots of custom styling in a component. The better way is to use the class name hack:
const Button = styled(MaterialButton)`
&&& {
color: blue;
}
`
These ampersands get replaced with the automatically generated class name meaning the outputted CSS looks something like this:
.sc-asdf123.sc-asdf123.sc-asdf123 {
color: blue;
}
This bumps specificity by a big margin, thusly overriding the defined inline styles, and is less annoying than having to put !important after each rule.
Now we can use <!-- material-ui --> to make sure the styles are injected after it.
By default, Material-UI will look for a html comment named to inject styles after. By adjusting the placement of this comment within your HTML body you can control the order that CSS rules are applied to your components. (ref)

Resources