This demo implements a dark and light Angular theme.
When a theme is selected the class name for the current theme set on the body element with an observable like this (app.component.html):
[class]="s.OS.S.theme.obs | async"
When a new theme is selected the current theme is removed from the overlay container and the new class name is added to the overlay container.
This is all done within app.component.ts. These snippets remove and add the class name from the overlay container:
Remove Theme Class from Overlay Container
overlayContainerClasses.remove(...themeClassesToRemove);
Add Theme Class from Overlay Container
this.overlayContainer.getContainerElement().classList.add(themeClass);
In general it works except when the dark theme is selected it looks like the overlay container stays in place, it is black and it covers the whole viewport. Clicking within the viewport makes it disappear. That is just an attempt by me to describe whats happening.
Any ideas on how to fix this?
Theme Implementation Within styles.scss
#use '#angular/material' as mat;
#include mat.core();
$my-theme-primary: mat.define-palette(mat.$indigo-palette, 500, 200, 800);
$my-theme-accent: mat.define-palette(mat.$cyan-palette);
$my-theme-warn: mat.define-palette(mat.$deep-orange-palette, A200);
$light-theme: mat.define-light-theme(
(
color: (
primary: $my-theme-primary,
accent: $my-theme-accent,
warn: $my-theme-warn,
),
)
);
// ====================================
// Make sure to have a dark background
// Dropdowns will blend in with light
// backgrounds.
// ====================================
$dark-theme: mat.define-dark-theme(
(
color: (
primary: $my-theme-primary,
accent: $my-theme-accent,
warn: $my-theme-warn,
),
)
);
// =====================================
// always include only once per project
// =====================================
.dark-theme {
// use our theme with angular-material-theme mixin
// #include angular-material-theme($dark-theme);
// #include mat.core-theme($light-theme);
#include mat.all-component-themes($dark-theme);
background-color: black;
}
.light-theme {
// use our theme with angular-material-theme mixin
// #include angular-material-theme($dark-theme);
// #include mat.core-theme($light-theme);
#include mat.all-component-themes($light-theme);
}
html,
body {
height: 100%;
}
body {
margin: 0;
font-family: Roboto, 'Helvetica Neue', sans-serif;
}
Found some issues in the theme implementation. Take a look at the update code: https://stackblitz.com/edit/angular-xvxuhg-acrvsa?file=src%2Fstyles.scss,src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts
Documentation - https://v14.material.angular.io/guide/theming
styles.scss - dark theme is implemented using #include mat.all-component-colors, not themes.
app.component.html - Apply the mat-app-background CSS class to your main content root element (typically body).
https://material.angular.io/guide/theming#application-background-color
app.component.ts - Instead of using an observable + async pipe to read the theme class. #HostBinding is a lot more straight forward.
#HostBinding('class')
themeMode: string = '';
Related
Trying change the background color for the class mat-app-background.
I assumed that the property that needs to be changed is the background property of the background map within the Angular theme.
The original value is #303030 and that is the the background color for the dark theme without any changes.
I tried changing this value with this function:
#function set-theme-background-color($theme, $color) {
$background: map.get($theme, background);
$background: map.merge(
$background,
(
background: $color,
)
);
#return map.merge(
$theme,
(
background: $background,
)
);
}
$dark-theme: set-theme-background-color($dark-theme, #4a8eb0);
And then generate the theme classes like this:
.dark-theme {
#include mat.core-theme($dark-theme);
#include mat.all-component-colors($dark-theme);
}
However as can be seen in this demo the background for the dark theme does not change.
How do we change the background color for the dark theme?
We have a web application built using Angular 9. It has colors for headers, borders and some menu backgrounds stored in multiple places in css files.
The ask is to change those as per client branding. So if ClientA logs in, they should start seeing those colors as #FF0000 and if ClientB logs in, they need to see those colors as #00FF00.
Other than inline styling every html with style="color:{{clientColor}} can you help suggest the best way to do this?
Thank you!
You can try to use :root selector and variables in it, and for body tag just overwrite these root variables, working example here: stackblitz
styles.scss:
:root {
--fontColor: #000;
}
.theme-dark {
--fontColor: red;
}
app.component.ts
export class AppComponent {
theme = 'theme-dark';
toggle(): void {
const body = document.body;
if (body.classList.contains(this.theme)) {
body.classList.remove(this.theme);
} else {
body.classList.add(this.theme);
}
}
}
app.component.html
<p>
Start editing to see some magic happen :)
</p>
<button (click)="toggle()">Toggle color font</button>
app.component.scss
p {
font-family: Lato;
color: var(--fontColor);
}
You can use this:
[style.color]="clientColor"
I use the Twemoji library to get emoji icons in my Next js App, they appear as <span><img></span> in the final HTML and I can override their default width and height using !important in my globals.scss file:
.customize { //Parent class containers 3 buttons, each one has an emoji element
top: 88%;
right: calc(50vw - (52.2px + 3rem));
button {
span img[style]{
width: 35px !important;
height: 35px !important;
}
}
}
Then I tried to extract it as a [].module.scss file, everything works but the images don't change size whatsoever. Why?
Edit
Here is the component I'm trying to style:
import ThemeButton from '../components/themeCustomizeButton' // a button that renders an emoji
import LanguageSwitcher from '../components/LanguageSwitch' // a button that renders an emoji
import Complex from '../components/ComplexitySwitch' // a button that renders an emoji
import styles from "../styles/local/components/customize.module.scss" // importing .module.scss
function Customizing() {
return(
<section className={styles.customize}>
<ThemeButton />
<LanguageSwitcher />
<Complex />
</section>
)
}
export default Customizing
Just use this code before wherever you are trying to extract
$(document).ready(function () {
// Set the size of the rendered Emojis
// This can be set to 16x16, 36x36, or 72x72
twemoji.size = '36x36';
});
I have 2 question.
Is it possible to change a sidebar backgorund colour based on it's id?
For example, in sidebar.js, I have sidebar1 and sidebar2. I want sidebar 1 to have a different colour than sidebar 2.
How do I input the default sidebar on a custom page? I created a new page, however, it only contain the default navbar and footer only, no sidebar.
Thanks for all the help :)
For now you can swizzle the theme sidebar and add a class according to the sidebar name.
If you open an issue, we could automatically add that class on the sidebar
Use CSS:
Add customCss to your theme config(docs), e.g.:
{
// ...
presets: [
[
'classic',
/** #type {import('#docusaurus/preset-classic').Options} */
({
// ...
theme: {
customCss: require.resolve('./src/css/custom.css'),
}
})
]
]
}
Add "className": "category1" to _category_.json
Add class of same name to src/css/custom.css:
.category1 > .menu__link {
color: red;
}
NOTE: the element that receives the class contains an a element of class .menu__link that defines the default color, so override that.
In my app, I have movies' details that can be opened, and I want the buttons of the detail to match the movie.
For instance, with the movie "Back to the Future", I have in my data colors = ["#000000","#123123"].
If I do <div [ngStyle]="{'background-color': movie?.colors[0]}"> the div will be of the color I wanted.
My question is, in Ionic, how can I change variables.scss to have these colors (updated when we open a new movie) ?
Because we can't modify tabs with custom css, so I have to add it to variables.scss...
if you want to update any css color or value like font-size like the sass variable at run time use css variables in this way you can update any css property value at run time if it base on css variable like the color in my example but it 's can be any css value
consider this example
style.css
:root {
--color : red;
}
* {
color:var(--color)
}
AppComponent
colorList = ['green', 'blue'];
updateColor(color) {
document.documentElement.style.setProperty(`--color`, color);
}
Template
<button *ngFor="let c of colorList" (click)="updateColor(c)">{{c}}</button>
stackblitz demo 🚀🚀
sass variable are going to compile at build time to there values so they are not reusable at run time
For most use cases, it is convenient to programmatically change the CSS value of an element by mapping it with a variable. We want the CSS value to change every time we update the variable, not only through this.ngZone.run().
<div class="progress" [style.height]=currentLevelPercentage>
This example has shown how we can map the height CSS property of the div element (class progress) to the variable currentLevelPercentage and change its value dynamically. currentLevelPercentage is the variable that must be compulsorily present in the TypeScript file.
For those here to know how to change color of each tab background in super-tabs (ionic) here's my 4 tabs code (I can now change height and width with code too ^^).
in tabs-page.scss :
:root {
--color1: white;
--color2: white;
--color3: white;
--color4: white;
}
super-tab-button:nth-of-type(1) {
background-color: var(--color1)
}
super-tab-button:nth-of-type(2) {
background-color: var(--color2)
}
super-tab-button:nth-of-type(3) {
background-color: var(--color3)
}
super-tab-button:nth-of-type(4) {
background-color: var(--color4)
}
in tabs-page.html : do nothing particular
in tabs-page.ts :
constructor(public navCtrl: NavController, public navParams: NavParams) {
document.documentElement.style.setProperty('--color1', this.movie.colors[0]);
document.documentElement.style.setProperty('--color2', this.movie.colors[1]);
document.documentElement.style.setProperty('--color3', this.movie.colors[2]);
document.documentElement.style.setProperty('--color4', this.movie.colors[3]);
}
Thank you #malbarmawi !
Just an idea about changing style dynamically. here is what i am using
<span [style.width]=foo></span>
Change the value of ‘foo’ in your .ts file
https://angular.io/docs/ts/latest/guide/template-syntax.html#!#style-binding
Simply try this
[ngStyle]="{'background-color': item.color}"