Using an Ember color picker to dynamically change SCSS variables

Is there a way to allow an admin to change the skin of one of my websites by selecting a color from a palette that will update a universal SASS variable?

I am aware of methods to dynamically change CSS using jQuery, but I specifically want to modify the SCSS variable. You can see an example of what I'm looking for here:

In the current setup, changing the color picker only affects the <body> attribute. However, my goal is to create a function that alters the $main-color variable so that the entire background of the <html> element changes.

I'm unsure whether implementing a SAVE button is necessary or how to make this feature functional. I am open to suggestions and alternative approaches.

If you prefer working with the actual code instead of the jsbin example, below are the relevant snippets:

templates/settings.hbs

<table>
  <tr>
    <th>Property</th>
    <th>Color</th>
  </tr>
  <tr>
    <td>$main-heading-color</td>
    <td>
      <div id="color-pick">
        {{col-pick value=mainHeadingColor}}
      </div>
    </td>
  </tr>
</table>

controllers/settings.js

import Ember from 'ember';

export default Ember.Controller.extend({
  mainHeadingColor: '0d723c'
});

styles/definitions/variables.scss

// project specific colors
$main-heading-color:      #0d723c; 

Ember add-on: https://www.npmjs.com/package/ember-colpick

Answer №1

SCSS files undergo preprocessing, meaning they are processed during project build time to generate CSS files for browser interpretation. This results in the loss of any context added by SCSS files, such as variables, once the project is built.

In simpler terms, any modifications you attempt to make no longer exist when a user views your webpage or web application.

To work around this limitation, consider:

  • Creating a unique class (e.g., moddable-background-color) with the property you wish to adjust (e.g., background-color).
  • Applying this class to the elements in your application that require runtime changes.
  • Utilizing jQuery to modify the property value within the designated class (e.g.,
    $('moddable-background-color').css('background-color', 'selected color');
    ).

While it may be feasible to send a request to your backend to alter SCSS variable values and trigger a CSS file rebuild, this approach is discouraged due to its complexity and costliness. Instead, sticking to the specialized class method offers a more straightforward solution without unnecessary complications.

Answer №2

After delving deeper into this topic, I have discovered methods to replicate this. Here are a few excellent examples:

  1. Adapting LESS variables using cssobj, essentially treating pre-compiled variables as javascript objects:

    GitHub Repository

    Demonstration Site

  2. Generating colors without relying on pre-compiled variables, but instead utilizing currentColor.

    Informative Blog Post (Contains CodePen)

The definitive answer remains "No, you can't dynamically change SASS variables", however, I trust that future individuals searching for solutions will come across a workaround similar to the one I have found.

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 methods can be used to validate an undefined value?

exploding right now if (myObject.myKey.myThing){ } undefined myThing cannot be read What approach can be taken to determine if myThing is defined before entering the if block? ...

Position a button freely on the grid layout of bootstrap4

I need help creating a sidebar in Bootstrap 4 with a floating arrow button to collapse it, similar to this example. Currently, I'm using a `container-fluid` and dividing the row into a 2-8-2 layout for left sidebar, main content, and right sidebar. I ...

What is the best way to pass JavaScript object literals from a Node.js server to the frontend browser?

