Leveraging the Power of SASS Variables Across Your Entire NativeScript Application

Currently, I am in the process of developing an app using NativeScript 6.4.1 along with Angular 8. In order to enhance my styling capabilities, I have decided to incorporate SASS due to its advantageous features like variable names.

My designer has supplied me with a range of colors that will be used across various pages of the application. Leveraging SASS variables for these hex colors will prove to be beneficial.

In my project, I have made use of the following dependencies:

"node-sass": "4.12.0",
"sass": "^1.23.7",
"sass-loader": "^8.0.0",

The main SASS file of my app is named app.scss and it includes:

@import '~@nativescript/theme/css/core.css';

@import 'variables';

However, despite defining the SASS variables in my variables.scss file, they are not accessible in my component scss files. For example, in home.component.scss:

.home-panel{
vertical-align: center; 
font-size: 20;
margin: 15;
background-color: $navy_blue; //this does not exist
}

The contents of the variables.scss file include:

$navy_blue: #105195;
$vivid_cerulean: #28abe2;
$white: #ffffff;
$dark_navy_blue: #063260;
$light_grey: #eeeeee;
$error_red: #eb2026;

For experimenting purposes, a playground link is provided but it does not reflect my actual setup:

If you have any insights on how I can access these variables throughout my scss folders or files without repeatedly importing the variables file, please share your suggestions.

Answer β„–1

It is vital to include the variable SCSS file in all component-specific SCSS files as there is no alternative solution. Without this, the compiler will not be able to identify what you are seeking.

If you are in urgent need of this functionality, I recommend delving into the Webpack documentation. You may discover a workaround for automatically injecting your variables.

Answer β„–2

To include the variables from variable.scss in your component.scss file, simply import it.

Alternatively, you can define your variables like this:

:root {
    --variable1: #fff;
    --variable2: #ddd;
}

Then use them in your styles like so:

color: var(--variable1)

within your component.scss file.

LATEST UPDATE

I believe I have resolved your issue. Please take a look at this link for more details.

The problem stemmed from using a css file instead of scss for the home component in the example. This has been rectified in the provided playground 😊

LATEST UPDATE Simply add the following code:

@Component({
    selector: "Home",
    moduleId: module.id,
    templateUrl: "./home.component.html",
    styleUrls: ["./home.component.scss"],
    encapsulation: ViewEncapsulation.None
})
export class HomeComponent implements OnInit {
...
}

This will apply all global styles without requiring the importing of .scss files.

I'm happy to assist you 😊

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

Using ngFor in Angular 2-5 without the need for a div container wrapping

Using ngFor in a div to display an object containing HTML from an array collection. However, I want the object with the HTML node (HTMLElement) to be displayed without being wrapped in a div as specified by the ngFor Directive. Below is my HTML code snipp ...

Tips on avoiding the collapse of the right div when positioned under a left float in a full page layout

Trying out a full page layout for the first time, with a left div serving as a block style nav. #admin_nav { width: 230px; height: 100%; background-color: #f9f9f9; border-right: 1px solid #ddd; float: left; } The content is on the rig ...

Sending a post request to an Express.js API from a different device

