Im currently learning reactjs and Ive just added SASS to my project. However, I want to create a single file that stores all my variables and mixins globally that can then be used by any other scss file in my project. How is this possible without having to manually import the file into every scss file that i make?
My folder structure:
/src
assets/
images/
components/
app/
app.js
app.scss
styles/
scss/
main.scss
_var.scss
_mixins.scss
_typography.scss
index.css
index.js
You'll have to import your variables at the top of a master import file, with all other imports coming after. So, assuming your directory structure above, in main.scss you'd want to do:
#import 'var';
#import 'mixins';
#import 'typography';
...
// Component imports
#import '../../components/app/app';
Alternatively, you could set up a shared master import file somewhere closer to the base of your project (say, index.scss), which would then look like:
// in `styles/scss/main.scss`
#import 'var';
#import 'mixins';
#import 'etc';
// in `index.scss`
// Base style import
#import 'styles/scss/main.scss';
// Component imports
#import 'components/app/app';
#import 'components/etc/etc';
The important factor here is that you need to be importing anything that needs access to your shared variables after your variable import. Otherwise the variables won't be in scope.
Related
I am trying to use react-alice-carousel,
And in the section of How To Use, It said something like this.
Style import
SCSS
#import "react-alice-carousel/src/alice-carousel.scss";
CSS
#import "react-alice-carousel/lib/alice-carousel.css";
Webpack
#import "react-alice-carousel/lib/alice-carousel.css";
And I am unable to import these files in my local project and so I am not getting the desired outcome.
You may have the wrong .scss file path. It should be:
#import "react-alice-carousel/lib/scss/alice-carousel.scss";
If you don't have node-sass you'll probably need to import the css file. I'd recommend importing it in your top level css file. Which would be index.css if you're using create-react-app.
Is there any way to import all _*.scss files to main App.scss file in React project? As I have separate folder for each component, I want to avoid typing
#import './components/UI/button';
#import './components/UI/menu';
#import './components/Forms/textInput';
What would be perfect is a working code like this:
#import './components/*';
I'm aware of order problem, I doesn't affect my css, files can be imported randomly.
The glob package will do it if you really wanted. I would advise against it though, including a sass file directly in the component is more efficient but mostly because order matters in CSS.
You can create file _index.scss in components directory and import all files from the subdirectories. After you can import writing #import './components/index'; or #import './components';
In my React app, instead of using styled-component or CSS-in-JS or importing a particular SCSS file into a component,
I used the SMACSS method where the only stylesheet that's being imported is the index.scss, which imports all the SCSS
files inside the styles directory.
Initially, I did the method of importing each SCSS file,
but I changed my mind after realizing that my file structure was messy and switched to the less modern way.
I'm just
wondering if there's a difference between importing each stylesheet and importing only one in terms of performance and
speed of an app.
I don't think it will make much difference in performance but you would be better off building up the good structure of code which is always recommended.
It's always good practice to create the single folder scss and then provide the references in main.scss of other files in folders like this way.
#import 'var';
#import 'mixins';
#import 'normalize';
#import 'default';
#import 'global/inputs';
#import 'global/btn';
#import 'components/modal-general';
#import 'components/modal-tabbed';
Then you can also use this single reference main.scss or index.scss in app.js or index.js
I'm using create-react-app and I integrated Sass to it. It is compiling just fine, the issue is that if I create a variable or mixin, use it in one of my sass files and then close the localhost and comeback later and run npm start, it will give me an error, like this one:
The files are not recognizing the variables (mixin in this case), which is located in a file named "base.scss".
I also have an index.scss file where I import all my other .scss components:
#import "./styles/base.scss";
#import "./styles/layout.scss";
#import "./styles/components/search-bar.scss";
#import "./styles/components/side-nav.scss";
#import "./styles/components/phone-navigation.scss";
#import "./styles/components/card.scss";
#import "./styles/components/pagination.scss";
#import "./styles/components/dropdown.scss";
#import "./styles/components/dashboard.scss";
#import "./styles/components/cast.scss";
In the index.css file I can see the compiled code from all my .scss components, so it seems that it is working properly, I can see even the mixin in the compiled index.css file.
Did you find an answer? I think in create-react-app you need to #import 'base' for every components.scss file, otherwise the variables are not found.
This is my current project directory structure, as I couldn't think of a better way of organizing for the use case that every component has it's own .scss file, however, the entire app are fed themes which include a global variables.scss file. Currently, I'm importing the variables.scss file at the top of every component's .scss file and I'm wondering what the best practices are for this sort of workflow.
/ app
/ components
/ Navigation
- Navigation.js
- Navigation.scss
/ styles
- globals.scss
- variables.scss
- main.scss
My main.scss file basically #import all .scss in its directory (e.g. variables.scss, globals.scss, etc.), however, I need to the following to have access to variables inside of each component:
// Navigation.scss
#import '../../styles/variables.scss
.class { ... }
I understand I could also import all component .scss files inside of the main.scss directory but that defeats the purpose of having css modules and being able to import them at component level for name spacing, etc.
It is possible but I don't recommend it.
Dependencies
Every component has it's dependencies. You have to require several dependencies in each Sass (ES6/React.js) module. That's how Webpack resolves dependencies.
I recommend add an resolve path to Webpack / Sass config and import the variables file in each Sass module.