In Node, I am working with JavaScript object literals containing methods in the backend. For example: const report = { id: 1, title: 'Quarterly Report for Department 12345', abstract: 'This report shows the results of the sales ...

Implement a function that attaches an event listener to generate a new table row dynamically

I am currently facing an issue with my table that has ajax call functionality to add rows within the tbody element. The table is already set up on the html page. <table id='mytable'> <thead> <tr> <th>First Col</th> & ...

Utilize XQuery to manipulate both outer elements and inner elements

As a newcomer to XQuery, I am seeking assistance with the following XML example: <lg> <l n="1">Z nutz vnd heylsamer ler / verma⸗ </l> <l n="2">nung vnd ervolgung der wyßheit / ver </l> <l n="3"> ...

Encoding multiple arrays in JSON using Zend PHP

I'm encountering an issue with the JSON functionality in Zend and JavaScript. Initially, I attempted to encode a single array that contained a certain number of models in the following manner: echo json_encode(Application_Model_Marker::getMarkers()) ...

Guide to utilizing the bootstrap grid system to stack below a width of 480px

I have been grappling with this issue, but unfortunately, there seems to be a lack of information on how to resolve it. I have gone through numerous old posts where individuals are "modding" the files and creating their own col-ms (medium-small) that stack ...

Receive a notification when the div element stops scrolling

I am attempting to replicate Android's expandable toolbar within an Angular component. My HTML code appears as follows: <div (scroll)="someScroll($event)"> <div class="toolbar"></div> <div class="body"></div> </d ...

How to align the markup of a dynamic directive with its host in Angular?

Introducing a simple directive called [popover], its main purpose is to dynamically inject a component (as a sibling). Implementation example: @Component({ selector: 'my-app', template: ` <div> <button popover>Popover ...

Is there a way to switch an input's appearance to resemble that of a label?

Looking to customize my input fields to resemble labels at first, then apply editable styles with angular.js upon button click. Utilizing bootstrap for my project, wondering if there is a built-in class that can be toggled on and off for these inputs? ...

Blocking the space bar in JavaScript is a useful technique that can be

I'm in the process of developing an application and I'm looking to prevent the space bar from scrolling my page My framework of choice is VUE, and I am trying to trigger a method using an event handler However, when I attempt to call the ' ...

How can we deliver pure JS, HTML, and CSS content without relying on static HTML pages?

Looking to create a fast app prototype without using React or Vue? I'd like to avoid simply making an html and js file imported within it. Can npm packages, SCSS be used while programming vanilla Javascript minus a framework? ...

Attempting to eliminate the parent container of a slide generated by the Slick Slider Plugin

I have developed a filter mechanism that hides specific classes within a slick slider based on the data-tag associated with the objects and the value of checkboxes. However, when these classes are hidden, they still occupy space because I should be hiding ...

Having trouble removing objects from a map results in having to invoke the function three times

I am facing an issue with the function findPrice where I need to call it multiple times to delete objects that match a specific price. The problem arises when dealing with large maps and two functions. Here is the JSON format: [ { _id: 5c6c408dbec3ab457c ...

Guide to fixing Vue app crashes caused by Web Sockets error

I'm encountering an issue with my Vue application as I am unable to load a specific page due to the app crashing. The console displays the following error, even though there is no implementation of Web Sockets in the app. Despite adding this snippet ...

Is there a way to automatically scroll 50 pixels down the page after pressing a button?

Is there a way to make my page scroll down in Angular when a button is clicked? I attempted to use this code, but it didn't have the desired effect. What is the best method for scrolling the page down by 50px? window.scrollBy(0, 50); ...

Guide to locating a particular node within an array of nested objects by utilizing the object

Dealing with an array of nested objects, the goal is to compare values with a flat array and update the property matchFound. If the parent's matchFound is true, then all its children should inherit this value. treeData = [{ field: 'make&a ...

"How to dynamically fill a text input field from a table using jQuery when a specific value is selected, potentially involving multiple rows (possibly

Scenario I created a form that allows users to place orders for articles. These articles are displayed in a table within another form, where each article is listed with its code, description, and price. The goal is for users to select an article from th ...

The contrast between utilizing jquery live('submit') and submit function in rails

There are two very similar pieces of code, but one works and the other doesn't. Here is the working code: $(document).ready(function() { return $("form#myform").live("submit", function() { var valuesToSubmit; valuesToSubmit = $(this).serial ...

Conceal the div once it reaches the top of the webpage

Is there a way to hide an "h1" element when its parent div reaches the top of the page and then show it again when it moves down using both JQuery and CSS? I attempted to utilize a scroll listener in JQuery to toggle a class, but this resulted in the "h1" ...