How can I use data in css with Vue? - css

I want to design a button component with Vue.
When I get main props, how can I get in css --mainColor variable without setting inline-style? thanks.
<script>
import Vue from "vue";
export default Vue.extend({
name: "Button",
props: {
text: {
type: String,
default: ""
},
main: {
type: String,
default: "#10b981"
}
}
});
</script>
<style lang="scss" scoped>
.btn {
--mainColor: #10b981;
display: inline-block;
border: 1px solid var(--mainColor);
color: var(--mainColor);
&:hover {
background-color: var(--mainColor);
color: #fff;
}
}
</style>

I doubt that's possible. But you can run conditional logic in sass, like their official example:
$light-background: #f2ece4;
$light-text: #036;
$dark-background: #6b717f;
$dark-text: #d2e1dd;
#mixin theme-colors($light-theme: true) {
#if $light-theme {
background-color: $light-background;
color: $light-text;
} #else {
background-color: $dark-background;
color: $dark-text;
}
}
.banner {
#include theme-colors($light-theme: true);
body.dark & {
#include theme-colors($light-theme: false);
}
}
https://sass-lang.com/documentation/at-rules/control/if

This is super easy if using Vue3:
<template>
<h1>HELLO</h1>
</template>
<script>
export default {
data() {
return {
h1Color: "red",
};
},
};
</script>
<style>
h1 {
color: v-bind(h1Color);
}
</style>
For a more extensive guide - here
If using Vue2 I would suggest using the same approach as in Steven B's answer, here the ref

Related

How to change css width 50% to 100% using Vue

How can I change css width from 50% to 100 % when click the button see more detail here >>> Sample sandbox
<template>
<div id="theSpecial">Hello World Special</div>
<button #click="changeWidth">Change width</button>
</template>
<script>
export default {
data() {
return {
testBoolean: false,
};
},
methods: {
changeWidth() {
this.testBoolean = true;
//change width to 100%
},
},
};
</script>
CSS
#theSpecial {
background-color: purple;
color: white;
width: 50%;
}
You have to make some change on your code
First of all add this to your css
.theSpecial{width:50%}
.fullWidth{width:100%}
To toggle the full width modify the method
changeWidth() {
this.testBoolean = !this.testBoolean;
//this will toggle the width on every click
},
and then use this in your component template
<div class="theSpecial" v-bind:class="{fullWidth:testBoolean}">
N.B. change the id into class, beacuse id has more css specifity.
This will toggle the class full width accordly to the value of testBoolean.
This is your Sandbox
Here you can find documentation about class binding
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<div id="theSpecial" :class="{ 'full-width': testBoolean }">
Hello World Special
</div>
<button #click="changeWidth">Change width</button>
</div>
</template>
<script>
export default {
name: "HelloWorld",
props: {
msg: String,
},
data() {
return {
testBoolean: false,
};
},
methods: {
changeWidth() {
this.testBoolean = true;
},
},
};
</script>
#theSpecial {
background-color: purple;
color: white;
width: 50%;
}
#theSpecial.full-width {
width: 100%;
}
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
data() {
return {
testBoolean: false,
};
},
methods: {
changeWidth() {
this.testBoolean = !this.testBoolean;
//change width to 100%
},
},
.theSpecial {
background-color: purple;
color: white;
width: 50%;
}
.fullwidth {
background-color: purple;
color: white;
width: 100%;
}
<div :class="(this.testBoolean === true)? 'fullwidth':'theSpecial'">Hello World Special</div>
<button #click="changeWidth">Change width</button>

how to dynamically apply Multiple CSS files as per URL Param value in typescript or in Javascript

