Getting the value of a CSS variable under <script> in Vue.js

I am working on implementing a Doughnut chart using chartJs in my application. However, I want to set the color of the chart within the <script> tag and retrieve the color from theme/variables.css.

In the current code snippet below, there is a hardcoded backgroundColor, but I would prefer to use --ion-color-secondary and --ion-color-secondary-contrast instead. How can I make this adjustment?

Here is the existing code:

<template>
  <Doughnut
    :chart-data="chartData"
    :chart-options="chartOptions"
  /> 
</template>

<script lang=ts>
import { defineComponent, PropType } from 'vue'

import { Doughnut } from 'vue-chartjs'
import {
  Chart as ChartJS,
  Title,
  Tooltip,
  Legend,
  ArcElement,
  CategoryScale,
  Plugin
} from 'chart.js'

ChartJS.register(Title, Tooltip, Legend, ArcElement, CategoryScale)

export default defineComponent<{fastdata: any}>({
  name: 'MrProgress',
  components: {
    Doughnut
  },
  inject: ["fastdata"],
  setup() {
    const chartOptions = {
      responsive: true,
      maintainAspectRatio: true,
      cutout: "90%",
      borderWidth: 0,
    }
    return {
        chartOptions,
    };
  },
  computed: {
    chartData() {
      const pts = this.fastdata.user.pts;
      const missing = this.fastdata.user.proximo_nivel - pts;
      return {
        datasets: [
          {
            backgroundColor: ['#41B883', '#e4e0d8'],
            data: [pts, missing]
          }
        ]
      }
    }
  }
})
</script>

Answer №1

After taking advice from @EstusFlask in the comment about userCssVar, I was able to resolve the issue by implementing the following solution:

npm install @vueuse/core

...
import { useCssVar } from '@vueuse/core'
import { ref } from 'vue'

<script>
...
      const color_primary = useCssVar(ref('--ion-color-primary'), ref(null)).value;
      const color_secondary = useCssVar(ref('--ion-color-secondary'), ref(null)).value;
      return {
        datasets: [
          {
            backgroundColor: [color_primary, color_secondary],
            data: [primary_data, secondary_data]
          }
        ]
      }
...
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to design a navigation bar for a one-page application using Vue?

Currently, I am developing a Vuejs single-page application and I'm exploring ways to implement a navbar that can toggle the visibility of different sections within the app upon clicking. While I have successfully designed the navbar layout, I am encou ...

Inheritance of currentColor in SVG URLs

Seeking the same solution as mentioned in How can an SVG image inherit colors from the HTML document?, specifically when applied to an svg image used as content on a :before pseudo-element. (The intended outcome is for both checkmarks to adopt the red col ...

Convert a file into an empty array when sending a post request from Angular to Laravel

I am currently working on a simple Laravel 5 post request that aims to achieve the following tasks: Save some data to a database. Store a file (image) in public storage with a name consisting of 20 random numbers followed by '.jpg'. Save a URL ...

Click on the link or tab to update the marker location on the Google Map

Can you click on Tab 2 to change the marker/location, and then go back to Tab 1 to switch it back to the original location? [Links to Fiddle] http://jsfiddle.net/ye3x8/ function initialize() { var styles = [{ stylers: [{ saturati ...

Is there a way to incorporate multiple functions into a single sx property, such as color, zIndex, backgroundColor, etc? Can this be achieved in any way?

I am currently developing a single search component that will be used across various sections of my application. Each component receives a prop called search: string to determine its type and apply specific styles accordingly. Although I could use classNam ...

Desktop users can enjoy the off-canvas menu feature, while mobile users will have access to the accordion menu option with Foundation 6

My goal is to customize my off-canvas menu in Foundation 6.3.1 so that it appears on desktop while displaying an accordion menu on mobile devices. On desktop, I want the off-canvas menu always visible (see example). For mobile, when the hamburger icon is ...

Safari IOS experiencing issue with element disappearing unexpectedly when input is focused

I am facing a situation similar to the one discussed in the question (iOS 8.3 fixed HTML element disappears on input focus), but my problem differs slightly. My chatbox iframe is embedded within a scrollable parent, and when the iframe is activated, it exp ...

A guide on defining the width of an element within a flex container when it overflows

Here is the code snippet I have: .divA { background: red; width: 500px; display: flex; flex-direction: row; overflow-y: scroll; } .epi { width: 50%; background: blue; } <div class="divA"> <div class="epi"> <h1>dsds ...

Invoking a Node.js function using a URL reference

Is there a way to call a function located in a remote URL? I have the page URL and the function name. The server-side application is running on NodeJs Express, and the function structure is as follows: function executer(param1, param2){ //logic re ...

Why is my Cloud Storage Cloud upload file showing a file size of "0 bytes" (Node)?

I am having issues uploading a PDF file to Google Cloud Storage as the file size appears to be 0 bytes. I have been trying to resolve this for the past 3 days but have not received any response. Here is the code snippet that I am using: const cv = req ...

Laravel7 CORS issue: CORS policy blocks request due to access control check failure - missing 'Access-Control-Allow-Origin' header

Scenario: Integrating VueJS/Laravel app inventory with Magento2 using SOAP API to update Quantity. Error encountered: The following error occurred when trying to access '' from origin '' - CORS policy blocked the request: Th ...

My attempts to use media queries are being ignored by Internet Explorer

While other browsers recognize and function properly, it seems like IE is not recognizing my media queries. Could there be something wrong with my code? I've created a similar design before without any issues, so why is this one causing problems? Her ...

Utilize Bootstrap's custom validation exclusively on fields that are invalid

Exploring Bootstrap features has led me to wonder if there's a straightforward method to change the styling of a field specifically when it is considered invalid. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" ...

Skewed div with a stylish background image

Struggling with an issue here. I've managed to skew my div just right, but now I'm trying to get the image to fit without tiling. If anyone can offer some assistance, that would be greatly appreciated. This is what I'm aiming for: Currentl ...

modify the values of directive elements using a templateUrl

HTML Template: I have an element in the template file seats.tpl.html like this: <select id="guest_table"> <option tables="emptySeats" ng-repeat="table in emptySeats" value="{{table.tableId}}">{{table.tableNumber}}</option& ...

The link-dark bootstrap class fails to have an impact

As a beginner in using bootstrap, I am creating my first simple website. However, I am facing an issue with the footer copyright section where the class "link-dark" is not taking effect. <!-- Copyright --> <div class="container-fluid ...

What benefits can be gained from utilizing the beforeEach function within Jest testing?

I am currently immersing myself in the world of Jest by following this informative guide. Can you explain the benefits of utilizing the beforeEach function in Jest? I have a particular interest in identifying action dispatches. I believe that two segments ...

The best way to close a swipeable drawer from a different class

I'm feeling a bit puzzled by the explanation of SwipeableDrawer on the Material-ui website. Here's the setup I have: there's a Component called 'Sidebar' that opens a SwipeableDrawer when a user clicks a button on the appbar or swi ...

personalizing jquery templates

Currently, I am utilizing a jQuery template to showcase specific values. Within this template, I have included an if statement to determine if the age is considered old or not, with the result altering the background color accordingly. Previously, the co ...

The key you entered in local storage was not defined and the value associated with

Having an issue with my HTML signup form where the key is showing as undefined (email should be the key) and the value displays as [object Object]. Any help in resolving this problem would be greatly appreciated. Thank you. <!DOCTYPE html> <html& ...