Since these values are defined at root level, you can just update them using document. I am not exactly sure on how you can access the document in angular, but once you have document, you can do something like this:
document.documentElement.style.setProperty(`--${your-variable}`, value + suffix); //suffix may be px or ''
This way, you will directly set the css variable which will take effect at all the places and you will not have to target individual elements.
Answer from ashfaq.p on Stack Overflowreactjs - How to define css variables in style attribute in React and TypeScript - Stack Overflow
angular - How to set TypeScript variable value as CSS property? - Stack Overflow
Setting a CSS variable in `attrs` throws a TypeScript error
Get CSS Parameter from Typescript Angular - Stack Overflow
Like this:
function Component() {
const style = { "--my-css-var": 10 } as React.CSSProperties;
return <div style={style}>...</div>
}
Or without the extra style variable:
function Component() {
return <div style={{ "--my-css-var": 10 } as React.CSSProperties} />
}
This works in both .tsx files and .d.ts files.
The key is to include import 'react' at the top. This turns the file into a module (even if it is a .d.ts file), allowing you to use Module Augmentation to extend the existing React types rather than overwriting them.
The Solution
Place this snippet at the top of your component file (.tsx) or in any definition file (e.g., types.d.ts).
import 'react';
declare module 'react' {
interface CSSProperties {
// Allow any CSS variable starting with '--'
[key: `--${string}`]: string | number
}
}
Usage
Now you can pass CSS variables to the style prop without TypeScript errors:
<div style={{ "--value": percentage, color: "var(--value)" }} />
The {'min-height': '{{taille}}'} part should in fact be {'min-height': taille}.
Angular documentation presents the below very similar example:
<div [ngStyle]="{'color': colorPreference}">
From your sample code it seems that taille is initially undefined which you might want to take into account (initialize taille or use a ngIf or other approach).
Using [ngStyle] -
<mat-card [ngStyle]="{'min-height': taille}">
Or you can use [style.min-height] = "taille" instead.
<mat-card [style.min-height] = "taille">
please go through this for more insights.
» npm install css-variable
I have a way to do this using a styles service based on https://en.programqa.com/question/52907585/
Within Global.SCSS
@mixin ExportVariables($map, $prefix: null) {
$mapPrefix: "--#{$prefix}";
@if ($prefix){
$mapPrefix: "#{$mapPrefix}-";
}
body {
@each $name, $value in $map {
#{$mapPrefix}#{$name}: $value;
}
}
}
--idle-state: #29ABE2;
// Import each of these in the theme service
$stateSCSS:(
idle: var(--idle-state),
);
@include ExportVariables($stateSCSS, 'stateSCSS');
In the Service
const bodyStyles = window.getComputedStyle(document.body);
this.stateSCSS = {
idle: bodyStyles.getPropertyValue('--stateSCSS-idle'),
};
I think this answers your questions: access SASS values ($colors from variables.scss) in Typescript (Angular2 ionic2)
TLDR:
Unfortunately, there is no way to access SASS variable directly from typescript/javascript code. However, we can make a workaround to access those variables.
You can view the workaround in the post mentioned above
Yes you can set variables in root scope:
:root {
--main-color: red
}
Yes you can use :host selector to target element in which the component is hosted.
:host {
display: block;
border: 1px solid black;
}
You can also use, :host-context to target any ancestor of the component. The :host-context() selector looks for a CSS class in any ancestor of the component host element, up to the document root.
:host-context(.theme-light) h2 {
background-color: #eef;
}
Note: ::ng-deep or /deep/ or >>> has been deprecated.
Read more about it here: special css selectors in angular
Just an additional information. It works both inside ':root' as well as ':host' We can set values to them by:
constructor(private elementRef: ElementRef) { }
then
this.elementRef.nativeElement.style.setProperty('--color', 'red');
The most constructive and modular way to use css vars in components (with viewEncapsulation) is as such:
// global css
:root {
--main-color: red
--alt-color: blue
}
// inside component component css
::ng-deep :root {
--specific-css-var: var(--main-color)
}
:host {
background-color: var(--specific-css-var)
}
:host(.conditional-class) {
--specific-css-var: var(--alt-color)
}
NOTE: despite ::ng-deep being deprecated, it hasn't been replaced yet (and has no replacement), as can be read in several discussion like this
So I've been delving more into styling and styled-components, I came across Josh Comeau's article on styled-components and liked his approach to pass a style object with css variables instead of props, however I can't get typescript to stop complaining, here's an example:
<Button style={{'--accent': 'red'}}>And the styled component:
type Props = { style: CSSProperties }
export const Button = styled.button<Props>`
border: 2px solid var(--accent);
border-radius: 4px;
padding: 16px;
margin: 8px;
background: white;
color: var(--accent);
`;
But I get the error "Object literal may only specify known properties...", I've done a bit of googling and found the following solution, but it feels like a pain in the ass to smear "as CSSProperties" everywhere:
<Button style={{ '--accent': 'black', } as CSSProperties}>Is there a better way to do this?