I have written the code for getting the URL param value
export class RedirectingComponent implements OnInit {
constructor(private activatedRoute: ActivatedRoute) { }
ngOnInit()
{
this.activatedRoute.paramMap.subscribe(params=>
{
let color=params.get('color');
console.log(color);
if(color)
{
if(color=='red')
{
//So If the color is red then we should apply red.css
}
}
})
}
}
Here are the css files
red.css
.my-container{
height: 100%;
background-color: red;
}
blue.css
.my-container{
height: 100%;
background-color: red;
}
Here is the Main Page
<mat-toolbar color="primary">
<span class="mainheading">Theme Changing</span>
</mat-toolbar>
<div class="my-container">
<h1 class="sideheading">URL that entered are processed for Theme Changing of the Page</h1>
</div>
Now I have to use the param value like color and change the background to the color mentioned like if the color mentioned is red the background should change in to red.
Note:Their should be different CSS files
You can't control header tags by angular directives.
You can create style tags and import different files in them. then control them by *ngIf directive.
<style type="text/css" *ngIf="rule1">
#import 'path\to\file-1';
</style>
<style type="text/css" *ngIf="rule2">
#import 'path\to\file-2';
</style>
Guys I have found a solution for this question
import { ActivatedRoute } from '#angular/router';
constructor(private activatedRoute: ActivatedRoute) { }
ngOnInit()
{
this.activatedRoute.paramMap.subscribe(params=>
{
let color=params.get('color');
console.log(color);
if(color)
{
if(color=="red")
{
loadCss("assets/styles/red.css");
console.log("This should be able to change to red color background");
}
else if(color=='blue')
{
loadCss("assets/styles/blue.css");
console.log("This should be able to change to Blue color background");
}
else if(color=='green')
{
loadCss("assets/styles/green.css");
console.log("This should be able to change to Green color background");
}
}
function loadCss(filename:any)
{
var fileref=document.createElement("link");
fileref.setAttribute("rel","stylesheet");
fileref.setAttribute("type","text/css");
fileref.setAttribute("href",filename);
if(typeof fileref!="undefined")
{
document.getElementsByTagName("head")[0].appendChild(fileref)
}
}
})
}
}
Main CSS Files
.mainheading
{
width: 100%;
text-align: center;
}
.sideheading{
padding-top: 20px;
color: white;
font-style:italic;
text-align:center ;
}
red.css
.my-container{
height: 100%;
background-color: red;
}
blue.css
.my-container{
height: 100%;
background-color: blue;
}
green.css
.my-container{
height: 100%;
background-color: green;
}
<mat-toolbar color="primary">
<span class="mainheading">Theme Changing</span>
</mat-toolbar>
<div id="theme" class="my-container" >
<h1 class="sideheading">
URL that entered are processed for Theme Changing of the Page
</h1>
</div>
Note:
Put the css files in assets/styles folder.
For getting the value from the url you should add the script in the app routing module
const routes: Routes = [
{
path:'redirecting/:color',component:RedirectingComponent
}];

Is it possible to use styles from an Angular service in scss?

