1

In my css i have several color variables like:

$bkg-color: #ffffff

white is what i want the default color to be but the $bkg-color could also be customized to a different color based on what the user creates and saves. So in my typescript i have:

backgroundColor?: string = '';

my get call response data is set to pageDetails and then i check

if(pageDetails.backgroundColor) { 
this.backgroundColor = pageDetails.backgroundColor; 
}

I cannot directly bind the background color class to the html because my page content is displayed via v-html='displayedHtml'

How can i set $bkg-color: this.backgroundColor (if exists || set default color of white).

i am using vue and typescript

i believe i can set the default like so: $bkg-color: #ffffff !default; but how i can make it assigned based on my data is where i am a bit lost.

1
  • Not fully following, scss variables cannot be changed at runtime Commented Jul 2, 2019 at 0:11

1 Answer 1

1
How can i set $bkg-color: this.backgroundColor (if exists || set default color of white).

Unfortunately, Scss variables do not exist at runtime, they exist only at compile time and then the python-written compiler replaces them with their value in the css style sheet. If you're not supporting Internet Explorer You can use css variables to solve this problem which can be changed at run time dynamically.

If iv'e misunderstood the question let me know and i'll add an edit.

EDIT: https://jsfiddle.net/njsrLywt/ Here's a demo showing changing css variables at run-time.

EDIT2: You will also no longer be able to use lighten/darken however you can use a javascript implementation of lighten/darken which exists at run-time. Also on a side note you should avoid using lighten/darken in favour of scale-color which allows for better color scaling because it scales the remaining % of 100

EDIT3: Here is a quick implemtation of scale-color implemented with javascript 100% fully immutable FP style use it in the following way

ColorUtils.scaleColor("#294e80", -35);  //darken 35%
ColorUtils.scaleColor("#F6F6F6", 35); // lighten 35%

https://typescript-play.js.org/#code/MYGwhgzhAEDCD2J4CcCqAXAliGBvAUAJATphbDQTBggCmCSy0AvNABTCIoAStAHgC5K6ZJgB2AcwA00AA61kwWmPRCxAVwC2AIwUBKFgD5oBQoQD0AKmiZ0AchgALMMgAm0dPGgSvAM2TwmtCO-NAAtMaOECDQYMDA6shktCAAnpRemN5+AUEhfOHGyBLahdBitADu0NwAygAyZfnQluZEhJxiJNAASgDiAEIswfwAKvA9JRxcyLx8egDc7Zi+7P1DzJvl6iAgBsi06Ili0JyMc0tmnd0A2o4yEDIgALrDxdoATFEg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RqxYzkBrqVtWfy+m5C+uylTLDmRC9qOufZAepngjIgzYrpvuEb6OQAb6BC5bxyPcrIV4DXPNHDs8KHmnkKnjn-Qoojmp11tGvw-WpFY8oW1U5XrOYg5feiDF00tm1DEzYAAw7WdcE8EV6j0BV78f91n9fh-x4-l-r7ble3B1C0A+EmPMshF6hQAI6QJkKoMKIURYYkrEpAAWgoLwrANRamgCxJElI4pCHQDyHK6AADyFQyToHQQEZEnJcEMmoRg+khDGE0PgPLcG5DKGajihyCSH1sayCZlA+ksgDDWDEkyCQ3DaCcJkRQ2g0BeH1Rll9KB8jZHKJKuvNRJxhH41ERTcRloPjrxZrIiR0AxI8jNrIHmHst7lCqAARWGIrLUKsGE52pKBWoBghCNEpELCmPjahpwqriAACsMMx1hGgU1xC4iJ3QZo1GAaAqY0SvSVBcTIKu9COSmXGjFECrAgEgPeNRKoUScl5JqCUiquZ0mVKyTUup+SWG6MuDfO+38RSuy-u-AaEgX5vx-gNbQf9h7phisMm6ggEFLzLN+UeLZoBzV6gEdQ9Y2AFGsKVPQ-xPC1BvJINglkemgxGHwf4dBJC1hYFsOKLEABEKJXmgWaEIfIw8rDQAAKIADkAAi0BuKdSifUAFnVgWjA8qMbiZCgUtDaB7IAA

Sign up to request clarification or add additional context in comments.

6 Comments

how can use css variables to solve the problem? I tried declaring my variable as so: --bkg-color: #ffffff an in my code if i do have a background color value then i document.body.style.setProperty('--bkg-color', pageDetails.backgroundColor) but that didn't seem to work.
Iv'e added a edit with a working fiddle to show how to change css variables at runtime.
Thanks for the demo - i realized i didn't have :root and needed to a # tag before my string value. Also would i still be able to use: styleName: lighten(var(--class-color), 20%);
No you wont as lighten only exists at compile-time unfortunatly :( you can however use a javascript implementation of lighten/darken
Added a final edit with a javascipt implementation for scale-color in typescript
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.