I currently have a server running on localhost:3000, with my app.js set up to utilize the angular router. When attempting to access localhost:3000 in my browser (for example: app.use('/', express.static(path.join(__dirname, '/dist/Client&apo ...

Trigger the phone to vibrate once the animation has completed. This feature will only activate upon clicking the button

After the image completes its animation by sliding in from the left, I would like it to vibrate for 2 seconds using the HTML 5 vibrate API: navigator.vibrate(2000); An on-click event successfully triggers the vibration when a button is clicked: <butt ...

A guide to adjusting the font size and placement of text in a precise manner

Is there a way to adjust the font size and position in a particular text? How can this be achieved? doc.text( 40, 30, "jspdf" ); https://i.stack.imgur.com/Io7RE.png ...

Discover the pixel width of a Bootstrap grid row or container using JavaScript

How can I calculate the width of a Bootstrap grid row or container in pixels using JavaScript? I am working with Aurelia for my JavaScript project, but I'm open to solutions in standard JS (no jQuery, please). Looking at the Bootstrap documentation, ...

Leveraging the ngFor local variable within nested elements

Is there a way to properly display a property of the local variable theme, such as theme.name? Below is an example of how my *ngFor is structured: <ul> <li *ngFor="#theme of themes"> <span>theme.name</span> </li> ...

Use Filterpipe within Angular Template to sort content based on Object Value

I have an Object Mockup: export const IMAGES: Image[] = [ { ID: 1, NAME: 'Werk1', YEAR: 1992, IMGURL: 'assets/Images/1/img1.jpg', MATERIA ...

Issues with click events in the navigation menu

How can I make my menu close when clicking on other parts of my website, instead of opening? I know that I should use a click event for this, but when I implemented a click event, my menu encountered 2 unwanted problems: 1- Whenever I clicked on a menu i ...

Tips for keeping your Angular CDK up to date and improving its performance

Having just started with Angular, I'm stuck trying to figure out why the update is throwing errors. Purpose: Update the cdk and material versions, then install file-saver. Command: npm update @angular/cdk How can I update my angular cdk? Here&apos ...

Center-aligned Bootstrap responsive column with a fixed width

Currently, I am working on creating a login page with a fixed width and a centered column layout. This is similar to the design of the login page at https://www.amazon.com/sign-in. I attempted to achieve this layout using offset columns, as shown below: ...

Guide to utilizing custom fonts in a VUE application

I'm currently working on my first Vue project and despite following various examples and the official documentation, I am struggling to resolve an issue regarding importing fonts locally in my project. Within my `` tag, I am importing the font that I ...

Enhance the visual appeal of incoming data using Angular Material's dynamic styling feature

Is it possible to enhance the text with some CSS styling to make each item stand out like in this example: https://i.stack.imgur.com/kSyZE.png I prefer not to include a cross button or provide users with the option to add tags. The data is coming from a R ...

When text exceeds multiple lines, display an ellipsis to indicate overflow and wrap only at word boundaries

Here is an example snippet of my code: <div class="container"> <div class="item n1">Proe Schugaienz</div> <div class="item n2">Proe Schugaienz</div> </div> and I am using the following jQuery code: $(&apos ...

What is the best way to implement coding for portrait styles specifically on the Kindle Fire device?

Any recommendations on how to specifically code the styles for portrait orientation solely on the Kindle Fire device? ...

Looking to update properties for elements within the Angular Material 16 mat-menu across the board?

Currently working with Angular Material 16 and I have a question regarding the height of buttons inside the mat-menu element. Here is an example of the code: <button mat-icon-button> <mat-icon>more_vert</mat-icon> </button> ...

Using the CdkDragDrop functionality alongside the ngTemplateOutlet

I'm experimenting with the Drag&Drop functionality introduced in Angular Material 7. To make my template more modular, I've utilized ngTemplateOutlet to create reusable components. Each option can either be a primary Thingβ„’ or a nested Thi ...

Tips for obtaining an Instance of a Mat-Table when there are multiple tables within the component

Encountering an issue where I have a component in Angular 6 generating multiple Mat-Tables with Data. Using the ng-for loop to iterate through all the Mat-Table Data sources within a div tag. However, when trying to add a new row to a table, unable to iden ...

Upon calling the createModalAddPost() function, a single window is triggered to open

Hey there, I'm having a JavaScript question. So, I have a panel that opens a window, and it works fine. But the issue arises when I close the window and try to open it again - it doesn't work. I have to reload the page every time in order to open ...

Converting an array of date strings to a single string in JavaScript

Here is the JSON format I received with dynamic data: {"range":["2018-07-23T16:03:26.861Z","2018-07-23T16:03:26.861Z"]} Now, I need to convert this into the following format: range(20180723,20180723) Below is my code snippet : var data:Date[] = {"rang ...