I have a change-color.service.ts that has the following:
public defaultStyles = {
firstDesignBackgroundColor: '#a31329',
firstDesignFontColor: '#ffffff',
secondDesignBackgroundColor: '#d1cfcfff',
secondDesignFontColor: '#000000'
};
now I will like to add to my style.scss for the statement
:host ::ng-deep th span#optimize-checkbox-header .mat-checkbox label.mat-checkbox-layout .mat-checkbox-inner-container.mat-checkbox-inner-container-no-side-margin .mat-checkbox-frame {
border: 2px solid #fff !important;
}
replace the #fff with firstDesignFontColor from the change-service. Do you know how I can create this dependency? Is this possible at all?
There is actually a way to realize it with css variables that I will post here as a second answer.
You can change css variables from JavaScript code, so if you use variables for your class like this simplified example:
:root {
--bg-color: red;
}
.test {
background-color: var(--bg-color);
}
then you can change this from your ChangeColorService
interface Colors {
background: string;
}
#Injectable()
export class ChangeColorService {
colors$ = new BehaviorSubject<Colors>({ background: 'red' });
constructor(#Inject(DOCUMENT) private document: Document) { }
change(colors: Colors) {
const root = this.document.documentElement;
root.style.setProperty('--bg-color', colors.background);
this.colors$.next(colors);
}
}
Full example:
https://stackblitz.com/edit/angular-change-css-variable?file=src/app/app.component.ts
No that's not possible. What is possible, is to use style bindings.
class YourComponent {
styles: any;
constructor(private color: ChangeColor) {}
ngOnInit() { this.styles = this.color.defaultStyles; }
}
<div [style.background-color]="styles.firstDesignBackgroundColor"></div>

How can I globally style the scrollbar in Vuetify according to the user's theme?

I've created a custom css file that applies styles to the global scrollbar! But, I'd like to only show a dark scrollbar to users when $vuetify.theme.dark is set to true.
Is there a way that I can apply scrollbar css globally once that theme variable changes?
Here's my App.vue file
<template>
<v-app
id="inspire"
:style="{ background: $vuetify.theme.themes[theme].background }"
>
<header-bar />
<v-main>
<v-container fluid fill-height>
<keep-alive>
<router-view />
</keep-alive>
</v-container>
</v-main>
</v-app>
</template>
<script>
import HeaderBar from "./components/Navigation/HeaderBar.vue";
import store from "./store";
export default {
name: "App",
components: {
HeaderBar,
},
computed: {
theme() {
return this.$vuetify.theme.dark ? "dark" : "light";
},
},
store: store,
beforeCreate() {
this.$store.commit("initializeStore");
this.$vuetify.theme.dark = this.$store.state.DarkMode;
},
};
</script>
<style>
#import "./DarkScrollbar.css";
html {
overflow: auto !important;
}
.v-btn.theme--light.v-btn--has-bg:not(.primary):not(.success):not(.error) {
background-color: #ffffff;
}
</style>
In the above, I've created a computed variable "theme" to store the theme's name, which I believe I can place a watcher and trigger a function call on change.
Here's the contents of the DarkScrollbar.css file that I'm wanting to dynamically toggle!
/* Dark Scrollbar CSS */
::placeholder {
color: #b2aba1;
}
input:-webkit-autofill,
textarea:-webkit-autofill,
select:-webkit-autofill {
background-color: #555b00 !important;
color: #e8e6e3 !important;
}
::-webkit-scrollbar {
background-color: #202324;
color: #aba499;
}
::-webkit-scrollbar-thumb {
background-color: #454a4d;
}
::-webkit-scrollbar-thumb:hover {
background-color: #575e62;
}
::-webkit-scrollbar-thumb:active {
background-color: #484e51;
}
::-webkit-scrollbar-corner {
background-color: #181a1b;
}
::selection {
background-color: #004daa !important;
color: #e8e6e3 !important;
}
::-moz-selection {
background-color: #004daa !important;
color: #e8e6e3 !important;
}
I've actually just fixed this myself. I'll post the answer here so that other folks might be able to use this too! Programatically adding and removing a class on the body element allows you to toggle scrollbar styling.
In my computed property, I just had to add the class based on Vuetify's selected theme.
computed: {
theme() {
const bodyElement = document.getElementsByTagName("body")[0];
if (this.$vuetify.theme.dark == true) {
bodyElement.classList = "darkScrollbar";
} else {
bodyElement.classList = "";
}
return this.$vuetify.theme.dark ? "dark" : "light";
},
},
In my custom CSS file that I import, I added body as well as the custom class to each ::webkit style rule.
body.darkScrollbar::placeholder {
color: #b2aba1;
}
body.darkScrollbar::-webkit-scrollbar {
background-color: #202324;
color: #aba499;
}
body.darkScrollbar::-webkit-scrollbar-thumb {
background-color: #454a4d;
}
body.darkScrollbar::-webkit-scrollbar-thumb:hover {
background-color: #575e62;
}
body.darkScrollbar::-webkit-scrollbar-thumb:active {
background-color: #484e51;
}
body.darkScrollbar::-webkit-scrollbar-corner {
background-color: #181a1b;
}
body.darkScrollbar::selection {
background-color: #004daa !important;
color: #e8e6e3 !important;
}
body.darkScrollbar::-moz-selection {
background-color: #004daa !important;
color: #e8e6e3 !important;
}
body.darkScrollbar input:-webkit-autofill,
body.darkScrollbar textarea:-webkit-autofill,
body.darkScrollbar select:-webkit-autofill {
background-color: #555b00 !important;
color: #e8e6e3 !important;
}

Changing styles and text modification of alert controller in ionic 2

Actually my alert controller "Title" is too lengthy , so i want to resize the text and i added Css class then tried to change the style in SASS by giving font size to it unfortunately didn't worked and the buttons text are uppercase.So is there anyway to change font size of text ,buttons color and size and changing button text to lowercase?
My typescript file
import { IonicPage,AlertController} from 'ionic-angular';
import { Component,ViewChild } from '#angular/core';
import 'rxjs/add/operator/toPromise';
#IonicPage()
#Component({
selector: 'page-kpi',
templateUrl: 'kpi.html',
})
export class KpiPage {
#ViewChild(Nav) nav: Nav;
temp2:any;
lastUpdate:any;
constructor(
private alertCtrl: AlertController,
public menuCtrl : MenuController,
) {
this.platform.ready().then(() => {
this.refreshbutton();
}
});
refreshbutton(){
this.getSession();
if(this.offline == true)
{
this.offlineRefreshAlert();
}
else{
let alert = this.alertCtrl.create({
title: 'Do you really want to refresh the widgets?',
message: 'Refresh process will take time to complete.',
cssClass: 'alertLogCss',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
alert = null;
}
},
{
text: 'Refresh now',
handler: () => {
if(this.online == true){
this.refreshdata(this.result.sid);
this.loadingrefreshdashboard();
}
if(this.offline == true){
this.offlineRefreshAlert();
}
}
}
]
});
alert.present();
}
}
}
My SASS(CSS) file
.alertLogCss{
background-color: white;
color: red;
font-size: 4px;
button{
color: red;
font-size: 5px;
}
}
You have to add css out of that's page scss block
.alertLogCss{
background-color: white;
color: red;
font-size: 4px;
button{
color: red !important;
font-size: 10px;
text-transform: lowercase !important;
}
}
Put in app.scss

Resources