Vue.js - Loading a global _variables.scss file - javascript

Building a Vue app using #vue/cli 3. I am at a point where I need to integrate SCSS, haven't used SCSS with vue before so having some trouble.
My SCSS structure is pretty conventional:
_variables.scss containing all my variables
_layout.scss containing some layout related stuff
_componentX.scss styles for componentX
...
_componentN.scss styles for componentN
app.scss import everything
I want break this up by removing the component#.scss imports from app.scss and instead putting those styles in the component's (SFC) style tags.
When I try this however, the component throws errors for unrecognized variables (even though app.scss is imported in main.js)
The workaround would essentially be importing app.scss in every component. Is this the way to go? (seems strange) or am I doing something wrong?

Related

Where to put scss affecting multiple components throughout multiple apps in Angular?

I'm refactoring some Angular (7) components, and I don't know if there is some optimal location to put common scss style to reference it in many components' styleUrls.
Before refactoring, the scss was all global, and had to be built in order to be used between the different apps. Now, most of the scss has been encapsulated, requiring as few global style as possible.
However, there are still some global scss that I could get ride of in the build since they affect a known number of components. In order to do that, I extracted the scss at the root of all those components, and refered it, just before each individual components scss, in the styleUrls. (using some structure tips from this article )
Here is what the import looks like for every components requiring controls.component.scss
styleUrls: ['../controls.component.scss', './autocomplete.component.scss']
The structure looks somewhat like this :
LibName1
src
assets_source
global_styles
_global-files.scss
main.scss
lib
controls
control1
control2
control2.component.html
control2.component.scss
control2.component.ts --> here is the styleUrls
...
controls.component.scss --> the file I'm using to store cross-components style, importing in styleUrls.
Up until this point, everything is working out fine. The problem arises when I want to use this same scss file (controls.component.scss) outside of this app. The more high-level structure of our project and libs look like this :
ProjectName
src
app
components
component1
component1.html
component1.scss
component1.ts --> Where I want to refer to the controls.component.scss in the styleUrls
component2
...
libs
LibName1 --> The LibName1 from the previous structure example
I would like to be able to use the styles in the file controls.component.scss inside different components without having to build the lib and referencing its css in the main Project. I'm open to moving this file somewhere else, and what I would like is not to have to manage super longue relative paths.
I want to know what are the best practices in this scenario.
To use global for your app just put everything into your style.scss it will affect whole application style
If you want to import scss file from the library simply do like this
#import "~bootstrap/scss/bootstrap";
#import "~toastr/toastr";
#import "~font-awesome/scss/font-awesome";
Simply use ~ to import the scss file from node_modules folder

Does this SASS structure affect React app/component loadtime/performance?

I'm working on my first production-ready React application, and I'm using SASS as a pre-processor for my CSS. One huge benefit I find when styling my React components is being able to break my CSS into separate modules and import the specific modules needed into the corresponding React component.
The more my application grows, the harder it has become to quickly find and update the correct CSS I need while developing. Which leads to my question...
I'm thinking of implementing the following styling, and I'm not sure if it will have any effect on the performance of my app and components:
1 - Continue to separate all CSS into SASS modules
2 - Import all of those modules into a single app.scss file
3 - Import that single app.scss file into all of the components that need styling
This will let me browse a single place - app.scss - for the specific css module I need, and then quickly tell me where I need to go in my app directory to access that file. I see this being beneficial as I could write brief comments in the app.scss about each of the individual modules. That way as the app scales, I'll have documentation about what each css module accomplishes and what components those css modules correspond to.
Thus, my question is will this style of loading so many modules into a single css file and then loading that single css file into each component that needs styling slow down my app/component load time/performance?

How to override Bootstrap variables in Vue workflow ( Vanilla Bootstrap )?

The officially recommended way to customize / theme bootstrap is by overriding the bootstrap variables using sass. But how do I do this, or rather, how do I add this part of the process into the Vue webpack workflow ?
Googling led to try editing the vue.config.js file to add scss loader into webpack but I am unable to find the required file.
This is the directory structure
I have used vue-cli v3.4 to setup the project.
I am using vanilla bootstrap and not the bootstrap-vue components.
Make a file called custom.scss. Go into the node_modules/bootstrap/scss directory and copy everything from _variables.scss. Paste these into your custom.scss.
In your style.scss import your custom.scss before you import bootstrap.scss.
Now in your main.js import #/assets/style.scss.
You will need to remove the !default flag from any variables you wish to override in your custom.scss for them to take effect, as well.
Create a file /css/bootstrap-custom.scss, with the following:
// your variable overrides
// modify theme colors map
$theme-colors: (
"primary":#42b883,
//...other variables, you can find them in node_modules/bootstrap/scss/_variables.scss
);
// import to set changes
#import "~bootstrap/scss/bootstrap";
In the main script, import the added file instead of the current imported bootstrap css file:
// import '../node_modules/bootstrap/dist/css/bootstrap.css';
import '#/css/bootstrap-custom.scss';
Reference: Bootstrap, Variable defaults

Difference between style #import and js import in webpack

I was reading the source code of ant-design and noticed each component got a index.ts and index.less in the style folder. Why js is used here for managing dependencies? What's the difference if I just move it to the less file and use #import?
int ant design each component using less file as modular less component it means you less never applying for another components except in :global pseudoclass
have a look "react-app-rewire-less-modules"

React JS Component based styling

I have this problem, I have these scss files for all my global variables:
colors.scss
fonts.scss
helpers.scss
And I have these scss files for my component based and import it on top of the .jsx files:
header.scss
footer.scss
searchbar.scss
My problem in all of my component based scss files, I need to import the color.scss and helpers.scss in that way it will be included multiple times in header.scss,footer.scss and searchbar.scss
How do you guys work on it, been searching for an hour and did not found any solutions.
Thanks!
Just make a app.scss file and import all the scss files into that one starting with your variable files.
We do a similar thing at my work and have one file that we import all our component scss files into and any shared dependencies go above those that way you are only having to import once. The main thing is to make sure that files with dependencies go below the dependency files.
Create a global scss like so:
global.scss => imports colors.scss, fonts.scss, helpers.scss
and include it in your root JS file.
This way, every time you create a module, it will have these default styles.

Categories